I just had a quiz (luckily no a test) on constructors and objects which went relatively badly.
I thought I had it figured out, but I just don't.
The question we were given was basically to make a sub sandwich simulator. We were just told that it has "bread" "number of vegetables" and "type of meat"
The problem here is genuinely that I do not understand this, and not that I haven't read countlessly about it. So I am reaching out to see if somebody here can actually explain this to me in a way I can understand. It seems like there are just too many methods for what we're trying to do for one.
This is how far I got with it:
import java.util.Scanner;
public class SubSandwich{
Scanner in = new Scanner(System.in);
private String bread;
private int numVegs;
private String meat;
// Constructor
public SubSandwich(String b, int nv, String m){
bread = b;
numVegs = nv;
meat = m;
}
// Get the value of bread
public String getBread(){
return bread;
}
// Get the value of numVegs
public int getNumVegs(){
return numVegs;
}
// Get the value of meat
public String getMeat(){
return meat;
}
// Modify the value of bread
public void setBread(String b){
}
// Modify the value of numVegs
public void setNumVegs(int nv){
}
// Modify the value of meat
public void setMeat(String m){
}
// Calculate and return the total cost of a sub sandwich.
// The total cost is $5.00,
// plus $0.50 for each vegetable.
public double getTotalCost(){
double totalCost = 5.00 + (getNumVegs() * .50);
return totalCost;
}
public String toString(){
System.out.println("You have a ");
}
public static void main(String[]args){
SubSandwich sandwich1 = new SubSandwich("White", 2, "Chicken");
}
public static void printSandwich(SubSandwich sandwich1){
System.out.println("The sandwich you ordered is " + sandwich1.getBread() + " and " + sandwich1.getNumVegs() + " vegetables, and " + sandwich1.getMeat() + ".\n That comes to $" + sandwich1.getTotalCost() + "." );
}
}
What I'm wondering is what exactly are the 'modify' methods for? How should I use them?
I am sorry if this seems extremely vague, I thought it was, and maybe I just don't understand it enough. This is the template we were given, but I have filled in a few things that I think I understood. I tried asking questions and taking data in the main method, but I can't use non-static methods to return to the main method.
The main thing I am curious about it the modification methods and how exactly I access them are use them?