I need help making a Driver/Runner in Java for these four classes listed below.
ID: 3776945 • Letter: I
Question
I need help making a Driver/Runner in Java for these four classes listed below. I am using JGrasp
Specific Requirements
We will begin the development of a simple University registration system that keeps track of students, instructors, and courses. We will have students and instructors that share many common traits such as names and social security numbers. By using the concept of inheritance, we can separate out the common entities that both Students and Instructors have and place these common elements in a separate Person class. Then we can inherit from this Person class and avoid writing the same code several times. Correspondingly, in our classes, if we have code that appears several times, we can abstract it out and place it in a separate method and have the method called when we need this particular functionality. In Instructor and Student, we will abstract out the common code in the two constructors (for each class) and place it in a method called setAttributes. This way we have the code in a single place and, if we find errors, we only have to change it in one place to fix it.
In this project you will write five classes. The first will be a class that represents a person. The second will be a class that represents a college student. The third will an instructor. The fourth class will simulate a class membership list. The fifth will be a test class to verify exhaustively the correctness of the other four classes; you should test at least once for each method.
Notice that both the Student Class and the Instructor Class inherit from the Person Class. A student is a special Person. An Instructor is also a special person, but is a different kind of special person than is a Student. It is important that you take advantage of the functionality of the super-class when you write these subclasses. The primary advantage of inheritance is to be able to reuse code.
Person Class
Instance Fields
// You need not use these names, but you must use these types
String firstName
String lastName
String ssn
Constructors
// Default - Sets last name, first name and ssn to "unknown"
Person()
// Three string parameters are first name, last name and ssn
// Be certain that the parameters are in that order
Person ( String, String, String )
Methods
// Sets the person's first name, last name and ssn
// Be certain that the parameters are in that order
setAttributes( String, String, String )
// Returns a String containing the person’s first name
getFirstName()
// Returns a String containing the person’s last name
getLastName()
// Returns a String containing the person’s ssn
getSSN()
// This method is a member of the Comparable interface.
// Briefly, it compares the parameter Object to this object
// and decides whether this object is less than, equal to or greater than
// the parameter Object. It then returns an integer indicating the
// outcome of that comparison. The possible values for the return value
// are a negative value, zero or a positive value.
//
// There are several things to consider here. First of all we need a policy.
// This method is going to be used to assist in getting persons into
// alphabetical order, so the policy will be that this method will
// look at both last name and first name when comparing persons. It
// will not look at ssn. The first names need be
// compared only if the last names are the same. Remember that case
// matters. If ASCII codes are used to compare Strings, all uppercase
// characters come before all lowercase characters.
// ASCII --- 'A' = 65, 'Z' = 90, 'a' = 97
// (ex. Zac comes before alice alphabetically)
//
// One option would be to convert all names to the same case for
// purposes of comparison. In this project we will simply assume that
// all names are supplied correctly (always dangerous) and compare them
// as is. (Let "Zac" come before "al" )
// Finally, remember to indicate that Person implements the
// comparable interface in your class declaration statement and
// be sure that your parameter type is Object and not Student
compareTo( Object )
// Returns a boolean indicating whether the parameter Object is equal to
// this object
// Note: the policy will be that two persons with the same ssn are duplicates
// even if the names are different
equals( Object )
// Returns a String containing the person’s information
// Use roughly the format shown below. You need not count spaces and make
// everything line up perfectly. You might use a tab character to
// separate the first name and the ssn.
toString()
// Here is the desired format
// Smith, Julie 123-45-6789
Student Class (a subclass of Person)
Instance Fields
// You need not use these names, but you must use these types
String yearInSchool
// Note: The student id should be unique for each student
// The student id is not their ssn
int id
// Note: One way to assign a unique ID for each student is to declare a
// static variable in the Student class and use it to keep track of the
// next ID number to be assigned and increment it each time a new student
// is created.
// The policy in this case will be that the first student is assigned
// ID number 1000, the next student ID number 1001, etc.
static int nextID
Constructors
// Default - Sets last name, first name and ssn to "unknown"
// assigns the next sequential ID number and sets year in school
// to "unknown
Student()
// Four string parameters are first name, last name, ssn and year in school
// Be certain that the parameters are in that order
// This constructor should also assign the next sequential ID number
Student( String, String, String, String )
Methods
// Sets the student’s first name, last name, ssn and year in school
// Be certain that the parameters are in that order
setAttributes( String, String, String, String )
// Returns an integer containing the student’s ID number
getID()
// Returns a boolean indicating whether the parameter Object is equal to
// this object
// Note: the policy will be that two students with the same ID are duplicates
// even if the names are different
equals( Object )
// Returns a String containing the student’s information
// Use roughly the format shown below. Use the tab character for spacing
toString()
// here is the desired format
// Smith, Julie 123-45-6789 Sophomore 1001
Instructor Class (a subclass of Person)
Instance Fields
// You need not use these names, but you must use these types
// rank can be one of four values: instructor, assistant, associate or professor
String rank
// the salary will depend on the rank of the instructor
double salary
// Note: The instructor id should be unique for each instructor and starts at 10000
// The instructor id is not their ssn
// Look at the definition for student for ideas on unique IDs.
// But remember, this is a different id than the student id.
int id
Constructors
// Default - sets the salary to zero and sets rank to "unknown"
// - Sets last name, first name and ssn to "unknown"
Instructor()
// Four string parameters are first name, last name, ssn and rank
// Be certain that the parameters are in that order
// This constructor should also assign salary based on rank
// if the rank is the salary is
// instructor 20,000
// assistant 40,000
// associate 60,000
// professor 80,000
// creates an unique, consecutive id for the instructor
Instructor( String, String, String, String )
Methods
// Sets the instructor's first name, last name, ssn, rank and salary
// Be certain that the parameters are in that order
setAttributes( String, String, String, String )
// Returns an double containing the instructor's salary
getSalary()
// Returns a boolean indicating whether the parameter Object is equal to
// this object
// Note: the policy will be that two instructors with the same ID are duplicates
// even if the names are different
equals( Object )
// Returns a String containing the student information
// Use roughly the format shown below. You need not count spaces
// and make everything line up perfectly, use tabs instead.
toString()
// here is the desired format
// Smith, Julie 123-45-6789 Instructor 20000.00
The fourth class we will write is a class that will contain an Instructor and a group of students. This Course will also have a name and a room number. The course can be used in further developing our registration system. It contains an array of type Student to hold the student list. The array will not be full, so you have to keep track of the number of students actually inserted into the array. We will insert a group of students that may be in any order. After we insert all the students, then we need to sort them in alphabetical order. Remember that the person has a compareTo method that you can use to do the comparison while sorting the students.
Course Class
Instance Fields
// You need not use these names, but you must use these types
String courseName
int room
Instructor instructor
int numberStudents
Student [] studentList
Constructors
// Default - set the name to "unknown", the room number to 0,
// the number of students to zero and the instructor will be set
// to "unknown"
Course()
// sets the name, the room number for the course. Number of students will
// be set to zero and the instructor will be set to "unknown"
Course( String, int )
Methods
// Sets the course name, room number and number of students. Be certain
// that the parameters are in that order. Initializes the size of the array
// to hold 30 students. Note that it is empty at this point.
setAttributes( String, int, int )
// Returns the instructor assigned to this course
getInstructor()
// Assigns an instructor to the course
setInstructor( Instructor )
// Returns the name of this course
getCourseName()
// Assigns new name to the course
setCourseName( String )
// Returns the room number assigned to this course
getRoomNumber()
// Assigns a room number to the course
setRoomNumber( int )
// Adds a student to the course
void addStudent(Student)
// Sorts all the students in the course
void sortStudents()
// Returns a boolean indicating whether the parameter Object is equal to
// this object
// Note: the policy will be that courses with the same name and the same
// instructor are duplicates even if the student in the course are different
equals( Object )
// Returns a String containing the studentís information
// Use roughly the format shown below. You need not count spaces
// and make everything line up perfectly
toString()
// here is the desired string format
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Course: CIS 3020 Room: 333
// Instructor: Kerr, Lorn 123-456-7890 Instructor 20000.0
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Students:
// Jones, Paul 123-45-6789 Senior 1005
// Smith, Julie 123-45-6789 Sophomore 1001
// ... etc.
Explanation / Answer
package com.ip.resource;
public class Person implements Comparable<Person> {
// declaration of variables
protected String firstName;
protected String lastName;
protected String ssn;
public Person() {
this.firstName = firstName;
this.lastName = lastName;
this.ssn = ssn;
}
public Person(String firstName, String lastName, String ssn) {
this.firstName = firstName;
this.lastName = lastName;
this.ssn = ssn;
}
/**
* @return the firstName
*/
public String getFirstName() {
return firstName;
}
/**
* @param firstName
* the firstName to set
*/
public void setFirstName(String firstName) {
this.firstName = firstName;
}
/**
* @return the lastName
*/
public String getLastName() {
return lastName;
}
/**
* @param lastName
* the lastName to set
*/
public void setLastName(String lastName) {
this.lastName = lastName;
}
/**
* @return the ssn
*/
public String getSsn() {
return ssn;
}
/**
* @param ssn
* the ssn to set
*/
public void setSsn(String ssn) {
this.ssn = ssn;
}
@Override
public int compareTo(Person p) {
if (this.firstName == p.firstName && this.lastName == p.lastName
&& this.ssn == p.ssn)
return 0;
else
return -1;
}
public void getNamesTest(Person p) {
// initialising the local variables
int firstNameAsciivalue = 0;
int otherfirstNameAsciivalue = 0;
int lastNameAsciivalue = 0;
int otherlastNameAsciivalue = 0;
int i = 0;
int j = 0;
int a = 0;
int b = 0;
int lastNamelen = this.lastName.length();
int otherLastNamelen = p.lastName.length();
int firstNamelen = this.firstName.length();
int otherFirstNamelen = p.firstName.length();
while (i < lastNamelen) {
char character = this.lastName.charAt(i);
lastNameAsciivalue += (int) character;
}
while (j < otherLastNamelen) {
char character = this.lastName.charAt(j);
otherlastNameAsciivalue += (int) character;
}
if (otherlastNameAsciivalue == lastNameAsciivalue) {
System.out.println("two persons last names are same");
while (a < firstNamelen) {
char character = this.firstName.charAt(i);
firstNameAsciivalue += (int) character;
}
while (b < otherFirstNamelen) {
char character = this.firstName.charAt(j);
otherfirstNameAsciivalue += (int) character;
}
if (firstNameAsciivalue == otherfirstNameAsciivalue) {
System.out.println(" two persons first names are same");
}
} else
System.out.println("two persons names are not same");
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((ssn == null) ? 0 : ssn.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Person other = (Person) obj;
if (this.ssn == other.ssn) {
return true;
}
return false;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return firstName + "," + lastName + " " + ssn;
}
}
--------------------------------------------
package com.ip.resource;
public class Student extends Person {
protected String yearInSchool;
protected static int id;
protected static int nextId;
public Student() {
super();
}
public Student(String firstName, String lastName, String ssn,
String yearInSchool) {
super(firstName, lastName, ssn);
this.firstName = firstName;
this.lastName = lastName;
this.ssn = ssn;
this.yearInSchool = yearInSchool;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return firstName + "," + lastName + " " + ssn + " " + yearInSchool;
}
@Override
public int hashCode() {
final int prime = 31;
int result = super.hashCode();
result = prime * result
+ ((yearInSchool == null) ? 0 : yearInSchool.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (!super.equals(obj))
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (yearInSchool == null) {
if (other.yearInSchool != null)
return false;
} else if (!yearInSchool.equals(other.yearInSchool))
return false;
return true;
}
}
------------------------
package com.ip.resource;
public class Instructor extends Person {
private String rank;
private double salary;
private int id;
public String getRank() {
return rank;
}
public void setRank(String rank) {
this.rank = rank;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Instructor() {
super();
}
public Instructor(String firstName, String lastName, String ssn, String rank) {
super(firstName, lastName, ssn);
this.rank = rank;
if (rank.equals("instructor")) {
rank = "instructor";
salary = 20000.00;
}
if (rank.equals("assistant")) {
rank = "assistant";
salary = 40000.00;
}
if (rank.equals("professor")) {
rank = "professor";
salary = 80000.00;
}
if (rank.equals("associate")) {
rank = "associate";
salary = 60000.00;
}
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (!super.equals(obj))
return false;
if (getClass() != obj.getClass())
return false;
Instructor other = (Instructor) obj;
if (id != other.id)
return false;
return true;
}
@Override
public String toString() {
return firstName + ", " + lastName + " " + ssn + " " + rank + " "
+ salary;
}
}
--------------------------
package com.ip.resource;
import java.util.ArrayList;
import java.util.List;
public class Course {
private String courseName;
private int room;
private Instructor instructor;
private int numberStudents;
protected List<Student> studentList;
public Course() {
this.courseName = null;
this.room = 0;
this.numberStudents = 0;
}
public Course(String courseName, int room) {
instructor = new Instructor("Smith", "julie", "123-45-6789",
"instructor");
this.courseName = courseName;
this.room = room;
}
public String getCourseName() {
return courseName;
}
public void setCourseName(String courseName) {
this.courseName = courseName;
}
public int getRoom() {
return room;
}
public void setRoom(int room) {
this.room = room;
}
public Instructor getInstructor() {
return instructor;
}
public void setInstructor(Instructor instructor) {
this.instructor = instructor;
}
public int getNumberStudents() {
return numberStudents;
}
public void setNumberStudents(int numberStudents) {
this.numberStudents = numberStudents;
}
public List<Student> getStudentList() {
return studentList;
}
public void setStudentList(List<Student> studentList) {
this.studentList = studentList;
}
public void addStudent(Student s)
{
if (studentList == null) {
studentList = new ArrayList<Student>();
++numberStudents;
studentList.add(s);
} else
studentList.add(s);
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Course other = (Course) obj;
if (courseName == null) {
if (other.courseName != null)
return false;
} else if (!courseName.equals(other.courseName))
return false;
if (instructor == null) {
if (other.instructor != null)
return false;
} else if (!instructor.equals(other.instructor))
return false;
return true;
}
@Override
public String toString() {
return "Course [courseName=" + courseName + ", room=" + room
+ ", instructor=" + instructor;
}
}
----------------------------
package com.ip.resource;
public class Test {
public static void main(String[] args) {
Person p=new Person("Smith","julie","123-45-6789");
Student s=new Student("Smith","julie", "123-45-6789","1001");
Instructor i=new Instructor("Smith","julie", "123-45-6789","instructor");
Course c=new Course("cis 3020",333 );
System.out.println(p);
System.out.println(s);
System.out.println(i);
System.out.println(c);
}
}
-------------
output
Smith,julie 123-45-6789
Smith,julie 123-45-6789 1001
Smith, julie 123-45-6789 instructor 20000.0
Course [courseName=cis 3020, room=333, instructor=Smith, julie 123-45-6789 instructor 20000.0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.