-5

I don't really know if my question makes sense but my assignment says:

"Write a third class, StudentRecord, that has two attributes: Student stu; Address addr; and two constructors. The first constructor is given a Student object and an Address object to initialize the attributes. The second constructor is given a first name, a last name, a student ID, a gpa, a street address, a city, a state, and a zipcode and uses these to initialize the attributes"

I don't understand how exactly I'm supposed to make two constructors take info from two different java files.

Here's the code I have for the third class named "StudentRecord". I have no doubt it's incorrect.

public class StudentRecord {
   Student stu;
   Address addr;

   public StudentRecord() {
      Student stu;
      Address addr;
   }

   public StudentRecord(String _fName, String _lName, int _id, double _gpa, String _street, String _city, String _state, int _zip){

   }

   public String toString() {
      return String.format(stu + "\n" + addr);
   }
}

Here's the code I have for the TestStudentRecord class, all I get is "null null" when I run the program.

public class TestStudentRecord {
   public static void main(String[] args) {
      StudentRecord stu1 = new StudentRecord("Jane", "Brown", 182765, 2.333, "13 Flower St.", "Pulteneyville", "NY", 14386);
      System.out.println(stu1);
   }
}

All I get is "null null" when I run the program instead of the toString method giving me the student info I have typed into the test class.

For those asking for the Student and Address classes, here you go:

public class Student {
    // attributes of a Student
    private String firstName;
    private String lastName;
    private int studentId;
    private double gpa;

   /**
    * Student constructor.
    * @param _fName student's first name
    * @param _lName student's last name 
    * @param _id student's id number
    * @param _gpa students GPA
    */
    public Student(String _fName, String _lName, int _id, double _gpa) {
        firstName = _fName;
        lastName = _lName;
        studentId = _id;
        gpa = _gpa;
    }

   /**
    * getFirstName - Accessor for first name
    * @return the student's first name
    */
    public String getFirstName() {
        return firstName;
    }

   /**
    * getLastName - Accessor for last name
    * @return the student's last name
    */
    public String getLastName() {
        return lastName;
    }

   /**
    * getId - Accessor for ID
    * @return the student's ID
    */
    public int getStudentId() {
        return studentId;
    }

   /**
    * getGpa - Accessor for gpa
    * @return the student's gpa
    */
    public double getGpa() {
        return gpa;
    }

   /**
    * setFirstName - Mutator for first name
    * @param the new first name
    */
    public void setFirstName(String _fName) {
        firstName = _fName;
    }

   /**
    * setLastName - Mutator for last name
    * @param the new last name
    */
    public void setLastName(String _lastName) {
        lastName = _lastName;
    }

   /**
    * setStudentId - Mutator for ID
    * @param the new ID
    */
    public void setStudentId(int _id) {
        studentId = _id;
    }

   /**
    * setGpa - Mutator for gpa
    * @param the new gpa
    */
    public void setGpa(double _gpa) {
        gpa = _gpa;
    }

   // toString Method
   public String toString() {
      return String.format(getLastName() + ", " + getFirstName() + "\n" + "ID: " + getStudentId() + " GPA: %3.1f", getGpa());
   }
}
public class Address {
   private String street;
   private String city;
   private String state;
   private int zip;

   public Address(String _street, String _city, String _state, int _zip) {
      street = _street;
      city = _city;
      state = _state;
      zip = _zip;
   }

   // Accessors
   public String getStreet() {
      return street;
   }

   public String getCity() {
      return city;
   }

   public String getState() {
      return state;
   }

   public int getZip() {
      return zip;
   }

   // Mutators
   public void setStreet(String _street) {
      street = _street;
   }

   public void setCity(String _city) {
      city = _city;
   }

   public void setState(String _state) {
      state = _state;
   }

   public void setZip(int _zip) {
      zip = _zip;
   }

   // toString Method
   public String toString() {
      return String.format(getStreet() + "\n" + getCity() + ", " + getState() + " " + getZip());
   }
}
4
  • 2
    Your constructor StudentRecord(String _fName, String _lName, int _id, double _gpa, String _street, String _city, String _state, int _zip) has an empty body. It should assign the relevant parameters to the relevant instance variables of your class.
    – Eran
    Commented Sep 25, 2019 at 6:23
  • You are not doing anything in constructor what did you expect? Commented Sep 25, 2019 at 6:24
  • Think about the constructor of Student, could you call that when a StudentRecord gets initialized with values that belong to a Student? If you have no parametrized constructor in Student, then you could call getters and setters, at least. Main hint: You have to initialize the class attributes of StudentRecord, too, which are objects as well and have constructors, getters and setters...
    – deHaar
    Commented Sep 25, 2019 at 6:28
  • can you please share student and Address class as well so that we can provide you complete solution. also as per your code you haven't initialised your address and student class through argumented construtor. which you need to do. Commented Sep 25, 2019 at 6:30

3 Answers 3

0

would suggest you to please follow below approach to initialize your object through argumented construtor.

public class StudentRecord {
   Student stu;
   Address addr;

   public StudentRecord() {
      Student stu;
      Address addr;
   }

   public StudentRecord(String _fName, String _lName, int _id, double _gpa, String _street, String _city, String _state, int _zip){
        this.stu = new Student(_fName, _lName, _id, _gpa);
        this.addr=new Address(_street,_city,_state,_zip);
   }

   public String toString() {
      return String.format(stu + "\n" + addr);
   }
}

As you have already overided toString method in address and student class so you will get the object.

0
0

Your constructor is like below. Empty Constructor will not assign values to your class variables.

  public StudentRecord(String _fName, String _lName, int _id, double _gpa, String _street, String _city, String _state, int _zip){

   }

it should be like below as mentioned by Eran.

public StudentRecord(String _fName, String _lName, int _id, double _gpa, String _street, String _city, String _state, int _zip){
    this.stu = new Student(_fName, _lName, _id, _gp);
    this.addr = new Address(_street, _city, _state, _zip);
   }

Now your toString will have the values for stu and addr. Which will get printed.

0

You have to initilize the variables with this.{variable name} = {variable name}.
Also it is often better to chain the Constructors (see How do I call one constructor from another in Java?), in this way changes in classes, with many constructors, are way easier to implement.
I would also suggsest to rename your variables, because in Java they should not start with an underscore (see https://www.javatpoint.com/java-naming-conventions).

public class StudentRecord {
   Student student;
   Address addr;

   public StudentRecord(Student student, Address addr) {
      this.student = student;
      this.addr = addr;
   }

   public StudentRecord(String fName, String lName, int id, double gpa, String street, String city, String state, int zip){
    this(new Student(fName, lName, id, gpa),
        new Address(street,city,state,zip));
   }

   public String toString() {
      return String.format(student + "\n" + addr);
   }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.