Question

Do the UML class diagram for the Triangle class (in assignments 4 and 7, above). Supply...

  1. Do the UML class diagram for the Triangle class (in assignments 4 and 7, above). Supply a class diagram and activity diagrams for every method in the class (excluding the accessor and toString methods, but including the constructors, the predicate methods, and area and perimeter methods).
  1. Modify the Triangle class (from assignment 4 above) to throw an exception in the constructor and set routines if the triangle is not valid (i.e., does not satisfy the triangle inequality). Modify the main routine to prompt the user for the sides of the triangle and to catch the exception and re-prompt the user for valid triangle sides.

In addition, for any valid triangle supply a method to compute and display the area of the triangle using Herod’s method: where a, b, and c are the lengths of the sides and p is 1/2 of the perimeter of the triangle, A = [p(p-a)(p-b)(p-c)]1/2. The area, A, should be a double displayed with 2 digits after the decimal point. Supply a getPerimeter method to compute the perimeter as an integer.

Display the triangle, its area and perimeter and all its other properties ONLY IF the triangle is valid. Allow the user to repeatedly enter triangle sides and do not exit the process until the user enters 0 for a side 1 length. Here is a sample dialog:

Enter the sides of the triangle as integers

Enter 0 for side 1 to exit:

Enter side 1: 3

Enter side 2: 4

Enter side 3: 5

Triangle with sides: 3 4 5

Is right

Is not isosceles

Is not equilateral

Area = 6.00

Perimeter = 12

Enter the sides of the triangle as integers

Enter 0 for side 1 to exit:

Enter side 1: 1

Enter side 2: 1

Enter side 3: 2

Not a valid triangle

Enter the sides of the triangle as integers

Enter 0 for side 1 to exit:

Enter side 1: -1

Enter side 2: 2

Enter side 3: 3

Not a valid triangle

Enter the sides of the triangle as integers

Enter 0 for side 1 to exit:

Enter side 1: 1

Enter side 2: 2

Enter side 3: 2

Triangle with sides: 1 2 2

Is not right

Is isosceles

Is not equilateral

Area = 0.97

Perimeter = 5

Enter the sides of the triangle as integers

Enter 0 for side 1 to exit:

Enter side 1: 3

Enter side 2: 3

Enter side 3: 3

Triangle with sides: 3 3 3

Is not right

Is isosceles

Is equilateral

Area = 3.90

Perimeter = 9

Enter the sides of the triangle as integers

Enter 0 for side 1 to exit:

Enter side 1: 0

Exiting

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Create a new Triangle object like this, so that you have a reference

Trangle triangle = new Triangle(side1, side2, side3);

// ^^^^^^ This is the most important thing you're missing. You need a reference point for your object. That's the only way you can access it's properties.

//You also need to set it's filled and color properties

triangle.setFilled(filled);

triangle.setColor(color);

//Then you can invoke its methods like this:

System.out.println("The Triangle Sides are \n side 1: " + triangle.getSide1() + "\n Side 2: " + triangle.getSide2() + "\n Side 3: " + triangle.getSide3());

System.out.println("The Triangle's Area is " + triangle.getArea());

System.out.println("The Triangle's Perimeter is " + triangle.getPerimeter();

System.out.println("The Triangle's Color is " + triangle.getColor());

System.out.println("Is the Triangle filled? " + triangle.isFilled());

//You are able to access Geometric isFilled(), setFilled(), getColor(), and setColor() because a Triangle is a GeometricObject (extends), so it inherits all its methods.

//Triangle formula

public double getArea() { int p = getPerimeter() / 2 return Math.sqrt(p * ((p - side1) * (p - side2) * (p - side3)); }

//Replace the setSide in your code with getSide if you have it like this

public double setSide1() { return side1; }

public double setSide2() { return side2; }

public double setSide3() { return side3; }

/**** Should Be ******/

public double getSide1() { return side1; }

public double getSide2() { return side2; }

public double getSide3() { return side3; }

Add a comment
Know the answer?
Add Answer to:
Do the UML class diagram for the Triangle class (in assignments 4 and 7, above). Supply...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT