Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Concepts Of Database Management

Authors: Philip J. Pratt, Joseph J. Adamski

4th Edition

ISBN: 0619064625, 978-0619064624

More Books

Students also viewed these Databases questions

Question

Presentations Approaches to Conveying Information

Answered: 1 week ago