Results 1 to 5 of 5
  1. #1
    Alex is offline Member
    Join Date
    Apr 2015
    Posts
    7
    Rep Power
    0

    Default Sphere Java Assignment

    Hi everyone, I'm new to Java and I have an assignment to create a Sphere class that will allow you to create Sphere objects using the code below. Then create a program called SphereTester that prompts the user for the radii of two spheres in meters. The program should display which sphere is larger and by how many cubic meters and display only four digits after the decimal point.
    I have the sphere class given to us for the assignment which is this:

    Java Code:
    public class Sphere {
    // instance variable (i.e., a field)
    private double radius;
    // constructor for the Sphere class
    public Sphere(double r) {
    radius = r;
    }
    // instance method that computes and returns the sphere's volume
    public double volume() {
    double v = 4.0 / 3.0 * Math.PI * Math.pow(radius, 3);
    System.out.println(v);
    return v;
    }
    // accessor method that returns the sphere's radius
    public double getRadius() {
    return radius;
    }
    }
    Here is a sample run of what the final result should look like

    Enter the radius of a sphere (in meters): 1
    Enter the radius of a 2nd sphere (in meters): 2
    Sphere 2 is greater than Sphere 1 by 29.3215 cubic meters


    I don't really know how to start this typing the Spheretester so any help at all would be helpful!

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Sphere Java Assignment

    What problems are you having? You have done the hard part. Do you know how to create sphere objects?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Alex is offline Member
    Join Date
    Apr 2015
    Posts
    7
    Rep Power
    0

    Default Re: Sphere Java Assignment

    This is what I have so far:

    import java.util.Scanner;
    public class SphereUser {

    public static void main(String[] args) {

    Scanner keyboard = new Scanner(System.in);

    System.out.println("Enter the radius of a sphere (in meters): ");
    Sphere sphere1 = new Sphere(keyboard.nextDouble());
    System.out.println("Enter the radius of the second sphere (in meters): ");
    Sphere sphere2 = new Sphere(keyboard.nextDouble());






    }
    }

    but I don't know how to calculate the difference in spheres in cubic meters and when I try to type

    System.out.println("Sphere 1 has a volume of " + sphere1.volume);

    I keep getting an error saying volume is not resolved or is not a field

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,716
    Rep Power
    18

    Default Re: Sphere Java Assignment

    I keep getting an error saying volume is not resolved or is not a field
    The compiler is saying that there is no volume field. In fact it is a method so you must call it with parentheses:

    Java Code:
    System.out.println("Sphere 1 has a volume of " + sphere1.volume());
    ---

    Once you have it displaying the volumes correctly you can start figuring out which is larger (probably using an if statement) and by how much (probably subtraction is involved).

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,716
    Rep Power
    18

Similar Threads

  1. How can I use a loop for volume of sphere?
    By wheehoowaffles in forum New To Java
    Replies: 8
    Last Post: 03-27-2013, 10:49 PM
  2. Volume of a sphere problem
    By dj_ee3 in forum New To Java
    Replies: 8
    Last Post: 02-14-2011, 02:10 AM
  3. 3D Terrain on Sphere?
    By quddusaliquddus in forum Advanced Java
    Replies: 1
    Last Post: 06-22-2009, 11:50 PM
  4. Help with sphere.java - not coming out correctly
    By JavaInLove in forum Java 2D
    Replies: 3
    Last Post: 03-17-2009, 08:38 PM
  5. Please help. Trying to display a sphere.
    By aknbad23 in forum New To Java
    Replies: 6
    Last Post: 10-07-2008, 05:37 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •