New Programming Student, Having Trouble With Abstract Classes/Interfaces
As the title states, I am a new programming student having issues with the concept of abstract classes and interfaces. My current instructor is definitely not qualified to teach Java so I find myself in a bit of a pinch.
Our class assignment involves creating an interface, abstract class, and concrete class to build a registry (array of sorts) to store students' names (string) and majors (string). Sounds simple on its face, but the assignment was to use three classes that were already provided for us, and "fill in the blanks" with the remaining necessary classes. The classes given to us are a Generic Registry abstract class, which is supposed to implement a Registry interface (that we are supposed to create), a test class for compiling the program, and a Student class, which creates a Comparable Student object to be used in the Student Registry class (extends the abstract Generic Registry class), which is to include a print() method. I apologize for the wall of text, hopefully the code below will clarify what I'm struggling with:
Code Java:
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.
Code Java:
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.
Code Java:
public class Lab3 {
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();
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.
...Now, here is my code for the assigned classes, which is eliciting an "Exception in thread "main" java.lang.NullPointerException" error:
Code Java:
public interface Registry {
public void addMember(Comparable object);
public void sort();
}
^ This is my interface class to be implemented by the abstract class above.
Code Java:
public class StudentRegistry extends GenericRegistry{
private int size;
public StudentRegistry(int size){
this.size = size;
}
public void print(){
System.out.println("The registry contains" + count + "members.");
}
}
^ This is my concrete StudentRegistry class.
Thanks a MILLION to anyone who can even remotely get headed in the right direction!
-Tony
Re: New Programming Student, Having Trouble With Abstract Classes/Interfaces
Quote:
Exception in thread "main" java.lang.NullPointerException" error:
Please post the full text of the error message. It shows where the error occurred.
Look at the line number given in the error message, find the source at that line and find the variable that has a null value. Then backtrack through your code to see why that variable does not have a valid value.
Re: New Programming Student, Having Trouble With Abstract Classes/Interfaces