Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Identify objects associated with a stated problem, then design, code, and test a

ID: 674523 • Letter: I

Question

Identify objects associated with a stated problem, then design, code, and test a set of classes for their representation that demonstrates encapsulation, data abstraction, and information hiding.

·        Organize programs based on separate declaration, definition, and usage files that demonstrate security considerations.

·        Develop applications based on code designed for reuse and extensibility using best practice software engineering techniques.

Problem Statement

Write a class called Triangle that can be used to represent a triangle.

Write a class called Describe that will interface with the Triangle class.

Requirements

The Server

·         A Triangle will have 3 sides.  It will be able to keep track of the number of Triangle objects created.  It will also hold the total of the perimeters of all the Triangle objects created.

·         It will allow a client to create a Triangle, passing in integer values for the three sides.  If the values for the sides that are passed in do not represent a valid Triangle, then all sides will be set to a value of 1.   The constructor should add 1 to the count of the number of Triangles created and also call a method to calculate the perimeter and then add the perimeter for that object to an accumulator.

·         It will contain a separate private method called isValid().  The sum of any two sides of a triangle must be greater than the third in order to represent a valid triangle.  Also, no side may be 0 or negative.

·        The Triangle class must have the following methods that return a Boolean value:

·        is_right () -  a right triangle

·         is_scalene() - no two sides are the same length

·         is_isosceles() - exactly two sides are the same length

·         is_equilateral() - all three sides are the same length

·         In addition, the Triangle class will need the following methods:

·         toString() – returns a String that specifies the values for the 3 sides of the triangle

·         equals (Triangle) – compares two Triangle objects to determine if they are equal.  We will compare their perimeters to determine equality.

·         calcPerim() – calculate and return the perimeter of the object

·         isValid() – returns a Boolean based on the criteria described above.

·         addTotalPerim().  This method will call calc_perim() and add the perimeter for that object to an accumulator.

·         reduceTotalPerim().  This method should subtract the perimeter for that object from the accumulator.

·         Accessor and mutator methods for all properties

·         Be sure to specify proper visibility for all methods and properties

The Client

·        The Describe class will contain main()

·        It will instantiate 5 Triangles

·        tri1 with sides 3, 4, 5

·        tri2 with sides 3, 3, 3

·        tri3 with sides 4, 4, 5

·        tri4 with sides 1, 1, 2

·        tri5 with sides -3, 4, 5

·        Allow the user to type 3 positive integers on separate lines for each triangle.

·        Instantiate five separate objects of the Triangle class (tri_1…tri_5).

·        Use if statements to test each triangle for its properties and print appropriate output.

·        Print the values of the sides of each triangle.

·        Print the properties that apply for each triangle.  See the sample output.

·        Print the Perimeter for each triangle.

·        At the end print the total of the perimeters for all triangles.

·        Output must be labeled, aligned, and reasonably well-spaced

·        Since we haven't discussed arrays in class, do not try to use a loop to instantiate these objects.  There will be a lot of duplicate code in Describe.  One approach is to create your first triangle, test for the various properties, and display its output.  Then simply copy and paste that code replacing tri1 with tri2.  Do the same for all 5 triangles.

·         This project is to be done by you individually. You will need to turn in:

·         A disk with a folder called "Your Last Name", containing the .java files for this project

·         Hard copy of your source code

·         Hard copy of your output

Staple the hard copies in the following order:

1.      Rubric

2.      UML diagram

3.      Test Plan

4.      Output

5.      Describe.java

6.      Triangle.java

Sample Dialog

Enter an integer dimension for side a of triangle 1: 3

Enter an integer dimension for side b of triangle 1: 4

Enter an integer dimension for side c of triangle 1: 5

Enter an integer dimension for side a of triangle 2: 3

Enter an integer dimension for side b of triangle 2: 3

Enter an integer dimension for side c of triangle 2: 3

Enter an integer dimension for side a of triangle 3: 4

Enter an integer dimension for side b of triangle 3: 4

Enter an integer dimension for side c of triangle 3: 5

Enter an integer dimension for side a of triangle 4: 1

Enter an integer dimension for side b of triangle 4: 1

Enter an integer dimension for side c of triangle 4: 2

Enter an integer dimension for side a of triangle 5: -3

Enter an integer dimension for side b of triangle 5: 4

Enter an integer dimension for side c of triangle 5: 5

Triangle 1 has sides of:

Side a: 3

Side b: 4

Side c: 5

The perimeter is 12

Triangle properties:

   Scalene

   Right Triangle

Triangle 2 has sides of:

Side a: 3

Side b: 3

Side c: 3

The perimeter is 9

Triangle properties:

   Equilateral

Triangle 3 has sides of:

Side a: 4

Side b: 4

Side c: 5

The perimeter is 13

Triangle properties:

   Isosceles

Triangle 4 has sides of:

Side a: 1

Side b: 1

Side c: 1

The perimeter is 3

Triangle properties:

   Equilateral

Triangle 5 has sides of:

Side a: 1

Side b: 1

Side c: 1

The perimeter is 3

Triangle properties:

   Equilateral

The total perimeter for all 5 triangles is 40

Explanation / Answer


//class Triangle

public class Triangle {
int A;
int B;
int C;
static int numberOfTriangles;
static int perimeter;
public int getA()
{
return A;
}
public int getB()
{
return B;
}
public int getC()
{
return C;
}
public int Perimeter()
{
return perimeter;
}
public int NumberOfTriangles()
{
return numberOfTriangles;
}
public boolean is_right()
{
boolean f=false;
if((A*A + B*B) >(C*C))
f=true;
return f;
}
public boolean is_scalene()
{
boolean f=false;
if(A!=B && A!=C && B!=C)
f=true;
return f;
}
public boolean is_isosceles()
{
boolean f=false;
if(A==B && A==C || B==C && B==A || C==A && C==B)
f=true;
return f;
}
public boolean is_equilateral()
{
boolean f=false;
if(A==B && A==C)
f=true;
return f;
}
public String toString()
{
return ("A: "+A+"; B: "+B+"; C: "+C);
}
public boolean equals(Triangle T)
{
boolean f=false;
if(perimeter==T.Perimeter())
f=true;
return f;
}
private boolean isValid()
{
boolean f=false;
if(A>0 && B>0 && C >0)
{
if( A+B>C || B+C>A || A+C>B)
{
f=true;
}
}
return f;
}
public int calcPerim()
{
return A+B+C;
}
private void addTotalPerim()
{
perimeter+=calcPerim();
}
private void reduceTotalPerim()
{
perimeter-=calcPerim();
}
public Triangle(int x, int y, int z)
{
if(isValid())
{
A=x;
B=y;
C=z;
}
else
{
A=1;B=1;C=1;
}
numberOfTriangles++;
addTotalPerim();
}
  
}