|
|
| Java Methods Problem |
Expert:tay smith
I have to write a program that has three types of pie. Apple, which is the default pie, cherry and pumpkin. I then have to ask the customer what type of pie they like and store it in kind and then ask the user how many slices they would like and store it in num. Then i have to reduce the number of slices in the corresponding pie. How do I loop it until all the slices in each pie are done and then how do I create class variables that tell me the total slices sold and the total amount made for all the pies? This is what I have so far:
import java.util.*;
public class Pie {
//variables defined here: two types which include class variable (static- access using the class name and the dot operator) and an instance variable (non-static variable- access using the object name and the dot operator) private int noOfSlices; private static double price= 3.49; private static final int DEFAULT_SLICES=8; // private static final String DEFAULT_TYPE="apple";// default type of pie private String type; private OctaviaR_A15 ()// constructor; doesn't take any arguments { noOfSlices=DEFAULT_SLICES; } private OctaviaR_A15 (int a) { noOfSlices=a;} public static OctaviaR_A15 bakeSpecial (String str) { Pie specialPie= new OctaviaR_A15();//special pie is a local variable and its not accessible anywhere else in the program specialPie.type=str; //change the type return specialPie; }
public static void main (String[]args) {
Scanner reader= new Scanner (System.in); String kind; int num; Pie applePie; Pie thePie; applePie=new OctaviaR_A15(); //have an applePie ready System.out.println("What kind of pie would you like?"); kind=reader.nextLine(); if (kind.equals("apple"))// if the customer asks for a default pie thePie=applePie; else// otherwise have a special pie thePie=bakeSpecial(kind); System.out.println("How many slices would you like?"); num=reader.nextInt(); System.out.println("The amount due is" + num*price); System.out.println("The number of slices left is" + (DEFAULT_SLICES-num));
} }
|
| Answers |
| More Questions |
|
|
Post Answers
Ask Question
Facing Programming Problem?
|
|
|
|
|