Results 1 to 20 of 29
Thread: Help with multiple classes
- 11-27-2011, 08:11 PM #1
Member
- Join Date
- Sep 2011
- Posts
- 45
- Rep Power
- 0
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()); } }
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(){ //??? } }
- 11-27-2011, 08:46 PM #2
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
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.
- 11-27-2011, 09:08 PM #3
Member
- Join Date
- Sep 2011
- Posts
- 45
- Rep Power
- 0
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++; }
Last edited by Aimforthehead; 11-27-2011 at 09:10 PM.
- 11-27-2011, 09:15 PM #4
Re: Help with multiple classes
That almost looks like what I was talking about.
Did it compile?
Have you tried it? Did it work?
- 11-27-2011, 09:26 PM #5
Member
- Join Date
- Sep 2011
- Posts
- 45
- Rep Power
- 0
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 ()
- 11-27-2011, 09:30 PM #6
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; ^
- 11-27-2011, 09:46 PM #7
Member
- Join Date
- Sep 2011
- Posts
- 45
- Rep Power
- 0
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)
- 11-27-2011, 09:50 PM #8
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.
- 11-27-2011, 09:58 PM #9
Member
- Join Date
- Sep 2011
- Posts
- 45
- Rep Power
- 0
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?
- 11-27-2011, 10:02 PM #10
Re: Help with multiple classes
it says they cannot be resolved
Why do those methods need arguments?
- 11-27-2011, 10:08 PM #11
Member
- Join Date
- Sep 2011
- Posts
- 45
- Rep Power
- 0
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)
- 11-27-2011, 10:10 PM #12
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 10:12 PM.
- 11-27-2011, 10:14 PM #13
Member
- Join Date
- Sep 2011
- Posts
- 45
- Rep Power
- 0
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.
- 11-27-2011, 10:18 PM #14
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.
- 11-27-2011, 10:22 PM #15
Member
- Join Date
- Sep 2011
- Posts
- 45
- Rep Power
- 0
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
- 11-27-2011, 10:27 PM #16
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?
- 11-27-2011, 10:32 PM #17
Member
- Join Date
- Sep 2011
- Posts
- 45
- Rep Power
- 0
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.
- 11-27-2011, 10:34 PM #18
Re: Help with multiple classes
But I don't think it is available there? Or am I missing something obvious.
Do a Find on radius in source code for the Sphere class.
- 11-27-2011, 10:37 PM #19
Member
- Join Date
- Sep 2011
- Posts
- 45
- Rep Power
- 0
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?
- 11-27-2011, 10:42 PM #20
Similar Threads
-
Multiple classes error
By nat45928 in forum New To JavaReplies: 0Last Post: 05-09-2011, 03:10 AM -
Multiple images from different classes
By superbriggs in forum Java 2DReplies: 1Last Post: 03-20-2011, 09:59 AM -
Multiple classes
By Lund01 in forum New To JavaReplies: 1Last Post: 03-16-2011, 03:58 PM -
help with GUI and multiple classes
By sssss in forum Advanced JavaReplies: 14Last Post: 01-16-2011, 11:08 PM -
Help with multiple frames/classes
By Celletti in forum AWT / SwingReplies: 1Last Post: 04-28-2010, 04:18 AM
Bookmarks