0
我現在需要添加一個無參數構造函數,我有我的課程和一個驅動程序。我假設我將構造函數與其他人一起放入類中,但不知道如何去做。我需要沒有參數的構造函數默認2勺香草與所有標準澆頭。 這裏是我到目前爲止有:需要添加無參數構造函數。困惑什麼也
import java.util.Scanner;
public class Sundae
{
public static final double TAX = 0.08625;
private String icecream;
private int scoops;
private double scoopCost;
private String toppings;
private String allChoices;
private String deluxeToppings;
private int counter = 0;
private double costOfDeluxeToppings = 0;
private double costOfSundae = 0;
Scanner input = new Scanner(System.in);
public String iceCreamFlavor()
{
System.out.println("1. vanilla | 2. peanut butter | 3. chocolate "
+ "| 4. mint chocolate chip |\n 5. coffee | 6. strawberry | 7. raspberry "
+ "| 8. knickerbocker glory | 9. cookies and creme");
int flavor = input.nextInt();
switch(flavor)
{
case 1:
icecream = "vanilla";
break;
case 2:
icecream = "peanut butter";
break;
case 3:
icecream = "chocolate";
break;
case 4:
icecream = "mint chocolate chip";
break;
case 5:
icecream = "coffe";
break;
case 6:
icecream = "strawberry";
break;
case 7:
icecream = "raspberry";
break;
case 8:
icecream = "knickerbocker glory";
break;
case 9:
icecream = "cookies and cream";
break;
default:
icecream = "vanilla";
}
return icecream;
}
public int numberOfScoops()
{
scoops = input.nextInt();
switch(scoops)
{
case 1:
scoops = 1;
break;
case 2:
scoops = 2;
break;
case 3:
scoops = 3;
break;
case 4:
scoops = 4;
break;
case 5:
scoops = 5;
break;
case 6:
scoops = 6;
break;
default:
scoops = 2;
}
return scoops;
}
public double costForScoops()
{
switch(scoops)
{
case 1:
System.out.println("1 scoop cost $1.79");
scoopCost = 1.79;
break;
case 2:
System.out.println("2 scoops cost $2.79");
scoopCost = 2.79;
break;
case 3:
System.out.println("3 scoops cost $3.79");
scoopCost = 3.79;
break;
case 4:
System.out.println("4 scoops cost $4.79");
scoopCost = 4.79;
break;
case 5:
System.out.println("5 scoops cost $5.79");
scoopCost = 5.79;
break;
case 6:
System.out.println("6 scoops cost $6.79");
scoopCost = 6.79;
break;
default:
System.out.println("2 scoops cost $2.79");
scoopCost = 2.79;
break;
}
return scoopCost;
}
public String standardToppingList()
{
System.out.println("1. whipped cream | 2. syrup | 3. chocolate sprinkes "
+ "| 4. rainbow sprinkes | 5. cherry");
String choice1 = "";
String choice2 = "";
String choice3 = "";
String choice4 = "";
String choice5 = "";
int choice = 0;
while(choice != -1)
{
choice = input.nextInt();
switch(choice)
{
case 1:
choice1 = "whipped cream, ";
System.out.println(choice1);
break;
case 2:
choice2 = "syrup, ";
System.out.println(choice2);
break;
case 3:
choice3 = "choclate sprinkles, ";
System.out.println(choice3);
break;
case 4:
choice4 = "rainbow sprinkles, ";
System.out.println(choice4);
break;
case 5:
choice5 = "cherry, ";
System.out.println(choice5);
break;
case -1:
break;
default:
choice = -1;
choice1 = "whipped cream, ";
choice2 = "syrup, ";
choice3 = "choclate sprinkles, ";
choice4 = "rainbow sprinkles, ";
choice5 = "cherry, ";
break;
}
}
toppings = choice1 + choice2 + choice3 + choice4 + choice5;
System.out.println("Your choices of toppings are: " + toppings);
return toppings;
}
public String freeSyrupChoice()
{
System.out.println("1. hot fudge | 2. chocolate | 3. caramel | 4. strawberry");
int choice = 0;
String choice1 = "";
String choice2 = "";
String choice3 = "";
String choice4 = "";
while(choice != -1)
{
choice = input.nextInt();
switch(choice)
{
case 1:
choice1 = "hot fudge, ";
System.out.println(choice1);
break;
case 2:
choice2 = "chocolate, ";
System.out.println(choice2);
break;
case 3:
choice3 = "caramel, ";
System.out.println(choice3);
break;
case 4:
choice4 = "strawberry, ";
System.out.println(choice4);
break;
case -1:
break;
default:
choice = -1;
choice1 = "hot fudge, ";
break;
}
}
allChoices = choice1 + choice2 + choice3 + choice4;
System.out.println("Your choices of syrup include " + allChoices);
return allChoices;
}
public String deluxeToppingList()
{
System.out.println("1. M&Ms | 2. Crushed Oreos | 3. Reeses | 4. Brownie Crunchies"
+ " | 5. Brownie Crunchies | 6. Kit Kats \n| 7. Marshmallow | 8. Granola clusters"
+ " | 9. Peanuts | 10. Walnuts");
String choice1 = "";
String choice2 = "";
String choice3 = "";
String choice4 = "";
String choice5 = "";
String choice6 = "";
String choice7 = "";
String choice8 = "";
int choice = 0;
while(choice != -1)
{
choice = input.nextInt();
switch(choice)
{
case 1:
choice1 = "M&M's, ";
System.out.println(choice1);
counter++;
break;
case 2:
choice2 = "Crushed Oreos, ";
System.out.println(choice2);
counter++;
break;
case 3:
choice3 = "Reeses, ";
System.out.println(choice3);
counter++;
break;
case 4:
choice4 = "Brownie Crunchies, ";
System.out.println(choice4);
counter++;
break;
case 5:
choice5 = "Kit Kats, ";
System.out.println(choice5);
counter++;
break;
case 6:
choice6 = "Granola Clusters, ";
System.out.println(choice6);
counter++;
break;
case 7:
choice7 = "Peanuts, ";
System.out.println(choice7);
counter++;
break;
case 8:
choice8 = "Walnuts, ";
System.out.println(choice8);
counter++;
break;
case -1:
break;
default:
choice = -1;
break;
}
}
deluxeToppings = choice1 + choice2 + choice3 + choice4 + choice5 + choice6 + choice7 + choice8;
System.out.println("Your choices of deluxe toppings are: " + deluxeToppings);
return deluxeToppings;
}
public int counterD()
{
System.out.println("You got " + counter + " deluxe toppings");
return counter;
}
public double costOfDeluxeToppings()
{
costOfDeluxeToppings = counter * 1.25;
System.out.printf("%s%.2f%s" , "Each deluxe topping cost $1.25 each ",
costOfDeluxeToppings, " will be added to your total.");
return costOfDeluxeToppings;
}
public double costOfSundae()
{
costOfSundae = scoopCost + costOfDeluxeToppings;
return costOfSundae;
}
public void outputOrder()
{
System.out.println("Your order consists of " + scoops + " scoop/s of " + icecream +
" icecream with " + toppings + allChoices + deluxeToppings);
System.out.printf("%s%.2f\n" , "Your sub-total is: $" , costOfSundae);
double tax = costOfSundae * TAX;
System.out.println("+ 8.625%");
double total = costOfSundae + tax;
System.out.printf("%s%.2f" , "Your total is: $" , total);
}
}
'公共新地(){/ * ... * /}' –
哦,哇......開關在大多數實際上是不必要的情況下,這些方法 –
一併閱讀。談論沒有參數構造函數。 https://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html –