views:

88

answers:

4

I'm working through an exercise sheet regarding interfaces, generics and abstract classes in Java. No matter what way I seem to code it, the class Exercise1 won't work. The question asked are commented in the code. Any help would be appreciated, I'm not sure if the error is in the Exercise one code or the implementation of the interface in the classes Time and Point.

/*Exercise 2

(a)     Write an interface Printable which has a single method put whose intention is to display a string representation of any object whose class implements Printable. Check it by compiling it. */

interface Printable {

        public void put(Object o);

}

/*(b)   Enhance classes  Point and Time from Chapter 2 of the notes to implement Printable. Check them by compiling them. */

class Point implements Printable {

        static Scanner sc = new Scanner(System.in);

        private double x, y; // coordinates

        Point(double x, double y){ // all-args constructor
            this.x = x; this.y = y;
        }

        Point(){}; // no-args constructor (defaults apply)

        void get() {
            x = sc.nextDouble();
                y = sc.nextDouble();
        }

        public String toString() {
                return "(" + x + "," + y + ")";
        }

        double distance(Point r) { // distance from r
                double xdist = x-r.x; double ydist = y-r.y;
                return(Math.sqrt(xdist*xdist+ydist*ydist));
        }

        public void put(Object o) {

                if(o==null) return;
                Point p = (Point)o;
                System.out.println(x + ":" + y);
        }

}


class Time implements Order, Printable {

        private int hours; // 0..23
        private int mins; // 0..59


        Time() { }

        Time (int hours, int mins) {

                this.hours = hours;
                this.mins = mins;

        }

        public boolean lte(Object other) { // Note public
           if (other==null) return false;
           Time t = (Time) other;
           return hours*60+mins<=t.hours*60+t.mins;
        }

        public void put(Object o) {

                if(o==null) return;
                Time t = (Time)o;
                System.out.printf("%02d:%02d\n", t.hours, t.mins);
        }
}

/*(c)   Write a static method print which takes an array of objects whose class implements Printable, and prints each element in  the array, one element per line. Check it by placing it in an otherwise empty class and compiling it.  */

//this is the bit that is giving me grief, I've tried :

public class Exercise1 {

        static void print(Printable[] a) {

        for(int i = 0; i < a.length ; i++) {

                a[i].put(); // put(java.lang.Object) in Printable cannot be applied to ()                                                    //a[i].put();


                }

        }

        public static void main(String[] args) {

                Time[] t = new Time[10];
                for(int i = 0; i < t.length; i++) {
                        t[i] = new Time();
                }
                print(t);

        }
}

public interface  Order {
    boolean lte (Object obj); // Object for max generality
    // is this object less than or equal to obj?
}
+1  A: 

You want to print this, not some arbitrary object o.

starblue
A: 

you might want your Printable method to be print(Object object), not put.

hvgotcodes
+1  A: 

I think the problem is the interface Printable. It doesn't match the question correctly.

to display a string representation of any object whose class implements Printable

This doesn't mean, that the put() method should have a parameter of type Object.

The object here refers to this, the object which class implements Printable. And the method put() should print out a string representation of this. So in the simplest case you can implement it with the help of toString().

interface Printable {
  /**
   * Display a string representation of this
   */
  void put();
}

class Point implements Printable {

  // ...

  /**
   * returns a string representation of this
   */
  public String toString() {
    return "(" + x + "," + y + ")";
  }

  public void put() {
    // this is the same as System.out.println(this.toString());
    System.out.println(this); 
  }
}

Then you can call put() for every Printable in your array.

vanje
A: 

What you want to do is make the interface Printable look something like this:

interface Printable {

    public void print(); //The name of the method should be similar to the name of the interface
}

Then your classes will work like this:

public class Time implements Printable {

    private int hours, minutes;

    public void print() {
        System.out.println( hours + ":" + minutes );
    }
}

For Exercise 1, you would then call print() for each element of the array.

By the way: There's already an interface similar to Order. It's called Comparable, and it looks like this:

public interface Comparable<T> {

    public void compareTo(T other); //Returns -1 if less than, 0 if equal, and 1 if greater than other object.

}

If no parameter T is given, it becomes Object.

Christian Mann