Answer to Question #288603 in Java | JSP | JSF for yash

Question #288603

Design and implement a class named RightangleTriangle that extends a GeometricObject class. The class contains:  

  • Two double data fields named baze and hite with default values 3.0 and 5.0 to denote base and height of the right-angle triangle. 
  • A no-arg constructor that creates a default triangle. 
  • A constructor that creates a triangle with the specified base and height. 
  • The accessor methods (getter and setter) for all the data fields.  
  • A method named getArea() that returns the area of this triangle. 
  • A method named printData() to print all data field values in console. 

The superclass à GeometricObject class should have following data fields: private String color = "white"; private java.util.Date dateCreated; 

  • Implement all types of constructors to initialize and set these data fields. Use super keyword to set these values in your triangle class.  
  • Implement another class mainClass where you create an object t1 of the RightangleTriangle class and print its area and the value of all its data fields. 




1
Expert's answer
2022-01-19T14:27:16-0500
import java.time.Instant;
import java.util.Date;

public class GeometricObject {
    private String color;
    private Date dateCreated;

    public GeometricObject() {
        color = "white";
        dateCreated = Date.from(Instant.now());
    }

    public GeometricObject(String color, Date dateCreated) {
        this.color = color;
        this.dateCreated = dateCreated;
    }

    public String getColor() {
        return color;
    }

    public Date getDateCreated() {
        return dateCreated;
    }
}


import java.util.Date;

public class RightangleTriangle extends GeometricObject {
    private double baze;
    private double hite;

    public RightangleTriangle() {
        baze = 3;
        hite = 5;
    }

    public RightangleTriangle(double base, double height, String color, Date createDate) {
        super(color, createDate);
        baze = base;
        hite = height;
    }

    public void setBase(double base) {
        this.baze = base;
    }

    public double getBase() {
        return baze;
    }

    public void setHeight(double height) {
        this.hite = height;
    }

    public double getHeight() {
        return hite;
    }

    public double getArea() {
        return (baze * hite) / 2;
    }

    public void printData() {
        System.out.println("Base: " + baze + " Height: " + hite + " Color: " + getColor() + " Created date: " + getDateCreated());
    }
}


import java.time.Instant;
import java.util.Date;

public class Main {
    public static void main(String[] args) {
        RightangleTriangle t1 = new RightangleTriangle(3, 4, "red", Date.from(Instant.now()));
        System.out.println("Area: " + t1.getArea());
        t1.printData();
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog