//Point.java
// Definition of class Point
public class Point {
protected int x, y; // coordinates of the Point
public Point() {setPoint( 0, 0 ); } // no-argument constructor
public Point( int a, int b ) { setPoint( a, b ); } // constructor
public void setPoint( int a, int b ) // Set x and y coordinates of Point
{ x = a;
y = b;}
public int getX() {return x; } // get x coordinate
public int getY() {return y; } // get y coordinate
public String toString() { return "[" + x + ", " + y + "]"; } // convert the point into a String representation
} // end of Class Point
-----------------------------------
//Test.java
// to test class Point
public class Test {
public static void main( String args[] )
{
Point p = new Point( 72, 115 );
String output;
output = "X coordinate is " + p.getX() + "\nY coordinate is " + p.getY();
p.setPoint( 10, 10 );
// use implicit call to p.toString()
output += "\n\nThe new location of p is " + p;
System.out.println("Demonstrating Class Point\n" + output);
System.exit( 0 );
} // end of main method
} // end of Class Test
----------------------------
//Circle.java
// Definition of class Circle
public class Circle extends Point { // inherits from Point
protected double radius;
public Circle() // no-argument constructor
{ // implicit call to superclass constructor
setRadius( 0);}
public Circle(double r, int a, int b ) // Constructor
{ super( a, b); // call the superclass constructor
setRadius( r);}
public void setRadius( double r ) // Set radius of Circle
{ radius = ( r >= 0.0 ? r : 0.0 ); }
public double getRadius() { return radius; } // Get radius of Circle
public double area() { return Math.PI * radius * radius; } // Calculate area of Circle
public String toString() // convert the Circle to a String
{return "Center = " + super.toString() + "; Radius = " + radius;}
} // end of class Circle
----------------------------
//Test.java
// to test class Circle
import java.text.DecimalFormat;
public class Test {
public static void main( String args[] )
{
Circle c = new Circle( 2.5, 37, 43 );
DecimalFormat precision2 = new DecimalFormat( "0.00" );
String output;
output = "X coordinate is " + c.getX() + "\nY coordinate is " + c.getY() + "\nRadius is " + c.getRadius();
// getX() and gety() are inherited from class Point, getRadius() and setRadius() are defined in Class Circle
c.setRadius(4.25 );
c.setPoint( 2, 2); // setPoint() inherited from class Point
output += "\n\nThe new location and radius of c are\n" + c +
"\nArea is " + precision2.format( c.area() ); // area() of class Circle
System.out.println("Demonstrating Class Circle\n" + output);
System.exit(0);
} // end of main method
} // end of Class Test
------------------------------------
//Cylinder.java
// Definition of class Cylinder
public class Cylinder extends Circle {
protected double height; // height of Cylinder
public Cylinder() // No-argument constructor
{ // implicit call to superclass constructor here
setHeight( 0 ); }
public Cylinder(double h, double r, int a, int b ) // constructor
{ super( r, a, b);
setHeight( h );
}
public void setHeight( double h ) { height = ( h >= 0 ? h : 0 ); } // Set height of Cylinder
public double getHeight() { return height; } // Get height of Cylinder
public double area() // Calculate area of Cylinder (i.e., surface area)
{ return 2 * super.area() + 2 * Math.PI * radius * height;}
// this overrides area() in class Circle; area() is class Cylinder defined using area() from base class
public double volume() { return super.area() * height; } // Calculate volume of Cylinder using area() method of
// class Circle
public String toString() {return super.toString() + "; Height = " + height;}
// Convert the Cylinder to a String using the toString method of class Circle
} // end of Class Cylinder
-----------------------------
//Test.java
// Application to test class Cylinder
import java.text.DecimalFormat;
public class Test {public static void main( String args[] )
{
Cylinder c = new Cylinder( 5.7, 2.5, 12, 23 );
DecimalFormat precision2 = new DecimalFormat( "0.00" );
String output;
output = "X coordinate is " + c.getX() + "\nY coordinate is " + c.getY() +
"\nRadius is " + c.getRadius() + "\nHeight is " + c.getHeight();
c.setHeight( 10);
c.setRadius(4.25 );
c.setPoint( 2, 2);
output += "\n\nThe new location, radius " + "and height of c are\n" + c + "\nArea is " + precision2.format( c.area() ) +
"\nVolume is " + precision2.format( c.volume() );
System.out.println("Demonstrating Class Cylinder\n" + output);
System.exit(0);
} // end of main method
} // end of Class Test
Revised: November 18, 2008