Solved

overloading program

Posted on 2009-05-16
2
479 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
Comment Utility

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
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

744 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now