Question: Create a Person class. The class stores first name, last name, and email address. Create a class named Student that extends the Person class. This
Create a Person class. The class stores first name, last name, and email address. Create a class named Student that extends the Person class. This class stores a student id, major, student level (freshman, sophomore, junior, or senior).
Create a College class with the main method.
Student (example data)
Name: Frank Jones Email: frank44@hotmail.com Student id: M10293 Major: Business Level: Sophomore
Create a class named Instructor that inherits from the Person class. This class stores a social security number, subject area taught and number of years worked as an instructor.
Faculty (example data)
Name: Anne Price Email: anne@nctc.edu Social security number: 111-11-1111 Subject area: Physics for 3 years.
Ask if you want to see the student or instructor information. Based on the response, display the appropriate information. The individual subclasses should handle the information for the student and the instructor.
--------------------
Further clarification:
In main, ask if they are a student or an instructor. Create the appropriate object and update the attributes. Then based on their response for a student or instructor, you will either go to the Student class or the Instructor class and display the information.
For extra credit (10 points): Create an array of student and instructor objects to store multiple instances.
Example output for the program on inheritance:
Are you a student or instructor: student
OUTPUT: Name: Frank Jones Email: frank44@hotmail.com Student id: M10293 Major: Business Level: Sophomore
Are you a student or instructor: instructor
OUTPUT: Name: Anne Price Email: anne@nctc.edu Social security number: 111-11-1111 Subject area: Physics for 3 years.
---------------------------
Class Layout:
class Person { }
class Student extends Person { }
class Instructor extends Person { }
public class College {
class Student student; class Instructor instructor;
main () {
// question: add student or instructor? // create student object and populate the data in the object // create instructor object and populate the data in the object // question: Ask if you want to see the student or the instructor? // Display either the student information or the instructor information } }
package college; import java.util.Scanner;
// Person CLASS class Person { private String firstName; private String lastName; private String emailAddress;
public Person() { // initialize the three class attributes firstName, lastName, emailAddress }
public Person(String emailAddress) { this.firstName = ""; this.lastName = ""; this.emailAddress = emailAddress; }
public Person(String firstName, String lastName) { this.firstName = firstName; this.lastName = lastName; this.emailAddress = ""; }
public Person(String firstName, String lastName, String emailAddress) { // initialize the three class attributes firstName, lastName, emailAddress }
//Create setters and getters for firstName, lastName, emailAddress } // Instructor CLASS class //Define the Instructor class as a subclass of Person {
private long SSN; private String subject; private long yearsWorked;
public Instructor() { this.SSN = 0; this.subject = ""; this.yearsWorked = 0; }
public Instructor(String subject) { //initialize the class attibutes SSN, subject, yearsWorked }
public Instructor(long SSN, long yearsWorked) { //initialize the class attibutes SSN, subject, yearsWorked }
public Instructor(long SSN, String subject, long yearsWorked) { //initialize the class attibutes SSN, subject, yearsWorked } //Create setters and getters for SSN, subject, yearsWorked public void addInstructor() { Scanner input = new Scanner(System.in); String userValue = ""; System.out.println("*** INSTRUCTOR INFORMATION ***"); System.out.print("First Name ?:"); userValue = input.nextLine(); setFirstName(userValue); System.out.print("Last Name ?:"); userValue = input.nextLine(); setLastName(userValue); System.out.print("Email Address ?:"); userValue = input.nextLine(); setEmailAddress(userValue); System.out.print("SSN ?:"); this.SSN = input.nextLong(); input.nextLine(); System.out.print("Subject ?:"); this.subject = input.nextLine(); System.out.print("Years Worked ?:"); this.yearsWorked = input.nextLong(); input.nextLine(); }
public void showInstructor() { System.out.println("*** DISPLAY INSTRUCTOR INFORMATION ***"); System.out.println("Name :" + getFirstName() + " " + getLastName()); System.out.println("Email Address :" + getEmailAddress()); System.out.println("Social Security Number :" + this.SSN); System.out.println("Subject Area :" + this.subject + " for " + this.yearsWorked); } } // Student CLASS
class //Create the Student class as a subclass (child of Person) {
private long studentId; private String major; private String studentLevel;
//Complete the constructor. Initialize the attributes studentID, major, studentLevel public Student() { }
//Complete the overloaded constructor.Initialize the attributes studentID, major, studentLevel public Student(long studentId) { } //Complete the overloaded constructor.Initialize the attributes studentId, major, studentLevel public Student(String major, String studentLevel) { } //Complete the overloaded constructor.Initialize the attributes studentId, major, studentLevel public Student(long studentId, String major, String studentLevel) { } //Create setters and getters for studentId, major, studentLevel public void addStudent() { Scanner input = new Scanner(System.in); String userValue = ""; System.out.println("*** STUDENT INFORMATION ***"); System.out.print("First Name :"); userValue = input.nextLine(); setFirstName(userValue); System.out.print("Last Name :"); userValue = input.nextLine(); setLastName(userValue); System.out.print("Email Address :"); userValue = input.nextLine(); setEmailAddress(userValue); System.out.print("Student Id :"); studentId = input.nextLong(); input.nextLine(); System.out.print("Major :"); this.major = input.nextLine(); System.out.print("Student Level :"); this.studentLevel = input.nextLine(); }
public void showStudent() { System.out.println("*** DISPLAY STUDENT INFORMATION ***"); System.out.println("Name :" + getFirstName() + " " + getLastName()); System.out.println("Email Address :" + getEmailAddress()); System.out.println("Student Id :" + this.studentId); System.out.println("Major :" + this.major); System.out.println("Student Level :" + this.studentLevel); } } // COLLEGE CLASS
public class College { // Create a class attribute called student of type Student // Create a class attribute called instructor of type Instructor
public College() { this.student = new Student(); this.instructor = new Instructor(); }
public College(Student student) { this.student = student; this.instructor = new Instructor(); }
public College(Instructor instructor) { this.student = new Student(); this.instructor = instructor; }
public College(Student student, Instructor instructor) { this.student = student; this.instructor = instructor; }
public College(Instructor instructor, Student student) { this.student = student; this.instructor = instructor; } //Create setters and getters for student and instructor
public void addSudent() { student.addStudent(); }
public void addInstructor() { instructor.addInstructor(); }
public void displayStudent() { student.showStudent(); }
public void displayInstructor() { instructor.showInstructor(); } // --------------
/** * *** MAIN LOGIC * * @param args */ public static void main(String[] args) { String flag = ""; String userValue = ""; College Baylor = new College(); Scanner userInput = new Scanner(System.in);
do { System.out.print(" Add student or instructor (s/i)?"); userValue = userInput.nextLine();
if ("s".equalsIgnoreCase(userValue)) { Student student = new Student(); student.addStudent(); Baylor.setStudent(student);
} else if ("i".equalsIgnoreCase(userValue)) { Instructor instructor = new Instructor(); instructor.addInstructor(); Baylor.setInstructor(instructor); } else { System.out.println("ERROR: Invalid Entry"); }
System.out.print(" Display student or instructor (s/i)?"); userValue = userInput.nextLine(); if ("s".equalsIgnoreCase(userValue)) { Baylor.getStudent(); Baylor.displayStudent(); } else if ("i".equalsIgnoreCase(userValue)) { Baylor.getInstructor(); Baylor.displayInstructor(); }
System.out.print(" Would you like to continue or exit(c/x):"); flag = userInput.nextLine(); } while (flag.equalsIgnoreCase("C")); }
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
