Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

creating rectangle using cartesian coords

807600Oct 16 2007 — edited Oct 16 2007
I am trying to solve a problem that requires creating a Rectangle class that stores only the Cartesian coordinates of the 4 coords of the rectangle. A constructor must call a set method that accepts 4 sets of coords and runs a few validation checks (points x,y >=0 && <x,y 20). The set also checks to see if the supplied cords specify a rectangle. Methods are provided for calculating the area, width, perimeter, and length of a side (the longest of 2 sides). A predicate method isSquare should be provided to determine whether rectangle is square. In case you were wondering this is a homework example, but one that I feel I am close to figuring out with a little help.....

My problem is that I cannot seem to set up the appropriate method to capture all 4 coordinates and determine whether the object instantiated is a rectangle or a square. I can perform the required calculations but cannot put together a method that uses all of the coordinate values to determine shape. I think I am close but not close enough... Also the boolean check to determine whether the shape is a square is not ideal. I would like it to force the user to re-enter new coords (unless this whole class gets re-written in which case it may not be necessary).
thanks in advance to anyone who can lend some assistance in a pinch.
//my class begins here
import java.util.Scanner;

class CartRectangle
{
    private int x1;
    private int x2;
    private int x3;
    private int x4;
    private int y1;
    private int y2;
    private int y3;
    private int y4;
    private int side1;
    private int side2;

   public int setSide1()
   {
       return side1;
   }
 
    public int setSide2()
   {

       return side2;
   }
 
   public int getSide1()
    {
        side1 = y2-y1;     
        return side1; 
    }
    public int getSide2()
    {
        side2 = x3-x1;
        return side2;
    }
  
    public int getX1()
    {
        return x1;  
    }  
  
    public void setX1(int x1)
    {      
        this.x1 = x1;
        x1 = (x1 >=0 && x1 < 20 ? x1 : 1);
    }
  
    public int getX2()
    {
    return x2;  
    }
      
    public void setX2(int x2)
    {      
        this.x2 = x2;
        x2 = (x2 >=0 && x2 < 20 ? x2 : 1);
    }
     
    public int getX3()
    {      
        return x3;  
    }
 
    public void setX3(int x3)
    {
    this.x3 = x3;
        x3 = (x3 >=0 && x3 < 20 ? x3 : 1);
    }
        
    public int getX4()
    {      
        return x4;  
    }
 
    public void setX4(int x4)
    {      
        this.x4 = x4;
        x4 = (x4 >=0 && x4 < 20 ? x4 : 1);
    }

    public int getY1()
    {         
        return y1;  
    }
  
    public void setY1(int y1)
    {
    this.y1 = y1;
        y1 = (y1 >=0 && y1 < 20 ? y1 : 1);
    }
   
    public int getY2()
    {
    return y2;  
    }
 
    public void setY2(int y2)
    {
    this.y2 = y2;
        y2 = (y2 >=0 && y2 < 20 ? y2 : 1);
    }
      
    public int getY3()
    {
    return y3;
    }
 
    public void setY3(int y3)
    {
        this.y3 = y3;
        y3 = (y3 >=0 && y3 < 20 ? y3 : 1);
    }
      
    public int getY4()
    {
    return y4;
    }
 
    public void setY4(int y4)
    {
    
        this.y4 = y4;
        y4 = (y4 >=0 && y4 < 20 ? y4 : 1);
    }
    public int perimeter()
    {
        return 2 * side1 + 2 * side2;
    }
    
    public int area()
    {
        return side1 * side2;
    }
   

    public String toString()
   {
        return String.format( "%s: %2f\n" + "%s: %2f\n",
         getSide1(), getSide2());
   }
    private boolean determineIfSquare()
    {
        if( side1 == side2)
        {
            System.out.println("This is a square");
        }else 
        {         
            System.out.print("This is a rectangle.");
        } return(side1 == side2);
        
    }
    
    public static void main(String args[]) throws Exception {
 
        Scanner input = new Scanner(System.in);
        CartRectangle rectangle = new CartRectangle();
        System.out.println("First point starts at origin 0,0");
        rectangle.setX1(0);
        rectangle.setY1(0);
        System.out.println("Enter upper left Y: ");
        rectangle.setY2(input.nextInt());
        rectangle.setX2(rectangle.getX1());
        System.out.println("Enter upper right X: ");
        rectangle.setX3(input.nextInt());
        rectangle.setY3(rectangle.getY2());
        rectangle.setY4(rectangle.getY1());
        rectangle.setX4(rectangle.getX3());
      
      
        System.out.printf("(%s, %s)\n",rectangle.x1,rectangle.y1);
        System.out.printf("(%s, %s)\n",rectangle.x2,rectangle.y2);
        System.out.printf("(%s, %s)\n",rectangle.x3,rectangle.y3);
        System.out.printf("(%s, %s)\n",rectangle.x4,rectangle.y4);
        System.out.printf("first side is: %s\n", rectangle.getSide1());
        System.out.printf("second side is: %s\n", rectangle.getSide2());
        System.out.printf("The length is: %s\n", Math.max(rectangle.side1,rectangle.side2));
        System.out.printf("The width is: %s\n", Math.min(rectangle.side1,rectangle.side2));
        System.out.printf("The perimeter is: %s\n", rectangle.perimeter());
        System.out.printf("The area is: %s\n", rectangle.area());
        System.out.printf("  Square checker is %s\n", rectangle.determineIfSquare());
        System.out.println();
        //System.out.printf("The width is: %s\n", rectangle.getWidth());
    }
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 13 2007
Added on Oct 16 2007
9 comments
516 views