Could you please help me on this JAVA program I want to add triangle and square
ID: 3594257 • Letter: C
Question
Could you please help me on this JAVA program I want to add triangle and square classes that inerits similarly from abstract GeometricObject class and implements the same functionality as Rectangle and Circle:
Add a Triangle class that inherits similarly from the abstract GeometricObject class and implements the same functionality as Rectangle and Circle.
Add a Square class that inherits from the Rectangle class. A Square should be constructed AS a rectangle, but with only one parameter. That is, if a Square of side 6 is desired, the parent constructor should be called with 6, 6 so that you get a Rectangle with height = width = 6. All other implementation of a Square should mirror the other shapes.
Please add to this program:
public class GeometricObject {
private String color = "white";
private boolean filled;
private java.util.Date dateCreated;
/** Construct a default geometric object */
public GeometricObject() {
dateCreated = new java.util.Date();
}
/** Construct a geometric object with the specified color
* and filled value */
public GeometricObject(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 get 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;
}
/** Return a string representation of this object */
public String toString() {
return "created on " + dateCreated + " color: " + color +
" and filled: " + filled;
}
}
public class Circle extends GeometricObject {
private double radius;
public Circle() {
}
public Circle(double radius) {
this.radius = radius;
}
public Circle(double radius, String color, boolean filled) {
this.radius = radius;
setColor(color);
setFilled(filled);
}
/** Return radius */
public double getRadius() {
return radius;
}
/** Set a new radius */
public void setRadius(double radius) {
this.radius = radius;
}
/** Return area */
public double getArea() {
return radius * radius * Math.PI;
}
/** Return diameter */
public double getDiameter() {
return 2 * radius;
}
/** Return perimeter */
public double getPerimeter() {
return 2 * radius * Math.PI;
}
/* Print the circle info */
public void printCircle() {
System.out.println("The circle is created " + getDateCreated() +
" and the radius is " + radius);
}
}
public class Rectangle extends GeometricObject {
private double width;
private double height;
public Rectangle() {
}
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
public Rectangle(double width, double height, String color,
boolean filled) {
this.width = width;
this.height = height;
setColor(color);
setFilled(filled);
}
/** Return width */
public double getWidth() {
return width;
}
/** Set a new width */
public void setWidth(double width) {
this.width = width;
}
/** Return height */
public double getHeight() {
return height;
}
/** Set a new height */
public void setHeight(double height) {
this.height = height;
}
/** Return area */
public double getArea() {
return width * height;
}
/** Return perimeter */
public double getPerimeter() {
return 2 * (width + height);
}
}
Listing11.4 TestCircleRectangle.java
public class TestCircleRectangle {
public static void main(String[] args) {
Circle circle = new Circle(1);
System.out.println("A circle " + circle.toString());
System.out.println("The color is " + circle.getColor());
System.out.println("The radius is " + circle.getRadius());
System.out.println("The area is " + circle.getArea());
System.out.println("The diameter is " + circle.getDiameter());
Rectangle rectangle = new Rectangle(2, 4);
System.out.println(" A rectangle " + rectangle.toString());
System.out.println("The area is " + rectangle.getArea());
System.out.println("The perimeter is " +
rectangle.getPerimeter());
}
}
OUT PUT SHOULD BE LIKE THIS for Triangle and Square
A circle created on Thu Sep 10 20:48:13 EDT 2015
color: white and filled: false
The color is white
The radius is 1.0
The area is 3.141592653589793
The diameter is 2.0
A rectangle created on Thu Sep 10 20:48:13 EDT 2015
Explanation / Answer
Given below is the code with output. Please don't forget to rate the answer if it helped. Thank you.
To indent code in eclipse, select the code using Ctrl+A and then press Ctrl+i
Triangle.java
public class Triangle extends GeometricObject{
private double side1, side2, side3;
public Triangle()
{
}
public Triangle(double side1, double side2, double side3)
{
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}
public Triangle(double side1, double side2, double side3, String color, boolean filled)
{
super(color, filled);
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}
public double getSide1() {
return side1;
}
public void setSide1(double side1) {
this.side1 = side1;
}
public double getSide2() {
return side2;
}
public void setSide2(double side2) {
this.side2 = side2;
}
public double getSide3() {
return side3;
}
public void setSide3(double side3) {
this.side3 = side3;
}
public double getArea()
{
double s = (side1 + side2 + side3)/2;
double a2 = s * (s - side1) * ( s - side2) * (s - side3);
double area = Math.sqrt(a2);
return area;
}
public double getPerimeter()
{
return side1 + side2 + side3;
}
}
Square.java
public class Square extends Rectangle {
public Square()
{
}
public Square(double side)
{
super(side, side);
}
public Square(double side, String color, boolean filled)
{
super(side, side, color, filled);
}
public double getSide()
{
return getWidth();
}
}
TestTriangleSquare.java
public class TestTriangleSquare {
public static void main(String[] args) {
Triangle triangle = new Triangle(3, 4, 5);
System.out.println("A triangle " + triangle.toString());
System.out.println("The color is " + triangle.getColor());
System.out.println("The sides are " + triangle.getSide1() + ", " + triangle.getSide2()
+ ", " + triangle.getSide3());
System.out.println("The area is " + triangle.getArea());
System.out.println("The perimeter is " + triangle.getPerimeter());
System.out.println();
Square square = new Square(5);
System.out.println("A square " + square.toString());
System.out.println("The color is " + square.getColor());
System.out.println("The side of square is " + square.getSide());
System.out.println("The area is " + square.getArea());
System.out.println("The perimeter is " + square.getPerimeter());
}
}
output
A triangle created on Tue Oct 17 19:43:37 IST 2017
color: white and filled: false
The color is white
The sides are 3.0, 4.0, 5.0
The area is 6.0
The perimeter is 12.0
A square created on Tue Oct 17 19:43:37 IST 2017
color: white and filled: false
The color is white
The side of square is 5.0
The area is 25.0
The perimeter is 20.0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.