In: Computer Science
For this lab, you will design classes to represent three simple geometric shapes: circles, rectangles, and triangles.
Implement the classes for these three shapes in Java, each in a separate class file, according to the guidelines given below:
The classes must include instance fields (also known as attributes or data members) for the information necessary to represent objects of these classes. Recall that the instance fields represent the properties of objects. The most fundamental property of a circle is its radius, so this would be an ideal candidate for an instance field. Likewise, rectangles have a length and width, and a triangle has a base and a height (we will assume that these are all isosceles triangles). These fields must all be floating-point values, so you should use double as the data type for these fields.
The classes should include constructors, which allow us to initialize new objects of these classes with sensible starting values. The constructors should allow us to set the values of every instance field in each class.
The classes should include accessors (also known as "getter methods") for all of the instance fields.
The classes should include a method to compute and return the area of each of the three types of shapes. The area() method does not need to accept an argument, since the object already "knows" (through the value(s) of its instance field(s)) all the information it needs to compute its own area.
The classes should also include a toString() method which returns the string representation of the object, in the form of its area surrounded by symbols which indicate the shape of the object. For example, printing each of the three types of shape objects to the screen should look like this (assume the area is 4 units for each of these shapes):
/ 4.0 \ (for triangles)
[ 4.0 ] (for rectangles)
( 4.0 ) (for circles)
Finally, write a main() method in a fourth class which does the following ...
Create at least two shape objects of each class, initialized to different sizes ...
Print a string representation of every shape object, and ...
Call the area() method of every shape object to
get its area, and then compute the total area of all
objects of the same kind (that is, the total area of all
rectangles, the total area of all circles, and the total area of
all triangles). It should then print these totals along with a
descriptive label; for example ...
Total Triangle Area = 8.0
(These shape objects can be declared as individual variables; later, we will see how to unify these objects into a collection under a common type.)
this is java language
Dear Student ,
As per the requirement submitted above , kindly find the below solution.
Below java classes are created.
Circle.java :
//Java class
public class Circle {
// instance fields/data fields
private double radius;
// default constructor
public Circle() {
this.radius = 0.0;// set radius to
0.0
}
// parameterized constructor
public Circle(double r) {
this.radius = r;
}
// getter method
public double getRadius() {
return this.radius;// return
radius
}
// method to get area
public double area() {
// return area of circle
return Math.PI * this.radius *
this.radius;
}
// method to string
public String toString() {
return "(" + this.area() +
")";
}
}
****************************
Rectangle.java :
//java class
public class Rectangle {
// instance fields/data fields
private double length;
private double width;
// default constructor
public Rectangle() {
this.length =
0.0;// set length to 0.0
this.width =
0.0;// set width to 0.0
}
// parameterized constructor
public Rectangle(double l,double w)
{
this.length =
l;// set length
this.width =
w;// set width
}
// getter method
public double getLength() {
return
this.length;// return length
}
public double getWidth() {
return
this.width;// return width
}
// method to get area
public double area() {
// return area
of rectangle
return
this.length * this.width;
}
// method to string
public String toString() {
return "[" +
this.area() + "]";
}
}
**************************
Triangle.java :
//Java class
public class Triangle {
// instance fields/data fields
private double
base;
private double
height;
// default
constructor
public
Triangle() {
this.base = 0.0;// set base to 0.0
this.height = 0.0;// set height to 0.0
}
// parameterized
constructor
public
Triangle(double b,double h) {
this.base = b;// set base
this.height = h;// set height
}
// getter
method
public double
getBase() {
return this.base;// return base
}
public double
getHeight() {
return this.height;// return width
}
// method to get
area
public double
area() {
// return area of triangle
return (this.base * this.height)/2;
}
// method to
string
public String
toString() {
return "/" + this.area() + "\\";
}
}
*************************
geometricShapes.java :
//import package
import java.util.*;
//Java class
public class geometricShapes {
// entry point of program , main() method
public static void main(String[] args) {
// create object of Circle
class
Circle c = new Circle(4.0);
// create object of Rectangle
class
Rectangle rect = new Rectangle(4.0,
10.0);
// creating object of Triangle
class
Triangle t = new Triangle(4.0,
8.0);
// print shape of circle
System.out.println(c.toString());
// print shape of Triangle
System.out.println(t.toString());
// print shape of Rectangle
System.out.println(rect.toString());
// print area of circle
System.out.printf("Area of circle
:%.1f ", c.area());
System.out.println();// used for
new line
// print area of circle
System.out.printf("Area of
Rectangle :%.1f ", rect.area());
// print area of Triangle
System.out.printf("\nArea of
Triangle :%.1f ", t.area());
}
}
======================================================
Output : Compile and Run geometricShapes.java to get the screen as shown below
Screen 1 :geometricShapes.java
NOTE : PLEASE FEEL FREE TO PROVIDE FEEDBACK ABOUT THE SOLUTION.