Results 1 to 4 of 4
- 04-25-2010, 08:24 PM #1
Member
- Join Date
- Dec 2009
- Posts
- 22
- Rep Power
- 0
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
It has to apply to this homework assignment and I don't get what the method would be like in this.Java Code:public int compareTo(BasicJean2 obj) { if (pantSize < obj.pantSize) return -1; else if (pantSize == obj.pantSize) return 0; else return 1; }
-
Question: is the red "2" in error?
Because it would make more sense to me if Homework3 implements Comparable<Homework3>, not "2".a. It will be same as Homework2, but also implement Comparable<Homework2>.
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!
- 04-25-2010, 09:08 PM #3
Member
- Join Date
- Dec 2009
- Posts
- 22
- Rep Power
- 0
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"); } }All of the other subclasses are just like MyMath3 except they are English, java, and science. Here is my main.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; } }
I don't know what my compareTo method should be :(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()); } }
-
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:
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!Java Code:int compareResult = homework3A.compareTo(homework3B);
Similar Threads
-
How can I call abstract class methods from another class
By srinivas2828 in forum New To JavaReplies: 13Last Post: 03-12-2010, 02:33 PM -
NullPointerException even when class is instantiated as expected
By DCC1 in forum New To JavaReplies: 17Last Post: 07-12-2009, 06:42 PM -
Difference between Abstract class having only abstract method and a Interface class
By Santoshbk in forum New To JavaReplies: 6Last Post: 02-11-2009, 10:51 AM -
what is the Priority for execution of Interface class and a Abstract class
By Santoshbk in forum Advanced JavaReplies: 0Last Post: 04-02-2008, 07:04 AM -
Abstract Class
By gapper in forum New To JavaReplies: 4Last Post: 01-31-2008, 01:25 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks