我在处理一个基于测试创建矩形的类时遇到问题。

huangapple go评论124阅读模式
英文:

I have problem with class that creates rectangle based on a test

问题

好的,以下是您提供的代码的中文翻译:

  1. // 测试矩形的方法,基于给定的点、高度和宽度创建矩形
  2. public void testRectangle1() {
  3. Point center = new Point(20, 30);
  4. Rectangle rect = new Rectangle(center, 20, 20);
  5. assertAll(
  6. () -> assertEquals(10, rect.getTopLeft().getX()),
  7. () -> assertEquals(20, rect.getTopLeft().getY()),
  8. () -> assertEquals(30, rect.getBottomRight().getX()),
  9. () -> assertEquals(40, rect.getBottomRight().getY()),
  10. () -> assertEquals(20, rect.getWidth()),
  11. () -> assertEquals(20, rect.getHeight())
  12. );
  13. }
  14. // Point 类定义
  15. package net.thumbtack.school.figures.v1;
  16. public class Point {
  17. private int x, y;
  18. public Point(int x, int y) {
  19. this.x = x;
  20. this.y = y;
  21. }
  22. public Point() {
  23. this(0, 0);
  24. }
  25. public int getX() {
  26. return x;
  27. }
  28. public void setX(int x) {
  29. this.x = x;
  30. }
  31. public int getY() {
  32. return y;
  33. }
  34. public void setY(int y) {
  35. this.y = y;
  36. }
  37. public void moveTo(int newX, int newY) {
  38. x = newX;
  39. y = newY;
  40. }
  41. public void moveRel(int dx, int dy) {
  42. x += dx;
  43. y += dy;
  44. }
  45. @Override
  46. public int hashCode() {
  47. final int prime = 31;
  48. int result = 1;
  49. result = prime * result + x;
  50. result = prime * result + y;
  51. return result;
  52. }
  53. @Override
  54. public boolean equals(Object obj) {
  55. if (this == obj)
  56. return true;
  57. if (obj == null)
  58. return false;
  59. if (getClass() != obj.getClass())
  60. return false;
  61. Point other = (Point) obj;
  62. if (x != other.x)
  63. return false;
  64. if (y != other.y)
  65. return false;
  66. return true;
  67. }
  68. }
  69. // 创建矩形的类定义
  70. package net.thumbtack.school.figures.v1;
  71. public class Rectangle {
  72. public int width;
  73. public int height;
  74. public Point center;
  75. public int xCenter;
  76. public int yCenter;
  77. private Point point;
  78. // 矩形构造方法
  79. public Rectangle(Point center, int width, int height) {
  80. this.width = width;
  81. this.height = height;
  82. this.center = center;
  83. }
  84. // 获取矩形左上角的点
  85. public Point getTopLeft() {
  86. Point point = getCenter();
  87. point.moveRel(- width / 2, - height / 2);
  88. return point;
  89. }
  90. // 获取矩形右下角的点
  91. public Point getBottomRight() {
  92. Point point = getCenter();
  93. point.moveRel(width / 2, height / 2);
  94. return point;
  95. }
  96. // 获取矩形宽度
  97. public int getWidth() {
  98. return width;
  99. }
  100. // 获取矩形高度
  101. public int getHeight() {
  102. return height;
  103. }
  104. // 获取矩形中心点
  105. public Point getCenter() {
  106. Point center = new Point(xCenter, yCenter);
  107. return center;
  108. }
  109. }

关于您提到的问题,您的构造函数 public Rectangle(Point center, int width, int height) 中似乎存在问题,导致测试返回错误的值。您可能需要仔细检查该构造函数内部的逻辑,确保正确计算矩形的位置和属性。

英文:

Ok so i have rectangle that should be created based on: method point that will crete coordinates of it center and values of hight and width, all value are based on this test

  1. public void testRectangle1() {
  2. Point center = new Point(20, 30);
  3. Rectangle rect = new Rectangle(center, 20, 20);
  4. assertAll(
  5. () -> assertEquals(10, rect.getTopLeft().getX()),
  6. () -> assertEquals(20, rect.getTopLeft().getY()),
  7. () -> assertEquals(30, rect.getBottomRight().getX()),
  8. () -> assertEquals(40, rect.getBottomRight().getY()),
  9. () -> assertEquals(20, rect.getWidth()),
  10. () -> assertEquals(20, rect.getHeight())
  11. );
  12. }

I have prewritten class point that i will add here for overall clarity

  1. package net.thumbtack.school.figures.v1;
  2. public class Point {
  3. private int x, y;
  4. public Point(int x, int y) {
  5. this.x = x;
  6. this.y = y;
  7. }
  8. public Point() {
  9. this(0, 0);
  10. }
  11. public int getX() {
  12. return x;
  13. }
  14. public void setX(int x) {
  15. this.x = x;
  16. }
  17. public int getY() {
  18. return y;
  19. }
  20. public void setY(int y) {
  21. this.y = y;
  22. }
  23. public void moveTo(int newX, int newY) {
  24. x = newX;
  25. y = newY;
  26. }
  27. public void moveRel(int dx, int dy) {
  28. x += dx;
  29. y += dy;
  30. }
  31. @Override
  32. public int hashCode() {
  33. final int prime = 31;
  34. int result = 1;
  35. result = prime * result + x;
  36. result = prime * result + y;
  37. return result;
  38. }
  39. @Override
  40. public boolean equals(Object obj) {
  41. if (this == obj)
  42. return true;
  43. if (obj == null)
  44. return false;
  45. if (getClass() != obj.getClass())
  46. return false;
  47. Point other = (Point) obj;
  48. if (x != other.x)
  49. return false;
  50. if (y != other.y)
  51. return false;
  52. return true;
  53. }
  54. }

And this is class for creating rectangle

  1. package net.thumbtack.school.figures.v1;
  2. public class Rectangle {
  3. public int width;
  4. public int height;
  5. public Point center;
  6. public int xCenter;
  7. public int yCenter;
  8. private Point point;
  9. public Rectangle(Point center, int width, int height) {
  10. this.width=width;
  11. this.height=height;
  12. this.center=center;
  13. }
  14. public Point getTopLeft() {
  15. Point point = getCenter();
  16. point.moveRel(- width / 2, - height / 2);
  17. return point;
  18. }
  19. public Point getBottomRight() {
  20. Point point = getCenter();
  21. point.moveRel(width / 2, height / 2);
  22. return point;
  23. }
  24. public int getWidth() {
  25. return width;
  26. }
  27. public int getHeight() {
  28. return height;
  29. }
  30. public Point getCenter() {
  31. Point center = new Point(xCenter, yCenter);
  32. return center;
  33. }

So problem is in constructor public Rectangle(Point center, int width, int height) when i run the test it returns wirng values

expected: <10> but was: <-10>
Comparison Failure:
Expected :10
Actual :-10

expected: <20> but was: <-10>
Comparison Failure:
Expected :20
Actual :-10

expected: <30> but was: <10>
Comparison Failure:
Expected :30
Actual :10

expected: <40> but was: <10>
Comparison Failure:
Expected :40
Actual :10

I dont this that the problem is into other methods becouse when i use them in different but simmilar constrictors everything works.

答案1

得分: 0

如maloomeister所提到的,您必须在使用之前初始化xCenteryCenter,否则xCenteryCenter的值将为0。

修改getCenter()方法:

  1. public Point getCenter() {
  2. Point point = new Point(center.getX(), center.getY());
  3. return point;
  4. }
英文:

As maloomeister mentioned you must init xCenter or yCenter before used,or the value of xCenter and yCenter is 0.

modify getCenter() method:

  1. public Point getCenter() {
  2. Point point = new Point(center.getX(), center.getY());
  3. return point;
  4. }

huangapple
  • 本文由 发表于 2020年9月21日 13:15:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/63986533.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定