Great Deal! Get Instant $10 FREE in Account on First Order + 10% Cashback on Every Order Order Now

CIS 265 Homework 1 Due Feb 1, 2022 at 11:59pm Description This assignment is based on problem P-2.34 in the text, but many of the details have been modi- fied. You will write several classes that...

1 answer below »

CIS 265 Homework 1
Due Feb 1, 2022 at 11:59pm
Description
This assignment is based on problem P-2.34 in the text, but many of the details have been modi-
fied. You will write several classes that implement a given interface, as well as several specialized
methods for each class.
You may find some of the below methods to be easier to implement than others; I encourage you
to work on them in any order you see fit and test as you go. The harder problems might become
more clear after spending some time working on other parts.
Requirements
The interface ComputableShape is given below. It specifies methods to compute the area and
perimeter of a shape. You will code the following classes, each of which implements ComputableShape
in addition to its own particular requirements:
• Triangle
– The constructor should take the triangle’s three side lengths.
– There should be a method isValid() that returns true if the three side lengths co
e-
spond to a valid triangle, and false otherwise.
– There should be a method isIsoceles() that returns true if the triangle is isoceles, and
false otherwise.
– There should be a method isRight() that returns true if the triangle is a right triangle,
and false otherwise.
– The methods getArea() and getPerimeter() should first check if the triangle is valid.
If it is, the methods compute the area or perimeter, respectively. However, for invalid
triangles, the methods should return a negative value.
• Rectangle
– The constructor should take the rectangle’s height and width.
– There should be methods getLargeSide() and getSmallSide() that return the length
of the larger and smaller side, respectively.
– There should be a method fitsInside(Rectangle) that returns true if this rectangle
can be entirely contained within the other Rectangle, and false otherwise.
• RegularPolygon
– The constructor should take the number of sides of the polygon as well as the length of
a side (because this class only represents regular polygons, all the sides will have the
same length).
– There should be a method getNumberOfSides() that returns the number of sides.
– There should be a method getInteriorAngle() that returns, in degrees, the measure of
the interior angle of this polygon.
Note that you will still turn in ComputableShape.java despite the fact that you do not need to make
any changes to it.
Template Code
ComputableShape.java:
1 public interface ComputableShape {
2 public double getArea ();
3 public double getPerimeter ();
4 }
Triangle.java:
1 public class Triangle implements ComputableShape {
2 private double [] sideLengths;
3
4 public Triangle(double side1 , double side2 , double side3) {
5
constructor code goes here
6 }
7
8
implement the rest of your code here
9 }
Rectangle.java:
1 public class Rectangle implements ComputableShape {
2 private double width , height;
3
4 public Rectangle(double width , double height) {
5
constructor code goes here
6 }
7
8
implement the rest of your code here
9 }
RegularPolygon.java:
1 public class RegularPolygon implements ComputableShape {
2 private double sideLength;
3 private int numSides;
4
5 public RegularPolygon(double side , int n) {
6
constructor code goes here
7 }
8
9
implement the rest of your code here
10 }
2 of 4
Testing
You can use the following code to test your program. Note that this may not exhaustively test all
equirements; you are still responsible for making sure your code works as intended in all cases.
1 public class Test {
2
3 public static void main(String [] args) {
4 ComputableShape [] shapes = {new Triangle (3.0, 4.0, 5.0),
5 new Rectangle (2.0, 5.0),
6 new RegularPolygon (5.0, 8)};
7
8 for (int i = 0; i < 3; i++) {
9 System.out.println("Area of shape " + (i+1) + " is " +
10 shapes[i]. getArea ());
11 System.out.println("Perimeter of shape " + (i+1) + " is " +
12 shapes[i]. getPerimeter ());
13 }
14
15 Triangle t = (Triangle) shapes [0];
16 System.out.println("\nThe triangle is" +
17 (t.isValid () ? " " : " not ") + "valid.");
18 System.out.println("The triangle is" +
19 (t.isIsoceles () ? " " : " not ") + "isoceles.");
20 System.out.println("The triangle is" +
21 (t.isRight () ? " " : " not ") + "right.");
22
23 Rectangle r = (Rectangle) shapes [1];
24 Rectangle r2 = new Rectangle (3,6);
25 Rectangle r3 = new Rectangle (3,4);
26 System.out.println("\nThe rectangle ’s large side has length " +
27 r.getLargeSide ());
28 System.out.println("The rectangle ’s small side has length " +
29 r.getSmallSide ());
30 System.out.println("The rectangle does" +
31 (r.fitsInside(r2) ? " " : " not ") +
32 "fit in the second rectangle.");
33 System.out.println("The rectangle does" +
34 (r.fitsInside(r3) ? " " : " not ") +
35 "fit in the third rectangle.");
36
37 RegularPolygon p = (RegularPolygon) shapes [2];
38 System.out.println("\nThe polygon has " +
39 p.getNumberOfSides () + " sides.");
40 System.out.println("The polygon ’s interior angles are " +
41 p.getInteriorAngle () + " degrees each.");
42 }
43 }
Grading Criteria
Your program must compile and run with my test program(s) to receive credit. The assignment is
worth 100 points total,
oken down as follows:
• ComputableShape.java: 10 points
• Triangle.java: 40 points
• Rectangle.java: 25 points
3 of 4
• RegularPolygon.java: 25 points
Example point deductions:
• Compiler warning: 3 points
• Minor e
or (any issue that does not impact core functionality): 5 points
• Extraneous code unrelated to assignment: 10 points
• Major e
or (e.g runtime exception, infinite loop): 15 points
Submission
Create a zip file with all of your code for the assignment and submit it on Blackboard. Be sure to
include an appropriate header with your name, CSU ID, etc (see syllabus) in each file you submit.
Note that you do not need to submit a class with a main method; I will supply that myself fo
grading purposes.
4 of 4
Answered 1 days After Jan 29, 2022

Solution

Arun Shankar answered on Jan 31 2022
108 Votes
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here