Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Shape.java: class Shape { //Attributes private int numOfSides; private String color; private boolean striped; //Constructors public Shape(int s,String c,boolean str) { numOfSides=s; color=c; striped=str; }

image text in transcribed

image text in transcribed

Shape.java:

class Shape { //Attributes private int numOfSides; private String color; private boolean striped; //Constructors public Shape(int s,String c,boolean str) { numOfSides=s; color=c; striped=str; } public Shape() { numOfSides=4; color="red"; striped=false; } //get methods public int getNumOfSides() { return numOfSides; } public String getColor() { return color; } public boolean getStriped() { return striped; } //set methods public void setNumOfSides(int n) { numOfSides=n; } public void setColor(String c) { color=c; } public void setStriped(boolean b) { striped=b; } //Methods public String toString() { return "numOfSides: "+numOfSides+" color: "+color+" striped: "+striped; } public boolean equals(Shape s) { if(this.numOfSides==s.getNumOfSides() && this.color.equals(s.getColor()) && this.striped==s.getStriped()) return true; return false; } }
1. Using Shape.java from blackboard you are going to create a class Triangle that extends Shape. (You may refer to Rectangle.java for reference if you get stuck.) Part a: using the specifications below draw a UML diagram for the Triangle class. Triangle has 3 double attributes, sidel, side2 and side3 The default values for the sides of a triangle are 1.0 You should have a constructor that constructs the default triangle. You should have a constructor the constructs a triangle with parameter values as the sides but default shape attributes. (You may choose the default settings to initialize shape with.) You should have a constructor that takes parameter values for the sides, color and striped. Each attribute should have a get and set method There should be a perimeter method There should be an area method that uses the following formulas: S =- (side1 + side2 + side3) 2 area = Vs (s side1)(s side2)(s side 3) There should be an equals method that returns true if the sides are the same and the shape attributes are the same. The toString method should output the following: numOfSides: 3 color: blue striped: false Triangle sidel: 3.0 side2: 4.0 side3: 5.0 area: 6.0 perimeter: 12.0 Part b: Implement the Triangle class in java. Part c: Write a test class. You should get the sides for the triangle and the values for the shape attributes from the keyboard. Triangles have a special property that must be true about their sides: The sum of any two sides in a triangle must be greater than the third side. Add a method in the test class to check if the sides the user enters will create a valid triangle. This method should also make sure the sides are positive values only. If the sides entered are ok then create the triangle. If the sides are not ok then have the user re-enter sides. If the user enters bad sides 3 times, then the program should terminate with some message like: Too many bad attempts at creating a triangle. Sample Output: Enter a color: purple Is the triangle striped? (yes or no) yes Enter 3 sides of a triangle: 3 4 5 numOf Sides: 3 color: purple striped: true Triangle side1 = 3.0 side2 = 4.0 side3 = 5.0 Area = 6.0 Perimeter = 12.0 Enter a color: red Is the triangle striped? (yes or no no Enter 3 sides of a triangle: 1 7 3 Enter 3 sides of a triangle : 10-8 Enter 3 sides of a triangle: 28 13 Too many bad attempts to make a triangle! SSSSSS Enter a color: jfj Is the triangle striped? (yes or no)re3 Enter 3 sides of a triangle: 3 0 2 Enter 3 sides of a triangle : 9 6 2 nunOf Sides: 3 color: jfj striped: false Triangle side1 = 9.0 side2 = 6.0 side3 = 2.0 Area = 20.97617696340303 Perimeter = 22.0

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_2

Step: 3

blur-text-image_3

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

Professional SQL Server 2012 Internals And Troubleshooting

Authors: Christian Bolton, Justin Langford

1st Edition

1118177657, 9781118177655

More Books

Students also viewed these Databases questions