help implementing an interface

import java.awt.*;

publicclass Shape{

privateint x = 0;//x coordinate

privateint y = 0;//y coordinate

private Color c =null;//color of the shape

//constructor

public Shape(Color c,int x,int y){

this.x = x;

this.y = y;

this.c = c;

}

//getter of x

publicint getX(){

return x;

}

//getter or y

publicint getY(){

return y;

}

//getter of c which is the color

public Color getColor(){

return c;

}

//getter of the color name, its a brute force way but i dont wanna think of something smart :(

public String getColorName(){

if (c.equals(Color.black))

return"black";

if (c.equals(Color.blue))

return"blue";

if (c.equals(Color.cyan))

return"cyan";

if (c.equals(Color.darkGray))

return"dark gray";

if (c.equals(Color.gray))

return"gray";

if (c.equals(Color.green))

return"green";

if (c.equals(Color.lightGray))

return"light gray";

if (c.equals(Color.magenta))

return"magneta";

if (c.equals(Color.orange))

return"orange";

if (c.equals(Color.pink))

return"pink";

if (c.equals(Color.red))

return"red";

if (c.equals(Color.white))

return"white";

if (c.equals(Color.yellow))

return"yellow";

return"?";

}

//setter of x

publicvoid setX(int x){

this.x = x;

}

//setter of y

publicvoid setY(){

this.y = y;

}

//setter of c which is the color :P

publicvoid setColor(Color c){

this.c = c;

}

}

import java.awt.*;

publicclass Squareextends Question1.Shape{

privatedouble s = 0;//side of the square

//simple constructor

public Square(Color c,int x,int y){

super(c, x, y);

s = 10;//default side length

}

//advanced constructor

public Square(Color c,int x,int y,double s){

super(c, x, y);

//condition that restrict to instintiate a square object with side <= 0.

//if s <= 0, it is then given a default value 10 :(

if (s <= 0)

s = 10;

this.s = s;

}

publicdouble area(){

return (s * s);//side*side

}

publicdouble perimeter(){

return (4 * s);//side+side+side+side = 4*side

}

public String toString(){

return"Question1.Square\n"

+"x-coordinate: " + getX() +"\n"

+"y-coordinate: " + getY() +"\n"

+"side: " + s +"\n"

+"area: " + area() +"\n"

+"perimeter: " + perimeter() +"\n"

+"color: " + getColorName() +"\n";

}

}

//the TestProgram class should implemnt Sartable in order to implement its menthod compareTo

publicclass TestProgramimplements Sortable{

staticfinalint SIZE = 5;

publicstaticvoid sort(Sortable[] items){

for (int i = 1; i < items.length; i++){

Sortable key = items[i];

int position = i;

while (position > 0 && items[position - 1].compareTo(key) > 0){

items[position] = items[position - 1];

position--;

}

items[position] = key;

}

}

publicstaticvoid show(Sortable[] items){

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

if (items[i] !=null)

System.out.print(items[i]);

}

}

publicstaticvoid main(String[] args){

Shape[] shapes =new Shape[SIZE];

//sort(shapes);

}

publicint compareTo(Object o){

if(o.)

}

}

package Question2;

publicinterface Sortable{

publicint compareTo(Object o);

}

help me, i am stuck in the TestProgram class, i need to implement the compareTo methode in such a way that if a square a that has x coordinate less than a square b then the method should return a negative number and so on but i cant figure out how to compare the object parameter that the compareTo methods take with the calling object....

please help me

Thanks in advance

[10431 byte] By [fouadka] at [2007-11-26 22:30:50]
# 1
You made the wrong class implement the comparable interface. Either Shape or Square should implement it. You want to compare the Object you pass to the compareTo method to the object you call compareTo on. In the compareTo method you just compare the coordinates of those 2 objects.
Peetzorea at 2007-7-10 11:35:57 > top of Java-index,Java Essentials,Java Programming...
# 2

