0

In the below code I've already declared that room = r; subject = s; and time = t; in the user defined constructor, so why is it necessary to do so again in set methods, my lecturer specifically asked that we add set methods for the room subject and time but it's redundant code as when I comment it out it still works. Do you only need to include set methods when there is no used defined constructor? What could be the advantage of having them set methods there?

class LectureTest{
public static void main (String [] args){

Lecture l1 = new Lecture(140, "Comp", 5);
l1.display();

Lecture l2 = new Lecture(280, "Sports", 3);
l2.display();

Lecture l3 = new Lecture(101, "Business", 5);
l3.display();

Lecture l4 = new Lecture(360, "Shooting", 4);
l4.display();

Lecture l5 = new Lecture();
l5.display();

 }
}//end of LectureTest

class Lecture{
private int room;
private String subject;
private int time;

Lecture(int r, String s, int t){
    room = r;
    subject = s;
    time = t;
    }

Lecture(){}

public void setroomNumber(int r){
  room = r;
}
public void setSubject(String s){
  subject = s;
}
public void setTime(int t){
   time = t;
}
public int getroomNumber(){
  return room;
}
public String getSubject(){
  return subject;
}
public int getTime(){
  return time;
}

  public void display(){
  System.out.printf("\n" + "Room Number: " + getroomNumber()  + "\n" +     "Subject: " + getSubject()  + "\n" + "Time " + getTime() + "\n");
  }
}

3 Answers3

0

The set methods make your object mutable. If you don't have the set methods and your variables are private then the Object will be immutable. You won't be able to change the values after it is constructed...If the values need to change you would have to create a new Object.

brso05
  • 13,142
  • 2
  • 21
  • 40
0

"Setters" allow you to modify private attributes of your object after instantiating. For example:

Lecture l1 = new Lecture(140, "Comp", 5);
//Since "room" is private you can't write l1.room = 4 
//and have to use the setter method instead:
l1.setroomNumber(4);
l1.display();

They are also very useful if you want to do something if an attribute changes.

Let's assume you are using Observers, then you could call notifyObservers() or setChanged() in your setter method and never have to worry about these methods not getting called if your attribute changes.

sknt
  • 963
  • 6
  • 16
0

The constructor "initializes" your values. Let's say you have...

public class Person {
   public String name;
   public int age;

   public Person (String name, int age) {
      this.name = name;
      this.age = age;
   }

   public void setName(String name) {
      this.name = name;
   }

   public void setAge(int age) {
      this.age = age;
   } 

   public String toString() {
      String str;
      str = "My name is "+name+" and I am "+age+" years old!";
      return str;
   }
}//End of Person


public class Main {
   public static void main(String [] args) {
      Person person = new Person("Bob", 15);
      System.out.println(person.toString());
      System.out.println("Switching my name...");
      person.setName("Joe");
      System.out.println(person.toString());
   }
}//End of main

You see the difference? You should use the constructor if you want to create a new instance of the object. This way, you can set all the fields of the object at once and not need to call 490832490 setters (in this case, one for name and one for age...). You then can use the setter approach when you want to change the value of a field, PRIOR TO the object been created.

I DID ALL THIS ON THIS FORUM SO I MIGHT HAVE SYNTAX ERRORS SO CAREFUL...DIDN'T USE AN IDE IF YOU WANT TO TEST IT

Yusuf Jama
  • 123
  • 13