Creating a class, then testing this class

I learn Java, and I look through Deitel and Deitel's books, "Java: How to Program." During training, I try to do exercises in the back. The specific exercise I'm working on is:

Create a Rectangle class with the length and width of the attributes, each of which defaults to 1. Provide methods that calculate the perimeter and area of ​​the rectangles. Use the set and get methods for length and width. Installed methods will verify that length and width are floating point numbers greater than 0.0 and less than 20.0. Write a program to test the Rectangle class.

Here is the class I created:

 package rectangle;

 public class Rectangle {

     public float rectangle;
     public float length;
     public float width;
     public float perimeter;
     public float area;

     public Rectangle(float length, float width){

        if(length < 0.0 || length >= 20.0){
             throw new IllegalArgumentException("Length must be between 0.0 and 20.0");
         }
        if(width < 0.0 || width >= 20.0){
             throw new IllegalArgumentException("Width must be between 0.0 abnd 20.00");
         }

         this.length = length;
         this.width = width;
     }


     public float getLength(){
         return length;
     }

     public float getWidth(){
         return width;
     }

     public void setPerimeter(float perimeter){
         perimeter = ((getLength() *2) + (getWidth()*2));
     }

     public float getPerimeter(){
         return perimeter;
     }

     public void setArea(float area){
         area = getLength() * getWidth();
     }

     public float area(){
         return area;
     }

 }

Here is a test class or disk class:

 package rectangle;

 public class TestRectangle {

         public static void main(String[] args){
                Rectangle rectangle1 = new Rectangle (3.2, 3.3);

                System.out.printf("The perimeter of rectangle1 is: %d%n", rectangle1.getPerimeter());      

         }

 }

When I run the test class (in the IDE), this warning shows:

" : double float".

, IDE . - ?

+4
2

Java 1.0 (64- ). (32- ). 64- , 32- . Java -, (, float int float). :

Rectangle rectangle1 = new Rectangle (3.2f, 3.3f);

f java, - 32- float, 64- double. :

Rectangle rectangle1 = new Rectangle ((float) 3.2, (float) 3.3);

f .

+4

.

Rectangle rectangle1 = new Rectangle (3.2, 3.3);

, :

Rectangle rectangle1 = new Rectangle (3.2f, 3.3f); 
+1

All Articles