Question: What am i doing wrong? Task Create four methods for the Triangle class that allow the user to retrieve (get) or update (set) the private
What am i doing wrong?
Task
Create four methods for the Triangle class that allow the user to retrieve (get) or update (set) the private variables of the class.
Declare two variables of type double positioned in the code just below the class declaration (line 3). One variable is base, the other is height.
Write a method setBase() with a parameter double b that sets base to b.
Write a method setHeight() with a parameter double h that sets height to h.
Write a method getBase() with no parameters that returns the value of base.
Write a method getHeight() with no parameters that returns the value of height.
Write a method getArea() with no parameters that returns one-half base times height.
public class Triangle {
private double base; private double height; public Triangle() { //constructor } Triangle(double base, double height) { this.base = base; this.height = height; } // this.(base/height) takes the private variables and sets it equal to the variables in the method
public void setBase(double base) { this.base = base; } public double getBase() { return base; } public void setHeight(double h) { this.height = height; } public double getHeight() { return height; } public double getArea() { return (0.5 * base) * height; } }
EXPECTED OUTPUTS :
2 / 2
Test triangle.getArea().where base is 4.0 and height is 2.0.
Test feedback
area(4.0, 2.0) correctly returned 4.0
2: Unit testkeyboard_arrow_up
2 / 2
Test triangle.setBase(3.0) and triangle.getBase()
Test feedback
setBase(3.0) correctly returned 6.0
3: Unit testkeyboard_arrow_up
0 / 2
Test triangle.setHeight(1.0) and triangle.getHeight()
Test feedback
setHeight(1.0) incorrectly returned 2.0
4: Unit testkeyboard_arrow_up
2 / 2
Test triangle(4.0, 2.0), then setting base * 2.0 and height / 2.0
Test feedback
area(8.0, 1.0) correctly returned 8.0
5: Unit testkeyboard_arrow_up
1 / 1
Test triangle.getArea() for triangle with base 15.0 and height 4.5
Test feedback
area(15.0, 4.5) correctly returned 33.75
6: Unit testkeyboard_arrow_up
1 / 1
Test triangle.setBase(22.5)
Test feedback
setBase(3.0) correctly returned 22.5
7: Unit testkeyboard_arrow_up
0 / 1
Test triangle.setHeight(2.25)
Test feedback
setHeight(1.0) incorrectly returned 4.5
8: Unit testkeyboard_arrow_up
1 / 1
Test triangle(15.0, 4.5), then setting base * 2.0 and height / 2.0
Test feedback
area(8.0, 1.0) correctly returned 67.5
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
