Question

In: Computer Science

Java programming question:Consider the inheritance hierarchy in the figure on the next page.The top...

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.

Solutions

Expert Solution

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 shapes=new 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:


Related Solutions

JAVA programming - please ONLY answer prompts with java code *Inheritance* will be used in code...
JAVA programming - please ONLY answer prompts with java code *Inheritance* will be used in code Classwork A zoo is developing an educational safari game with various animals. You will write classes representing Elephants, Camels, and Moose. Part A Think through common characteristics of animals, and write a class ZooAnimal. ☑ ZooAnimal should have at least two instance variables of different types (protected), representing characteristics that all animals have values for. ☑ It should also have at least two methods...
Code in Java In Chapter 9, we created the CommissionEmployee-BasePlusCommissionEmployee inheritance hierarchy to model the relationship...
Code in Java In Chapter 9, we created the CommissionEmployee-BasePlusCommissionEmployee inheritance hierarchy to model the relationship between two types of employees and how to calculate the earnings for each. Another way to look at the problem is that CommissionEmployees and BasePlusCommissionEmployees are each Employees and that each has a different CompensationModel object. A CompensationModel would provide an earnings method. Classes or subclasses of CompensationModel would contain the details of a particular Employee's compensation: CommissionCompensationModel - For Employees who are paid...
Java Code: Problem #1: Create an inheritance hierarchy of Rodent: mouse, gerbil, hamster, guinea pig. In...
Java Code: Problem #1: Create an inheritance hierarchy of Rodent: mouse, gerbil, hamster, guinea pig. In the base class, provide methods that are common to all rodents based on behaviours you find with a quick Internet search. Be sure to document the behaviours you implement (e.g., eat, sleep, groom, move, etc.). Each behaviour should print its action to standard output (e.g., rodent eating). Next, refine these behaviours in the child classes to perform different behaviours, depending on the specific type...
JAVA programming language: For refrence: nextInt ( ) Scans the next token of the input as...
JAVA programming language: For refrence: nextInt ( ) Scans the next token of the input as an int. How many times does the while loop execute, if the input is 38 20 4 0? Assume a Scanner object scnr has been instantiated, and that the tokens in the input are delimited by the space character. That means there are 4 tokens in the input. int num = 2000; while (num >= 20) { //Do something num = scnr.nextInt(); } select...
Java Programming Question The problem is to count all the possible paths from top left to...
Java Programming Question The problem is to count all the possible paths from top left to bottom right of a MxN matrix with the constraints that from each cell you can either move to right or down.Input: The first line of input contains an integer T, denoting the number of test cases. The first line of each test case is M and N, M is number of rows and N is number of columns.Output: For each test case, print the...
Java Programming 5th edition Chapter 10 Inheritance Programs Part 1 Number 3 on page 719 which...
Java Programming 5th edition Chapter 10 Inheritance Programs Part 1 Number 3 on page 719 which creates a Point class with 2 instance variables; the xCoordinate and yCoordinate. It should have a default constructor and a values constructor. Also include a set method that sets both attributes, a get method for each attribute, and a method that redefines toString() to print the attributes as follows. point: (x, y) Part 2 Do number 4 on page 719 which creates a Circle...
c++ using polymorphyism , create an inheritance hierarchy for the following types of insurance : Home,...
c++ using polymorphyism , create an inheritance hierarchy for the following types of insurance : Home, vehicle , life 2 member functions should be included: - calcpremium    - Home- 0.5% of the value of the home    - Life- 1% of the value of the policy    - Vehicle - 0.5% of the value of the policy calcCommission Home- 0.2% of the value of the home Life- 1% of the value of the policy Vehicle - 0.5% of the...
In c++, when dealing with inheritance in a class hierarchy, a derived class often has the...
In c++, when dealing with inheritance in a class hierarchy, a derived class often has the opportunity to overload or override an inherited member function. What is the difference? and which one is the better?
Answer the following Java questions with True or False: a) Java supports Multiple Inheritance in a...
Answer the following Java questions with True or False: a) Java supports Multiple Inheritance in a way that a class can inherit from multiple classes as long as they do not have the overridden methods. (T/F) b) A class in Java cannot implement more than one interface. (T/F) c) A class is in instance of an object (T/F) d) In Java a class can implement multiple interfaces only if that class inherits from another base class (T/F) e) Constructors are...
Please do this in C++ Objective: Create an Inheritance Hierarchy to Demonstrate Polymorphic Behavior 1. Create...
Please do this in C++ Objective: Create an Inheritance Hierarchy to Demonstrate Polymorphic Behavior 1. Create a Rectangle 2. Class Derive a Square Class from the Rectangle Class 3. Derive a Right Triangle Class from the Rectangle Class 4. Create a Circle Class 5. Create a Sphere Class 6. Create a Prism Class 7. Define any other classes necessary to implement a solution 8. Define a Program Driver Class for Demonstration (Create a Container to hold objects of the types...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT