User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 392,207 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,818 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Sep 20th, 2004
Views: 5,166
A program similar to CircleClass.java which defines a rectangle object. ClientProgram.java is the driver program for the Rectangle.java file.
java Syntax | 3 stars
  1. // Rectangle.java:
  2.  
  3. package rectangle;
  4. public class Rectangle
  5. {
  6. private double width = 1;
  7. private double height = 1;
  8. private static String color = "white";
  9. public Rectangle() { }
  10. public Rectangle(double w, double h, String c)
  11. {
  12. width = w;
  13. height = h;
  14. color = c;
  15. }
  16. public double getWidth() { return width; }
  17. public void setWidth(double w) { width = w; }
  18. public double getHeight() { return height; }
  19. public void setHeight(double h) { height = h; }
  20. public static String getColor() { return color; }
  21. public static void setColor(String c) { color = c; }
  22. public double findArea() { return height * width; }
  23. }
  24.  
  25. // ClientProgram.java
  26.  
  27. package rectangle;
  28. public class ClientProgram
  29. {
  30. public static void main(String args[])
  31. {
  32. Rectangle mySquare = new Rectangle(5, 5, "green");
  33. System.out.print("\n Height = " + mySquare.getHeight() );
  34. System.out.print("\n Width = " + mySquare.getWidth() );
  35. System.out.print("\n Color = " + mySquare.getColor() );
  36. mySquare.setHeight(10);
  37. mySquare.setWidth(15);
  38. System.out.print("\n\n Height = " + mySquare.getHeight() );
  39. System.out.print("\n Width = " + mySquare.getWidth() );
  40. System.out.print("\n Area = " + mySquare.findArea() );
  41. }
  42. }
Post Comment

Only community members can submit or comment on code snippets. You must register or log in to contribute.

DaniWeb Marketplace (Sponsored Links)
All times are GMT -4. The time now is 4:33 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC