user1577173
user1577173

Reputation: 91

Getters, Setters, Constructors and their parameters

Ye've been very helpful so far even though I havn't been great at wording my questions. I think I almost know what I'm doing but I'm trying to get my head around the relationship between getters, setters and constructors. I have two classes as follows Student and Name and I'm confused about the relationship between the parameters in the getters and setters and constructor

If I remove the parameters from the Name constructor, i.e. this

public Name (){
this.firstName = firstName;
this.lastName = lastName;

and aslo edited the Student constructor to reflect this

public Student(int id, String firstName, String lastName, String street, String area, String city, String country, int age, char gender, String college, String course,  int level, int gradePointAverage){ //This is the list of variables called from the Student class
this.id = id;
this.name = new Name(); //I have removed the parameters here
this.name.setFirstName(firstName);
this.name.setLastName(lastName);
this.address = new Address(street, area, city, country);
this.age = age;
this.gender = gender;
this.college = college;
this.course = course;
this.level = level;
this.gradePointAverage = gradePointAverage; 

}

I'm not sure what impact it would have. I would be very greatful of someone could explain to me where I should/shouldn't have parameters and why? Understand this concept is what is holding me back from moving any further with coding at the moment.

public class Student {
    private Name name; // This is calling from the Name class, giving it the name 'name'
    private Address address; // This calls from Address, giving it the name 'address'

    private char gender;

    private String course, college;

    private int gradePointAverage, id, age, level;

    public Student(int id, String firstName, String lastName, String street, String area, String city, String country, int age, char gender, String college, String course,  int level, int gradePointAverage){ //This is the list of variables called from the Student class
        this.id = id;
        this.name = new Name(firstName, lastName);
        //this.name = new Name();
        this.name.setFirstName(firstName);
        this.name.setLastName(lastName);
        this.address = new Address(street, area, city, country);
        this.age = age;
        this.gender = gender;
        this.college = college;
        this.course = course;
        this.level = level;
        this.gradePointAverage = gradePointAverage;
    }

    public int getId(){
        return id;
    }

    public String getName(){
        return name.toString();
    }

    public String getAddress(){
        return address.toString();
    }

    public int getAge(){
        return age;
    }

    public char getGender(){
        return gender;
    }

    public String getCollege(){
        return college;
    }

    public int getLevel() {
        return level;
    }

     public String getCourse() {
        return course;
    }

    public int getGradePointAverage() {
        return gradePointAverage;
    }

    public void printStudent() {
        System.out.println("The Student " + name.toString() + " is logged under the student ID number " + id + ".");
        System.out.println("They live at " + address.toString() + " and their age is " + age + ".");
        System.out.println("Their gender is " + gender + ".");
        System.out.println("The student studies at " + college + " attending classes in " + course + ".");
        System.out.println("Their level is " + level + " and the student grade average in points is " + gradePointAverage + ".");
        System.out.println();
    }

}

and Name

public class Name{

private String firstName, lastName;

public Name (String firstName, String lastName){
//public Name (){
    this.firstName = firstName;
    this.lastName = lastName;
}

public void setFirstName(String firstName){
    this.firstName = firstName;
}

public void setLastName(String lastName){
    this.lastName = lastName;
}

//public String getFirstName() {
//  return firstName;
//}

//public String getLastName() {
//  return lastName;
//}

///** Returns first name concatenated to last name */
//public String toString() {
//  return firstName + " " + lastName;
//}

}

Upvotes: 1

Views: 7784

Answers (1)

user177800
user177800

Reputation:

The constructors are for initialization of an instance of an Object to ensure that all the minimum amount of data required for a valid object state are provided at creation time.

In your case Name should have a constructor that requires firstName and lastName because those things are what make the Name object fully initialized. This Object should work just like your Address object you have shown as well.

Otherwise if you use setXXX methods, the Name object is incomplete with the two String objects initialized to null or some other undefined state.

Upvotes: 5

Related Questions