In: Computer Science
Java programming question:
Consider the inheritance hierarchy in the figure on the next page.
The top of the hierarchy begins with class Shape, which is extended
by subclasses TwoDShape and ThreeDShape, corresponding to 2D and 3D
shapes, respectively. The third level of this hierarchy contains
specific types of 2D and 3D shapes such as circles and spheres,
respectively. The arrows in the graph represent is a(inheritance) relationships and the boxes represent classes. For
example, a Triangle is a TwoDShape, which in turn is a Shape. Likewise, a Sphere is a ThreeDShape, which in
turn is a Shape.
Your task is to implement the entire hierarchy of classes in the figure below. Each two-dimensional shape should contain a getArea() method to calculate the area of the shape. Each three-dimensional shape should contain methods getArea() and getVolume() to calculate the area and volume of the shapes.
Create a program that reads in a set of shape specifications from a text file. It is up to you to design each shape specification. For example, a circle will likely need an (x,y) co-ordinate for its center in addition to the length of the circle’s radius. So in the file you might specify a circle as:
circle 10 20 5
This would be interpreted as a circle object whose center is in co-ordinate (10,20) and has a radius of length 5. The specifications for other shapes are similar in concept. To simplify things your input file may assume that each line in the input text file corresponds to one shape.
The shapes, once read in from the input text file should be stored in an array of Shapes. The program should open an output text file, loop through the Shapes array, and for each shape write to the output file a text description of the object (e.g., circle, square) and its area, and optionally, its volume if it is a three-dimensional shape. Again you may assume that the output file stores one shape per line.
package May_2018;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
abstract class Shape{
abstract public String disp();
}
abstract class TwoDShape extends Shape{
private double x;
private double y;
/**
* @return the x
*/
public double getX() {
return x;
}
/**
* @param x the x to set
*/
public void setX(double x) {
this.x = x;
}
/**
* @return the y
*/
public double getY() {
return y;
}
/**
* @param y the y to set
*/
public void setY(double y) {
this.y = y;
}
abstract public double getArea();
}
abstract class ThreeDShape extends Shape{
private double x;
private double y;
private double z;
abstract public double getArea();
abstract public double getVolume();
/**
* @return the x
*/
public double getX() {
return x;
}
/**
* @param x the x to set
*/
public void setX(double x) {
this.x = x;
}
/**
* @return the y
*/
public double getY() {
return y;
}
/**
* @param y the y to set
*/
public void setY(double y) {
this.y = y;
}
/**
* @return the z
*/
public double getZ() {
return z;
}
/**
* @param z the z to set
*/
public void setZ(double z) {
this.z = z;
}
}
class Circle extends TwoDShape{
private double radius;
public Circle(double x, double y, double radius)
{
setX(x);
setY(y);
this.radius=radius;
}
@Override
public double getArea() {
// TODO Auto-generated method stub
return Math.PI*radius*radius;
}
@Override
public String disp() {
// TODO Auto-generated method stub
return String.format("%30s%30f%30s", "Circle", getArea(), "NA");
}
}
class Triangle extends TwoDShape{
private double base;
private double height;
public Triangle(double x, double y, double base, double height)
{
setX(x);
setY(y);
this.base=base;
this.height=height;
}
@Override
public double getArea() {
// TODO Auto-generated method stub
return 0.5*base*height;
}
@Override
public String disp() {
// TODO Auto-generated method stub
return String.format("%30s%30f%30s","Triangle",getArea(),"NA");
}
}
class Rectangle extends TwoDShape{
private double length;
private double breadth;
public Rectangle(double x, double y, double length, double breadth)
{
setX(x);
setY(y);
this.length=length;
this.breadth=breadth;
}
@Override
public double getArea() {
// TODO Auto-generated method stub
return 0;
}
@Override
public String disp() {
// TODO Auto-generated method stub
return String.format("%30s%30f%30s", "Rectangle", getArea(), "NA");
}
}
class Square extends TwoDShape
{
private double side;
public Square(double x, double y, double side)
{
setX(x);
setY(y);
this.side=side;
}
@Override
public double getArea() {
// TODO Auto-generated method stub
return side*side;
}
@Override
public String disp() {
// TODO Auto-generated method stub
return String.format("%30s%30f%30s", "Square", getArea(), "NA");
}
}
class Sphere extends ThreeDShape{
private double radius;
public Sphere(double x, double y, double z, double radius)
{
setX(x);
setY(y);
setZ(z);
this.radius=radius;
}
@Override
public double getArea() {
// TODO Auto-generated method stub
return 4*Math.PI*radius*radius;
}
@Override
public double getVolume() {
// TODO Auto-generated method stub
return (4.0/3.0)*Math.PI*radius*radius*radius;
}
@Override
public String disp() {
// TODO Auto-generated method stub
return String.format("%30s%30f%30f", "Sphere", getArea(), getVolume());
}
}
class Cube extends ThreeDShape{
private double side;
public Cube(double x, double y, double z, double side)
{
setX(x);
setY(y);
setZ(z);
this.side=side;
}
@Override
public double getArea() {
// TODO Auto-generated method stub
return 6*side*side;
}
@Override
public double getVolume() {
// TODO Auto-generated method stub
return side*side*side;
}
@Override
public String disp() {
// TODO Auto-generated method stub
return String.format("%30s%30f%30f", "Cube", getArea(), getVolume());
}
}
class Tetrahedron extends ThreeDShape{
private double anlgeBisectorLength;
public Tetrahedron(double x, double y, double z, double abl)
{
setX(x);
setY(y);
setZ(z);
this.anlgeBisectorLength=abl;
}
@Override
public double getArea() {
// TODO Auto-generated method stub
return this.anlgeBisectorLength*this.anlgeBisectorLength*Math.sqrt(3);
}
@Override
public double getVolume() {
// TODO Auto-generated method stub
return (this.anlgeBisectorLength*this.anlgeBisectorLength*this.anlgeBisectorLength)/(6*Math.sqrt(2));
}
@Override
public String disp() {
// TODO Auto-generated method stub
return String.format("%30s%30f%30f", "Tetrahedron", getArea(), getVolume());
}
}
public class ShapeTest {
public static void main(String[] args) throws FileNotFoundException {
ArrayList
File file=new File("shapeInput.txt");
Scanner obj=new Scanner(file);
//print read message
System.out.println("Reading from input file named "+file.getName());
while(obj.hasNextLine())
{
String line=obj.nextLine();
String split[]=line.split(" ");
if(split[0].equalsIgnoreCase("circle"))
{
double x=Double.parseDouble(split[1]);
double y=Double.parseDouble(split[2]);
double radius=Double.parseDouble(split[3]);
Shape shape=new Circle(x, y, radius);
shapes.add(shape);
}
else if(split[0].equalsIgnoreCase("rectangle"))
{
double x=Double.parseDouble(split[1]);
double y=Double.parseDouble(split[2]);
double length=Double.parseDouble(split[3]);
double breadth=Double.parseDouble(split[4]);
Shape shape=new Rectangle(x, y, length, breadth);
shapes.add(shape);
}
else if(split[0].equalsIgnoreCase("square"))
{
double x=Double.parseDouble(split[1]);
double y=Double.parseDouble(split[2]);
double side=Double.parseDouble(split[3]);
Shape shape=new Square(x, y, side);
shapes.add(shape);
}
else if(split[0].equalsIgnoreCase("triangle"))
{
double x=Double.parseDouble(split[1]);
double y=Double.parseDouble(split[2]);
double base=Double.parseDouble(split[3]);
double height=Double.parseDouble(split[4]);
Shape shape=new Triangle(x, y, base, height);
shapes.add(shape);
}
else if(split[0].equalsIgnoreCase("sphere"))
{
double x=Double.parseDouble(split[1]);
double y=Double.parseDouble(split[2]);
double z=Double.parseDouble(split[3]);
double radius=Double.parseDouble(split[4]);
Shape shape=new Sphere(x, y, z, radius);
shapes.add(shape);
}
else if(split[0].equalsIgnoreCase("Cube"))
{
double x=Double.parseDouble(split[1]);
double y=Double.parseDouble(split[2]);
double z=Double.parseDouble(split[3]);
double side=Double.parseDouble(split[4]);
Shape shape=new Cube(x, y, z, side);
shapes.add(shape);
}
else if(split[0].equalsIgnoreCase("Tetrahedron"))
{
double x=Double.parseDouble(split[1]);
double y=Double.parseDouble(split[2]);
double z=Double.parseDouble(split[3]);
double side=Double.parseDouble(split[4]);
Shape shape=new Tetrahedron(x, y, z, side);
shapes.add(shape);
}
}
//write all the data to the output file
File output=new File("shapeOutput.txt");
PrintWriter pw=new PrintWriter(output);
pw.println(String.format("%30s%30s%30s", "Shape", "Area", "Volume"));
for(Shape shape: shapes)
{
pw.println(shape.disp());
}
//print a successful message
System.out.println("Successfully written to output file named "+output.getName());
//close the streams
obj.close();
pw.close();
}
}
PROGRAM SCREENSHOTS:
INPUT FILE:
OUTPUT FILE:
CONSOLE OUTPUT: