Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

HW 3: Objects and Classes Write the following classes: 1. Person class (10 point

ID: 3672385 • Letter: H

Question

HW 3: Objects and Classes
Write the following classes:
1. Person class (10 points)
Fields (should be private): String name int age
Methods (should be public): Constructor(String name, int age) Constructor (String name) No-arg Constructor Accessors and Mutators for each private field
2. Person class tester – should test each method in the person class (10 points)
3. Student class (10 points)
Fields (should be private): String name int age int id
Methods (should be public): Constructor(String name, int age, int id) Constructor (String name, int age) Constructor (String name) No-arg Constructor Accessors and Mutators for each private field
4. Student class tester – should test each method (10 points)
5. GradStudent class (10 points)
Fields (should be private): String name int age int id Person advisor
<Continued on next page>

GradStudent class continued
Methods (should be public): Constructor(String name, int age, int id, Person advisor) Accessors and Mutators for each private field
6. GradStudent class tester – should test each method (10 points)
7. Course class (20 points)
Fields (should be private): String number String name GradStudent instructor Student[] students – the students in the class max enrollment is 20.
Methods (should be public): Constructor(String number, String name, GradStudent instructor) Accessors and Mutators for each private field boolean add(Student student) – adds a student to the class if space is available. Returns true if the student is added or false if the student is not added. Student drop(int id) – removes the student from the class if they’re enrolled and returns a reference to the Student otherwise returns null. Be sure to shift elements in the array down as necessary to reuse the space for the dropped student. void classList() – outputs a numbered list of the students in the course containing the students name and id.
8. Course tester – tests all the methods in the course class. (20 points)
Remember to follow Java convention and include comments (including Javadoc).
Must be in java program

Explanation / Answer

package com.test.java.chegg;

/**
*
* @author YourName
*
*/
public class Person {
   private String name;
   private int age;

   // Constructor with two arguments
   Person(String personName, int personAge) {
       this.name = personName;
       this.age = personAge;
   }

   // Constructor with single argument
   Person(String personName) {
       this.name = personName;
   }

   // Default Constructor
   Person() {

   }

   /**
   * @return the name
   */
   public String getName() {
       return name;
   }

   /**
   * @param name
   * the name to set
   */
   public void setName(String name) {
       this.name = name;
   }

   /**
   * @return the age
   */
   public int getAge() {
       return age;
   }

   /**
   * @param age
   * the age to set
   */
   public void setAge(int age) {
       this.age = age;
   }
public String toString()
{
   return "Name: "+name+" Age: "+age;
}
}

PersonTester.java

package com.test.java.chegg;

/**
*
* @author YourName
*
*/
public class PersonTester {
   public static void main(String[] args) {
       // Creating Person object with default constructor
       Person person = new Person();
       // Accessing public methods
       person.setAge(23);
       person.setName("jhon");
       System.out.println(person.toString());
       // Creating person2 object with constructor having 2 arguments
       Person person2 = new Person("pavan", 27);
       System.out.println(person2.toString());
       // Creating person3 object with constructor having 1 argument
       Person person3 = new Person("kumar");
       person3.setAge(23);
       System.out.println("Name: " + person3.getName() + " Age: "
               + person3.getAge());

   }
}

Student.java

/**
*
*/
package com.test.java.chegg;

/**
* @author yourName
*
*/
public class Student {
   private String name;
   private int age;
   private int id;

   // Constructor with 3 arguments
   Student(String studentName, int studentAge, int studentId) {
       this.name = studentName;
       this.age = studentAge;
       this.id = studentId;
   }

   // Constructor with 2 arguments
   Student(String studentName, int studentAge) {
       this.name = studentName;
       this.age = studentAge;
   }

   // Constructor with 1 argument
   Student(String studentName) {
       this.name = studentName;
   }

   // Default Constructor
   Student() {

   }

   /**
   * @return the name
   */
   public String getName() {
       return name;
   }

   /**
   * @param name
   * the name to set
   */
   public void setName(String name) {
       this.name = name;
   }

   /**
   * @return the age
   */
   public int getAge() {
       return age;
   }

   /**
   * @param age
   * the age to set
   */
   public void setAge(int age) {
       this.age = age;
   }

   /**
   * @return the id
   */
   public int getId() {
       return id;
   }

   /**
   * @param id
   * the id to set
   */
   public void setId(int id) {
       this.id = id;
   }

   public String toString() {
       return "Name: " + name + " Age: " + age + " Id: " + id;
   }

}

StudentTester.java

package com.test.java.chegg;

public class StudentTester {
   public static void main(String[] args) {
       // Creating Object with default constructor
       Student student = new Student();
       // Accessing public methods
       student.setAge(23);
       student.setId(1004);
       student.setName("pavan");
       System.out.println("Name: " + student.getName() + " Age: "
               + student.getAge() + " Id: " + student.getId());
       // Creating object with constructor having one argument
       Student student2 = new Student("kumar");
       student2.setAge(24);
       student2.setId(1003);
       System.out.println(student2.toString());

       // Creating object with constructor having two argument
       Student student3 = new Student("Jhon", 24);
       student3.setId(1002);
       System.out.println(student3.toString());

       // Creating object with constructor having 3 argument
       Student student4 = new Student("paul", 28, 1001);
       System.out.println(student4.toString());
   }
}

GradStudent.java

package com.test.java.chegg;

/**
*
* @author YourName
*
*/
public class GradStudent {
   private String name;
   private int age;
   private int id;
   private Person advisor;

   GradStudent(String name, int age, int id, Person advisor) {
       this.name = name;
       this.age = age;
       this.id = id;
       this.advisor = advisor;
   }

   /**
   * @return the name
   */
   public String getName() {
       return name;
   }

   /**
   * @param name
   * the name to set
   */
   public void setName(String name) {
       this.name = name;
   }

   /**
   * @return the age
   */
   public int getAge() {
       return age;
   }

   /**
   * @param age
   * the age to set
   */
   public void setAge(int age) {
       this.age = age;
   }

   /**
   * @return the id
   */
   public int getId() {
       return id;
   }

   /**
   * @param id
   * the id to set
   */
   public void setId(int id) {
       this.id = id;
   }

   /**
   * @return the advisor
   */
   public Person getAdvisor() {
       return advisor;
   }

   /**
   * @param advisor
   * the advisor to set
   */
   public void setAdvisor(Person advisor) {
       this.advisor = advisor;
   }

}

GradStudentTester.java

package com.test.java.chegg;

public class GradStudentTester {
   public static void main(String[] args) {
       Person person = new Person("Rohit", 23);
       GradStudent gradeStudent = new GradStudent(person.getName(),
               person.getAge(), 1004, person);
       System.out.println(gradeStudent.getAdvisor() + " Id: "
               + gradeStudent.getId());

   }
}

Course.java

package com.test.java.chegg;

public class Course {
   private String number;
   private String name;
   private GradStudent instructor;
   private Student[] students;
   int studentCount = 0;

   Course(String number, String name, GradStudent instructor) {
       this.number = number;
       this.name = name;
       this.instructor = instructor;
       students = new Student[20];
   }

   /**
   * @return the number
   */
   public String getNumber() {
       return number;
   }

   /**
   * @param number
   * the number to set
   */
   public void setNumber(String number) {
       this.number = number;
   }

   /**
   * @return the name
   */
   public String getName() {
       return name;
   }

   /**
   * @param name
   * the name to set
   */
   public void setName(String name) {
       this.name = name;
   }

   /**
   * @return the instructor
   */
   public GradStudent getInstructor() {
       return instructor;
   }

   /**
   * @param instructor
   * the instructor to set
   */
   public void setInstructor(GradStudent instructor) {
       this.instructor = instructor;
   }

   /**
   * Adds a student to the class if space is available. Returns true if the
   * student is added or false if the student is not added.
   *
   * @param student
   * @return true/false
   */
   public boolean add(Student student) {
       if (studentCount < 20) {
           students[studentCount] = student;
           studentCount++;
           return true;
       } else {

           return false;
       }

   }

   /**
   * Removes the student from the class if they’re enrolled and returns a
   * reference to the Student otherwise returns null
   *
   * @param id
   * @return true/false
   */
   public Student drop(int id) {
       Student tempStudent;
       for (int i = 0; i < students.length; i++) {
           if (null != students[i]) {
               if (students[i].getId() == id) {
                   tempStudent = students[i];
                   students[i] = students[i + 1];
                   return tempStudent;
               }
           }
       }
       return null;
   }

   /**
   * Outputs a numbered list of the students in the course containing the
   * students name and id
   */
   public void classList() {
       for (int i = 0; i < students.length; i++) {
           if (null != students[i])
               System.out.println(students[i]);
       }
   }

   /**
   * @return the students
   */
   public Student[] getStudents() {
       return students;
   }
}

CourseTester.java

package com.test.java.chegg;

public class CourseTester {
   public static void main(String[] args) {
       Person person = new Person();
       GradStudent instructor = new GradStudent("Kumar", 23, 1001, person);
       Course course = new Course("1", "kumar", instructor);
       Student student = new Student("Jhon", 21, 234);
       if (course.add(student)) {
           System.out.println("Student Added Successfully");
       } else {
           System.out.println("No More Students Can Be Added");
       }
       Student student2 = new Student("Jhon2", 22, 235);
       if (course.add(student2)) {
           System.out.println("Student Added Successfully");
       } else {
           System.out.println("No More Students Can Be Added");
       }

       Student student3 = course.drop(235);
       System.out.println(student3.toString());

       course.classList();
   }
}