矩形类java,带有单独的测试程序

Rectangle class java with separate tester

本文关键字:单独 测试程序 java      更新时间:2024-03-10

所以我的目标是让一个有多种方法来操作矩形的程序正常工作。到目前为止,我的大多数代码都能工作,但当我使用测试仪时,我会遇到错误。任何地方都将不胜感激!第一个是我的测试班。

 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.*;

公共类矩形{

//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)));        
}

}

好吧,我发现的第一个问题:

//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
}

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

//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;
}

到目前为止,这些都是我能找到的问题。

编辑:我将如何测试。

例如测试复制()

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());