Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using the GeometricObject.java code below: public abstract class GeometricObject

ID: 3815120 • Letter: U

Question

Using the GeometricObject.java code below:

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

/** Construct a default geometric object */
protected GeometricObject() {
dateCreated = new java.util.Date();
}

/** Construct a geometric object with color and filled value */
protected 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,
   * the 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;
}

@Override
public String toString() {
return "created on " + dateCreated + " color: " + color +
" and filled: " + filled;
}

/** Abstract method getArea */
public abstract double getArea();

/** Abstract method getPerimeter */
public abstract double getPerimeter();
}

QUESTION:

Design a class names Triangle that extends GeometricObject. The class contains:

Three double data fields named side1, side2, and side3 with default values 1.0 to denote three sides of a triangle.

A no-arg constructor that creates a default triangle.

A constructor that creates a triangle with the specified side1, side2, and side3.

The accessor/mutator methods for all three data fields.

A method named getArea() that returns the area of this triangle.

A method named getPerimeter() that returns the perimeter of this triangle.

A method named toString() that returns a string description of the triangle

QUESTION PT 2:

Write a test program called “Lab16.java” that prompts the user to enter three sides of the triangle, a color, and a boolean value to indicate whether the triangle is filled. The program should create Triangle object with these sides and set the color and filled properties using the input. The program should display the triangle object followed by its area and perimeter.

Explanation / Answer

GeometricObject.java

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

/** Construct a default geometric object */
protected GeometricObject() {
dateCreated = new java.util.Date();
}

/** Construct a geometric object with color and filled value */
protected 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,
* the 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;
}

@Override
public String toString() {
return "created on " + dateCreated + " color: " + color +
" and filled: " + filled;
}

/** Abstract method getArea */
public abstract double getArea();

/** Abstract method getPerimeter */
public abstract double getPerimeter();
}

Triangle.java


public class Triangle extends GeometricObject
{
private double side1;
private double side2;
private double side3;

public Triangle()
{
this(1.0, 1.0, 1.0);
}

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 isFilled)
{
super(color, isFilled);
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}

@Override
public double getArea()
{
double p = getPerimeter();
double area = Math.sqrt(p*(p-side1)*(p-side2)*(p-side3));
return area;
}

@Override
public double getPerimeter()
{
return side1 + side2 + 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;
}

@Override
public String toString() {
return "Triangle [side1=" + side1 + ", side2=" + side2 + ", side3=" + side3 + "]" + super.toString();
}
}

Lab16.java

import java.util.Scanner;


public class Lab16
{
private static Scanner sc = new Scanner(System.in);
  
private static double getSideFromUser(int i)
{
System.out.print("Enter side" + i + " of traingle: ");
return sc.nextDouble();
}
  
public static void main(String[] args)
{
double side1 = getSideFromUser(1);
double side2 = getSideFromUser(2);
double side3 = getSideFromUser(3);
  
System.out.print("Enter color: ");
String color =sc.next();
  
System.out.println("Is trinagle filled. 1. Yes, 2. No? : ");
boolean isFilled = sc.nextInt() == 1;
  
sc.close();
  
Triangle triangle = new Triangle(side1, side2, side3, color, isFilled);
  
System.out.println(triangle);
  
System.out.println("Perimeter: " + triangle.getPerimeter());
System.out.println("Area: " + triangle.getArea());
}
}

Sample output:

Enter side1 of traingle: 3
Enter side2 of traingle: 4
Enter side3 of traingle: 5
Enter color: red
Is trinagle filled. 1. Yes, 2. No? :
1
Triangle [side1=3.0, side2=4.0, side3=5.0]created on Tue Apr 11 09:06:03 IST 2017
color: red and filled: true
Perimeter: 12.0
Area: 77.76888838089432

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote