Advertisement
akosiraff

RectangleArea

Apr 20th, 2014
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1.  
  2. Download: http://solutionzip.com/downloads/rectanglearea/
  3. Refer to the example code of the Circle class in Chapter 8.2, design a class named Rectangle to represent a rectangle. The class contains:
  4. Two double data fields named width and length that specify the width and length of the rectangle. The default values are 0.0 for both width and length.
  5. A no-arg constructor that creates a default rectangle.
  6. A constructor that creates a rectangle with the specified width and length.
  7. A method named getArea() that returns the area of this rectangle. A method named getPerimeter() that returns the perimeter. Accessor and Mutator methods for length and width;
  8. Make sure that your constructors and mutator methods can check the new length and width to make sure only positive values can be used in your code, otherwise print out error message to the user.
  9. Create your own project in Eclipse, add a test class with main method as usual first. Then add another new class called Rectangle without the main method in the same project. Now you should have two class files in your project. Implement the Rectangle class first. In your test class main method, creates two Rectangle objects—one with width 4 and length 40 and the other with width 3.5 and length 35.9. Display the width, length, area, and perimeter of each rectangle in this order.
  10. There are two java files, one is the partial code of the Rectangle class, you still need to define the constructors with two parameters, accessor and mutator methods for data field length, and the getPerimeter() method. The second file is the partial code of the test program class, you need to create two other rectangle objects, calculate and print out the width, length, area and perimeter for each rectangle object. You can add your code to these classes.
  11. public class Rectangle {
  12. private double width;
  13. // define length
  14. private double length;
  15. // default constructor
  16. public Rectangle (){
  17. width = 0.0;
  18. // initialize length too
  19. length = 0.0;
  20. }
  21. public double getWidth(){
  22. return width;
  23. }
  24. public void setWidth (double newWidth)
  25. {
  26. if (newWidth > 0)
  27. width = newWidth;
  28. else
  29. System.out.println(“Width must be positive numbers”);
  30. }
  31. public double getArea(){
  32. return width*length;
  33. }
  34. }
  35. public class TestProgramm {
  36. /**
  37. * @param args
  38. */
  39. public static void main(String[] args) {
  40. // TODO Auto-generated method stub
  41. Rectangle r1 = new Rectangle ();
  42. r1.setWidth(5.0);// the length is still 0, so the area is 0;
  43. System.out.println(“the area is :” + r1.getArea());
  44. }
  45. }
  46.  
  47. Download: http://solutionzip.com/downloads/rectanglearea/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement