Point - Circle - Cylinder

 

Now we enhance the Point and Circle classes and develop a package for them so that we can import methods and variable instances.  We develop the Point class then extend it into the Circle.  Then we extend the Circle class to develop the Cylinder class.  This also serves to illustrate some differences between packaging and using some features of a class, of ten called composition and using inheritance to extend a class.

First we will redevelop the Point class, then we will develop the Circle class and then we will test them.

Now the code for Point.java should be copied into a new directory.

// Point.java
// Setting a point

package edu.quinnipiac.javaProgs.geometry;

public class Point
{
protected int x, y;

// No argument constructor
public Point()
{
// implicit call to superclass constructor occurs here
setPoint(0,0);
}

// Constructor
public Point(int a, int b)
{
// implicit call to superclass constructor occurs here
setPoint(a,b);
}

// Set the x and y coordinates of Point
public void setPoint(int a, int b)
{
x = a;
y = b;
}

// get x coordinate
public int getX()
{
return x;
}

// get y coordinate
public int getY()
{
return y;
}

// convert the point into a String representation
public String toString()
{
return "[" + x + ", " + y + "]";
}
}

Now we want to test this class we've developed after making sure we use the 

javac -d c:\jdk1.2.1\jre\classes Point.java

to create the package class.

Now you want to test this with TestPoint.java.

// TestPoint.java
// Application to test class Point

import javax.swing.JOptionPane;
import edu.quinnipiac.javaProgs.geometry.Point;

public class TestPoint
{
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 = output + "\n\nThe new location of p is " + p;

JOptionPane.showMessageDialog(null, output, "Demonstrating Class Point", JOptionPane.INFORMATION_MESSAGE);

System.exit(0);
}
}

 

Now we are going to extend this Point class into a subclass called Circle.

Now the code for Circle.java should be copied into a new directory.

// Circle.java

package edu.quinnipiac.javaProgs.geometry;

public class Circle extends Point
{
protected double radius;

// No argument constructor
public Circle()
{
// implicit call to superclass constructor
setRadius(0);
}

// Constructor
public Circle(double r, int a, int b)
{
super(a,b); // call to superclass constructor
setRadius(r);
}

// Set radius of circle
public void setRadius(double r)
{
if (r >= 0.0)
radius = r;
else
radius = 0.0;
}

// Get radius of circle
public double getRadius()
{
return radius;
}

// Calculate area of circle
public double area()
{
return Math.PI * radius * radius;
}

// Convert the circle to a string
public String toString()
{
return "Center = " + "[" + x + ", " + y + "];" + "\nRadius = " + radius;
}
}

You need to make sure to compile this in order to develop the package.  Then you want to test this class with the following code, TestCircle.java.

// TestCircle.java

import javax.swing.JOptionPane;
import java.text.DecimalFormat;
import edu.quinnipiac.javaProgs.geometry.Circle;

public class TestCircle
{
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();

c.setRadius(4.25);
c.setPoint(2,2);
// Implicit call to toString()
output = output + "\n\nThe new location and radius of c are|n" + c +
"\nArea is " + precision2.format(c.area());

JOptionPane.showMessageDialog(null, output, "Demonstrating Class Circle",
JOptionPane.INFORMATION_MESSAGE);

System.exit(0);
}
}

 

Now we are going to take this even further by developing a Cylinder class by inheriting the Circle class.  You want to copy the following code and call it Cylinder.java.

// Cylinder.java

package edu.quinnipiac.javaProgs.geometry;

public class Cylinder extends Circle
{
protected double height;

// No-argument constructor
public Cylinder()
{
// implicit call to superclass constructor
setHeight(0);
}

// Constructor
public Cylinder(double h, double r, int a, int b)
{
super(r,a,b);
setHeight(h);
}

// Set height of cylinder
public void setHeight(double h)
{
if (h >= 0)
height = h;
else
height = 0;
}

// Get height of cylinder
public double getHeight()
{
return height;
}

// Calculate surface area of the cylinder
public double area()
{
return 2 * super.area() + (2 * Math.PI * radius * height);
}

// Calculate volume of Cylinder
public double volume()
{
return super.area() * height;
}

// Convert the cylinder to a string
public String toString()
{
return super.toString() + "; Height = " + height;
}
}

Because you are creating a package you need to make sure you use the compile command with the path and the -d.  

Now you need to execute the TestCylinder.java application.

// TestCylinder.java

import javax.swing.JOptionPane;
import java.text.DecimalFormat;
import edu.quinnipiac.javaProgs.geometry.Cylinder;

public class TestCylinder
{
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 = 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());

JOptionPane.showMessageDialog(null, output, "Demonstrating Class Cylinder",
JOptionPane.INFORMATION_MESSAGE);

System.exit(0);

}
}

This makes use of a fairly elaborate system of packaging and inheritance.