Results 1 to 4 of 4
  1. #1
    Jamison5213 is offline Member
    Join Date
    Dec 2009
    Posts
    22
    Rep Power
    0

    Default class is abstract; cannot be instantiated

    I am very confused as to what I am supposed to be doing. The instructions for this assignment are as follows:

    15.04 Assignment Instructions
    Instructions: For this assignment, you are going to modify the classes you have made in the second assignment, to implement Comparable<t>.
    1. Create a folder called 15.04 Assignment in your module 15 assignments folder.
    2. You are to create an abstract class called Homework3.
    a. It will be same as Homework2, but also implement Comparable<Homework2>.
    b. It will implement compareTo() using the number of pages. Homework can be ordered based upon the pages that are part of the homework.
    c. Save the class as Homework3.java.
    3. You are to create a class called MyMath3 that extends class homework.
    a. MyMath3 will be the same as MyMath2, except it extends Homework3.
    b. Save the class as MyMath3.java.
    4. You are to create a class called MyScience2 that extends class homework.
    a. MyScience3 will be the same as MyScience2, except it extends Homework3.
    b. Save the class as MyScience3.java.
    5. You are to create a class called MyEnglish2 that extends class homework.
    a. MyEnglish3 will be the same as MyEnglish2, except it extendsHomework3.
    b. Save the class as MyEnglish2.java.
    6. You are to create a class called MyJava2 that extends class homework.
    a. MyJava3 will be the same as MyJava2, except it extends Homework3.
    b. Save the class as MyJava3.java.
    7. Create a test program called TestHomework3.java to test your class. Use an ArrayList of type Homework3 to test your class (Remember to declare it properly using List). Demonstrate the results of compareTo().

    The output should look like

    Math - must read 4 pages.
    English - must read 4 pages.
    Java - must read 5 pages.
    Science - must read 2 pages.

    The homework for math and english are the same number of pages.

    I have to write some kind of compareTo method that looks like this

    Java Code:
        	public int compareTo(BasicJean2 obj)
    	{
    	    if (pantSize < obj.pantSize)
    	        return -1;
    	    else if (pantSize == obj.pantSize)
    	        return 0;
    	    else
    	        return 1;
    	}
    It has to apply to this homework assignment and I don't get what the method would be like in this.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Question: is the red "2" in error?
    a. It will be same as Homework2, but also implement Comparable<Homework2>.
    Because it would make more sense to me if Homework3 implements Comparable<Homework3>, not "2".

    Otherwise your assignment pretty well spells out the steps you must take to solve your problem, so my main suggestion is to do each step one at a time, to compile frequently and to fix any compile issues before moving on. Then if you hit a snag in your code that you can't fix, come on back with your code and question.

    Much luck!

  3. #3
    Jamison5213 is offline Member
    Join Date
    Dec 2009
    Posts
    22
    Rep Power
    0

    Default

    Here's what I have so far...

    Java Code:
    public abstract class Homework3 implements Comparable<Homework3> {
    
    	public String mySubject;
    	public int myPages;
    	public int myPages2;
    
        public Homework3(String subject, int pages, int pagesSubtracted) {
        	mySubject = subject;
        	myPages = pages;
        	myPages2 = pagesSubtracted;
        }
        
         public String doReading()
        {
        	return (mySubject + " - must read " + myPages + " pages.\n");
        }
            
        
    }
    Java Code:
    public abstract class MyMath3 extends Homework3 {
    	
    	public String mySubject;
    	public int myPages;
    	public int myPages2;
    
    
        public MyMath3(String subject, int pages, int pagesSubtracted) {
        	super(subject, pages, pagesSubtracted);
        	mySubject = "Math";
        	myPages = pages;
        	myPages2 = pagesSubtracted;
        }
        
        
    }
    All of the other subclasses are just like MyMath3 except they are English, java, and science. Here is my main.

    Java Code:
    public class TestHomework3 {
            
        public static void main(String[] args) {
        	
            MyMath3 math1 = new MyMath3("Math", 5, 2);
    	    MyScience3 science1 = new MyScience3("Science", 4, 1);
    	    MyEnglish3 english1 = new MyEnglish3("English", 6, 2);
    	    MyJava3 java1 = new MyJava3("Java", 9802, 5);
    	    
    	    System.out.print(math1.doReading());
    	    System.out.print(science1.doReading());
    	    System.out.print(english1.doReading());
    	    System.out.print(java1.doReading());
            
    
    
        }
    }
    I don't know what my compareTo method should be :(

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    What is this pagesSubtracted parameter for? Is it is a requirement for Homework2? I don't see its value every being used in your code.

    First you'll need to give Homework3 a compareTo method since it implements the Comparable<Homework3> interface. This method will be used by sorting routines to compare the current Homework3 object ("this") with another Homework3 object like so:

    Java Code:
    int compareResult = homework3A.compareTo(homework3B);
    So your compareTo method will take a Homework3 parameter. The method should return an int, either 1, 0, or -1 depending on the number of pages held by the current Homework3 object (again, this) vs the Homework3 object passed as a parameter. Your method will be very similar to the example method given you. Why not give it a go and please report back on your progress? Good luck!

Similar Threads

  1. How can I call abstract class methods from another class
    By srinivas2828 in forum New To Java
    Replies: 13
    Last Post: 03-12-2010, 02:33 PM
  2. Replies: 17
    Last Post: 07-12-2009, 06:42 PM
  3. Replies: 6
    Last Post: 02-11-2009, 10:51 AM
  4. Replies: 0
    Last Post: 04-02-2008, 07:04 AM
  5. Abstract Class
    By gapper in forum New To Java
    Replies: 4
    Last Post: 01-31-2008, 01:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •