Code for Review Exercises (Set C2)


public class PeopleTester {

   public static void main(String[] args) {
		
      StudentTutor tutor1 = new StudentTutor("Bob Wu", 102, Student.SOPHOMORE, "math");//line A
      System.out.println();
		
      System.out.println(tutor1);                                                      //line B
      System.out.println();
		
      Student student1 = new Student("Alice Flyn", 104, Student.FRESHMAN);  				
      System.out.println();
		
      Student student2 = new Student();                                                //line C
      System.out.println();
		
      Student student3 = tutor1;
      System.out.println();																
		
      System.out.println(student3);                                                    //line D
      System.out.println();
		
      Student student4 = new Student("Ima Gradstudent", 106);  							
      System.out.println();
		
      FacultyMember facultyMember1 = new FacultyMember("Ima Gradstudent", 106, 122);   //line E
      System.out.println();
		
      FacultyMember facultyMember2 = new FacultyMember("Ima Gradstudent", 106, 122);
      System.out.println();
		
      System.out.println(facultyMember1.equals(facultyMember2));                       //line F
      System.out.println(facultyMember1.equals(student4));                             //line G
      System.out.println();
		
																	
      Person[] people = new Person[3];                                                 //line H
      people[0] = tutor1;
      people[1] = student2;
      people[2] = facultyMember1;
		
		
      tutor1.whoAmI();	                                                               //line I
      System.out.println();
		
      /*
      AvailableForHelp helpfulPerson = facultyMember1;                                 //line J
      System.out.println(helpfulPerson.getTimesAvailable());                           //line K
      */
   }
}

public class StudentTutor extends Student implements AvailableForHelp {
	
	private String discipline_;
	
	public StudentTutor(String name, int idNum, int year, String discipline) {
		super(name, idNum, year);
		discipline_ = discipline;
		System.out.println("A student tutor was created");
	}
	
	public String toString() {
		return super.toString() + ", and is a tutor in " + discipline_;
	}

	public void help() {
		System.out.println("Advice: Work as many problems as you can before the test!");
	}

	public String getTimesAvailable() {
		return "See my facebook page";
	}
}

public class Student extends Person {
	
	public static final int GRADUATED = 0;
	public static final int FRESHMAN = 1;
	public static final int SOPHOMORE = 2;
	public static final int JUNIOR = 3;
	public static final int SENIOR = 4;
	
	private int year_;
	
	public Student(String name, int idNum, int year) {
		super(name, idNum);
		year_ = year;
		System.out.println("A student named " + name + " was created");
	}
	
	public Student(String name, int idNum) {
		super(name, idNum);
		year_ = GRADUATED;
		System.out.println("A graduated student named " + name + " was created");
	}
	
	public Student() {
		System.out.println("A student was created.");
	}
	
	public String toString() {
		String output = super.toString() + " who is also a ";
		switch (year_) {
			case FRESHMAN: output += "freshman"; break;
			case SOPHOMORE: output += "sophomore"; break;
			case JUNIOR: output += "junior"; break;
			case SENIOR: output += "senior"; break;
			default: output += "graduated student";
		}
		return output;
	}
	
	public String greeting() {
		return "What's up?";
	}

}

public class FacultyMember extends Person implements AvailableForHelp {
	
	int officeNumber_;
	
	public FacultyMember(String name, int idNum, int officeNumber) {
		System.out.println("A faculty member with name " + name_ + " was created.");
		name_ = name;
		idNum_ = idNum;
		officeNumber_ = officeNumber;
	}
	
	public int getOfficeNumber() {
		return officeNumber_;
	}

	public String getTimesAvailable() {
		return "See my office hours in the syllabus.";
	}

	public void help() {
		System.out.println("Advice: Study, get enough sleep, and eat healthy!");
	}
	
	public String greeting() {
		return "Welcome to my class!";
	}
	
	public boolean equals(Person someoneElse) {
		if (someoneElse instanceof FacultyMember) {
			return (this.getOfficeNumber() == 
                                            ((FacultyMember) someoneElse).getOfficeNumber());
		}
		else {
			return false;
		}
	}

}

public abstract class Person {

	public String name_;	
	public int idNum_;
	
	public Person() {
		name_ = "John Doe";
		System.out.println("A John Doe was created");
	}
	
	public Person(String name, int idNum) {
		name_ = name;
		idNum_ = idNum;
		System.out.println("A person with a given name and id created");
	}
	
	public int getIdNum() {
		return idNum_;
	}
	
	public void whoAmI() {
		System.out.println("I am a ...");
		if (this instanceof Person) {
			System.out.println("person");
		}
		if (this instanceof Student) {
			System.out.println("student");
		}
		if (this instanceof StudentTutor) {
			System.out.println("student tutor");
		}
	}
	
	public abstract String greeting();
	
	public String toString() {
		return "person " + idNum_ + " named " + name_;
	}
	
	public boolean equals(Object o) {
		if (o instanceof Person) {
			return this.idNum_ == ((Person) o).getIdNum();
		}
		return false;
	}

}

public interface AvailableForHelp {
	
	String getTimesAvailable();
	void help();

}