Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: New Programming Student, Having Trouble With Abstract Classes/Interfaces

  1. #1
    Junior Member bulx0001's Avatar
    Join Date
    Jun 2011
    Location
    Minnesota
    Posts
    9
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default 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:

    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.

    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 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:

    public interface Registry {
     
    	public void addMember(Comparable object);
    	public void sort();
    }
    ^ This is my interface class to be implemented by the abstract class above.

    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


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: New Programming Student, Having Trouble With Abstract Classes/Interfaces

    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.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: New Programming Student, Having Trouble With Abstract Classes/Interfaces

    Cross posted at New Programming Student, Having Trouble With Abstract Classes/Interfaces - Dev Shed

Similar Threads

  1. About abstract, interfaces, and when i should use this.
    By Yoga Herawan in forum Object Oriented Programming
    Replies: 2
    Last Post: October 4th, 2011, 10:38 AM
  2. what is the use of interfaces and abstract classes?
    By sagar474 in forum Java Theory & Questions
    Replies: 5
    Last Post: September 18th, 2011, 02:34 PM
  3. Interfaces Vs Abstract classes
    By tcstcs in forum Java Theory & Questions
    Replies: 1
    Last Post: April 20th, 2011, 07:49 AM
  4. Java: interfaces and abstract classes
    By pinansonoyon in forum Object Oriented Programming
    Replies: 1
    Last Post: May 6th, 2010, 10:17 AM
  5. [SOLVED] Abstract Classes Help
    By SweetyStacey in forum Object Oriented Programming
    Replies: 10
    Last Post: May 6th, 2010, 06:15 AM