• Post Reply Bookmark Topic Watch Topic
  • New Topic

program displays 0.0 for both area and volume  RSS feed

 
Jonothon Turner
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Stephan van Hulst
Saloon Keeper
Posts: 7821
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your Cylinder class, area and volume are variables. You only set them once. When do you set them, and what is the value of height and radius at that time?

The solution is to get rid of the area and volume variables. Calculate these properties on the fly in your getArea() and getVolume() methods.
 
Henry Wong
author
Sheriff
Posts: 23284
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Makes sense. At the time that you calculated the area and volume, the radius and height has not been set yet. So, they are both zero when the calculation is done, yielding zero for both area and volume.

Henry
 
Jonothon Turner
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if i add this into my getvolume and area i get same isssue
public double getArea()
     {
         area = (2 * Math.PI * radius * height) + (2 * Math.PI * Math.pow(radius, 2));
         return area;
        }
     public double getVolume()
     {
         volume = height * Math.PI * radius * radius;
         return volume;
i get a compile error if i do not have double area and double volume at top of code
 
Jonothon Turner
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I am getting the same problem here too
 
Stephan van Hulst
Saloon Keeper
Posts: 7821
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because when you print your object, the toString() method shows the current value of area and volume, which have never been set.

Get rid of the variables, and return the result of the expression directly from your getters without assigning them to a variable. The toString() method should then call the getters.
 
Henry Wong
author
Sheriff
Posts: 23284
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jonothon Turner wrote:if i add this into my getvolume and area i get same isssue


You are getting the same issue because the code that prints out the area and volume do not use the getArea() and getVolume() methods. So, basically, you moved the expression from a location where it is executed too early, to a location where it is never executed.

Henry
 
Jonothon Turner
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't mean to be dumb here and I don't know why I am having a hard time understanding this but I still am not sure what you mean I think I am way to tired I have been working on this for about 2 hours and I don't typically ask for help I am sure when I actually see the issue and it work I will do a huge face palm
 
Stephan van Hulst
Saloon Keeper
Posts: 7821
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At line 49 you print your Cylinder object. That causes toString() to execute, which evaluates the area and volume variables.

The only thing you have done before that is construct the Cylinder with a radius and a height. The constructor sets these two variables, but area and volume remain zero.

A quick fix would be to call getArea() and getVolume() before you print the object, but you would have to do this every time you update the height and the radius.

A more robust solution is to just get rid of area and volume, because they are not independent variables. Your toString() method should then call getArea() and getVolume(), instead of referring to the variables.
 
Risha Rommi
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is very useful sharing
 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another minor point:-
Avoid Math.pow(x, 2)
You will usually get faster execution and better precision in the result by using
x * x

Have you managed to sort out the problem? Try simplifying the problem by using a circle rather than a cylinder. A circle has one attribute, its radius. (Well, it might have location of centre and orientation of plane, too, but those features will just add confusion if you try to implement them.) So implement a Circle class with one field, one constructor taking radius as its parameter and three get methods: getRadius, getArea and getCircumference. Implement the following as its toString method, then you can write System.out.println(c); and get something sensible displayed:-There are other ways to implement that method. Once you have got it working for a circle, you shоuld find it easy to extend to cylinders.
 
Jonothon Turner
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I appreciate all the help i do
i just am still not getting it
i feel like an idiot here sorry guys
thanks for the help anyway
 
Bill foster
Ranch Hand
Posts: 63
Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jonothon,

You might want to take a break, it is hard sometimes if you are tired to solve a problem.

Sometimes when my students are having trouble with a problem the best way is to break it down in to steps.
In understanding algorithms, we must go step by step.

Do a few problems on paper and then try to use pseudo code to write the steps then afterwards you can translate to code.

You are not stupid and please do not get frustrated we all make msitakes.

Take care!

Bill
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!