thanks man, now i see the picture a little bit more clearer, but i kinda faced new problems implementing the compareTo method,

this is my code :

import java.awt.*;

//the Shape class should implemnt Sartable in order to implement its menthod compareTo

public class Shape implements Sortable{

private int x = 0;//x coordinate

private int y = 0;//y coordinate

private Color c = null;//color of the shape

//constructor

public Shape(Color c, int x, int y) {

this.x = x;

this.y = y;

this.c = c;

}

//getter of x

public int getX() {

return x;

}

//getter or y

public int getY() {

return y;

}

//getter of c which is the color

public Color getColor() {

return c;

}

//getter of the color name, its a brute force way but i dont wanna think of something smart :(

public String getColorName() {

if (c.equals(Color.black))

return "black";

if (c.equals(Color.blue))

return "blue";

if (c.equals(Color.cyan))

return "cyan";

if (c.equals(Color.darkGray))

return "dark gray";

if (c.equals(Color.gray))

return "gray";

if (c.equals(Color.green))

return "green";

if (c.equals(Color.lightGray))

return "light gray";

if (c.equals(Color.magenta))

return "magneta";

if (c.equals(Color.orange))

return "orange";

if (c.equals(Color.pink))

return "pink";

if (c.equals(Color.red))

return "red";

if (c.equals(Color.white))

return "white";

if (c.equals(Color.yellow))

return "yellow";

return "?";

}

//setter of x

public void setX(int x) {

this.x = x;

}

//setter of y

public void setY() {

this.y = y;

}

//setter of c which is the color :P

public void setColor(Color c) {

this.c = c;

}

public int compareTo(Object o) {

if(o instanceof Shape){

if(o.x < this.x){

return 1;

}else if(o.x > this.x){

return -1;

}else return 0;

}

}

}

import java.awt.*;

public class Square extends Shape {

private double s = 0; //side of the square

//simple constructor

public Square(Color c, int x, int y) {

super(c, x, y);

s = 10; //default side length

}

//advanced constructor

public Square(Color c, int x, int y, double s) {

super(c, x, y);

//condition that restrict to instintiate a square object with side <= 0.

//if s <= 0, it is then given a default value 10 :(

if (s <= 0)

s = 10;

this.s = s;

}

public double area() {

return (s * s); //side*side

}

public double perimeter() {

return (4 * s); //side+side+side+side = 4*side

}

public String toString() {

return "Question1.Square\n"

+ "x-coordinate: " + getX() + "\n"

+ "y-coordinate: " + getY() + "\n"

+ "side: " + s + "\n"

+ "area: " + area() + "\n"

+ "perimeter: " + perimeter() + "\n"

+ "color: " + getColorName() + "\n";

}

}

public class TestProgram {

static final int SIZE = 5;

public static void sort(Sortable[] items) {

for (int i = 1; i < items.length; i++) {

Sortable key = items;

int position = i;

while (position > 0 && items[position - 1].compareTo(key) > 0) {

items[position] = items[position - 1];

position--;

}

items[position] = key;

}

}

public static void show(Sortable[] items) {

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

if (items != null)

System.out.print(items);

}

}

public static void main(String[] args) {

Shape[] shapes = new Shape[SIZE];

//sort(shapes);

}

}

i modified my code this way but im still having problems in the compareTo method, what i tryed to do is compare o.x which is an instance of SHape to this.x which would be the object calling the compareTo method, what is the problem with what im thinking...

Thanks in advance

fouadka at 2007-7-10 11:35:57 > top of Java-index,Java Essentials,Java Programming...
# 3

I guess the problem you are facing is that you should return a value in case o isn't an instance of Shape? Have a look at the Comparable interface from the Java api. Document that you can get ClassCastExceptions...

You could then write compareTo like this:

public int compareTo(Object o) {

return this.x - ((Shape)o).x;

}

}

Peetzorea at 2007-7-10 11:35:57 > top of Java-index,Java Essentials,Java Programming...