Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Feb 2008
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Java Class/Main Help (homework)

    Hey =)

    I've got a Java assignment for school... I'm not asking for you to do the code for me

    Here is the problem:
    "Design and implement a class called 'Sphere' that contains instance data that represents the sphere's diameter. Define the 'sphere constructor' to accept and initialize the diameter, and include getter and setter methods for the diameter. Include methods that calculate and return the volume and surface area of the sphere. Included a 'toString' method that returns a one-line description of the sphere. Create a driver class called 'Multisphere', whose 'main' method instantiates and updates several Sphere objects"

    Apparently there are two files that are required.. I've got the Sphere.java file down.. I think. But what I am unsure about is what needs to be in the other file.. and in the 'main' method. Am I just supposed to make up values? I'm not really sure what I am doing. haha. I think I know how to create a driver class inside that file.. maybe.. but I don't know what it needs to do.
    Should I just like.. create 3 spheres and then assign them random diameters?

    Here is the code that I have for the Sphere.java file.

    Code:
    public class Sphere {
    
    	public double diameter;
    	
    	public Sphere(){
    		this.diameter = 0;}
    		
    	public void setDiameter(double d){
    		this.diameter = d;}
    		
    	public double getDiameter(){
    		return diameter;}
    		
    	public String toString(){
    		System.out.println("The diameter of the Sphere is: ");
    		String result = Double.toString(diameter);
    		return result;}
    		
    	public double volume(double v, double vr, double r){
    		r = ( diameter / 2 );
    		vr = ( r * r * r );
    		v = ( 4 / 3 ) * (Math.PI * vr);
    		return v;}
    	
    	public double surface(double sa, double r, double sr){
    		r = ( diameter / 2);
    		sr = ( r * r );
    		sa = ( 4 ) * (Math.PI * sr);
    		return sa;
    		}
    		
    }
    It compiles.. but I'm not sure if it's right.

    Again, I'm not asking for answers; I just need a little push in the right direction, because I am stuck =/

  • #2
    Regular Coder brad211987's Avatar
    Join Date
    Sep 2005
    Location
    Ohio
    Posts
    631
    Thanks
    10
    Thanked 50 Times in 50 Posts
    A few things first,

    Your volume and surface methods probably do not need to take any parameters, as they are using the diameter for the same class, which is stored as an instance variable. The methods look fine, I would just remove the parameters and declare the local variables inside the method, that way it uses the existing diameter already stored in the object. For example:

    Code:
    public double surface(){
    		double r = ( diameter / 2);
    		double sr = ( r * r );
    		double sa = ( 4 ) * (Math.PI * sr);
    		return sa;
    		}
    For the driver class, you will need to create instances of your sphere object, here is an example:

    PHP Code:
    MyObject obj = new MyObject(parameter1parameter2parameter3 etc...);
    obj.setData(5);
    int data obj.getData(); 
    This is just a general example, but it creates an object from a class called MyObject(would be stored in MyObject.java), and it calls the reference to this object, obj. Next, I call a set method, to pass in and set some data in the object, then I use a get method to get that piece of data from the object. You already have the object class almost ready to go, so all you will need to do is create a main method to hold the logic for creating multiple instances of your Sphere object.

  • Users who have thanked brad211987 for this post:

    Cynosure (02-24-2008)

  • #3
    New to the CF scene
    Join Date
    Feb 2008
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks for the help brad!!

    I've fixed up what you've suggested.. yet I am still a bit stuck. =(

    This is the code I have now..


    Sphere.java
    Code:
    public class Sphere {
    
    	public double diameter;
    	double r;
    	double sr;
    	double vr;
    	
    	public Sphere(){
    		this.diameter = 0;}
    		
    	public void setDiameter(double d){
    		this.diameter = d;}
    		
    	public double getDiameter(){
    		return diameter;}
    		
    	public String toString(){
    		System.out.println("The diameter of the Sphere is: ");
    		String result = Double.toString(diameter);
    		return result;}
    		
    	public double volume(){
    		r = ( diameter / 2 );
    		vr = ( r * r * r );
    		double v = ( 4 / 3 ) * (Math.PI * vr);
    		return v;}
    	
    	public double surface(){
    		r = ( diameter / 2);
    		sr = ( r * r );
    		double sa = ( 4 ) * (Math.PI * sr);
    		return sa;
    		}
    	
    		
    }


    GoSphere.java
    Code:
    public class GoSphere{
    
    	public static void main(String[] args){
    		
    		Sphere s1 = new Sphere();
    	
    		Sphere s2 = new Sphere();
    
    	s1.setDiameter(5);
    	double d1 = s1.getDiameter();  
    	
    	s2.setDiameter(8);
    	double d2 = s2.getDiameter();  
    		
            System.out.println ("Diamater of s1: " + d1 + ", Diameter of s2: " + d2);
    		
    		//s1.volume();
    		//s2.volume();
    		
    		//s1.surface();
    		//s2.surface();
    		
    	
    		
    		}
    }
    I get an error (total of 4) that says "cannot find symbol for 'Sphere'" in both of the object creation lines. I think with "s1.volume();" that I am calculating the volume.. right? But the instructions don't say to print it. I'm sooo lost. haha

    I think it'd be a bit easier if I understood what this thing is supposed to do. haha. It says all it needs to do is print the diameter of a sphere.. but it just has to have all this other junk that doesn't do anything =(

  • #4
    Regular Coder brad211987's Avatar
    Join Date
    Sep 2005
    Location
    Ohio
    Posts
    631
    Thanks
    10
    Thanked 50 Times in 50 Posts
    The "other junk" is important. Placing these types of methods in your Sphere class is important to object oriented programming, as these are core properties that would represent any Sphere object. Even if you are not using them in your testing class, some other time that you would use a Sphere object, you may want to use these methods to obtain the surface area and volume. It all depends on the context in which the object is used. To answer your questions, you are calculating the volume with the statement s1.volume(); but typically you would want to store that in a variable with a statement like: double s1Volume = s1.volume();

    On another note, these files compile and run fine for me, even after being modified to print the volume and surface area. Make sure they are both in the same directory, otherwise you will need to specify an import statement to access your Sphere class from your GoSphere class.

  • #5
    New to the CF scene
    Join Date
    Feb 2008
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Okay, thanks!

    I think I understand why the methods are there now.

    My friend says that it compiles on his computer, too. Kinda weird.. mine are in the same file and I'm doing everything right, but I still keep getting that error. But I guess since it works for the both of you I can turn it in =D

    I'm starting the second problem now.. I can only assume that I'll have problems there, as well. haha

    Thanks for the help =D

  • #6
    Regular Coder
    Join Date
    Jan 2008
    Location
    Willow Grove, PA
    Posts
    169
    Thanks
    1
    Thanked 27 Times in 27 Posts
    Quote Originally Posted by Cynosure View Post
    Define the 'sphere constructor' to accept and initialize the diameter
    Looks like you may have forgotten one little detail.


  •  

    Posting Permissions

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