Solved

overloading program

Posted on 2009-05-16
2
486 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

690 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