liangm20

Unit 3 Program, Edited

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