hello everyone,

I am doing a program for a class which is suppose to say the amount of pages one needs to read and then say if any of the homeworks have the same amount of pages. My teacher wants me to override the compareTo() method and then test it in the tester class. I am pretty sure I did it correctly for everything except testing it. I have been trying to test it but I keep getting the error " non-static method compareTo(Homework3) cannot be referenced from a static context".

Any Help?


Here is the code:

/**
 * Write a description of class Homework3 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public abstract class Homework3 implements Comparable<Homework3>
{
    int pagesRead;
    String typeHomework;
    public Homework3()
    {
        pagesRead =0;
        typeHomework= "";
    }
    public abstract void createAssignment(int p);
    public abstract int returnPages();
    public abstract int compareTo(Homework3 a);
}



/**
 * Write a description of class MyMath2 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MyMath3 extends Homework3
{
    int pages;
    public MyMath3()
    {
        super();
    }
    public void createAssignment(int p)
    {
        typeHomework = "Math";
        pages = p;
    }
    public String toString()
    {
        return typeHomework + " - must read " + pages + " pages";
    }
    public int returnPages()
    {
        return pages;
    }
        public int compareTo(Homework3 a)
    {
        if(pages==a.returnPages())
        {
            return 0;
        }
        else
            return 1;
    }
}



/**
 * Write a description of class MyScience2 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MyScience3 extends Homework3
{
    int pages;
    public MyScience3()
    {
        super();
    }
    public void createAssignment(int p)
    {
        typeHomework = "Science";
        pages = p;
    }
    public String toString()
    {
        return typeHomework + " - must read " + pages + " pages";
    }
    public int returnPages()
    {
        return pages;
    }
    public int compareTo(Homework3 a)
    {
        if(pages==a.returnPages())
        {
            return 0;
        }
        else
            return 1;
    }
}



/**
 * Write a description of class MyEnglish2 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MyEnglish3 extends Homework3
{
    int pages;
    public MyEnglish3()
    {
        super();
    }
    public void createAssignment(int p)
    {
        typeHomework = "English";
        pages = p;
    }
    public String toString()
    {
        return typeHomework + " - must read " + pages + " pages";
    }
    public int returnPages()
    {
        return pages;
    }
    public int compareTo(Homework3 a)
    {
        if(pages==a.returnPages())
        {
            return 0;
        }
        else
            return 1;
    }    
}



/**
 * Write a description of class MyJava2 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MyJava3 extends Homework3
{
    int pages;
    public MyJava3()
    {
        super();
    }
    public void createAssignment(int p)
    {
        typeHomework = "Java";
        pages = p;
    }
    public String toString()
    {
        return typeHomework + " - must read " + pages + " pages";
    }
    public int returnPages()
    {
        return pages;
    }
    public int compareTo(Homework3 a)
    {
        if(pages==a.returnPages())
        {
            return 0;
        }
        else
            return 1;
    }    
}



/**
 * Write a description of class TestHomework2 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
import java.util.*;
public class TestHomework3 {
    public static void main(String[] args) {
        
        List<Homework3> al = new ArrayList<Homework3>();
        
        MyMath3 math = new MyMath3();
        MyEnglish3 english = new MyEnglish3();
        MyJava3 java = new MyJava3();
        MyScience3 science = new MyScience3();
        
        al.add(math);
        al.add(science);
        al.add(english);
        al.add(java);
       

        int p = 6;
        for (Homework3 inner : al)
        {
            inner.createAssignment(p);
            System.out.println(inner);
            p++;
        }
        
        for(int i = 0 ; i <al.size(); i++)
        {
            Homework3.compareTo(al.get(i));
            
        }
    }
}
Homework3.compareTo(al.get(i));

Homework3 is a class. It can't be compared to a Homework3 object, any more than you can compare the Integer class to the Integer 3.

Calling a method in the form ClassName.method() is calling it as a static method - it's calling it without reference to any particular object of that class. This is useful in some cases, but obviously if it's not called with reference to a member of that class, it can't make reference to any instance variables - there's no instance!

So, to fix this, you need an instance of Homework3 that that you're going to call this method on.

Homework3.compareTo(al.get(i));

Homework3 is a class. It can't be compared to a Homework3 object, any more than you can compare the Integer class to the Integer 3.

Calling a method in the form ClassName.method() is calling it as a static method - it's calling it without reference to any particular object of that class. This is useful in some cases, but obviously if it's not called with reference to a member of that class, it can't make reference to any instance variables - there's no instance!

So, to fix this, you need an instance of Homework3 that that you're going to call this method on.

Hey thank you, I can believe I missed that. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.