Basically this program uses an abstract class which 3 other classes extends to so that in the end an overridden toString() method will print out how many pages must be read for each class.

Okay, well this program is for a class, but I am kinda of stuck because my teacher wants me to test the Homework methods below with ArrayList, but I cannot reference is from a static context.My teacher wasn't specific about the tester. All he said was to use Arraylist to test the classes. So I assume it was 4 different arraylist because there is 4 different classes.

import java.util.ArrayList;
public class testHomework
{
    public static void main(String []args)
    {
        ArrayList<MyMath> math = MyMath.createAssignment(5);
        ArrayList<MyScience> science = MyScience.createAssignment(6);
        ArrayList<MyEnglish> english = MyEnglish.createAssignment(10);
        ArrayList<MyJava> java = MyJava.createAssignment(2);
        
        MyMath.toString();
        MyScience.toString();
        MyEnglish.toString();
        MyJavatoString();
    }
}


public abstract class Homework
{
    int pagesRead;
    String typeHomework;
    public Homework()
    {
        pagesRead =0;
        typeHomework= "";
    }
    public abstract void createAssignment(int p);
}


public class MyMath extends Homework
{
    int pages;
    public MyMath()
    {
        super();
    }
    public void createAssignment(int p)
    {
        typeHomework = "Math";
        pages = p;
    }
    public String toString()
    {
        return typeHomework + " - must read " + pages + " pages";
    }
}


public class MyScience extends Homework
{
    int pages;
    public MyScience()
    {
        super();
    }
    public void createAssignment(int p)
    {
        typeHomework = "Science";
        pages = p;
    }
    public String toString()
    {
        return typeHomework + " - must read " + pages + " pages";
    }
}


public class MyEnglish extends Homework
{
    int pages;
    public MyEnglish()
    {
        super();
    }
    public void createAssignment(int p)
    {
        typeHomework = "English";
        pages = p;
    }
    public String toString()
    {
        return typeHomework + " - must read " + pages + " pages";
    }
}

public class MyJava extends Homework
{
    int pages;
    public MyJava()
    {
        super();
    }
    public void createAssignment(int p)
    {
        typeHomework = "Java";
        pages = p;
    }
    public String toString()
    {
        return typeHomework + " - must read " + pages + " pages";
    }
}

Any help on how to write the tester class?
Thanks.

import java.util.*;

public class testHomework {

    public static void main(String[] args) {
        // common list for Homework type objects
        // used List as interface type
        List<Homework> al = new ArrayList<Homework>();
        //
        MyMath math = new MyMath();
        al.add(math);
        //
        MyScience science = new MyScience();
        al.add(science);
        //
        MyEnglish english = new MyEnglish();
        Homework english2 = new MyEnglish();
        al.add(english);
        al.add(english2);
        //...

        int p = 6;
        for (Homework inner : al) {
            inner.createAssignment(p);
            p++;
            System.out.println(inner);
        }
    }
}

a few explanation at http://home.cogeco.ca/~ve3ll/jatutor5.htm

This article has been dead for over six months. Start a new discussion instead.