Question
Create a Java class called Triangle. The class should have: two floating-point fields: one for the base of the triangle and one for its height
Create a Java class called Triangle. The class should have:
-
two floating-point fields: one for the base of the triangle and one for its height
-
a constructor that takes an initial value for each of the fields
-
accessor and mutator methods for each field
-
a method called area that computes and returns the area of the triangle, which should be a floating-point value that is 0.5 times the product of its base and height.
-
an appropriate toString() method. For example:
> Triangle t = new Triangle(3.0, 4.0); > System.out.println(t); triangle with base 3.0 and height 4.0
-
an appropriate equals() method, that can be used to determine if two Triangle objects have the same base and same height.
Make sure to employ appropriate encapsulation. Protect the fields from direct access by clients, and make sure that only positive values are assigned to the fields.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started