nge each of instance variables (mutator); • A method toString that converts a stu
Types of Loop
Loops are the elements of programming in which a part of code is repeated a particular number of times. Loop executes the series of statements many times till the conditional statement becomes false.
Loops
Any task which is repeated more than one time is called a loop. Basically, loops can be divided into three types as while, do-while and for loop. There are so many programming languages like C, C++, JAVA, PYTHON, and many more where looping statements can be used for repetitive execution.
While Loop
Loop is a feature in the programming language. It helps us to execute a set of instructions regularly. The block of code executes until some conditions provided within that Loop are true.
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 additional getter method to return the instance variable (accessor);
• One setter method to change the instance variable (mutator);
1
• 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 file PersonTester.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 3 steps with 3 images