Java Class Rectangle并使用demo类测试它

时间:2012-12-22 08:10:03

标签: java

我创建了一个名为Rectangle的Java类,它有两个实例变量(width& height)& 两种实例方法(面积和周长)两种方法都不采用参数 返回双精度值。区域方法返回矩形区域(宽度*高度) 圆周返回(2 *宽+ 2 *高)。然后使用main方法创建Demo类进行测试 Rectangle类通过实例化4个对象并提示用户输入宽度和高度 每个实例。然后打印每个实例的区域和周长。

我创建了两个类,第一个类是Rectangle:

public class Rectagle {

    private double width;
    private double height;

    public double area() {
        return width * height;
    }

    public double circumference() {
        return 2*width+2*height;
    }
}

我创建了第二个类Demo来测试类:

import java.util.Scanner;
public class Demo {
    public static void main(String []args){
        Scanner console=new Scanner(System.in);
    Rectagle R1=new Rectagle();
    Rectagle R2=new Rectagle();
    Rectagle R3=new Rectagle();
    Rectagle R4=new Rectagle();

    }
}

我的问题,我不明白这一点“并提示用户输入宽度和高度 每个实例。然后打印每个实例的区域和周长。

3 个答案:

答案 0 :(得分:1)

您的构造函数没有参数。没有办法为宽度和高度赋值。

我建议你有这种构造函数

public Rectangle(double w, double h){
     width = w;
     height = h;
}

并以这种方式使用它:

 Rectagle R1=new Rectagle(30.0, 40.0);

或者如果需要,为您的实例变量添加一个setter和getter,如下所示:

public void setWidth(double w){
   width = w
}

public double getWidth(){
   return width;
}

现在你的班级已经完成了。请参阅正确使用Scanner类以了解如何从控制台读取。请阅读此内容,例如:How to read integer value from the standard input in Java

答案 1 :(得分:0)

这可以帮助你

public class Rectangle {

    private double width;
    private double height;

    public Rectangle(double width, double height) {
        this.width = width;
        this.height = height;
    }

    public double getArea() {
        return width * height;
    }

    public double getCircumference() {
        return 2*width+2*height;
    }

    @Override
    public String toString() {
        return "Rectangle["+width+","+height+"]Area:"+getArea()+",Circumference:"+getCircumference();
    }

    public static void main(String[] args) {
         Scanner console=new Scanner(System.in);
        double width = getValue(console, "Width");
        double height = getValue(console, "Height");
        Rectangle rectangle = new Rectangle(width, height);
        System.out.println(rectangle);

    }

    public static double getValue(Scanner console, String name) {
        System.out.println("Enter "+name + " : ");
        String widthStr = console.nextLine();
        double parseDouble;
        try {
            parseDouble = Double.parseDouble(widthStr);
        }catch(NumberFormatException ne) {
            System.out.println("Unable to parse your input, enter correct value ");
            return getValue(console, name);
        }
        return parseDouble;
    }
}

答案 2 :(得分:0)

公共类矩形{

private double width;
private double height;

public Rectangle(double width, double height) {
    this.width = width;
    this.height = height;
}

public double getArea() {
    return width * height;
}

public double getCircumference() {
    return 2*width+2*height;
}

@Override
public String toString() {
    return "Rectangle["+width+","+height+"]Area:"+getArea()+",Circumference:"+getCircumference();
}

public static void main(String[] args) {
     Scanner console=new Scanner(System.in);
    double width = getValue(console, "Width");
    double height = getValue(console, "Height");
    Rectangle rectangle = new Rectangle(width, height);
    System.out.println(rectangle);
}

public static double getValue(Scanner console, String name) {
    System.out.println("Enter "+name + " : ");
    String widthStr = console.nextLine();
    double parseDouble;
    try {
        parseDouble = Double.parseDouble(widthStr);
    }catch(NumberFormatException ne) {
        System.out.println("Unable to parse your input, enter correct value ");
        return getValue(console, name);
    }
    return parseDouble;
}

}