0

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?

asdfghjklm
  • 643
  • 2
  • 7
  • 18

2 Answers2

2

Your constructor is perfectly fine. It sets those private variables at the top of the class.

public class SubSandwich{
    private String bread;
    private int numVegs;
    private String meat;

    // Constructor
    public SubSandwich(String b, int nv, String m){
        bread = b;
        numVegs = nv;
        meat = m;
    }

Now, you don't understand setters? Try replacing the constructor bread = b with setBread(b). In other words, the setter methods are more or less subsets of operations done by the constructor. It sets values, nothing complex about that.

Now, this isn't exactly modeling a real world sandwich, but if you want to switch the bread for one particular sandwich

SubSandwich blah = new SubSandwich("White", 2, "Chicken");
printSandwich(blah);  // prints white 
blah.setBread("wheat");
printSandwich(blah); // prints wheat 

You need to implement your methods to reflect that update

You also could implement the toString method, so you can simply do

System.out.println(blah);
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • I think my main problem here is that I still don't understand what the modify methods are used for, I have changed bread = b; meat = m, numVeg = nv; to the corresponding things – asdfghjklm Nov 10 '16 at 03:45
  • Though setBread() the method is still empty and I don't know how to draw data to it – asdfghjklm Nov 10 '16 at 03:45
  • You don't have to replace those lines in the constructor. Those methods should be *functional equivalent* to the lines you replaced. One line per method for example `bread = b;` Done... – OneCricketeer Nov 10 '16 at 03:49
  • Thank you for all your help! – asdfghjklm Nov 10 '16 at 03:59
1

For example

   public void setBread(String b){
    bread = b;
    }

Basically what you're doing is modifying the private variable 'bread' which is being set to whatever 'b' is. When you create a SubSandwich object in your main method you can now decide that 'blah' uses Flat bread instead of White bread.

SubSandwich blah = new SubSandwich("White", 2, "Chicken");
blah.setBread("Flat");
FireWolf
  • 21
  • 6