Question

Exercise #3 - Geometrical Shapes Authored by Blanche Pinto, bipinto@mail.usf.edu Design a class named Shape and its two subcl

ThreeDimensionalShape class The following requirements specify what fields you are expected to implement in your Three Dimens

Sphere & Cube classes Derive classes Sphere and Cube from the Three DimensionalShape class. The following requirements specif

Language is JAVA.

Clarification for the Shape class (highlighted):

The following requirements specify what fields you are expected to implement in your Shape class;

- A private String color that specifies the color of the shape

- A private boolean filled that specifies whether the shape is filled

- A private date (java.util.date) field dateCreated that specifies the date the shape was created Your Shape class will provide the following constructors;

- A two-arg constructor that creates a shape with specified color and filled value.

Your Shape class will also provide the following methods;

- Mutator and accessor methods for all data fields (no mutator for dateCreated)

- A method named toString that returns a string representation of this object “created on: … color: ... filled: ...”

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.Scanner;

abstract class Shape
{
private String color = "white";
private boolean filled;
protected java.util.Date dateCreated;


public Shape() {
dateCreated = new java.util.Date();
}

public Shape(String color, boolean filled) {
dateCreated = new java.util.Date();
this.color = color;
this.filled = filled;
}
/** Return color */
public String getColor() {
return color;
}
/** Set a new color */
public void setColor(String color) {
this.color = color;
}
/** Return filled. Since filled is boolean,
its getter method is named isFilled */
public boolean isFilled() {
return filled;
}
/** Set a new filled */
public void setFilled(boolean filled) {
this.filled = filled;
}
/** Get dateCreated */
public java.util.Date getDateCreated() {
return dateCreated;
}

public String toString() {
return "\nCreated on "+getDateCreated()+"\ncolor: " + color + "\nfilled: " + filled ;
}
}
abstract class TwoDShape extends Shape
{
public TwoDShape(String color, boolean filled)
{
   super(color,filled);
}
public abstract double getArea();
}

class Circle extends TwoDShape
{
private double radius;
public Circle(String color, boolean filled,double radius)
{
   super(color,filled);
this.radius = radius;
}
public double getRadius()
{
return radius;
}
public double getArea()
{
return 3.14*radius*radius;
}
public double getPerimeter()
{
return 2*3.14*radius;
}
public String toString() {
return super.toString() +"\nShape Type : 2D Circle "+"\nRadius : "+radius;
}
}

class Square extends TwoDShape
{
private double side;

public Square(String color, boolean filled,double side)
{
   super(color,filled);
this.side = side;
}
public double getArea()
{
return side*side;
}
public double getPerimeter()
{
return 4*side;
}
public String toString() {
return super.toString() +"\nShape Type : 2D Square"+"\nSide : "+side;
}
}


abstract class ThreeDShape extends Shape
{
public ThreeDShape(String color, boolean filled)
{
   super(color,filled);
}
public abstract double getArea();
public abstract double getVolume();
};
class Sphere extends ThreeDShape
{
private double radius;
public Sphere(String color, boolean filled,double radius)
{
   super(color,filled);
this.radius = radius;
}
public double getArea()
{
return 4*3.14*radius*radius;
}
public double getVolume()
{
return 4*3.14*radius*radius*radius/3;
}
public String toString() {
return super.toString() +"\nShape Type : 3D"+"\nRadius : "+radius;
}
}
class Cube extends ThreeDShape
{
  
private double depth;

  
public Cube(String color, boolean filled,double depth)// constructor
{
super(color,filled);
this.depth = depth;
}


public double getDepth()
{
return depth;
}
  
  
public double getArea() //compute the area of the cube
{
double area;
area = 6*getDepth()*getDepth();
return area;
}
public double getVolume() //compute the volume of the cube
{
double vol;
vol = getDepth() * getDepth() * getDepth();
return vol;
}
public String toString() {
return super.toString() +"\nShape Type : 3D Cube"+"\ndepth : "+depth;
}
}

  
  
