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

Create Person class that implements the Comparable interface. A Person has a fir

ID: 3625492 • Letter: C

Question

Create Person class that implements the Comparable interface. A Person has a first name and a last name. Your class must implement the standard mutator (sets) and accessor (gets) methods along with a toString method.

2. Create a Student class that extends Person, therefore a Student has a first name and last name and also a 5-digit student identification number. Your class must implement the standard mutator and accessor methods along with a toString method.

3. Create an Instructor class that also extends Person. An instructor will have a first
name, last name, and a department such as “Math” or “CSCI”, etc. Your class must implement the standard mutator and accessor methods along with a toString method.
4. Using the Object-Oriented Programming concept of “Composition” and the Java class ArrayList, create a class named StudentStack that includes a private instance variable of type Arraylist as a stack collection and implements all 5 of the operations of a Stack collection.
a. A Stack implements the following methods (E means element type): boolean empty() Tests if this stack is empty.

E peek() Looks at the object at the top of this stack without removing it from the stack.

E pop() Removes the object at the top of this stack and returns that object as the value of this function.

E push(E item) Pushes an item onto the top of this stack.

int search(Object o) Returns the 1-based position where an object is on this stack.

Explanation / Answer

Person.java

public class Person {
    private String firstName;
    private String lastName;
    public Person(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }
    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    public String toString() {
        return lastName+", "+firstName;
    }
}

-----------------------------------------------------

Student.java

public class Student extends Person {
    private String id;
    public Student(String firstName, String lastName, String id) {
        super(firstName,lastName);
        this.id = id;
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
   
    public String toString() {
        return super.toString()+ " " + id;
    }
   
}

------------------------------------------------------------

Instructor.java

public class Instructor extends Person {

    private String department;
   
    public Instructor(String firstName, String lastName, String department) {
        super(firstName, lastName);
        this.department = department;
    }

    public String getDepartment() {
        return department;
    }

    public void setDepartment(String department) {
        this.department = department;
    }
   
    public String toString() {
        return super.toString() + " " + department;
    }

}

--------------------------------------------------------------------

StudentStack.java

import java.util.*;

public class StudentStack {
    private ArrayList<Student> stack;
    public boolean empty() {
        return stack.size() == 0;
    }
    public Student peek() {
        return stack.get(stack.size()-1);
    }
    public Student pop() {
        return stack.remove(stack.size()-1);
    }
    public void push(Student s) {
        stack.add(s);
    }
    public int search(Object o) {
        for (int i=0; i < stack.size(); i++) {
            if (stack.get(i).equals(o)) return i+1;
        }
        return -1;
    }
}