Results 1 to 3 of 3

Thread: Can anyone help me finish my code for cash ? :P

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2005
    Posts
    2

    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 !!

  2. #2

    Thread Starter
    New Member
    Join Date
    Aug 2005
    Posts
    2

    Re: Can anyone help me finish my code for cash ? :P

    Think i may have cracked it

  3. #3
    Fanatic Member
    Join Date
    Jan 2005
    Location
    Cebu
    Posts
    607

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width