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.


