It appears you have not yet registered with DEVPPL. To register please click here... (it's fast, easy and free!)

Forum

Log In Sponsors
Board index Programming Java Forum

Overloading methods

Overloading methods

Postby KayR on Wed Sep 24, 2008 3:06 pm

Hi, I'm new to this forum and need help with an assignment. Any help would be appreciated!!!! This is my assignment:
Modify the methods of the Lunch class you created in the Chapter 2 case projects. Specifically, overload the constructor to take the numbers of apples, the amount of cheese, and number of loaves as parameters. Also, use parameters in the mutator method to set new amounts. Finally, return a value from the calculation method that represents the total price of the items. Add a display method that calls the calculation method and displays the total. Test your program by modifying the main() method.

This is my code:
Code: Select all
import java.io.*;
import java.text.*;

public class ParameterValue {
   DecimalFormat df = new DecimalFormat("$,###.00");   
   private double x, y, z, sum;
   private String s1;
   private String s2;
   private String s3;
   
   public static void main (String[] args){
   ParameterValue pv = new ParameterValue();
   pv.showValues();
   try{
   pv.getValues();
   }
   catch (IOException e){
   System.out.println("IO error");
   }
   pv.calculateTotal();
   }
   
    public void showValues()
     {
        //Display prices of items
        System.out.println("Apples cost $.50 each " +
                "\n Cheese cost $2.05 per pound " +
                "\n Bread cost $1.00 per loaf");
    }
   
    public void getValues(String s1, String s2, String s3, double x, double y, double z) throws IOException {
        //set input stream
        InputStreamReader isr = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(isr);
        //get numbers from user input
        System.out.print("How many apples do you want? ");
        s1 = br.readLine();
        x = Double.parseDouble(s1) * .50f;
       
        //asks user for 2nd number       
        System.out.print("How many pounds of cheese do you want? ");
        s2 = br.readLine();
        y = Double.parseDouble(s2) * 2.49;
       
        //asks user for 3rd number
        System.out.print("How many loaves of bread do you want? ");
        s3 = br.readLine();
        z = Double.parseDouble(s3) * 1.28f;
    }
   
    public void calculateTotal(double x, double y, double z, double sum) {       
       
        //add x,y,z
        sum = (x + y + z);
        return sum;
        //Display sum of x, y, z
        System.out.println("Your total is $" + df.format(sum));
    }
}


I get the errors:
ParameterValue.java:21: getValues(java.lang.String,java.lang.String,java.lang.String,double,double,double) in ParameterValue cannot be applied to ()
pv.getValues();
^
ParameterValue.java:26: calculateTotal(double,double,double,double) in ParameterValue cannot be applied to ()
pv.calculateTotal();
^
ParameterValue.java:61: cannot return a value from method whose result type is void
return sum;
^
3 errors

----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.
KayR
 
Posts: 0
Joined: Wed Sep 24, 2008 1:58 pm

Who is online

Users browsing this forum: psbot [Picsearch] and 0 guests