In this assignment, you will make two classes,Student and Instructor, that inherit from a superclass Person. The implementation of class Person is given below. public class Person { private String name; private int age; public Person() { name=""; age=0; } /** Create a person with a given name and age. @param name the name @param age the age */ public Person(String name, int age) { this.name = name; this.age = age; } /** Get the name. @return the name */ public String getName() { return name; } /** Change the name. @param name the name */ public void setName(String name) { this.name = name; } /** Get the age. @return the age */ public int getAge() { return age; } /** Change the age. @param age the age */ public void setAge() { this.age = age; } /** Convert person to string form. */ public String toString() { return "Name: " + name + "\t" + "Age: " + age; } } You will also need to write a test program to test the methods you write for these two classes. The implementation details are described as follows. Stage 1:In the first file Student.java, you should include the following additional instance variables and methods (other than all instance variables and methods inherited from class Person): •Private instance variables studentID, and major; •A constructor takes four inputs (name, age, studentID and major); •Two additional getter methods to return each of instance variables (accessor); •Two setter methods to change each of instance variables (mutator); •A method toString that converts a student’s information into string form. The string should have the format as shown in Figure 1. You should override superclass toString( )method. •A method compareTo that implements the interface Comparable, so that Student objects can be sorted by studentID in an ascending order. Stage 2: In the second file Instructor.java, you should include the following additional instance variables and methods (other than all instance variables and methods inherited from class Person): •Private instance variable salary; •A constructor takes three inputs (name ,age, and salary); •One additionalgetter method to return the instance variable (accessor); •One setter method to change the instance variable (mutator); •A method toString that converts an instructor’s information into string form. The string should have the format as shown in Figure 1. Specifically, you need to format salary value to 2 decimal places, and make them right aligned. You should also override superclass toString() method. •A method compareTo that implements the interface Comparable, so that Instructor objects can be sorted by salary in an ascending order. Stage 3: In the third filePersonTester.java, you will need to do the followings: 1. You need to read data file “data1.txt” into an array of Student objects, Specifically, (a) The first number in the first line of the file is used to determine the array size. (b) The remaining lines are the student records. You will need to create a Student object using each line’s information, and put it into the array. 2. Do sorting of the initialized array of Student objects, and then print them out. The outputs should be nicely labeled and formatted, as shown in Figure 1. 3. Repeat the above steps to read data file “data2.txt” into an array of Instructor objects, do sorting and print them out. The outputs should be nicely labeled and formatted, as shown in Figure 1. To avoid code redundancy, you may use a loop to handle the repetitive procedure.
OOPs
In today's technology-driven world, computer programming skills are in high demand. The object-oriented programming (OOP) approach is very much useful while designing and maintaining software programs. Object-oriented programming (OOP) is a basic programming paradigm that almost every developer has used at some stage in their career.
Constructor
The easiest way to think of a constructor in object-oriented programming (OOP) languages is:
In this assignment, you will make two classes,Student and Instructor, that inherit from a superclass Person. The implementation of class Person is given below.
public class Person
{
private String name;
private int age;
public Person()
{
name="";
age=0;
}
/**
Create a person with a given name and age.
@param name the name
@param age the age
*/
public Person(String name, int age)
{
this.name = name;
this.age = age;
}
/**
Get the name.
@return the name
*/
public String getName()
{
return name;
}
/**
Change the name.
@param name the name
*/
public void setName(String name)
{
this.name = name;
}
/**
Get the age.
@return the age
*/
public int getAge()
{
return age;
}
/**
Change the age.
@param age the age
*/
public void setAge()
{
this.age = age;
}
/**
Convert person to string form.
*/
public String toString()
{
return "Name: " + name + "\t" + "Age: " + age;
}
}
You will also need to write a test program to test the methods you write for these two classes. The implementation details are described as follows. Stage 1:In the first file Student.java, you should include the following additional instance variables and methods (other than all instance variables and methods inherited from class Person):
•Private instance variables studentID, and major;
•A constructor takes four inputs (name, age, studentID and major);
•Two additional getter methods to return each of instance variables (accessor);
•Two setter methods to change each of instance variables (mutator);
•A method toString that converts a student’s information into string form. The string should have the format as shown in Figure 1. You should override superclass toString( )method.
•A method compareTo that implements the interface Comparable, so that Student objects can be sorted by studentID in an ascending order.
Stage 2: In the second file Instructor.java, you should include the following additional instance variables and methods (other than all instance variables and methods inherited from class Person):
•Private instance variable salary;
•A constructor takes three inputs (name ,age, and salary);
•One additionalgetter method to return the instance variable (accessor);
•One setter method to change the instance variable (mutator);
•A method toString that converts an instructor’s information into string form. The string should have the format as shown in Figure 1. Specifically, you need to format salary value to 2 decimal places, and make them right aligned. You should also override superclass toString() method.
•A method compareTo that implements the interface Comparable, so that Instructor objects can be sorted by salary in an ascending order.
Stage 3: In the third filePersonTester.java, you will need to do the followings:
1. You need to read data file “data1.txt” into an array of Student objects, Specifically,
(a) The first number in the first line of the file is used to determine the array size.
(b) The remaining lines are the student records. You will need to create a Student object using each line’s information, and put it into the array.
2. Do sorting of the initialized array of Student objects, and then print them out. The outputs should be nicely labeled and formatted, as shown in Figure 1.
3. Repeat the above steps to read data file “data2.txt” into an array of Instructor objects, do sorting and print them out. The outputs should be nicely labeled and formatted, as shown in Figure 1. To avoid code redundancy, you may use a loop to handle the repetitive procedure.
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 3 images