Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 492
  • Last Modified:

overloading program

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
USN4x4
Asked:
USN4x4
1 Solution
 
Todd MummertCommented:

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
 
avdejCommented:
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

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Tackle projects and never again get stuck behind a technical roadblock.
Join Now