Newbie Question


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: Newbie Question

  1. #1
    Join Date
    Oct 2004
    Location
    Chicago
    Posts
    108

    Arrow 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

  2. #2
    Join Date
    Jun 2004
    Posts
    199
    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.

  3. #3
    Join Date
    Oct 2004
    Location
    Chicago
    Posts
    108

    Thumbs up

    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?

  4. #4
    Join Date
    Jun 2004
    Posts
    199
    Not sure off the top of my head. Check out some of the java Math APIs.

  5. #5
    Join Date
    Oct 2004
    Location
    USA
    Posts
    2
    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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center