-
Newbie Question
I am taking a Java class and recently we have been learning how to write programs with multiple classes. For one of my homework assignments i have to write a program which would calculate volume of a sphere. I have written the programm here is my code
class sphere{
public double radius;
public double pi = Math.PI;
void volume(){
System.out.print("When the radius of the sphere is "+radius);
System.out.println(" its volume is"+ (4*pi*(Math.pow(radius,3)/3)));
}
}
class spherecalc{
public static void main(String[]args){
int number = 0;
while(number<=10){
sphere firstsphere = new sphere();
firstsphere.radius = 1.234;
firstsphere.volume();
firstsphere.radius++;
number++;
}
}
}
Now what I have to do is, increase the radius of the sphere. The teacher showed us how to use the incriment operator "++". He did not require a loop statement, but i wanted to do it. My question is how would i increase the value of the radius from within the loop, because the way i tried did not work
-
Not sure exactly what you were trying to do. But you are creating a new object every loop and your ++ is never used.
Do the following before the loop.
sphere firstsphere = new sphere();
firstsphere.radius = 1.234;
And use a for loop instead of while. Works the same, for now.
-
Thank you for help, i have gone over my code and resolved the problem with your help.
I have one more question. When I assign values to variable and multiply them together i get a number in engeenering notation, is there a way that i can keep the value of two numbers without it being returned in engeenering notation?
-
Not sure off the top of my head. Check out some of the java Math APIs.
-
Please take a look at java.text.DecimalFormat class.
Sanjeev Pandit
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks