Question
need some Java help please follow the instruction. Highlight: in Java, to compare primitives, we can use relational operators; to compare objects, we need to
need some Java help please follow the instruction.
Highlight: in Java, to compare primitives, we can use relational operators; to compare objects, we need to use the interface Comparable. For example, since String is a user defined class, we can use Comparables compareTo() method to compare two strings as shown in the following:
stringone.compareTo(stringtwo)
In this case, the result will be -5 since the parameter is 5 position greater than the invoking object stringone.
Part I: The Design
In this section, you are asked to finish the design of a computer program according to the information and instructions given below. A Registry is thought of as a store where you can remember objects. You can add a member to a registry, print the contents of the registry, and sort its members. Registry is a Java interface: it is a specification. In this lab, you are going to write an interface called Registry that has three abstract methods: print(), sort(), and addMember(Comparable object).
GenericRegistry is an abstract class that partially implements the Registry interface. This class implements addMember, and sort, has storage for the members and the number of elements, but the print method is not implemented. The implementation of an interface relationship is represented in UML by dotted big hollow arrow.
The arrow is at the interface side.
Now, connect the GenericRegistry to the Registry in the diagram given in Figure 1 using the above symbol.
StudentRegistry is a concrete class that extends the abstract class GenericRegistry. It is used for storing students. It implements the print() method; it borrows most of its functionality from GenericRegistry. At this level, all the methods declared by the interface Registry are defined. Thus we are able to instantiate objects from this class. The graphic notation of an abstract class is to italicize the name of the class; and the relationship for inheriting an abstract class is represented in UML by a big hollow arrow.
The arrow is at the abstract class side.
Now, connect the GenericRegistry to the StudentRegistry using the above symbol.
The Student class implements the Comparable interface of Java. Comparable is a Java interface supplied by the compiler. This interface contains one abstract method header int compareTo(Comparable). This method is used to compare two objects. In order to use it, each object involved must implement the interface Comparable. The following is an example of using it:
object1.compareTo(object2)
The result returned by the compareTo method is an integer and its value depends on the following rules:
Has a negative value if object1 is less than object2.
Has the value 0 if object1 is equal to objec2.
Has a positive value if object1 is greater than object2.
Now, connect the Student class to the Comparable interface by using dotted big hollow arrow.
Student class is a component of StudentRegistry class. Thus, connect the Student with StudentRegistry by using the follow composition symbol.
The solid shape is at the container side. Draw the above connection between the Student and the StudentRegistry and make sure that the solid shape is at the StudentRegistry side.
Part II: Completing the Implementation
Preparation knowledge:
Array in Java is a collection of same data type that is organized together. Each cell (or element) is referred to by an array index. For example, suppose that we have an array named a, then the first element of a will be a[0]. In this lab, we give you the first exposure to this data type. All you need to know is go through every element in an array using a for loop as shown in the following (in the following code, suppose that the count is the actual number of elements stored in the array (not the array length); also assume that the type of array is Student):
for (int index = 0; index
System.out.println(a[index]);
}
The above segment of code will go through each Student object and call the objects toString() method.
Your work
In this section, you are asked to implement the above design and do some screen captures of the outputs. You are given partial code and you need to finish the missing code. To finish the lab work in this section, you can follow the following steps:
Create a project named lab4 in Eclipse.
Cut and paste the classes given by create class names first in Eclipse; then cut the code in this documentation; and paste them into the appropriate part of the Eclipse. Note: make sure that you deleted all the hidden characters after pasting.
Create missing classes and interfaces.
Debug and run the application.
Specifically, you are given the following code:
class GenericRegistry
class Student
application class Lab4
And you need to supply the code for the following classes and interfaces:
interface Registry
class StudentRegistry
The following is the code for Student, GenericRegistry, and the application class Lab4:
public abstract class GenericRegistry implements Registry {
protected Comparable[] members;
protected int count;
public void addMember(Comparable object) {
members[count++] = object;
}
// Selection sort
public void sort() {
for (int i = 0; i
int min = i;
for (int j = i + 1; j
if (members[min].compareTo(members[j]) > 0) {
min = j;
}
}
Comparable object = members[i];
members[i] = members[min];
members[min] = object;
}
}
}
Note: the storage in this class is an array of Comparable objects. Thus, any objects to be stored in the array should be a type of Comparable. Since Student implements Comparable, thus Student can be stored in the array as well.
public class Student implements Comparable {
private String name;
private String major;
public Student(String name, String major) {
this.name = name;
this.major = major;
}
public String getName() {
return name;
}
public String getMajor() {
return major;
}
public int compareTo(Object student) {
return name.compareTo(((Student) student).name);
}
public String toString() {
return "Name: " + name + " major " + major;
}
}
Note: this class implements the Comparable interface. Thus, it can be stored in the array called members in the GenericRegistry. An interface is also a type. When a class implements an interface, the class will have two types. In this case, the class Student is both a type of Student and a type of comparable.
public class Lab4 {
public static void main(String[] s) {
StudentRegistry sr = new StudentRegistry(10);
sr.addMember(new Student("Close", "English"));
sr.addMember(new Student("Snow", "Physics"));
sr.addMember(new Student("Lal", "Physics"));
sr.addMember(new Student("Chow", "English"));
sr.print();
sr.sort();
System.out.println();
sr.print();
}
}
Note: we allocate 10 cells for the array members. This is the largest number of Student objects that can be stored in the array. When you write the print() method for the class StudentRegistry, you should use the instance variable count in the class GenericRegistry to decide the actual number of items in the array members (not the array length!).
Cut and paste the Java source code of StudentRegistry.java and Registry.java in the space below :
Cut and paste the output in the space below
Hand to your instructor the following:
1) this document (with all the bold faced sections finished), with output results.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started