Results 1 to 6 of 6
- 08-16-2008, 03:24 AM #1
Member
- Join Date
- Aug 2008
- Posts
- 1
- Rep Power
- 0
Proofreading this small Java program
I've recently got this new spiffy Java textbook, and upon doing one of the programming projects, it appears that I've encountered a problem. The logic of this program appears to be fine, but something sematically appears to have effected it to produce a non-sensical result.
The purpose of this program is to construct a class which computes the surface area and volume of a sphere, and return the values.
However, this is the output:Java Code:class Sphere { private final double FT = 1.333333; private final double PI = 3.14159; private double diameter; private double radius; private double volume; private double surfaceArea; public Sphere (double diameterofSphere) { diameterofSphere = diameter; } public double findRadius () { radius = diameter / 2; return radius; } public double getVolume () { volume = (FT * PI * Math.pow(radius, 3)); return volume; } public double getSurfaceArea () { surfaceArea = (4 * PI * Math.pow(radius, 2)); return surfaceArea; } public String toString () { return "The surface area and volume of your sphere is: " + surfaceArea + volume; } } public class SphereData { public static void main (String[] args) { Sphere sphere1 = new Sphere(100); sphere1.findRadius(); sphere1.getVolume(); sphere1.getSurfaceArea(); System.out.println (sphere1); } }
Thanks for reading. Also, if I'm doing something that's unnecessary, please tell me =)Java Code:The surface area and volume of your sphere is: 0.00.0
- 08-16-2008, 05:19 AM #2
Java Code:public Sphere (double diameterofSphere) { // diameterofSphere = diameter; diameter = diameterofSphere; }
- 10-22-2009, 02:03 PM #3
Member
- Join Date
- Oct 2009
- Posts
- 12
- Rep Power
- 0
From the looks of it, radius = diameter / 2; is dividing nothing by 2.
I think the line diameterofSphere = diameter; needs to be diameter = diameterofSphere
EDIT: Just realised that's what the guy before me was saying
- 10-22-2009, 05:56 PM #4
you may want to put a space in your output. At first I thought something was wrong when i saw 0.00.0, 2 decimals in a number O_O
Liberty has never come from the government.
Liberty has always come from the subjects of government.
The history of liberty is the history of resistance.
The history of liberty is a history of the limitation of governmental power, not the increase of it.
-
- 10-23-2009, 07:42 AM #6
Member
- Join Date
- Oct 2009
- Posts
- 12
- Rep Power
- 0
Similar Threads
-
small issues with a program
By jimJohnson in forum New To JavaReplies: 6Last Post: 04-25-2008, 08:28 AM -
Building small web application in java for practice.
By Saurabh321 in forum New To JavaReplies: 1Last Post: 02-01-2008, 03:38 PM -
Small scale Java Editor
By Greenfrog99 in forum AWT / SwingReplies: 0Last Post: 01-27-2008, 08:46 PM -
Execute small java programs through Internet.........is thst possible??
By atanu.dey in forum New To JavaReplies: 1Last Post: 01-05-2008, 07:48 PM -
Small tennis simulation in Java
By diego in forum New To JavaReplies: 1Last Post: 12-02-2007, 01:32 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks