Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

overloaded constructors & getters and setters problem

843789May 2 2009 — edited May 2 2009
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
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 30 2009
Added on May 2 2009
5 comments
473 views