Hello yet again =)
I am required to create a program that consists of two classes. The driver class calls on the other class to set/get radius and height variables and calculate the volume of a cylinder. The problem I am having is that; I create two objects with two different radius/height variables, but the program returns the same number for Volume each time. Im confused because radius and hieght all return properly. I tried debugging the program and it seems to set pi as 3.0 (We were told to define Pi as 22/7). Can someone help me please? Here are the codes for both classes.
Driver Class
/**
* Driver Class
*
* @author (your name)
* @version (a version number or a date)
*/
import javax.swing.*;
public class siloTest
{
public static void main (String[]args)
{
silo c1 = new silo();
silo c2 = new silo();
String pRadius1 = JOptionPane.showInputDialog(null, "Enter radius of Silo: 1");
double iRadius = Double.parseDouble(pRadius1);
String pHeight1 = JOptionPane.showInputDialog(null, "Enter the height of Silo: 1");
double iHeight = Double.parseDouble(pHeight1);
c1.setRadius(iRadius);
c1.setHeight(iHeight);
String pRadius2 = JOptionPane.showInputDialog(null, "Enter radius of Silo: 2");
iRadius = Double.parseDouble(pRadius2);
String pHeight2 = JOptionPane.showInputDialog(null, "Enter the height of Silo: 2");
iHeight = Double.parseDouble(pHeight2);
c2.setRadius(iRadius);
c2.setHeight(iHeight);
JOptionPane.showMessageDialog (null, "Silo 1 Dimensions\nRadius = " +
c1.getRadius() + "\nHeight = " + c1.getHeight() + "\nVolume = " +c1.calcVolume(iRadius, iHeight));
JOptionPane.showMessageDialog (null, "Silo 2 Dimensions\nRadius = " +
c2.getRadius() + "\nHeight = " + c2.getHeight() + "\nVolume = " +c2.calcVolume(iRadius, iHeight));
}
}
Other Class
/**
* Class to perform calculations etc
*
* @author (your name)
* @version (a version number or a date)
*/
import java.text.DecimalFormat;
public class silo
{
private double radius;
private double height;
public silo()
{
radius = 0;
height = 0;
}
public void setRadius(double iRadius)
{
radius = iRadius;
}
public String getRadius()
{
DecimalFormat fmt = new DecimalFormat (",###.00");
String fRadius = fmt.format(radius);
return fRadius;
}
public void setHeight(double iHeight)
{
height = iHeight;
}
public String getHeight()
{
DecimalFormat fmt = new DecimalFormat (",###.00");
String fHeight = fmt.format(height);
return fHeight;
}
public double calcVolume(double radius, double height)
{
double pi = 22/7;
double volume = pi*(Math.pow(radius,2))*height;
return volume;
}
}