使用单独的测试器的Rectangle类java

时间:2015-02-20 20:06:02

标签: javascript methods variable-assignment

所以我的目标是让一个程序工作,有多种方法来操作矩形。所以我的大部分代码都可以工作,但是当我使用我的测试器时,我会遇到错误。任何insite都会非常感激! 首先是我的Tester Class。

 public class MyTester {

public static void main(String[] args) {


    Rectangle i = new Rectangle(5,8);
    Rectangle p = new Rectangle(8,5);
    Rectangle o = new Rectangle(4);
    Rectangle n = new Rectangle();

    System.out.println("---------------------------------------------------------------"); //line seperation per testers

    //this prints out the width and height to the user (2 arg construct)
    System.out.println("i width: " + i.getWidth() + "\n" + "i height: " + i.getHeight());

    System.out.println("---------------------------------------------------------------");

    //This prints out the width and height to the user (1 arg construct)
    System.out.println("o width: " + o.getWidth() + "\n" + "o height: " + o.getHeight());

    System.out.println("---------------------------------------------------------------");

    System.out.println("n width: " + n.getWidth() + "\n" + "n height: " + n.getHeight());

    System.out.println("---------------------------------------------------------------");

    System.out.println("Here is the duplicate d of rectangle duplicate: ");// rectangleDuplicate
    System.out.println("Fix this sections of the tester and code!");

    System.out.println("---------------------------------------------------------------");

    //This prints out to the user if Rectangles are equal or not
    System.out.println(i.equals(p));

    System.out.println("---------------------------------------------------------------");

    //This prints out to the user the new width and height after rotating
    System.out.println(i.rotate());

    System.out.println("---------------------------------------------------------------");

    //This prints out to the user the area of the selected rectangle
    System.out.println("The area of Rectangle o: " + o.getArea());

    System.out.println("---------------------------------------------------------------");

    //This prints out to the user the diagonal of the Rectangle selected
    System.out.println("The diagonal of Rectangle p: " + p.getDiagonal());

    System.out.println("---------------------------------------------------------------");

    System.out.println("A copy of Rectangle i: " + i.copy());//.copy
    System.out.println("Fix this sections of the tester and code!");

    System.out.println("---------------------------------------------------------------");

    System.out.println(p.scale());//.scale
    System.out.println("Fix this sections of the tester and code!");

    System.out.println("---------------------------------------------------------------");

    System.out.println("Lets double the area and pull the new width and height for Rectangle o: " + o.doubleIt());//.doubleIt
    System.out.println("Fix this sections of the tester and code!");
    }

}

其次是我的矩形类

 import java.util.*;

public class Rectangle {

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



//One no-arg constructor that constructs a rectangle 1X1
public Rectangle() {
    width = 1.0;
    height = 1.0;
}



//One one-arg constructor that takes one double argument, and constructs a square that size
public Rectangle(double sameSides) {
    width = sameSides;
    height = sameSides;
}



//One two-arg constructor that takes two double arguments, and constructs a rectangle that size
//Getters and Setters
public double getWidth() {
    return width;
}
public void setWidth(double width) {
    this.width = width;
}
public double getHeight() {
    return height;
}
public void setHeight(double height) {
    this.height = height;
}



//One one-arg constructor that takes a Rectangle as an argument, and constructs as new Rectangle of identical size
public Rectangle(Rectangle rectangleDuplicate){ //calling rectangleDuplicate
    Rectangle d = new Rectangle();
    d.width = rectangleDuplicate.getWidth(); //set width to width of rectangleDuplicate
    d.height = rectangleDuplicate.getHeight(); //set height to height of rectangleDuplicate
}





//A ".equals()" method that accepts another Rectangle as argument, and returns TRUE if the other rectangle has an area within 1% of the instance rectangle.
public boolean equals(Rectangle p){
    double areaTest = (this.getArea()) / (getArea());
    if (areaTest <= 1.01 && areaTest >= 0.99){
        System.out.println("The area's are within 1% and thus are declared the same.");
        return true;
    }else{
        System.out.println("These two Rectangles are not the same.");
        return false;
    }
}

//A ".rotate()" method that reverses height and width of the instance rectangle.
public boolean rotate(){
    double newWidth = height;
    double newHeight = width;
    System.out.println("The new width is: " + newWidth + "\n" + "The new height is: " + newHeight);
    return true;
}




//A .getArea()" method that returns the area of the rectangle.
public double getArea() {
    double area = width * height;
    return area;
}


//A ".getDiagonal()" method that returns the diagonal of the rectangle.
public double getDiagonal() {
    double diagonal = 0;
    diagonal = (width * width) + (height * height);
    diagonal = Math.sqrt(diagonal);
    return diagonal;
}



//A ".copy()" method that returns a rectangle of identical size.
public void copy(){ 
    Rectangle rectangleCopy = new Rectangle(width,height);
    rectangleCopy.setWidth(width); //set width to width of rectangleCopy
    rectangleCopy.setHeight(height); //set height to height of rectangleCopy
}



//A ".scale()" method that accepts a single double argument, and scales the instance rectangle by that amount.  For example, a 4X3 rectangle with .scale(2.0) applied to it should become 8X6 in size.
public void scale(){
    System.out.println("By what scale would you like to multiply?");
    Scanner scan = new Scanner(System.in);
    double scale = scan.nextDouble();

    width = width * scale;
    height = height * scale;
}




//A ".doubleIt()" method which should double the area of the rectangle, without changing its aspect ratio (the ratio between the height and width).
public void doubleIt(){
    double area = getArea() * 2;
    height = area / (4*width);
    width = area / (4*((height) * (4*width / area)));        
}

}

1 个答案:

答案 0 :(得分:0)

好的第一个问题我发现了:

//One one-arg constructor that takes a Rectangle as an argument, and constructs as new Rectangle of identical size
public Rectangle(Rectangle rectangleDuplicate){ //calling rectangleDuplicate
    Rectangle d = new Rectangle();
    d.width = rectangleDuplicate.getWidth(); //set width to width of rectangleDuplicate
    d.height = rectangleDuplicate.getHeight(); //set height to height of rectangleDuplicate
}

您正在构造函数中为第二个矩形创建第三个矩形。它应该是:

//One one-arg constructor that takes a Rectangle as an argument, and constructs as new Rectangle of identical size
public Rectangle(Rectangle rectangleDuplicate){ //calling rectangleDuplicate
    this.width = rectangleDuplicate.getWidth(); //set width to width of rectangleDuplicate
    this.height = rectangleDuplicate.getHeight(); //set height to height of rectangleDuplicate
}

旁注:尝试始终使用此功能。使用对象的属性时,它使代码更容易阅读。如果您正在尝试更改当前对象属性,则其他人不必考虑。

第二期:

这实际上并没有改变矩形的属性,只是打印了值。

//A ".rotate()" method that reverses height and width of the instance rectangle.
public boolean rotate(){
    double newWidth = height;
    double newHeight = width;
    System.out.println("The new width is: " + newWidth + "\n" + "The new height is: " + newHeight);
    return true;
}

这使得改变:

//A ".rotate()" method that reverses height and width of the instance rectangle.
public boolean rotate(){
    double newWidth = height;
    double newHeight = width;

    // Access the rectangle attributes and change them to the new values
    this.height = newHeight;
    this.width = newWidth;

    System.out.println("The new width is: " + this.width + "\n" + "The new height is: " + this.height);

    // Why do you need a return? Is it part of the testing?
    return true;
}

第三个问题:

//A ".copy()" method that returns a rectangle of identical size.
public void copy(){ 
    Rectangle rectangleCopy = new Rectangle(width,height);
    rectangleCopy.setWidth(width); //set width to width of rectangleCopy
    rectangleCopy.setHeight(height); //set height to height of rectangleCopy
}

您不会返回新矩形。它在方法运行后被丢弃。

您为这些输入创建的构造函数已经设置了高度和宽度。之后你不需要再设置它们。

应用修正:

//A ".copy()" method that returns a rectangle of identical size.
public Rectangle copy(){ 
    Rectangle rectangleCopy = new Rectangle(width,height);
    return rectangleCopy;
}

这些是我到目前为止能找到的所有问题。

编辑:我将如何测试。

例如测试copy()

Rectangle result = i.copy();
// Check if height and width of the new rectangle have been initialized
// to the values of width and height of Rectangle i
System.out.println(result.getWidth());
System.out.println(result.getHeight());