Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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

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 < count - 1; i++) {

int min = i;

for (int j = i + 1; j < count; 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!).

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Datacasting How To Stream Databases Over The Internet

Authors: Jessica Keyes

1st Edition

007034678X, 978-0070346789

More Books

Students also viewed these Databases questions