Solved

overloading program

Posted on 2009-05-16
2
482 Views
Last Modified: 2013-11-23
The program overloads the getFinalPrice() method.

int getFinalPrice(boolean ac, boolean cd)

int getFinalPrice(boolean ac, boolean cd, int discount)

The second method is for friends of the dealer. When you invoke getFinalPrice() with
three arguments, the second one will be invoked, giving a discount. The third
argument specifies the percentage of discount.

Include one more overloaded method,
getFinalPrice(Boolean ac, Boolean cd, int discount, double tax).
This method adds tax amount to the final price.

Write the complete program and test all your methods.

Having problems getting the final outputs to match the above criteria, can you please assist? thanks
class OverLoadingMethods
{
public static void main(String[] args)
{
Autos carl = new Autos("Toyota","Camry",18000,6);
System.out.println("Final price for carl, with AC and CD "+
carl.getFinalPrice(true,true));
System.out.println("Same car for a good friend"+
carl.getFinalPrice(true,true,10));
}
class Autos
{
private String make;
private String model;
private int base_price;
private int cylinders;
Autos(String mk, String md, int bp, int cy)
{
make = mk;
model = md;
base_price = bp;
cylinders = cy;
}
int getFinalPrice(boolean ac, boolean cd)
{
int price = base_price;
if (ac) price += 500;
if(cd) price += 300;
return (price);
}
int getFinalPrice(boolean ac, boolean cd, int discount)
{
int price = base_price;
if (ac) price += 500;
if (cd) price += 300;
return (price - (price*discount/100));
}
}

Open in new window

0
Comment
Question by:USN4x4
2 Comments
 
LVL 11

Expert Comment

by:climbgunks
ID: 24403645

This is a class assignment...  general help only


add the "double tax" argument to another copy of the last getFinalPrice() function above

then when you calculate the return value just multiply by (1+tax amount) -- (assuming it is given as 0.06, and not 6, for example)..  if the latter, you will have to do something like  (1+tax_amount)/100
0
 
LVL 3

Accepted Solution

by:
avdej earned 500 total points
ID: 24415119
Hi, USN4x4!

I've placed the fully tested code for your program into the code snippet and attached file.
I think it is the best and the shortest way to explain you the issue.
Please compile and run it as following:
1. javac Autos.java
2. java Autos

Study the code, play with it and modify it to meet your particular needs.

The main point is:
You don't need to repeat the whole programming logic of your already written and tested methods. Instead you should use your available methods (with fewer arguments) as proven building blocks for building more specific methods.
And, of course, you don''t need to use any inner classes for that particular task -- all you need it the single class 'Autos'.

Feel free to ask me if I can be of further assistance in this matter.

Avdej

P.S. If 'tax' argument should be handled in some another way please change line 39 accordingly.

class Autos
{
    private String make;
    private String model;
    private int base_price;
    private int cylinders;
    
    // CONSTRUCTOR
    public Autos(String mk, String md, int bp, int cy)
    {
        make = mk;
        model = md;
        base_price = bp;
        cylinders = cy;
    }
    
    
    public int getFinalPrice(boolean ac, boolean cd)
    {
        int price = base_price;
        if (ac) price += 500;
        if(cd) price += 300;
        
        return price;
    }
    
    
    public int getFinalPrice(boolean ac, boolean cd, int discount)
    {
        int price = getFinalPrice(ac, cd);
        
        return (int)(price * (100 - discount) / 100.0);
    }
 
 
    public int getFinalPrice(boolean ac, boolean cd, int discount, double tax)
    {
        int price = getFinalPrice(ac, cd, discount);
        int taxValue = (int)(price * tax); 
        
        return price + taxValue;
    }
    
    
    // CREATE AND TEST AN INSTACE OF THE CLASS -----------------
    public static void main(String[] args)
    {
        Autos carl = new Autos("Toyota", "Camry", 18000, 6);
        
        System.out.println(
            "Price for Toyota Camry, with AC and CD: "
            + carl.getFinalPrice(true,true)
        );
        
        System.out.println(
            "Same car for a good friend (10% discount): "
            + carl.getFinalPrice(true, true, 10)
        );
        
        System.out.println(
            "Same car with 16% sales tax added: "
            + carl.getFinalPrice(true, true, 10, 0.16)
        );
    }
}

Open in new window

Autos.java.txt
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
The viewer will learn how to implement Singleton Design Pattern in Java.

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question