fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Rectangle {
  9.  
  10. double width;
  11. double height;
  12.  
  13. public Rectangle() {
  14. width = 1;
  15. height = 1;
  16. }
  17.  
  18. public Rectangle(double w, double h) {
  19. width = w;
  20. height = h;
  21. }
  22.  
  23.  
  24. public double getArea() {
  25. return (width*height);
  26. }
  27.  
  28. public double getPerimeter() {
  29. return ((2*width)+(2*height));
  30. }
  31.  
  32. public static void main(String[] args) {
  33. Rectangle oldRectangle = new Rectangle(4, 40);
  34. Rectangle newRectangle = new Rectangle(3.5, 35.9);
  35.  
  36. //In the following you access all the object variables and methods
  37.  
  38. System.out.println("Width of Rectangle 1 is: " + oldRectangle.width);
  39. System.out.println("Height of Rectangle 1 is: " + oldRectangle.height);
  40. System.out.println("Area of Rectangle 1 is: " + oldRectangle.getArea());
  41. System.out.println("Perimeter of Rectangle 1 is: " + oldRectangle.getPerimeter());
  42. System.out.println("Width of Rectangle 1 is: " + newRectangle.width);
  43. System.out.println("Height of Rectangle 1 is: " + newRectangle.height);
  44. System.out.println("Area of Rectangle 1 is: " + newRectangle.getArea());
  45. System.out.println("Perimeter of Rectangle 1 is: " + newRectangle.getPerimeter());
  46.  
  47. }
  48.  
  49. }
Success #stdin #stdout 0.1s 320320KB
stdin
Standard input is empty
stdout
Width of Rectangle 1 is: 4.0
Height of Rectangle 1 is: 40.0
Area of Rectangle 1 is: 160.0
Perimeter of Rectangle 1 is: 88.0
Width of Rectangle 1 is: 3.5
Height of Rectangle 1 is: 35.9
Area of Rectangle 1 is: 125.64999999999999
Perimeter of Rectangle 1 is: 78.8