Thursday, October 30, 2014

Java Excellent Pro



Cylinder in java
 
                   public class Cylinder extends Circle 
{
 private double height;   
 public Cylinder() 
{      
      super();              
      height = 1.0;
  }
  public Cylinder(double radius, double height) 
 {  
      super(radius);     
      this.height = height;
   }
   public double getHeight() 
  {
      return height;
   }
   public void setHeight(double height) 
  {
      this.height = height;
   }
   public double getVolume() 
  {
      return getArea()*height;   
   }
}
Test Cylinder in java
 
                    public class TestCylinder 
  {
   public static void main(String[] args) 
   {
      Cylinder cy1 = new Cylinder();        
      System.out.println("Radius is " + cy1.getRadius()
         + " Height is " + cy1.getHeight()
         + " Color is " + cy1.getColor()
         + " Base area is " + cy1.getArea()
         + " Volume is " + cy1.getVolume());
      Cylinder cy2 = new Cylinder(5.0, 2.0); 
      System.out.println("Radius is " + cy2.getRadius()
         + " Height is " + cy2.getHeight()
         + " Color is " + cy2.getColor()
         + " Base area is " + cy2.getArea()
         + " Volume is " + cy2.getVolume());
   }
}

 Method overriding variable hiding

public class Cylinder extends Circle 
{
 public double getArea() 
{
      return 2*Math.PI*getRadius()*height + 2*super.getArea();
 }
  public double getVolume() 
{
      return super.getArea()*height;   
 }
 public String toString() 
{
      return "Cylinder: radius = " + getRadius() + " height = " + height;   
   }
}
Subclass Point3D in java

public class Point3D extends Point 
{
   private int z;
 public Point3D() 
{  
      super();      
      z = 0;
   }
   public Point3D(int x, int y, int z) 
  {
      super(x, y);  
      this.z = z;
   }
   public int getZ() 
   {
      return z;
   }
   public void setZ(int z) 
  {
      this.z = z;
   }
   public String toString() 
  {
      return "(" + super.getX() + "," + super.getY() + "," + z + ")";
   }
}
Test Point3D in java

public class TestPoint3D 
{
 public static void main(String[] args) 
{
      Point3D p1 = new Point3D(1, 2, 3);
      System.out.println(p1);
      System.out.println(p1.getX());  
      System.out.println(p1.getY());  
      System.out.println(p1.getZ());  
      p1.setX(4);  
      p1.setY(5);  
      p1.setZ(6);  
      System.out.println(p1);
   }
}

No comments:

Post a Comment