Win a copy of Serverless Applications with Node.js this week in the NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

getting error: incompatible types: possible lossy conversion from double to int  RSS feed

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all, I'm getting errors on my Java. I'm not understanding what the error means. How do I fix them?
                                   
 

Problem Description:

Create an abstract class called Shape to store information about a shape. Each shape contains the figure type and a method to calculate the area. Create sub-classes of Shape called Square and Circle. Note: That radius, sides, and areas can be decimals.

Files

   ShapeMain.java
   Shape.java
   Square.java
   Circle.java

Shape Main Class

Use the main class for testing. You won't be graded on your main class other than it must exist.

Shape Class

Build the Shape class with the following specifications:

   Must be abstract
   Public abstract getArea()

Square Class

Build the Square class with the following specifications:

   Subclass of Shape
   Public setSide()
   Public getSide()
   Implements getArea() correctly

Ex: Using setSide(5) results in getArea() return 10. Area = side*side

Circle Class

Build the Circle class with the following specifications:

   Subclass of Shape
   Public setRadius()
   Public getRadius()
   Implements getArea() correctly

Ex: Using setRadius(5) results in getArea() return 10. Area = PI*(R^2). Hint use Math.PI

Here is my code:



                           
 
Sheriff
Posts: 13386
221
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks like the test is trying to pass double values and your methods only take int values. That's the "lossy conversion" referred to in the error message, meaning you could potentially lose decimals since the receiving variable is an int.
 
Ranch Hand
Posts: 1325
3
Java Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is a compilation error between types. Converting from double to int is potentially a lossy one because there are double values which don't correspond with any int value.
 
S. Eckstrom
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you guys, I adjusted the int to a double. The only trouble I have now is for this Test, "Circle setRadius & getArea."
It shows my output as:  Circle setRadius & getArea not implemented correctly.
Do you see a reason that it is giving me this? It is not an error in Java, it is just a portion of the test that is not correct.
Do I not have my math formula correct for the radius? (Circle class. Line 23)



 
Sheriff
Posts: 5813
150
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The formula looks okay to me (but I've been known to miss things).  Where is the test that outputs "Circle setRadius & getArea" coming from?  A test site?  A class someone else wrote?  Is it looking for a certain precision for the double?
 
Marshal
Posts: 63837
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can see problems with your design. Any shape will have a perimeter and an area. But you ahve all sorts of methods for setting dimensions. You are calling it side for a square and radius for a circle, but maybe those nethods aren't quite appropriate. You don't usually have squares which change size, so why do you need a setXXX() method at all? You cannot inherit those methods from Shape because they have different names.
Maybe you are better off setting the sizes in the constructor and making the classes immutable. Then you wouldn't have any problems about differently‑named setXXX() methods.
Who told you to call those constructors default constructors? They are not default constructors.
 
S. Eckstrom
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys, thanks for responding. The assignment is from ZyBooks, an online class, that is very strict about how code and spacing is written. The used items come from the lesson. (see below)
Sorry about the color, I just want to emphasize the wording for the examples. I have found throughout the class, that the examples are the information ZyBooks wants used. So that is why I used what I did.
I know that there has to be better ways of doing this, but since this is my first Java class, I am just barely beginning. I hope I addressed why my code is written like this. Please if there is something I can change that will help me it would be well received and I would be thankful for your help and advice.

Square Class

Build the Square class with the following specifications:
   Subclass of Shape
   Public setSide()
   Public getSide()
   Implements getArea() correctly

Ex: Using setSide(5) results in getArea() return 10. Area = side*side

Circle Class
Build the Circle class with the following specifications:
   Subclass of Shape
   Public setRadius()
   Public getRadius()
   Implements getArea() correctly

Ex: Using setRadius(5) results in getArea() return 10. Area = PI*(R^2). Hint use Math.PI
 
Junilu Lacar
Sheriff
Posts: 13386
221
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

S. Eckstrom wrote:Ex: Using setRadius(5) results in getArea() return 10.


Uhm...

jshell> Math.PI * 5 * 5
$10 ==> 78.53981633974483
 
S. Eckstrom
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your help!!! I needed to add another set of parenthesis () around my math equation.
 
Junilu Lacar
Sheriff
Posts: 13386
221
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see how that would have changed the outcome. Those parentheses shouldn't make any difference.

jshell> Math.PI * 5 * 5
$10 ==> 78.53981633974483

jshell> 5.0 * 5.0 * Math.PI
$11 ==> 78.53981633974483

jshell> Math.PI * (5 * 5)
$12 ==> 78.53981633974483

jshell> 5.0 * Math.PI * 5.0
$13 ==> 78.53981633974483
 
Beauty is in the eye of the tiny ad.
global solutions you can do in your home or backyard
https://coderanch.com/t/708587/global-solutions-home-backyard
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!