矩形类java,带有单独的测试程序
Rectangle class java with separate tester
所以我的目标是让一个有多种方法来操作矩形的程序正常工作。到目前为止,我的大多数代码都能工作,但当我使用测试仪时,我会遇到错误。任何地方都将不胜感激!第一个是我的测试班。
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());
相关文章:
- 使用Jest测试React Native应用程序
- 一个密码测试程序,如果存在空格,它会提醒用户
- Protractor:在Iframe中测试非角度应用程序
- 使用Node.js在应用程序引擎上进行本地单元测试
- 如何在rails应用程序中测试AJAX加载的内容
- 单页应用程序的javascript集成测试
- Minitest/Capibara:切换到JS驱动程序时,绿色测试变为红色
- 如何使用ngModel将自定义控件的提供程序扩展器分离到Angular 2中的单独文件中
- Angular,测试自定义提供程序和配置
- 为什么有单独的蒙戈.服务器和蒙戈.Db在Mongodb本地驱动程序中
- 在浏览器上显示实习运行程序功能测试的结果
- 使用 Jasmine 测试对象中的事件处理程序
- 测试 JavaScript Zombie 事件处理程序或 DOM 元素
- 矩形类java,带有单独的测试程序
- 如何链接到单独应用程序中的视图
- 如何使用javascript或html将数据传递到单独的程序
- 我应该测试返回的对象作为一个整体还是单独测试其属性
- regex可以在regex测试程序中工作,但不能在模式中工作
- JS新手对常见的测试程序感到困惑
- 为什么我可以't将这个简单的JavaScript测试程序执行到JSFiddle中