4

I am learning Java and I am going through Deitel & Deitel's book, "Java: How To Program." While learning, I am attempting to do the exercises in the back. The specific exercise I am working on is:

Create a class Rectangle with attributes length and width, each of which defaults to 1. Provide methods that calculate the rectangle’s perimeter and area. Use set and get methods for both length and width. The set methods will verify that length and width are each floating-point numbers larger than 0.0 and less than 20.0. Write a program to test class Rectangle.

Here is the class I have 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 the Test Class or Drive 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());      

         }

 }

As I run the Test Class (in a IDE) this warning is showing:

"Incompatible types: possible lossy conversion from double to float."

I do not understand why the IDE is coming up with this warning. Can anyone help?

Looneyviticus
  • 97
  • 4
  • 13
  • floating point literals in Java are `double` type but your `Rectangle` constructor takes `float`. Just change the data type of parameters in your constructor to `double` to eliminate possible bugs later. – jdphenix May 01 '15 at 01:42
  • 1
    Whats up with accessors for public instance vars? Make those private! And what is `public float rectangle`? – D. Ben Knoble May 01 '15 at 01:46

2 Answers2

4

In Java, the literal value 1.0 or any other normal decimal value is considered a double (64-bit floating point value). Your constructor takes floats (32-bit floating point values) as inputs. You are trying to pass 64-bit numbers to a function that takes 32-bit numbers. Java will never let you do anything that could lose precision (such as passing an float to an int method or a double to a float method). The best way around this is to say

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

Adding the f tells java that the literal is a 32-bit float, not a 64-bit double. You can also add a cast:

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

but using the f notation is greatly preferable.

Vincent
  • 484
  • 3
  • 9
1

You're instantiating doubles with this line.

Rectangle rectangle1 = new Rectangle (3.2, 3.3);

To make floats, you'd need to do this:

Rectangle rectangle1 = new Rectangle (3.2f, 3.3f); 
theguywhodreams
  • 315
  • 1
  • 8