I need constructor that will create rectangle using coordinates of the centers, high (X axis) and width (y axis)

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

I need constructor that will create rectangle using coordinates of the centers, high (X axis) and width (y axis)

问题

以下是已翻译的内容:

所以我的代码由3个部分组成,包括2个类和一个测试。

这是测试的代码:

  1. @Test
  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. }

Point 类运行正常,我只是为了清晰起见添加了它。

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

这是矩形本身的类,其中包括构造函数和附加方法。

  1. public class Rectangle {
  2. public int width = 0;
  3. public int height = 0;
  4. public Point center;
  5. public Rectangle(Point center, int width, int height) {
  6. int x = 0;
  7. int y = 0;
  8. width=x;
  9. height=y;
  10. }
  11. public Point getTopLeft() {
  12. Point point = new Point(center.getX(), center.getY());
  13. point.moveRel(- width / 2, height / 2);
  14. return point;
  15. }
  16. public Point getBottomRight() {
  17. Point point = new Point(center.getX(), center.getY());
  18. point.moveRel(width / 2, - height / 2);
  19. return point;
  20. }
  21. public int getWidth() {
  22. return width;
  23. }
  24. public int getHeight() {
  25. return height;
  26. }
  27. }

主要问题是此代码在测试中仅返回零,我猜问题可能在矩形类的构造函数或附加方法中。

英文:

So i code consist of 3 parts 2 classes and test.
This is code for test

  1. @Test
  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. }

Class Point works fine, and i am ading it just for clarity

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

This is class for Rectangle itself, and it includes both constructor and aditional methods.

  1. public class Rectangle {
  2. public int width = 0;
  3. public int height = 0;
  4. public Point center;
  5. public Rectangle(Point center, int width, int height) {
  6. int x = 0;
  7. int y = 0;
  8. width=x;
  9. height=y;
  10. }
  11. public Point getTopLeft() {
  12. Point point = new Point(center.getX(), center.getY());
  13. point.moveRel(- width / 2, height / 2);
  14. return point;
  15. }
  16. public Point getBottomRight() {
  17. Point point = new Point(center.getX(), center.getY());
  18. point.moveRel(width / 2, - height / 2);
  19. return point;
  20. }
  21. public int getWidth() {
  22. return width;
  23. }
  24. public int getHeight() {
  25. return height;
  26. }
  27. }

Main problem is that this code return only zeroes in tests, i guess problem is in rectangle class in constructor, or aditionalmethods.

答案1

得分: 1

你的Rectangle类的构造函数总是将宽度和高度设置为0。我认为构造函数应该如下所示...

  1. public Rectangle(Point center, int width, int height) {
  2. int x = 0;
  3. int y = 0;
  4. this.width = width;
  5. this.height = height;
  6. this.center = center;
  7. }
英文:

Your constructor for Rectangle is always setting width and height to 0. I think the constructor should look something like...

  1. public Rectangle(Point center, int width, int height) {
  2. int x = 0;
  3. int y = 0;
  4. this.width=width;
  5. this.height=height;
  6. this.center=center;
  7. }

huangapple
  • 本文由 发表于 2020年8月28日 02:17:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/63622022.html
匿名

发表评论

匿名网友

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

确定