class TestShapes
{
public static void main (String[] args)
{
Scanner input = new Scanner(System.in);
int option1;

System.out.println("Enter ");
System.out.println("1. Two Dimensional Shape ");
System.out.println("2. Three Dimensional Shape ");
  
int option = input.nextInt();
  
switch(option)
{
case 1: System.out.println("Enter ");
System.out.println("1. Circle ");
System.out.println("2. Square ");
  
option1 = input.nextInt();
  
switch(option1)
{
case 1: System.out.println("Enter radius of circle : ");
double radius = input.nextDouble();
Circle c = new Circle("Red",true,radius);
System.out.println(c);
System.out.printf("\nArea of circle : %.2f",c.getArea());
System.out.printf("\nPerimeter of circle : %.2f",c.getPerimeter());
break;
  
case 2: System.out.println("Enter side of square : ");
double side = input.nextDouble();
Square s = new Square("Yellow",false,side);
System.out.println(s);
System.out.printf("\nArea of square : %.2f",s.getArea());
System.out.printf("\nPerimeter of square : %.2f",s.getPerimeter());
break;
  
default: System.out.println("Invalid option");
break;
}
break;
case 2: System.out.println("Enter ");
System.out.println("1. Sphere ");
System.out.println("2. Cube ");
  
option1 = input.nextInt();
  
switch(option1)
{
case 1: System.out.println("Enter radius of sphere : ");
double radius = input.nextDouble();
Sphere sp = new Sphere("Black",true,radius);
System.out.println(sp);
System.out.printf("\nArea of sphere: %.2f",sp.getArea());
System.out.printf("\nVolume of sphere : %.2f",sp.getVolume());
break;
  
case 2: System.out.println("Enter depth of cube : ");
double depth = input.nextDouble();
Cube cube = new Cube("Orange",true,depth);
System.out.println(cube);
System.out.printf("\nArea of cube: %.2f",cube.getArea());
System.out.printf("\nVolume of cube : %.2f",cube.getVolume());
break;
  
  
  
default: System.out.println("Invalid option");
break;
}
default: System.out.println("Invalid option");
break;
  
}
  
  
  
}
}

Output:

Enter 
1. Two Dimensional Shape  
2. Three Dimensional Shape 
Enter 
1. Circle 
2. Square 
Enter side of square : 

Created on Wed Oct 09 05:48:36 GMT 2019
color: Yellow
filled: false
Shape Type : 2D Square
Side : 4.4

Area of square : 19.36
Perimeter of square : 17.60

Do ask if any doubt. Please upvote.

Add a comment
Know the answer?
Add Answer to:
Language is JAVA. Clarification for the Shape class (highlighted): The following requirements specify what fields you...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Lab # 8      The Fan class (Chapter 9:Programming Exercise 9.8)         CSCI 1302 Design a class...

    Lab # 8      The Fan class (Chapter 9:Programming Exercise 9.8)         CSCI 1302 Design a class named Fan to represent a fan. The class contains: ■ Three constants named SLOW, MEDIUM, and FAST with the values 1, 2, and 3 to denote the fan speed. ■ A private int data field named speed that specifies the speed of the fan (the default is SLOW). ■ A private boolean data field named on that specifies whether the fan is on (the...

  • Java code for the following inheritance hierarchy figure.. 1. Create class Point, with two private instance...

    Java code for the following inheritance hierarchy figure.. 1. Create class Point, with two private instance variables x and y that represented for the coordinates for a point. Provide constructor for initialising two instance variables. Provide set and get methods for each instance variable, Provide toString method to return formatted string for a point coordinates. 2. Create class Circle, its inheritance from Point. Provide a integer private radius instance variable. Provide constructor to initialise the center coordinates and radius for...

  • Define an interface named Shape with a single method named area that calculates the area of...

    Define an interface named Shape with a single method named area that calculates the area of the geometric shape:        public double area(); Next, define a class named Circle that implements Shape. The Circle class should have an instance variable for the radius, a constructor that sets the radius, an accessor and a mutator method for the radius, and an implementation of the area method. Define another class named Rectangle that implements Shape. The Rectangle class should have instance variables...

  • JAVA HELP Design a class named Employee. The class should keep the following information in fields:...

    JAVA HELP Design a class named Employee. The class should keep the following information in fields: Employee name Employee number in the format XXX–L, where each X is a digit within the range 0–9 and the L is a letter within the range A–M. Hire date then, Write one or more constructors and the appropriate accessor and mutator methods for the class. Next, write a class named ProductionWorker that extends the Employee class. The ProductionWorker class should have fields to...

  • 1. Please write the following program in Python 3. Also, please create a UML and write...

    1. Please write the following program in Python 3. Also, please create a UML and write the test program. Please show all outputs. (The Fan class) Design a class named Fan to represent a fan. The class contains: Three constants named SLOW, MEDIUM, and FAST with the values 1, 2, and 3 to denote the fan speed. A private int data field named speed that specifies the speed of the fan. A private bool data field named on that specifies...

  • Type up the GeometricObject class (code given on the back of the paper). Name this file Geometric...

    Python only please, thanks in advance. Type up the GeometricObject class (code given on the back of the paper). Name this file GeometricObjectClass and store it in the folder you made (xxlab14) 1. Design a class named Rectangle as a subclass of the GeometricObject class. Name the file RectangleClass and place it in the folder. 2. The Rectangle class contains Two float data fields named length and width A constructor that creates a rectangle with the specified fields with default...

  • Objective Extend the class GeometicShapes to include a Triangle class.. Background Reading ZyBooks Chapter 10 Task...

    Objective Extend the class GeometicShapes to include a Triangle class.. Background Reading ZyBooks Chapter 10 Task Create the following fields and methods for a Triangle class that extends the provided GeometricShapes class public class GeometricShapes { private String color = "red"; private boolean filled; private java.util.Date dateCreated;    public GeometricShapes() { dateCreated = new java.util.Date(); }    public GeometricShapes(String color, boolean filled) { dateCreated = new java.util.Date(); this.color = color; this.filled = filled; }    public void setColor (String color)...

  • A general shape class is shown below. Shape has a dimension. It also defines constructors, getters,...

    A general shape class is shown below. Shape has a dimension. It also defines constructors, getters, setters and a toString method. class Shape{ int dimension; public Shape(){} public Shape(int newDimension){ dimension = newDimension; } public int getDimension(){ return dimension; } public void setDimension(int newDimension){ dimension = newDimension; } public String toString(){ return "Shape has dimension "+dimension; } } a. Define classes Circle and Square, which inherit from Shape class. b. Both classes must have two constructors similar to Shape class....

  • java language Problem 3: Shape (10 points) (Software Design) Create an abstract class that represents a Shape and contains abstract method: area and perimeter. Create concrete classes: Circle, Rectang...

    java language Problem 3: Shape (10 points) (Software Design) Create an abstract class that represents a Shape and contains abstract method: area and perimeter. Create concrete classes: Circle, Rectangle, Triangle which extend the Shape class and implements the abstract methods. A circle has a radius, a rectangle has width and height, and a triangle has three sides. Software Architecture: The Shape class is the abstract super class and must be instantiated by one of its concrete subclasses: Circle, Rectangle, or...

  • Python only please, thanks in advance. Type up the GeometricObject class (code given on the back...

    Python only please, thanks in advance. Type up the GeometricObject class (code given on the back of the paper). Name this file GeometricObjectClass and store it in the folder you made (xxlab14) 1. Design a class named Rectangle as a subclass of the GeometricObject class. Name the file RectangleClass and place it in the folder. 2. The Rectangle class contains Two float data fields named length and width A constructor that creates a rectangle with the specified fields with default...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT