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)
{
this.color = color;
}
public String getColor ()
{
return color;
}
public void setFilled (boolean filled)
{
this.filled = filled;
}
public boolean getFilled ()
{
return filled;
}
}
Declare three double data fields named side1, side2, and side3. Set the default value of the fields to 1.0. These fields represent the three sides of the triangle.
Write a no argument constructor that creates a triangle with the default values..
Write a constructor that creates a triangle with values specified for side1, side2, and side3.
Write public accessor methods for all three sides..
Write public mutator methods for all three sides.
Write a public method getArea() with no parameters that returns the area of this triangle.
Write a public method getPerimeter() with no parameters that returns the perimeter of this triangle.`.
Overrirde the toString() method to read as follows:
return "Triangle: side1 = " + side1 + " side2 = " + side2 + " side3 = " + side3 + " filled = " + filled + " color = " + color;
I have my code now but I only got 15/40 could you please correct for me?
import java.lang.Math; // this library is used for sqrt function
in getArea() method
//This class remains the same
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)
{
this.color = color;
}
public String getColor ()
{
return color;
}
public void setFilled (boolean filled)
{
this.filled = filled;
}
public boolean getFilled ()
{
return filled;
}
}
// Triangle class estends the GeometricShapes class
class Triangle extends GeometricShapes
{
private double side1, side2, side3;
// default constructor for assigning default values
//set color to red and filled to false
public Triangle()
{
side1 = 1;
side2 = 1;
side3 = 1;
setColor("red");
setFilled(false);
}
//parameterized constructor for setting the given values
public Triangle(double s1, double s2, double s3, String color,
boolean f)
{
super(color,f);
side1 = s1;
side2 = s2;
side3 = s3;
}
//getter function to obtain the sides
public double getSide1()
{
return side1;
}
public double getSide2()
{
return side2;
}
public double getSide3()
{
return side3;
}
//setter functions for sides
public void setSide1(double s)
{
side1 = s;
}
public void setSide2(double s)
{
side2 = s;
}
public void setSide3(double s)
{
side3 = s;
}
//returns the perimeter
public double getPerimeter()
{
return (side1+side2+side3);
}
//returns the area
public double getArea()
{
double s = getPerimeter()/2;
return (Math.sqrt(s*(s-side1)*(s-side2)*(s-side3)));
}
//returns the string of values
public String toString()
{
return "Triangle: \nside1 = " + side1 + " \nside2 = " + side2 +
"\nside3 = " + side3 + "\nfilled = " + getFilled() + "\ncolor = " +
getColor();
}
public static void main(String args[]){
//a Triangle object with default constructor
Triangle t = new Triangle();
System.out.println(t.toString());
System.out.println("Perimeter is: "+ t.getPerimeter());
System.out.println("Area is: "+ t.getArea());
}
}
I have answered many different versions of this question before. Without knowing your test code, I can not guarantee that this solution would work 100%, but in some of the previous questions, the constructor taking side values must validate the sides confirming that the three sides constitute a valid triangle. May be that is the case here. So try this code, and if it is not working, please provide the test code /test suite or whatever thing you are using to run this code, I’ll help.
Here is the completed code for Triangle class. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
// Triangle class
package geometricobj;
public class Triangle extends GeometricObject {
// attributes
private double side1;
private double side2;
private double side3;
// default constructor, initializes fields to 1.0
public Triangle() {
this(1.0, 1.0, 1.0);
}
// constructor taking values for all three sides
public Triangle(double side1, double side2, double side3)
throws IllegalArgumentException {
// validating the sides
if (side1 > 0
&& side2 > 0
&& side3 > 0
&& ((side1 + side2 > side3) || (side2 + side3 > side1) || (side1
+ side3 > side2))) {
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
} else {
// these three sides cannot form a valid traingle, so throwing
// exception
throw new IllegalArgumentException("Invalid sides for triangle: ("
+ side1 + ", " + side2 + ", " + side3 + ")");
}
}
// parameterized constructor for setting the values including super class
// attributes
public Triangle(double s1, double s2, double s3, String color, boolean f) {
// invoking other constructor
this(s1, s2, s3);
setColor(color);
setFilled(f);
}
// setters
public void setSide1(double side1) {
this.side1 = side1;
}
public void setSide2(double side2) {
this.side2 = side2;
}
public void setSide3(double side3) {
this.side3 = side3;
}
// getters
public double getArea() {
double s = getPerimeter() / 2;
return Math.sqrt(s * (s - side1) * (s - side2) * (s - side3));
}
// get perimeter
public double getPerimeter() {
return side1 + side2 + side3;
}
// override toString
public String toString() {
return "Triangle: side1 = " + side1 + " side2 = " + side2 + " side3 = "
+ side3 + " filled = " + isFilled() + " color = " + getColor();
}
}
Objective Extend the class GeometicShapes to include a Triangle class.. Background Reading ZyBooks Chapter 10 Task...
Why is my program returning 0 for the area of a triangle? public class GeometricObjects { private String color = " white "; private boolean filled; private java.util.Date dateCreated; public GeometricObjects() { dateCreated = new java.util.Date(); } public GeometricObjects(String color, boolean filled) { dateCreated = new java.util.Date(); this.color = color; this.filled = filled; } public String getColor() { return color; } public void setColor(String color)...
The software I use is Eclipse, please show how to write
it, thanks.
GeometricObject class
public abstract class GeometricObject {
private String color = "white";
private boolean filled;
private java.util.Date dateCreated;
protected GeometricObject() {
dateCreated = new java.util.Date();
}
protected GeometricObject(String color, boolean filled) {
dateCreated = new java.util.Date();
this.color = color;
this.filled = filled;
}
public String getColor() { return color; }
public void setColor(String color) { this.color = color; }
public boolean isFilled() { return filled; }
public...
Can the folllowing be done in Java, can code for all classes and code for the interface be shown please. Modify the GeometricObject class to implement the Comparable interface and define a static max method in the GeometriObject class for finding the larger of two GeometricObject objects. Write a test program that uses the max method to find the larger of two circles, the larger of two rectangles. The GeometricObject class is provided below: public abstract class GeometricObject { private...
Write a class named Octagon (Octagon.java) that extends the following abstract GeometricObject class and implements the Comparable and Cloneable interfaces. //GeometricObject.java: The abstract GeometricObject class 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;...
Design a class named Triangle that extends GeometricObject class. The class contains: Three double data fields named side1, side2, and side3 with default values 1.0 to denote three sides of the triangle. A no-arg constructor that creates a default triangle with color = "blue", filled = true. A constructor that creates a triangle with the specified side1, side2, side3 and color = "blue", filled = true. The accessor functions for all three data fields, named getSide1(), getSide2(), getSide3(). A function...
(The Triangle class) Design a class named Triangle that extends the GeometricObject class. The Triangle class contains: - Three float data fields named side1, side2, and side3 to denote the three sides of the triangle. - A constructor that creates a triangle with the specified side1, side2, and side3 with default values 1.0. - The accessor methods for all three data fields. - A method named getArea() that returns the area of this triangle. - A method named getPerimeter() that...
this is for java programming Please Use Comments I am not 100% sure if my code needs work, this code is for the geometric if you feel like you need to edit it please do :) Problem Description: Modify the GeometricObject class to implement the Comparable interface and define a static max method in the GeometricObject class for finding the larger (in area) of two GeometricObject objects. Draw the UML and implement the new GeometricObject class and its two subclasses...
Please include comments in the program .The program must be able to be compiled.Design a class named Triangle that extends GeometricObject. This class contains:* Three double data fields named side1, side2, and side3 with default values 1.0 to denote the 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 methods for all three data fields.* A method named getArea() that returns...
Design a general class GeometricObject can be used to model all geometric objects. This class contains the properties color and filled and their appropriate get and set methods. Assume that this class also contains toString() methods. The toString() method returns a string representation of the object. Define the Triangle, Circle, and Rectangle classes that extend the GeometricObject class. The Triangle class inherits all accessible data fields and methods from the GeometricObject class.In addition, it has three double data fields named...
(Need to complete the methods and pass a Junit test i can email them to you.) public class Triangle implements Comparable { /** * Stores the number of objects instantiated from the {@code Triangle} class */ private static int numTriangles; /** * The shortest side of the triangle */ private final double a; /** * The side of medium length of the triangle */ private final double b;...