计算矩形的面积,长度和宽度 - Java

时间:2014-08-12 09:22:32

标签: java

我知道有一个类似的主题:

Calculating perimeter and area of a rectangle

但我仍然不知道该怎么做。

public class Rectangle {

double length, height, rectArea; // assigning variables for length, height and area of the rectangle

Point ul; // assigning variables for all corners
Point ur;
Point lr;
Point ll;

Rectangle(Point p_ul, Point p_ur, Point p_lr, Point p_ll) { //constructor assigning Point variables 
    ul = p_ul;
    ur = p_ur;
    lr = p_lr;
    ll = p_ll;        
}

Rectangle(int x_ul, int y_ul, int x_ur, int y_ur, int x_lr, int y_lr, int x_ll, int y_ll) { //constructor assigning coordinates for each corner Point variable
    ul = new Point (x_ul, y_ul);
    ur = new Point (x_ur, y_ur);
    lr = new Point (x_lr, y_lr);
    ll = new Point (x_ll, y_ll);        
}

public double rectArea() {
    double length = Math.sqrt( (ur.getX()-ul.getX()) * (ur.getX()-ul.getX()) ) + ( (ur.getY()-ul.getY()) * (ur.getY()-ul.getY()) ); //calculates length
    double height = Math.sqrt( (ur.getX()-lr.getX()) * (ur.getX()-lr.getX()) ) + ( (ur.getY()-lr.getY()) * (ur.getY()-lr.getY()) ); //calculates height

    return(length * height);
}

void displayInfo() { //Print statements   
    System.out.println("Rectangle Info: ");
    System.out.println("Upper Left Point (x,y) : (" + ul.getX() + "," + ul.getY() + ")");
    System.out.println("Upper Right Point (x,y) : (" + ur.getX() + "," + ur.getY() + ")");
    System.out.println("Lower Left Point (x,y) : (" + ll.getX() + "," + ll.getY() + ")");
    System.out.println("Lower Right Point (x,y) : (" + lr.getX() + "," + lr.getY() + ")");        
    System.out.println("Area: " + rectArea);
    System.out.println("Length: " + length);
    System.out.println("Height: " + height);
    System.out.println("ur-lr(Y): " + (ur.getY()-lr.getY()));// this prints 4
    System.out.println("ur-lr(X): " + (ur.getX()-lr.getX()));// this prints 0
}    
}

我不确定为什么当我用实际的ul.get(X)等调用长度和高度时,我得到正确的长度和高度输出但是当我打印"长度"和"身高"我得到0,因此我的区域为0。

你很有可能告诉我,我是一个n00b。谢谢你的帮助。

2 个答案:

答案 0 :(得分:1)

您有2个局部变量,其名称与实例变量相同。这称为遮蔽变量。因此,当您通过这些方法调用它们时,您将获得正确的结果。但是您的实例变量尚未初始化。所以他们有默认的双精度值,我是0.0

像这样改变你的方法

public double rectArea() {
    length = Math.sqrt((ur.getX()-ul.getX()) * (ur.getX()-ul.getX())) + ((ur.getY()-ul.getY()) * (ur.getY()-ul.getY())); //calculates length
    height = Math.sqrt((ur.getX()-lr.getX()) * (ur.getX()-lr.getX())) + ((ur.getY()-lr.getY()) * (ur.getY()-lr.getY())); //calculates height

    return(length * height);
}

答案 1 :(得分:0)

您尝试直接使用未计算的变量,而不是调用rectArea()方法。 以下是更正后的计划:

import java.awt.Point;
public class Rectangle {

double length, height, rectArea; // assigning variables for length, height and area of the rectangle

Point ul; // assigning variables for all corners
Point ur;
Point lr;
Point ll;

Rectangle(Point p_ul, Point p_ur, Point p_lr, Point p_ll) { //constructor assigning Point variables 
    ul = p_ul;
    ur = p_ur;
    lr = p_lr;
    ll = p_ll;        
}

Rectangle(int x_ul, int y_ul, int x_ur, int y_ur, int x_lr, int y_lr, int x_ll, int y_ll) { //constructor assigning coordinates for each corner Point variable
    ul = new Point (x_ul, y_ul);
    ur = new Point (x_ur, y_ur);
    lr = new Point (x_lr, y_lr);
    ll = new Point (x_ll, y_ll);        
}

public double rectArea() {
    length = Math.sqrt( (ur.getX()-ul.getX()) * (ur.getX()-ul.getX())  + (ur.getY()-ul.getY()) * (ur.getY()-ul.getY()) ); //calculates length
    height = Math.sqrt( (ur.getX()-lr.getX()) * (ur.getX()-lr.getX())  + (ur.getY()-lr.getY()) * (ur.getY()-lr.getY()) ); //calculates height

    return(length * height);
}

void displayInfo() { //Print statements   
    System.out.println("Rectangle Info: ");
    System.out.println("Upper Left Point (x,y) : (" + ul.getX() + "," + ul.getY() + ")");
    System.out.println("Upper Right Point (x,y) : (" + ur.getX() + "," + ur.getY() + ")");
    System.out.println("Lower Left Point (x,y) : (" + ll.getX() + "," + ll.getY() + ")");
    System.out.println("Lower Right Point (x,y) : (" + lr.getX() + "," + lr.getY() + ")"); 
    rectArea = rectArea();
    System.out.println("Area: " + rectArea);
    System.out.println("Length: " + length);
    System.out.println("Height: " + height);
    System.out.println("ur-lr(Y): " + (ur.getY()-lr.getY()));// this prints 2
    System.out.println("ur-lr(X): " + (ur.getX()-lr.getX()));// this prints 0
}
public static void main(String[] args) {
    Rectangle rr = new Rectangle(1,3,3,3,3,1,1,1);
    rr.displayInfo();
}
}