2矩形,重叠,相交,并集。 JUnit失败:testNotOverlaps(RectangleGraderTest)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2矩形,重叠,相交,并集。 JUnit失败:testNotOverlaps(RectangleGraderTest)相关的知识,希望对你有一定的参考价值。
尝试解决这个错误,尝试修复此错误,并在自己尝试了很多之后来到你们中间有些沮丧。我需要创建一个Java程序,该程序允许在2个矩形(类似于地图)上运行某些功能。我使用JUnit 4测试的两个矩形(另一个要求)为“真”表示重叠,但它们不应重叠。消息是:
“失败:testNotOverlaps(RectangleGraderTest)消息:x:0,y:0,w:20,h:10不应与x:-50,y:-50,w:5,h:5重叠,但是确实如此。“
屏幕快照以及我的代码和标题非常不言自明。我不断收到此错误,我需要它消除。我很高兴回答任何其他问题:)
Rectangle.java:
import java.util.NoSuchElementException;
/**
* This class represents a rectangle. A has x and y coordinates and width and height.
*/
public class Rectangle {
private int x;
private int y;
private int width;
private int height;
/**
* Constructs a rectangle object and initializes it with the given x, y, height and width.
*
* @param x the x coordinate of this rectangle
* @param y the y coordinate of this rectangle
* @param height the height of this rectangle
* @param width the width of this rectangle
* @throws IllegalStateException if width or/and height is non-positive
*/
public Rectangle(int x, int y, int width, int height) throws IllegalArgumentException {
this.x = x;
this.y = y;
if (height <= 0) {
throw new IllegalArgumentException("Height can't be non-positive.");
}
if (width <= 0) {
throw new IllegalArgumentException("Width can't be non-positive.");
}
if (width > 0) {
this.width = width;
}
if (height > 0) {
this.height = height;
}
}
/**
* Returns true if this rectangle overlaps with other, false otherwise.
*
* @param other the other rectangle
* @return true if this rectangle overlaps with other, false otherwise.
*/
public Boolean overlap(Rectangle other) {
if (this.y > other.y + other.height && other.x < this.x + this.width
|| this.y + this.height > other.y && this.x < other.x + other.width
|| this.x > other.x && this.y > other.y && this.x + this.width < other.x + other.width
&& this.y + this.height < other.y + other.height) {
return true;
} else {
return false;
}
}
/**
* Returns a Rectangle object that represents the overlap of the two rectangles, otherwise throws
* a NoSuchElementException.
*
* @param other the other rectangle
* @return Rectangle object that represents the overlap of the two rectangles
* @throws java.util.NoSuchElementException if no intersection exists
*/
public Rectangle intersect(Rectangle other) throws NoSuchElementException {
if (!this.overlap(other)) {
throw new NoSuchElementException("Rectangles do not overlap");
}
int xBottomLeft = this.x;
int yBottomLeft = this.y;
int xTopRight = this.x + this.width;
int yTopRight = this.y + this.height;
int xBottomLeftOther = other.x;
int yBottomLeftOther = other.y;
int xTopRightOther = other.x + other.width;
int yTopRightOther = other.y + other.height;
int xBottomLeftInter = Math.max(xBottomLeft, xBottomLeftOther);
int yBottomLeftInter = Math.max(yBottomLeft, yBottomLeftOther);
int xTopRightInter = Math.min(xTopRight, xTopRightOther);
int yTopRightInter = Math.min(yTopRight, yTopRightOther);
if (xBottomLeftInter > xTopRightInter || yBottomLeftInter > yTopRightInter) {
throw new NoSuchElementException("Rectangles do not overlap");
}
Rectangle interRect = new Rectangle(xBottomLeftInter, yBottomLeftInter,
xTopRightInter - xBottomLeftInter, yTopRightInter - yBottomLeftInter);
return interRect;
}
/**
* Returns a Rectangle object that represents the union of this rectangle and the other rectangle.
*
* @param other the other rectangle
* @return Rectangle object that represents the overlap of the two rectangles
*/
public Rectangle union(Rectangle other) {
int xBottomLeft = Math.min(this.x, other.x);
int yBottomLeft = Math.min(this.y, other.y);
int xTopLeft = xBottomLeft;
int yTopLeft = Math.max(this.y + this.height, other.y + other.height);
int xTopRight = Math.max(this.x + this.width, other.x + other.width);
int yTopRight = yTopLeft;
int xBottomRight = xTopRight;
int yBottomRight = yBottomLeft;
Rectangle unionRect = new Rectangle(xBottomLeft, yBottomLeft, xBottomRight - xBottomLeft,
yTopLeft - yBottomLeft);
return unionRect;
}
/**
* Get the toString method of this rectangle.
*
* @return the toString method of this rectangle
*/
public String toString() {
return ("x:" + x + ", y:" + y + ", w:" + width + ", h:" + height);
}
}
RectangleTest.java:
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import org.junit.Before;
import org.junit.Test;
/**
* A JUnit test class for the Rectangle class.
*/
public class RectangleTest {
private Rectangle rect4;
private Rectangle rect5;
private Rectangle rect6;
private Rectangle rect7;
private Rectangle rect8;
private Rectangle rect9;
private Rectangle grader1;
private Rectangle grader2;
@Before
public void setUp() {
rect4 = new Rectangle(1,1,4,7);
rect5 = new Rectangle(2,4,5,6);
rect6 = new Rectangle(3, 5, 6, 4);
rect7 = new Rectangle(2, 3, 49,4);
rect8 = new Rectangle(-87,-45, 2,3);
rect9 = new Rectangle(-1,-1,20,15);
grader1 = new Rectangle(0,0,20,10);
grader2 = new Rectangle(-50, -50, 5,5);
}
@Test(expected = IllegalArgumentException.class)
public void testTransmissionBoxIllegalArgumentException() {
Rectangle rect1 = new Rectangle(0,0,0,0);
Rectangle rect2 = new Rectangle(-1,-2,-3,-4);
Rectangle rect3 = new Rectangle(2, 4, -3, -3);
}
@Test
public void testOverlap() {
assertEquals(true, rect4.overlap(rect5));
assertEquals(true, rect4.overlap(rect7));
assertFalse("false", rect8.overlap(rect4));
assertEquals(true, rect7.overlap(rect6));
assertEquals(true, rect5.overlap(rect6));
assertFalse("false", rect8.overlap(rect5));
assertEquals(true, rect6.overlap(rect4));
assertEquals(true, rect9.overlap(rect4));
assertEquals(true, grader1.overlap(grader2)); // this should be false!!!
}
@Test
public void testIntersect() {
assertEquals(new Rectangle(2,4,3,4).toString(), rect4.intersect(rect5).toString());
}
@Test
public void testUnion() {
assertEquals(new Rectangle(1,1,6,9).toString(), rect4.union(rect5).toString());
assertEquals(new Rectangle(1,1,6,9).toString(), rect5.union(rect4).toString());
assertEquals(new Rectangle(-87,-45,106,59).toString(), rect8.union(rect9).toString());
assertEquals(new Rectangle(-87,-45,106,59).toString(), rect9.union(rect8).toString());
assertEquals(new Rectangle(2,3,49, 6).toString(), rect6.union(rect7).toString());
assertEquals(new Rectangle(2,3,49,6).toString(), rect7.union(rect6).toString());
}
@Test
public void testToString() {
assertEquals("x:1, y:1, w:4, h:7", rect4.toString());
}
}
评分详情:Junit分级机选定的测试结果
[检查重叠时,此条件this.y > other.y + other.height && other.x < this.x + this.width
检查“其他最大值y小于此最小值y”是否表示没有重叠,因此两个矩形重叠的部分不能成立。
以下是一组简单的条件,用于检查一个矩形的垂直边缘和水平边缘是否与另一个矩形的边缘交叉:
public boolean overlap(Rectangle other) {
//for overlap both vertical and horizontal edges need to cross
return
//check if horizontal edge cross
other.width + other.x > x && //other max x is bigger than min x and
other.x < width + x && //other min x is smaller than max x
//check if vertical edge cross
other.height+ other.y > y &&
other.y <height + y ;
}
一些注意事项:1.不要使用异常来控制程序的正常流程。异常就是这样:使用它们来处理运行时异常。例如,插入以下内容:
if (!this.overlap(other)) {
throw new NoSuchElementException("Rectangles do not overlap");
}
使用
if (!this.overlap(other)) {
return null;
}
2.查看评论
if (height <= 0) {
throw new IllegalArgumentException("Height can't be non-positive.");
}
if (width <= 0) {
throw new IllegalArgumentException("Width can't be non-positive.");
}
if (width > 0) { //width is always > 0 here so `if` is not needed
this.width = width;
}
if (height > 0) { //height is always > 0 here so `if` is not needed
this.height = height;
}
- 考虑导入和使用java.awt.Rectangle
以上是关于2矩形,重叠,相交,并集。 JUnit失败:testNotOverlaps(RectangleGraderTest)的主要内容,如果未能解决你的问题,请参考以下文章