Page 1 of 2 12 LastLast
Results 1 to 20 of 29
Like Tree1Likes

Thread: Help with multiple classes

  1. #1
    Aimforthehead is offline Member
    Join Date
    Sep 2011
    Posts
    45
    Rep Power
    0

    Default Help with multiple classes

    This program is meant to use three classes (Cone, Sphere, and Cylinder) and a tester class(GeometryTester) to compute volume and surface area of the various objects with a given height and/or radius.
    I have the formulas from a previous project I worked on with just one class, but I am having issues with multiple classes. What I don't know how to do is get the arraylist size from my tester class and get that info into my three other classes (it is called getNumObjects, as you can see, I do not know what to put in them.) I have some other issues but I'd like to start there if anyone can give me some tips.

    The getNumObjects() method needs to return the number of objects of that class that have been created.
    Java Code:
    import java.util.ArrayList;
    
    /**
     * Test the Sphere, Cone, and Cylinder classes
     * @author 
     * COMS B14 - Nov 27, 2011
     * GeometryTester.java
     */
    public class GeometryTester {
    
    public static void main(String[] args) {
    	// Places to store objects
    	ArrayList<Sphere> spheres = new ArrayList<Sphere>();
    	ArrayList<Cone> cones = new ArrayList<Cone>();
    	ArrayList<Cylinder> cylinders = new ArrayList<Cylinder>();
    
    	// Create various objects
    	for (int r = 1; r < 5; ++r)
    		for (int h = 1; h < 5; ++h) {
    			if (r == h)
    				spheres.add(new Sphere(r));
    			if (r > h)
    				cones.add(new Cone(r, h));
    			if (r + h == 6)
    				cylinders.add(new Cylinder(r, h));
    		}
    	
    	// Print information about the objects created
    	System.out.printf("%d spheres\n", Sphere.getNumObjects());
    	for (Sphere sphere : spheres)
    		// Replace the following line with a call to printSphere
    		System.out.printf("Sphere(r=%.1f): (volume, surface area) = (%.3f, %.3f)\n",
    				sphere.getRadius(), sphere.getVolume(), sphere.getSurfaceArea());
    
    	System.out.printf("%d cones\n", Cone.getNumObjects());
    	for (Cone cone : cones)
    		// Replace the following line with a call to printCone
    		System.out.printf("Cone(r=%.1f, h=%.1f): (volume, surface area) = (%.3f, %.3f)\n",
    				cone.getRadius(), cone.getHeight(),
    				cone.getVolume(), cone.getSurfaceArea());
    
    	System.out.printf("%d cylinders\n", Cylinder.getNumObjects());
    	for (Cylinder cylinder : cylinders)
    		// Replace the following line with a call to printCylinder
    		System.out.printf("Cylinder(r=%.1f, h=%.1f): (volume, surface area) = (%.3f, %.3f)\n",
    				cylinder.getRadius(), cylinder.getHeight(),
    				cylinder.getVolume(), cylinder.getSurfaceArea());
    
    }
    }
    Now the three classes I created
    Java Code:
    public class Cone {
    
    	private double radius;
    	private double height;
    
    	public Cone(double radius, double height) {
    		this.radius = radius;
    		this.height = height;
    
    	}
    
    	public double getVolume(double radius, double height) {
    		double volumeCone = ((1.0 / 3.0) * Math.PI * Math.pow(radius, 2) * height);
    		return volumeCone;
    
    	}
    
    	public double getSurfaceArea(double radius, double height) {
    		double surfaceCone = Math.PI
    				* radius
    				* (radius + (Math.sqrt(Math.pow(radius, 2)
    						+ Math.pow(height, 2))));
    		return surfaceCone;
    	}
    	
    	public double getRadius(){
    		return radius;
    	}
    	
    	public double getHeight(){
    		return height;
    	}
    	
    	public static int getNumObjects(){
    		//???
    	}
    }
    Java Code:
    public class Cylinder {
    
    	private double radius;
    	private double height;
    
    	public Cylinder(double radius, double height) {
    		this.radius = radius;
    		this.height = height;
    	}
    
    	public double getVolume(double radius, double height) {
    		double volumeCylinder = (Math.PI * Math.pow(radius, 2) * height);
    		return volumeCylinder;
    	}
    
    	public double getSurfaceArea(double radius, double height) {
    		double surfaceCylinder = ((2 * Math.PI * radius * height) + (2 * Math.PI * Math
    				.pow(radius, 2)));
    		return surfaceCylinder;
    
    	}
    	
    	public double getRadius(){
    		return radius;
    	}
    	
    	public double getHeight(){
    		return height;
    	}
    	
    	public static int getNumObjects(){
    		//???
    	}
    }
    Java Code:
    public class Sphere {
    
    	private double radius = 0;
    
    	public Sphere(double radius) {
    		this.radius = radius;
    	}
    
    	public double getVolume(double radius) {
    		double volumeS = (4.0 / 3.0) * Math.PI * Math.pow(radius, 3);
    		return volumeS;
    	}
    
    	public double getSurfaceArea(double radius) {
    		double surfaceS = (4 * Math.PI * Math.pow(radius, 2));
    		return surfaceS;
    	}
    	
    	public double getRadius(){
    		return radius;
    	}
    	
    	public static int getNumObjects(){
    		//???
    	}
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,578
    Rep Power
    25

    Default Re: Help with multiple classes

    how to do is get the arraylist size from my tester class and get that info into my three other classes
    Your arraylist is a local variable in the main() method. If you want other classes to see it while the main method is executing you will have to pass them a reference to it.

    What data in each class is the method getNumObjects() supposed to return?
    If you want to count the number of objects created in loop in the main() method you should have counters in that method.

    Another way to count the number of objects created would be to use a static variable in each class that is incremented in each class's constructor. Then make getNumObject static so it can return the count value.

  3. #3
    Aimforthehead is offline Member
    Join Date
    Sep 2011
    Posts
    45
    Rep Power
    0

    Default Re: Help with multiple classes

    I'm sorry I am a bit confused.
    So would it be something like this? Then just return that in my method? Or are you talking about something else?
    Java Code:
    public class Sphere {
            private numObjects = 0;
    	private double radius = 0;
    
    	public Sphere(double radius) {
    		this.radius = radius;
                    numObjects++;
    	}
    And the data the method getNumObjects() is supposed to return is the number of objects of that particular object (in class sphere that method would return the number of sphere objects created in the tester class as far as I know).
    Last edited by Aimforthehead; 11-27-2011 at 08:10 PM.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,578
    Rep Power
    25

    Default Re: Help with multiple classes

    That almost looks like what I was talking about.
    Did it compile?

    Have you tried it? Did it work?

  5. #5
    Aimforthehead is offline Member
    Join Date
    Sep 2011
    Posts
    45
    Rep Power
    0

    Default Re: Help with multiple classes

    Great thanks :) *I changed it to
    private static int numObjects = 0; and no errors there.

    Well, now I am running into another issue that is not letting me compile.
    In my tester class I am running into 6 errors, I will post them
    Exception in thread "main" java.lang.Error: Unresolved compilation problems:
    The method getVolume(double) in the type Sphere is not applicable for the arguments ()
    The method getSurfaceArea(double) in the type Sphere is not applicable for the arguments ()
    The method getVolume(double, double) in the type Cone is not applicable for the arguments ()
    The method getSurfaceArea(double, double) in the type Cone is not applicable for the arguments ()
    The method getVolume(double, double) in the type Cylinder is not applicable for the arguments ()
    The method getSurfaceArea(double, double) in the type Cylinder is not applicable for the arguments ()
    I am unsure why this is coming up.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,578
    Rep Power
    25

    Default Re: Help with multiple classes

    Your error messages do not show the source lines where the errors are occurring.
    Can you do a compile and get the compiler to show you error messages?
    Please copy full text of error message and paste it here. Here is a sample:
    Java Code:
    TestSorts.java:138: cannot find symbol
    symbol  : variable var
    location: class TestSorts
             var = 2;
             ^
    Look at where you are calling the methods and make sure you are using the correct arguments with it.

  7. #7
    Aimforthehead is offline Member
    Join Date
    Sep 2011
    Posts
    45
    Rep Power
    0

    Default Re: Help with multiple classes

    Hmm I am unsure how to get to that. When I compile in eclipse, the errors given are the ones I copied and pasted to you. Here let me see if this helps.

    Java Code:
    import java.util.ArrayList;
     
    /**
     * Test the Sphere, Cone, and Cylinder classes
     * @author
     * COMS B14 - Nov 27, 2011
     * GeometryTester.java
     */
    public class GeometryTester {
     
    public static void main(String[] args) {
        // Places to store objects
        ArrayList<Sphere> spheres = new ArrayList<Sphere>();
        ArrayList<Cone> cones = new ArrayList<Cone>();
        ArrayList<Cylinder> cylinders = new ArrayList<Cylinder>();
     
        // Create various objects
        for (int r = 1; r < 5; ++r)
            for (int h = 1; h < 5; ++h) {
                if (r == h)
                    spheres.add(new Sphere(r));
                if (r > h)
                    cones.add(new Cone(r, h));
                if (r + h == 6)
                    cylinders.add(new Cylinder(r, h));
            }
         
        // Print information about the objects created
        System.out.printf("%d spheres\n", Sphere.getNumObjects());
        for (Sphere sphere : spheres)
            // Replace the following line with a call to printSphere
            System.out.printf("Sphere(r=%.1f): (volume, surface area) = (%.3f, %.3f)\n",
                    sphere.getRadius(), sphere.getVolume(), sphere.getSurfaceArea());
     
        System.out.printf("%d cones\n", Cone.getNumObjects());
        for (Cone cone : cones)
            // Replace the following line with a call to printCone
            System.out.printf("Cone(r=%.1f, h=%.1f): (volume, surface area) = (%.3f, %.3f)\n",
                    cone.getRadius(), cone.getHeight(),
                    cone.getVolume(), cone.getSurfaceArea());
     
        System.out.printf("%d cylinders\n", Cylinder.getNumObjects());
        for (Cylinder cylinder : cylinders)
            // Replace the following line with a call to printCylinder
            System.out.printf("Cylinder(r=%.1f, h=%.1f): (volume, surface area) = (%.3f, %.3f)\n",
                    cylinder.getRadius(), cylinder.getHeight(),
                    cylinder.getVolume(), cylinder.getSurfaceArea());
     
    }
    }

    Now here are the errors with the line the error occurs at given as comment at the end. Let me know if that helps.
    Java Code:
    Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    	The method getVolume(double) in the type Sphere is not applicable for the arguments ()  //33
    	The method getSurfaceArea(double) in the type Sphere is not applicable for the arguments ()  //33
    	The method getVolume(double, double) in the type Cone is not applicable for the arguments ()  //40
    	The method getSurfaceArea(double, double) in the type Cone is not applicable for the arguments ()  //40
    	The method getVolume(double, double) in the type Cylinder is not applicable for the arguments ()  //47
    	The method getSurfaceArea(double, double) in the type Cylinder is not applicable for the arguments ()  //47
    
    	at GeometryTester.main(GeometryTester.java:33)

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,578
    Rep Power
    25

    Default Re: Help with multiple classes

    Does your IDE show you the source lines in your code where the errors occur?
    Look at where you are calling each of the methods listed in the error messages and make sure you are using the correct arguments when you call it.

  9. #9
    Aimforthehead is offline Member
    Join Date
    Sep 2011
    Posts
    45
    Rep Power
    0

    Default Re: Help with multiple classes

    yes it does, it underlines them in red and gives the errors there.
    Oh and of course, the arguments lol.

    I need to use the variables r and h I am assuming, but when I use them it says they cannot be resolved to a variable.

    For example there should be 4 spheres, the first with a radius of 1, the second with a radius of 2, etc.
    I do not understand why r and h do not work, since they were initialized in the for loop for the objects just above the printing?

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,578
    Rep Power
    25

    Default Re: Help with multiple classes

    it says they cannot be resolved
    If you get errors please post the text here

    Why do those methods need arguments?

  11. #11
    Aimforthehead is offline Member
    Join Date
    Sep 2011
    Posts
    45
    Rep Power
    0

    Default Re: Help with multiple classes

    Here is my new tester class with what I thought would be valid arguments..
    Java Code:
    import java.util.ArrayList;
    
    /**
     * Test the Sphere, Cone, and Cylinder classes
     * @author 
     * COMS B14 - Nov 27, 2011
     * GeometryTester.java
     */
    public class GeometryTester {
    
    public static void main(String[] args) {
    	// Places to store objects
    	ArrayList<Sphere> spheres = new ArrayList<Sphere>();
    	ArrayList<Cone> cones = new ArrayList<Cone>();
    	ArrayList<Cylinder> cylinders = new ArrayList<Cylinder>();
    
    	// Create various objects
    	for (int r = 1; r < 5; ++r)
    		for (int h = 1; h < 5; ++h) {
    			if (r == h)
    				spheres.add(new Sphere(r));
    			if (r > h)
    				cones.add(new Cone(r, h));
    			if (r + h == 6)
    				cylinders.add(new Cylinder(r, h));
    		}
    	
    	// Print information about the objects created
    	System.out.printf("%d spheres\n", Sphere.getNumObjects());
    	for (Sphere sphere : spheres)
    		// Replace the following line with a call to printSphere
    		System.out.printf("Sphere(r=%.1f): (volume, surface area) = (%.3f, %.3f)\n",
    				sphere.getRadius(), sphere.getVolume(r), sphere.getSurfaceArea(r));
    
    	System.out.printf("%d cones\n", Cone.getNumObjects());
    	for (Cone cone : cones)
    		// Replace the following line with a call to printCone
    		System.out.printf("Cone(r=%.1f, h=%.1f): (volume, surface area) = (%.3f, %.3f)\n",
    				cone.getRadius(), cone.getHeight(),
    				cone.getVolume(r,h), cone.getSurfaceArea(r,h));
    
    	System.out.printf("%d cylinders\n", Cylinder.getNumObjects());
    	for (Cylinder cylinder : cylinders)
    		// Replace the following line with a call to printCylinder
    		System.out.printf("Cylinder(r=%.1f, h=%.1f): (volume, surface area) = (%.3f, %.3f)\n",
    				cylinder.getRadius(), cylinder.getHeight(),
    				cylinder.getVolume(r,h), cylinder.getSurfaceArea(r,h));
    
    }
    }
    Java Code:
    Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    	r cannot be resolved to a variable //33
    	r cannot be resolved to a variable //33
    	r cannot be resolved to a variable //40
    	h cannot be resolved to a variable //40
    	r cannot be resolved to a variable //40
    	h cannot be resolved to a variable //40
    	r cannot be resolved to a variable //47
    	h cannot be resolved to a variable //47
    	r cannot be resolved to a variable //47
    	h cannot be resolved to a variable //47
    
    	at GeometryTester.main(GeometryTester.java:33)

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,578
    Rep Power
    25

    Default Re: Help with multiple classes

    Can you answer this?

    Why do those methods need arguments?

    the variables: r & h are local to the loops they are in. What useful values would they have where you are using them?
    Last edited by Norm; 11-27-2011 at 09:12 PM.

  13. #13
    Aimforthehead is offline Member
    Join Date
    Sep 2011
    Posts
    45
    Rep Power
    0

    Default Re: Help with multiple classes

    I thought those were the values that you needed to put in for the parameters when you call the methods?
    I am relatively knew to this stuff so I still have troubles understanding.

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,578
    Rep Power
    25

    Default Re: Help with multiple classes

    What values are needed for those methods? Where can the methods get those values?
    Look at the Sphere class's getVolume method. Explain what it is supposed to do and where it can get the values it needs to do that.

  15. #15
    Aimforthehead is offline Member
    Join Date
    Sep 2011
    Posts
    45
    Rep Power
    0

    Default Re: Help with multiple classes

    The values needed are the same values for the number of objects.
    For example
    The print for spheres should be
    Java Code:
    4 spheres
    Sphere(r=1.0): (volume, surface area) = //output
    Sphere(r=2.0): (volume, surface area) = //output
    Sphere(r=3.0): (volume, surface area) = //output
    Sphere(r=4.0): (volume, surface area) = //output
    So I thought I could just use r, since the object index and the value are always the same.

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,578
    Rep Power
    25

    Default Re: Help with multiple classes

    You are missing my point.
    What values are needed to compute the volume? Are they available in the sphere class when getVolume is called?

  17. #17
    Aimforthehead is offline Member
    Join Date
    Sep 2011
    Posts
    45
    Rep Power
    0

    Default Re: Help with multiple classes

    Oh I think I know what you mean...

    Radius is the value needed to compute volume.
    But I don't think the values are available there? Or am I missing something obvious.

  18. #18
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,578
    Rep Power
    25

    Default Re: Help with multiple classes

    But I don't think it is available there? Or am I missing something obvious.
    Does your IDE have a Find function?
    Do a Find on radius in source code for the Sphere class.

  19. #19
    Aimforthehead is offline Member
    Join Date
    Sep 2011
    Posts
    45
    Rep Power
    0

    Default Re: Help with multiple classes

    Omg it is so obvious. The variable I initialized at the top...
    However, when I set that to a value, it still gives me the errors in my test class that I brought up HERE.
    Is it because it is private that it can not be used as a value?

  20. #20
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,578
    Rep Power
    25

    Default Re: Help with multiple classes

    Back to what data is in an instance of an object. If you create a Sphere object, what data does it have it in?
    Does it need anything more to compute its volume?

Page 1 of 2 12 LastLast

Similar Threads

  1. Multiple classes error
    By nat45928 in forum New To Java
    Replies: 0
    Last Post: 05-09-2011, 02:10 AM
  2. Multiple images from different classes
    By superbriggs in forum Java 2D
    Replies: 1
    Last Post: 03-20-2011, 08:59 AM
  3. Multiple classes
    By Lund01 in forum New To Java
    Replies: 1
    Last Post: 03-16-2011, 02:58 PM
  4. help with GUI and multiple classes
    By sssss in forum Advanced Java
    Replies: 14
    Last Post: 01-16-2011, 10:08 PM
  5. Help with multiple frames/classes
    By Celletti in forum AWT / Swing
    Replies: 1
    Last Post: 04-28-2010, 03:18 AM

Posting Permissions

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