Can anyone help me finish my code for cash ? :P
Can anyone help me with this task ?
Implement a subclass Square that extends the Rectangle class. In the constructor, accept the the x- and y- coordinates of the centre and the siude length of the square. call the setLocation and setSize methods of the rectangle class (look these up !!) Also supply a method getArea that returns the area of the square. Write a test class that makes use of the square class.
So far I have this:
public class Square
{
private int length;
Square (int l) {
this.length =l;
}
public void setLength(int l)
{
this.length =l;
}
}
public int getArea() {
return this.length*this.length;
How do i finish it off ?
Theres a drink via Paypal in it for ya !! :)
Re: Can anyone help me finish my code for cash ? :P
Think i may have cracked it :)
Re: Can anyone help me finish my code for cash ? :P
Code:
import java.awt.*;
public class Square extends Rectangle {
public Square(int x, int y, int length) {
this.setLocation(x,y);
this.setSize(length);
}
public Square() {
this(0,0,0);
}
public void setSize(int length) {
this.setSize(length, length);
}
public void setLength(int length) {
this.setSize(length);
}
public double getArea() {
return (this.getWidth() * this.getHeight());
}
public double getLength() {
return this.getWidth();
}
}
Code:
class SquareTest {
public static void main(String[] args) {
Square square = new Square(0,0,4);
System.out.println("Location: " + square.getLocation());
System.out.println("Length: " + square.getLength());
System.out.println("Area: " + square.getArea());
}
}
Now I need cash. :(