# Rectangle Class

• 05-03-2012, 05:30 PM
RiterHeng
Rectangle Class
Create a class Rectangle. The class has attributes length and width, each of which defaults to 1. It has member functions that calculate the perimeter and the area of the rectangle. It has set and get functions for both length and width. The set functions should verify that length and width are each floating-point numbers larger than 0.0 and less than 20.0.

Here is what I wrote.....

Code:

```public class Rectangle {         import java.awt.Rectangle;         class RectangleClass {         float length=1;         float width=1;         public float getLength() {         return length;         }         private float setLength(float length) {         if ((length>0.0)&&(length<20.0))         {         return length;         }         else         {         System.out.printf("Invalid length (%.2f) set to 1.\n",length);         return (float)1;         }         }         public float getWidth() {         return width;         }         private float setWidth(float width) {         if ((width>0.0)&&(width<20.0))         {         return width;         }         else         {         System.out.printf("Invalid width (%.2f) set to 1.\n",width);         return 1;         }         }         public float calcPerimeter(float length, float width)         {         float perimeter=2 * (length + width);         return perimeter;         }         public float calcArea(float length, float width)         {         float area= length * width;         return area;         }         public static void main(String[] args) {         Rectangle r=new Rectangle();         System.out.println("*** Test 1 - Valid length and width ***");         float l=r.setLength(5);         float w=r.setWidth(2);         float a=r.calcArea(l, w);         float p=r.calcPerimeter(l, w);         System.out.println("Area: "a"\nPerimeter: "+p);         System.out.println();         // Test 2 - Invalid length and width         System.out.println("*** Test 2 - Invalid length and width ***");         float l2=r.setLength(21);         float w2=r.setWidth(-1);         float a2=r.calcArea(l2, w2);         float p2=r.calcPerimeter(l2, w2);         System.out.println("Area: "a2"\nPerimeter: "+p2);         }         }```
I get errors in my test 1 and 2 float lines, how do I fix them and what am I doing wrong to them?

Also I would like to incoperate the extra credit but not sure how to do it.

(5 pts) Put the class Rectangle into the package com.deitel.ch08. Modify Assign8.java to import it.

(5 pts) Modify the set methods in Rectangle to return a boolean indicating whether the input is valid. The test program should keep prompting for input if it is invalid.

Any help in appreciated.
• 05-03-2012, 07:10 PM
DarrylBurke
Re: Rectangle Class
Nothing at all to do with Applets. Moving to New to Java

db