liangm20

Test

Oct 17th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 KB | None | 0 0
  1. public class Rectangle //object
  2. {
  3. int x1;
  4. int y1;
  5. int h1;
  6. int w1;
  7. public Rectangle (int x2, int y2, int w2, int h2)
  8. {
  9. h1 = h2;
  10. w1 = w2;
  11. x1 = x2;
  12. y1 = y2;
  13. }
  14. public int getHeight()
  15. {
  16. return h1;
  17. }
  18. public int getWidth()
  19. {
  20. return w1;
  21. }
  22. public int getX()
  23. {
  24. return x1;
  25. }
  26. public int getY()
  27. {
  28. return y1;
  29. }
  30. public static int Area(int w, int h)
  31. {
  32. return w*h;
  33. }
  34. public String toString()
  35. {
  36. return ("Rectangle[x=" + x1 + "," + " y=" + y1 + "," + " width=" + w1 + "," + " height=" + h1);
  37. }
  38. }
  39.  
  40. import java.util.*;
  41. public class RectangleMain //main class
  42. {
  43. public void main(String[] args)
  44. {
  45. int x = scannerX();
  46. int y =scannerY();
  47. int w=scannerW();
  48. int h=scannerH();
  49. Rectangle r1 = new Rectangle(x,y,w,h);
  50. System.out.println("The top-left corner of the rectangle is (" + r1.getX() + "," + r1.getY() + ")");
  51. System.out.println("The width of the rectangle is " + r1.getWidth());
  52. System.out.println("The height of the rectangle is " + r1.getHeight());
  53. System.out.println("The area of the rectangle is " + r1.Area(w,h));
  54. System.out.println(toString());
  55. }
  56. public int scannerX()
  57. {
  58. Scanner xCoord = new Scanner(System.in);
  59. System.out.print("Type in an x-coordinate.");
  60. int x = xCoord.nextInt();
  61. System.out.println();
  62. return x;
  63. }
  64. public int scannerY()
  65. {
  66. Scanner yCoord = new Scanner(System.in);
  67. System.out.print("Type in a y-coordinate.");
  68. int y = yCoord.nextInt();
  69. System.out.println();
  70. return y;
  71. }
  72. public int scannerW()
  73. {
  74. Scanner width = new Scanner(System.in);
  75. System.out.print("Type in a width.");
  76. int w = width.nextInt();
  77. System.out.println();
  78. return w;
  79. }
  80. public int scannerH()
  81. {
  82. Scanner height = new Scanner(System.in);
  83. System.out.print("Type in a height.");
  84. int h = height.nextInt();
  85. System.out.println();
  86. return h;
  87. }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment