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

