Results 1 to 4 of 4
- 10-27-2012, 05:10 AM #1
Member
- Join Date
- Aug 2012
- Posts
- 16
- Rep Power
- 0
Why can't I state the values of the constructor??
I am trying to write a code where I state the height and radius and then calculates the surface area and volume of a code.
These are the formulas:
Surface = sqrt(r^2 + s^2)
Surface area = pi * r * s + pi * r^2
Volume = (1/3) * pi * r^2 * h
I think I wrote the class correctly, but I get a few errors that say the I am not using the instance variables height and radius. Then when I try to state a variable in the tester, it says that the constructor is undefined.
Here are the classes:
Java Code:public class IceCreamCone { private double height; // Says value height is not used. private double radius; // Says value radius is not used. private double area; private double volume; // Receives parameters; calculates surface area and volume. public void IceCreamCone(double h, double r) { height = h; radius = r; double s = Math.sqrt((r * r) + (h * h)); area = (Math.PI) * (r) * (s) * + (Math.PI) * (r * r); volume = (1 / 3) * (Math.PI) * (r * r) * (h); } // Returns calculated area. public double getSurfaceArea() { return area; } // Returns calculated volume. public double getVolume() { return volume; } }Thanks!Java Code:public class IceCreamtester { public static void main(String[] args) { IceCreamCone jean = new IceCreamCone(2,1); //says constructor is undefined. } }
- 10-27-2012, 05:22 AM #2
Member
- Join Date
- Aug 2012
- Posts
- 16
- Rep Power
- 0
Re: Why can't I state the values of the constructor??
I fixed a few things and now I can print the results, but now the results are wrong. What is wrong?
Java Code:public class IceCreamCone { private double height; private double radius; private double area; private double volume; private double surface; // Receives parameters; calculates surface. public IceCreamCone(double h, double r) { height = h; radius = r; surface = Math.sqrt((radius * radius) + (height * height)); } // Returns calculated area. public double getSurfaceArea() { area = ((Math.PI) * (radius) * (surface) * + (Math.PI) * (radius * radius)); return area; } // Returns calculated volume. public double getVolume() { volume = ((1 / 3) * (Math.PI) * (radius * radius) * (height)); return volume; } }
Java Code:public class IceCreamtester { public static void main(String[] args) { IceCreamCone jean = new IceCreamCone(2,1); //says constructor is undefined. System.out.println(jean.getSurfaceArea()); System.out.println("Expected value: 10.16"); System.out.println(jean.getVolume()); System.out.println("Expected value: 2.09"); } }22.069106351866907
Expected value: 10.16
0.0
Expected value: 2.09
- 10-27-2012, 05:43 AM #3
Senior Member
- Join Date
- Oct 2012
- Posts
- 108
- Rep Power
- 0
Re: Why can't I state the values of the constructor??
Java Code:IceCreamCone jean = new IceCreamCone(2d , 1d );
- 10-27-2012, 05:50 AM #4
Senior Member
- Join Date
- Oct 2012
- Posts
- 108
- Rep Power
- 0
Re: Why can't I state the values of the constructor??
Also, look at at your volume code!
HINT:
Java Code:double oneThird = 1/3; //double = int/int ~ (double)(int/int) System.out.println(oneThird); // 0.0! oneThird = 1d/3; //double = double/int ~ double/double System.out.println(oneThird); // 0.33333333333!
Similar Threads
-
call default constructor inside of parameterized constructor after conditional check
By Googol in forum New To JavaReplies: 5Last Post: 08-11-2012, 09:50 AM -
Why my constructor doesn't initialize these values?
By Renxx in forum New To JavaReplies: 2Last Post: 10-31-2011, 09:03 PM -
Assigning array values to objects in a constructor
By Dreaming in forum New To JavaReplies: 8Last Post: 10-25-2011, 06:17 PM -
How to release multiple threads from waiting state to runnable state
By Dayanand in forum New To JavaReplies: 2Last Post: 02-14-2011, 02:27 PM -
dynamically populate the city combo box based on the values of state combo +ajax+jsp
By sandy1000 in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 12-29-2010, 10:00 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks