Question

You will be creating a driver class and 5 class files for this assignment. The classes...

You will be creating a driver class and 5 class files for this assignment. The classes are Shape2D (the parent class), Circle, Triangle, and Rectangle (all children of Shape2D) and Square (child of Rectangle).

Be sure to include a message to the user explaining the purpose of the program before any other printing to the screen.

Within the driver class, please complete the following tasks:

Instantiate a Circle object with 1 side and a radius of 4; print it

Update the radius of the Circle to 7; reprint the circle using a heading like “Updated circle: “

Instantiate an equilateral Triangle object with 3 sides and a side length of 3; print it

Instantiate a Rectangle object with 4 sides, a length of 2 and a width of 5; print it

Instantiate a Square object with 4 sides and side lengths of 6; print it

Within each inherited (child) object class you must include the following methods unless inherited from the parent class or it provides updated functionality relevant to the class:

A constructor method that accepts parameters including the number of sides the object has and all values required to calculate area and perimeter/circumference.

Getter and setter methods for all instance data except the number of sides.

Methods to calculate the area and perimeter/circumference.

A toString method which provides a description (one or two lines) about the object that includes the number of sides it has, its area, and its perimeter/circumference.

All calculated values must be printed with up to 2 decimal places.

Sample format: A circle is an object with 1 side(s). It has a radius of 4, an area of 50.27 and a circumference of 25.13

Include clear comments throughout the program that explain the well named variables and what functionality the various lines/small sections of code are completing as related to the execution of the program.

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

package January;

import java.text.DecimalFormat;

// Class Shape2D definition

abstract class Shape2D

{

// Instance variable to store data

int numberOfSides;

double area;

double perimeter;

// Default constructor

Shape2D()

{

numberOfSides = 0;

area = 0.0;

perimeter = 0.0;

}// End of default constructor

// Parameterized constructor

Shape2D(int sides)

{

numberOfSides = sides;

area = 0.0;

perimeter = 0.0;

}// End of parameterized constructor

// Method to return number of sizes

int getSides()

{

return numberOfSides;

}// End of method

// Abstract methods

abstract void findArea();

abstract void findPerimeter();

}// End of class

// Class Circle derived from Shape2D

class Circle extends Shape2D

{

// Creates an object of the class DecimalFormat to display data with 2 decimal places  

DecimalFormat f = new DecimalFormat("#####.00");

// Instance variable to store data

final double PI = 3.141;

double radius;

// Default constructor

Circle()

{

super();

radius = 0.0;

}// End of default constructor

// Parameterized constructor

Circle(int side, int radius)

{

super(side);

this.radius = radius;

}// End of parameterized constructor

// Method to set radius

void setRadius(double r)

{

radius = r;

}// End of method

// Method to return radius

double getRadius()

{

return radius;

}// End of method

// Override Method to calculate area

void findArea()

{

area = PI * radius * radius;

}// End of method

// Override Method to calculate perimeter

void findPerimeter()

{

perimeter = 2.0 * PI * radius;

}// End of method

// Method to return area

double getArea()

{

findArea();

return area;

}// End of method

// Method to return perimeter

double getPerimeter()

{

findPerimeter();

return perimeter;

}// End of method

// Overrides toString() method

public String toString()

{

return "\n A circle is an object with " + getSides() + " side(s)." + " It has a radius of " + f.format(getRadius()) +

", an area of " + f.format(getArea()) + " and a circumference of " + f.format(getPerimeter());

}// End of method

}// End of class

// Class Triangle derived from Shape2D

class Triangle extends Shape2D

{

// Instance variable to store data

double side;

// Creates an object of the class DecimalFormat to display data with 2 decimal places

DecimalFormat f = new DecimalFormat("#####.00");

// Default constructor

Triangle()

{

super();

side = 0.0;

}// End of constructor

// Parameterized constructor

Triangle(int sides, double side)

{

super(sides);

this.side = side;

}// End of parameterized constructor

// Method to set side

void setSide(double s)

{

side = s;

}// End of method

// Method to return side

double getSide()

{

return side;

}// End of method

// Override Method to calculate area

void findArea()

{

area = (Math.sqrt(3) * side * side) / 4.0;

}// End of method

// Override Method to calculate perimeter

void findPerimeter()

{

perimeter = 3.0 * side;

}// End of method

// Method to return area

double getArea()

{

return area;

}// End of method

// Method to return perimeter

double getPerimeter()

{

return perimeter;

}// End of method

// Overrides toString() method

public String toString()

{

return "\n A equilateral triangle is an object with " + getSides() + " side(s)." + " It has a radius of " +

f.format(getSide()) + ", an area of " + f.format(getArea()) +

" and a circumference of " + f.format(getPerimeter());

}// End of method

}// End of class

