In: Computer Science
4. Modify the program geometryDemo to use the class basicGeometry and the threeSides code.
-----------------------------------------------------------------------------------------------------
geometryDemo Code
package com.java24hours;
/**
* geometryDemo to show class inheritance
* super class 'basicGeometry' is inherited by class
'fourSides'
* class fourSides is inherited by class 'threeSides'
*
*
*/
public class geometryDemo
{
public static void main(String[] args)
{
// Create the main object. the threeSides class includes all
functions, so use that
// because threeSides inherits (extends) all of the 4 sides public
data and methods
threeSides myGeometry = new threeSides();
//variables for calculation outputs
double outArea = 0;
double outVolume = 0;
//must have a numeric argument it it will fail
if (args.length == 0)
{
System.out.println("ERROR ** numeric input argument required: must
be 3 (triangle) or 4 (square/rectangle)");
return;
}
//pass in number of sides... NOTE this will still crash if input is
not a number
int acount = Integer.valueOf(args[0]);
//initialize all dimensions (firstDimension, secondDimension,
thirdDimension)
// This is INSTEAD of initializing the class object as you have
done before
// rather than passin the values in when you create the object
like
// threSides myGeometry = new threeSides(12, 20, 4);
// a method is needed to sent in the initial data...
myGeometry.setDimensions(12, 20, 4);
//initialize the 4 sided only object
geom4Sides.setDimensions(12,20,4);
//determine which methods for area and volume to call based on
number of sides
switch (acount)
{
case 3:
System.out.println("Triangle calculations:");
outArea = myGeometry.getTArea();
outVolume = myGeometry.getTVolume();
//need new call here to get perimeter,
// method added to 3 sdes, getTPerimeter
break;
case 4:
System.out.println("Square/rectangle calculations:");
outArea = myGeometry.getShapeArea();
outVolume = myGeometry.getVolume();
//let's see if we get the same values using the generic myGeometry
that is
//class threeSides inheriting (extending) 4 sides, or the actual
4sides class
double fsArea = geom4Sides.getShapeArea();
double fsVolume = geom4Sides.getVolume();
System.out.println("Using the fourSides class, Area=" + fsArea + "
Volume=" + fsVolume);
//need new call here to get perimeter,
// method added to 4 sides, getPerimeter
break;
default:
System.out.println("Invalid argument count = " + acount + " Pass in
3 or 4 arguments");
}
// Display the calculated area and volume.
System.out.println("Area=" + outArea + " Volume=" +
outVolume);
}
}
-----------------------------------------------------------------------------------------------------
basicGeometry Class Code
package com.java24hours;
public class basicGeometry
{
//length and width of initial shape; for triangle, length is
height
double firstDimension;
double secondDimension;
double thirdDimension;
//initialize shape dimensions
public void setShape(double dim1, double dim2, double dim3)
{
firstDimension = dim1;
secondDimension = dim2;
thirdDimension = dim3;
}
}
-----------------------------------------------------------------------------------------------------
threeSides Code
//triangle calculations inherit rectangle and main geometry
classes
public class threeSides extends fourSides
{
//triangles cut square/rectangle measurements in half
double inHalf = .5;
//triangle area is half of rectangle area
public double getTArea()
{
//use the inherited method for area, and cut in half for
triangle
return getShapeArea() * inHalf;
}
//triangle volume first 3 dimensions multiplied, then cut in
half
public double getTVolume()
{
//use the inherited method for volume, and cut in half for
triangle
return (firstDimension * secondDimension * thirdDimension *
inHalf);
}
//triangle perimeter... add a new method to calculate the perimeter
given the 3 dimensions
// remember, since setShape was called in the beginning, you have
access to the
// data from the class variables firstDimension,
secondDimension,
}
Here is the geometryDemo class:
import java.util.*;
import java.io.*;
public class geometryDemo extends threeSides
{
public static void main(String[] args)
{
threeSides myGeometry = new threeSides();
fourSides geom4Sides=new fourSides();
double outArea = 0;
double outVolume = 0;
double outPerimeter=0;
if (args.length == 0)
{
System.out.println("ERROR ** numeric input argument required: must be 3 (triangle) or 4 (square/rectangle)");
return;
}
int acount = Integer.valueOf(args[0]);
myGeometry.setDimensions(12, 20, 4);
geom4Sides.setDimensions(12,20,4);
switch (acount)
{
case 3:
System.out.println("Triangle calculations:");
outArea = myGeometry.getTArea();
outVolume = myGeometry.getTVolume();
outPerimeter=myGeometry.getTPerimeter();
break;
case 4:
System.out.println("Square/rectangle calculations:");
outArea = myGeometry.getShapeArea();
outVolume = myGeometry.getVolume();
outPerimeter=myGeometry.getPerimeter();
double fsArea = geom4Sides.getShapeArea();
double fsVolume = geom4Sides.getVolume();
//double fsPerimeter=geom4Sides.getPerimeter();
System.out.println("Using the fourSides class, Area=" + fsArea + " Volume=" + fsVolume);
break;
default:
System.out.println("Invalid argument count = " + acount + " Pass in 3 or 4 arguments");
}
System.out.println("Area=" + outArea + " Volume=" + outVolume+ " Perimeter=" + outPerimeter);
}
}
Now,here is the basicGeometry class:
import java.util.*;
public class basicGeometry{
double firstDimension=12;
double secondDimension=20;
double thirdDimension=4;
public void setShape(double dim1,double dim2,double dim3){
firstDimension=dim1;
secondDimension=dim2;
thirdDimension=dim3;
}
}
Now,here is the threeSides class:
import java.util.*;
public class threeSides extends fourSides{
double inHalf=.5;
public int setDimensions(int firstDimension,int secondDimension,int threeDimension){
firstDimension=12;
secondDimension=20;
threeDimension=4;
return 0;
}
public double getTArea(){
return getShapeArea()*inHalf;
}
public double getTVolume(){
return (firstDimension*secondDimension*thirdDimension*inHalf);
}
public double getTPerimeter(){
return (firstDimension+secondDimension+thirdDimension);
}
}
Now,here is the fourSides class:
import java.util.*;
public class fourSides extends basicGeometry{
public int setDimensions(int firstDimension,int secondDimension,int threeDimension){
firstDimension=12;
secondDimension=20;
threeDimension=4;
return 0;
}
public double getShapeArea(){
return (firstDimension*secondDimension);
}
public double getVolume(){
return (firstDimension*secondDimension*thirdDimension);
}
public double getPerimeter(){
return (2*firstDimension*secondDimension);
}
}
Here is the modification of the program.
Thanks!...