GovernorDynamic
GovernorDynamic

Reputation: 65

Set & Get Methods

Java I student here!

I am having a hard time understanding how to make set and get methods. I am working on a question in my textbook Java Programming, 9th Edition independently and I am asked to do the following:

"Create a class named Sandwich. Data fields include a String for the main ingredient (such as tuna), a String for bread type (such as wheat), ad a double for price (such as 4.99). Include methods to get and set values for each of these fields."

It then asks me to do this:

"Create an application named TestSandwich that instantiates one Sandwich object and demonstrates the use of the set and get methods."

So for the first part, I made a .java file with the following code:

public class Sandwich {
private String ingredient;
private String bread;
private double price;

public Sandwich(String ing, String bre, double pri) {
    ingredient = ing;
    bread = bre;
    price = pri;
}

public void setIngredient(String ing) {
    this.ingredient = ing;
}

public String getIngredient() {
    return ingredient;
}
public String getBread() {
    return bread;
}
public Double getPrice() {
    return price;
}
}

For the second part, I did the following:

import java.util.Scanner;
public class TestSandwich {
public static void main(String[] args) {
    String Ingredient;
    String Bread;
    Double Price;
    Scanner keyboard = new Scanner(System.in);

    System.out.println("MAKE A SANDWICH");
    System.out.println("Enter an ingredient:  ");
    Ingredient = keyboard.nextLine();
    System.out.println("Enter bread:  ");
    Bread = keyboard.nextLine();
    System.out.println("Enter a price");
    Price = keyboard.nextDouble();

    Sandwich obj = new Sandwich(Ingredient, Bread, Price);

    System.out.println("The ingredient is " + obj.getIngredient());
    System.out.println("The bread is " + obj.getBread());
    System.out.println("The price is " + obj.getPrice());       
}
}

I completed this and it works well, however I realize that I did not create any set methods. Could someone explain to me a better way to do this according to the directions? I'm sure that this way works but I would like to do it by the book and be able to understand it better. I'm not sure where to start with creating the set methods. Please let me know. Thanks so much!

PS: This is NOT homework, I'm just trying to understand this a little better.

-Mark

Upvotes: 3

Views: 2737

Answers (3)

arunken
arunken

Reputation: 497

Consider this code in your class named Sandwich :

public Sandwich(String ing, String bre, double pri) {
ingredient = ing;
bread = bre;
price = pri;
}

This is called a constructor, a special kind of method that is having the same name as that of the class. But it does not return a value. This constructor is accepting three parameters, of which two are strings and one is a double value. In the constructor body you are actually setting values that are passed to the constructor as parameters and so you can consider this as a setter method which is setting three values at once.

Now consider this code inside the same class :

public void setIngredient(String ing) {
this.ingredient = ing;
}

This method is a setter method which sets only one value, i.e. ingredient. You can create other setter methods as well like this giving any name you want to. For example setBread and setPrice method inside the Sandwich class as follows:

public setBread(String bre) {
bread = bre;
}
public setPrice(double pri) {
price = pri;
}

You can either use the constructor to set the values or the "setter methods"(It is just a normal method that is used to accept and set the values). You can use a single method to set all the values in one go, which is what the constructor is doing or use individual methods to set each values like the setter methods we have defined.

If you are using a single method to set all values at once(in this case the constructor) then during the time of instantiating Sandwich class you need to pass all the values at once to the constructor like you did this :

Sandwich obj = new Sandwich(Ingredient, Bread, Price);

If you do not pass three variables to the constructor at once in the correct order, then compilation error will occur. As you already have a constructor setting three values, the other setter methods are not of great use except when you need to change the values afterwards. I hope this helps.

Upvotes: 2

Jose
Jose

Reputation: 124

Here you create an object with these values

Sandwich obj = new Sandwich(Ingredient, Bread, Price);

Here you create an empty object and then set the values

Sandwich obj = new Sandwich();
obj.setIngredient(Ingredient);
obj.setBread(Bread);
obj.setPrice(Price);

Upvotes: 3

GhostCat
GhostCat

Reputation: 140613

Simple go like:

System.out.println("Enter another price");
double newPrice = keyboard.nextDouble();
obj.setPrice(newPrice);

and print your obj before / after that call (and of course: @Overwrite toString() on that class to get meaningful output).

Upvotes: 1

Related Questions