class Rectangle extends Shape2D

{

// Instance variable to store data

double length, width;

// Creates an object of the class DecimalFormat to display data with 2 decimal places

DecimalFormat f = new DecimalFormat("#####.00");

// Default constructor

Rectangle()

{

super();

length = width = 0.0;

}// End of constructor

// Parameterized constructor

Rectangle(int sides, double length, double width)

{

super(sides);

this.length = length;

this.width = width;

}// End of parameterized constructor

// Method to set length

void setLength(double l)

{

length = l;

}// End of method

// Method to return length

double getLength()

{

return length;

}// End of method

// Method to set width

void setWidth(double w)

{

width = w;

}// End of method

// Method to return width

double getWidth()

{

return width;

}// End of method

// Override Method to calculate area

void findArea()

{

area = length * width;

}// End of method

// Override Method to calculate perimeter

void findPerimeter()

{

perimeter = 2.0 * (length + width);

}// End of method

// Method to return area

double getArea()

{

return area;

}// End of method

// Method to return perimeter

double getPerimeter()

{

return perimeter;

}// End of method

// Overrides toString() method

public String toString()

{

return "\n A Rectangle is an object with " + getSides() + " side(s)." + " It has lenght of " +

f.format(getLength()) + ", and width of " + f.format(getWidth()) +

", an area of " + f.format(getArea()) + " and a circumference of " + f.format(getPerimeter());

}// End of method

}// End of class

class Square extends Shape2D

{

// Instance variable to store data

double side;

// Creates an object of the class DecimalFormat to display data with 2 decimal places

DecimalFormat f = new DecimalFormat("#####.00");

// Default constructor

Square()

{

super();

side = 0.0;

}// End of constructor

// Parameterized constructor

Square(int sides, double side)

{

super(sides);

this.side = side;

}// End of parameterized constructor

// Method to set side

void setSide(double s)

{

side = s;

}// End of method

// Method to return side

double getSide()

{

return side;

}// End of method

// Override Method to calculate area

void findArea()

{

area = Math.pow(side, 2);

}// End of method

// Override Method to calculate perimeter

void findPerimeter()

{

perimeter = 4.0 * side;

}// End of method

// Method to return area

double getArea()

{

return area;

}// End of method

// Method to return perimeter

double getPerimeter()

{

return perimeter;

}// End of method

// Overrides toString() method

public String toString()

{

return "\n A square is an object with " + getSides() + " side(s)." + " It has a side of " +

f.format(getSide()) + ", an area of " + f.format(getArea()) +

" and a circumference of " + f.format(getPerimeter());

}// End of method

}// End of class

// Driver class Shape2DDemo definition

public class Shape2DDemo

{

// main method definition

public static void main(String[] args)

{

// Creates an object of Circle class with parameterized constructor

Circle circle = new Circle(1, 4);

System.out.println(circle);

circle.setRadius(7.0);

System.out.println("Updated circle: " + circle);

// Creates an object of Triangle class with parameterized constructor

Shape2D triangle = new Triangle(3, 3.0);

System.out.println(triangle);

// Creates an object of Rectangle class with parameterized constructor

Shape2D rectangle = new Rectangle(4, 2.0, 5.0);

System.out.println(rectangle);

// Creates an object of Square class with parameterized constructor

Shape2D square = new Square(4, 6.0);

System.out.println(square);

}// End of main function

}// End of class

Sample Output:


A circle is an object with 1 side(s). It has a radius of 4.00, an area of 50.26 and a circumference of 25.13
Updated circle:
A circle is an object with 1 side(s). It has a radius of 7.00, an area of 153.91 and a circumference of 43.97

A equilateral triangle is an object with 3 side(s). It has a radius of 3.00, an area of .00 and a circumference of .00

A Rectangle is an object with 4 side(s). It has lenght of 2.00, and width of 5.00, an area of .00 and a circumference of .00

A square is an object with 4 side(s). It has a side of 6.00, an area of .00 and a circumference of .00

