I'm writing a driver class that generates two cylinders and displays some data. Problem is I don't understand the concept on overloaded constructors very well and seem to be stuck. What I'm trying to do is use an overloaded constructor for cylinder2 and getters and setters for cylinder1. Right now both are using getters and setters. Help would be appreciated.
Instantiable class
public class Silo2
{
private double radius = 0;
private double height = 0;
private final double PI = 3.14159265;
public Silo2 ()
{
radius = 0;
height = 0;
}
// Overloaded Constructor?
public Silo2(double radius, double height) {
this.radius = radius;
this.height = height;
}
// Getters and Setters
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public double calcVolume()
{
return PI * radius * radius * height;
}
public double getSurfaceArea()
{
return 2 * PI * radius * radius + 2 * PI * radius * height;
}
}
Driver class I'm not going to show all the code as it's rather long. Here's snippets of what I have so far
So here's input one using setters
validateDouble reads from a public double method that validates the inputs, which is working fine.
public static void main (String [ ] args)
{
Silo2 cylinder1 = new Silo2();
Silo2 cylinder2 = new Silo2();
//Silo inputs
double radSilo1 = validateDouble("Enter radius of Silo 1:", "Silo1 Radius");
cylinder1.setRadius(radSilo1);
double heiSilo1 = validateDouble("Enter height of Silo 1:", "Silo1 Height");
cylinder1.setHeight(heiSilo1);
Output using getters
//Silo1 output
JOptionPane.showMessageDialog(null,"Silo Dimensions 1 " +
'\n' + "Radius = " + formatter.format(cylinder1.getRadius()) +
'\n' + "Height = " + formatter.format(cylinder1.getHeight()) +
'\n' + "Volume = " + formatter.format(cylinder1.calcVolume()) +
'\n' + "Surface Area = " + formatter.format(cylinder1.getSurfaceArea()));
How can I apply an overloaded constructor to cylinder2?
Edited by: DeafBox on May 2, 2009 12:29 AM