0% found this document useful (0 votes)
32 views4 pages

Week 4: Sample Program - Point, Circle, Test-Inheritance Point

This document contains code examples for defining classes Point, Circle, and Test that demonstrate inheritance and composition relationships. The Point class defines x and y coordinates. The Circle class inherits from Point and adds a radius attribute. The Test class creates Circle objects and calls methods to output location and radius values. An alternative Circle class is also shown that uses composition by containing a Point object rather than inheriting from it.

Uploaded by

Nan Nan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views4 pages

Week 4: Sample Program - Point, Circle, Test-Inheritance Point

This document contains code examples for defining classes Point, Circle, and Test that demonstrate inheritance and composition relationships. The Point class defines x and y coordinates. The Circle class inherits from Point and adds a radius attribute. The Test class creates Circle objects and calls methods to output location and radius values. An alternative Circle class is also shown that uses composition by containing a Point object rather than inheriting from it.

Uploaded by

Nan Nan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

MSIS670 Fall 2007

Week 4: Sample Program – Point, Circle, Test-Inheritance

Point
// Example: Point.java
// Definition of class Point

public class Point {


protected int x, y; // coordinates of Point

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

// constructor
public Point( int xCoordinate, int yCoordinate )
{
// implicit call to superclass constructor occurs here
setPoint( xCoordinate, yCoordinate );
}

// set x and y coordinates of Point


public void setPoint( int xCoordinate, int yCoordinate )
{
x = xCoordinate;
y = yCoordinate;
}

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

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

// convert into a String representation


public String toString()
{
return "[" + x + ", " + y + "]";
}

} // end class Point


MSIS670 Fall 2007

Circle

// Example: Circle.java
// Definition of class Circle

public class Circle extends Point { // inherits from Point


protected double radius;

// no-argument constructor
public Circle()
{
// implicit call to superclass constructor (default) occurs here
setRadius( 0 );
}

// constructor
public Circle( double circleRadius, int xCoordinate,
int yCoordinate )
{
// call superclass constructor to set coordinates
super( xCoordinate, yCoordinate );

// set radius
setRadius( circleRadius );
}

// set radius of Circle


public void setRadius( double circleRadius )
{
radius = ( circleRadius >= 0.0 ? circleRadius : 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 (Note that this uses superclass variables, x and y)
public String toString()
{
return "Center = " + "[" + x + ", " + y + "]" +
"; Radius = " + radius;
}

} // end class Circle


MSIS670 Fall 2007

Test:

// Example: Test2.java
// Application to test class Circle and Point

// Java core packages


import java.text.DecimalFormat;

// Java extension packages


import javax.swing.JOptionPane;

public class Test2 {

// test class Circle


public static void main( String args[] )
{
// create a Circle (hard-coded, one example object)
Circle circle = new Circle( 2.5, 37, 43 );
DecimalFormat precision2 = new DecimalFormat( "0.00" );

// get coordinates and radius of the first object


String output = "X coordinate is " + circle.getX() +
"\nY coordinate is " + circle.getY() +
"\nRadius is " + circle.getRadius();

// set coordinates and radius; change the values


circle.setRadius( 4.25 );
circle.setPoint( 2, 2 );

// get String representation of Circle and calculate area using new values
output +=
"\n\nThe new location and radius of c are\n" + circle +
"\nArea is " + precision2.format( circle.area() );

JOptionPane.showMessageDialog( null, output,


"Demonstrating Class Circle",
JOptionPane.INFORMATION_MESSAGE );

System.exit( 0 );
}

} // end class Test


MSIS670 Fall 2007

Composition Circle:

// Definition of class Circle

public class Circle {


private double radius;
private Point point; // composition

// no-argument constructor
public Circle()
{
this( 0, 0, 0 );
}

// constructor
public Circle( double circleRadius, int xCoordinate,
int yCoordinate )
{
// instantiate point object
point = new Point( xCoordinate, yCoordinate );

setRadius( circleRadius );
}

// set radius of Circle


public void setRadius( double circleRadius )
{
radius = ( circleRadius >= 0 ? circleRadius : 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 = " + point + "; Radius = " + radius;
}

// convert Point to String


public String getPointString()
{
return point.toString();
}

} // end class Circle

You might also like