Add a comment
Know the answer?
Add Answer to:
You will be creating a driver class and 5 class files for this assignment. The classes...
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
  • Q2) Interface Create a program that calculates the perimeter and the area of any given 2D...

    Q2) Interface Create a program that calculates the perimeter and the area of any given 2D shape. The program should dynamically assign the appropriate calculation for the given shape. The type of shapes are the following: • Quadrilateral 0 Square . Perimeter: 4xL • Area:LXL O Rectangle • Perimeter: 2(L+W) • Area:LxW Circle Circumference: I x Diameter (TT = 3.14) Area: (TT xD')/4 Triangle (assume right triangle) o Perimeter: a+b+c O Area: 0.5 x base x height (hint: the base...

  • 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...

  • Design and implement class Circle to represent a circle object. The class defines the following attributes...

    Design and implement class Circle to represent a circle object. The class defines the following attributes (variables) and methods: 1.      A private variable of type double named radius to represent the radius. Set to 1. 2.      Constructor Methods: •        Python : An overloaded constructor method to create a default circle. •        C# & Java: Default Constructor with no arguments. •        C# & Java: Constructor method that creates a circle with user-specified radius. 3.      Method getRadius() that returns the radius. 4.     ...

  • Project 1 – Classes and Top-down Design Overview Software development projects using the object-oriented approach involve...

    Project 1 – Classes and Top-down Design Overview Software development projects using the object-oriented approach involve breaking the problem down into multiple classes that can be tied together into a single solution. In this project, you are given the task of writing some classes that would work together for providing a solution to a problem involving some basic computations. Learning Objectives The focus of this assignment is on the following learning objectives: • Be able to identify the contents of...

  • Creating a Class in C++ Summary In this lab, you create a programmer-defined class and then...

    Creating a Class in C++ Summary In this lab, you create a programmer-defined class and then use it in a C++ program. The program should create two Rectangle objects and find their area and perimeter. Instructions Ensure the class file named Rectangle.cpp is open in your editor. In the Rectangle class, create two private attributes named length and width. Bothlength and width should be data type double. Write public set methods to set the values for lengthand width. Write public...

  • Please show all work and answer all parts using python, thanks! Instructions You will need to...

    Please show all work and answer all parts using python, thanks! Instructions You will need to create four files: • Shape2D.py - file containing a class definition containing properties all Shapes could possibly have. • Circle.py - file containing a class definition of a Circle that inherits from the Shape2D class. Square.py - file containing a class definition of a Square that inherits from the Shape2D class. • testFile.py - file containing pytest functions testing the Shape2D, Circle, and Square...

  • Implement the Shape hierarchy -- create an abstract class called Shape, which will be the parent...

    Implement the Shape hierarchy -- create an abstract class called Shape, which will be the parent class to TwoDimensionalShape and ThreeDimensionalShape. The classes Circle, Square, and Triangle should inherit from TwoDimensionalShape, while Sphere, Cube, and Tetrahedron should inherit from ThreeDimensionalShape. Each TwoDimensionalShape should have the methods getArea() and getPerimeter(), which calculate the area and perimeter of the shape, respectively. Every ThreeDimensionalShape should have the methods getArea() and getVolume(), which respectively calculate the surface area and volume of the shape. Every...

  • Creating a Programmer-Defined Class in Java Summary In this lab, you will create a programmer-defined class...

    Creating a Programmer-Defined Class in Java Summary In this lab, you will create a programmer-defined class and then use it in a Java program. The program should create two Rectangle objects and find their area and perimeter. Instructions Make sure the class file named Rectangle.java is open. In the Rectangle class, create two private attributes named lengthand width. Both length and width should be data type double. Write public set methods to set the values for length and width. Write...

  • how would i write code in java/netbeans for this: You will be creating the driver class...

    how would i write code in java/netbeans for this: You will be creating the driver class for this homework (PA05_TVs.java) and the object/element class, TV.java. Your TV class should include only a channel value for instance data (3-99) and the following methods: constructor [requires an initial channel value], getter, setter, randomChannel() [does not return the new channel value], and toString(). The driver class must complete the following: Instantiate a TV object and set the initial channel to 97. Instantiate a...

  • SOLVE IN PYTHON: Exercise #1: Design and implement class Circle to represent a circle object. The...

    SOLVE IN PYTHON: Exercise #1: Design and implement class Circle to represent a circle object. The class defines the following attributes (variables) and methods: A private variable of type double named radius to represent the radius. Set to 1. Constructor Methods: Python : An overloaded constructor method to create a default circle. C# & Java: Default Constructor with no arguments. C# & Java: Constructor method that creates a circle with user-specified radius. Method getRadius() that returns the radius. Method setRadius()...

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
ADVERTISEMENT