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

write this exercise in java code. 1. A triangle is a 3-sided polygon. Each side

ID: 3756208 • Letter: W

Question

write this exercise in java code.

1. A triangle is a 3-sided polygon. Each side of the triangle has a positive length. Additionally, the sum of each two sides must be greater than the third side Class Definition a. Define the class Triangle. a. Define private data members. b. Define constructor (s) for the class Object Creation and initialization Define a tester class with main method as follows. a. Create the triangle t1 with sides b. Create the triangle t2 with sides c. Write code to print the sides of t1 as follows: 4,3, and 5 i) Default constructor: sets the data members to 0.1, 0.1, 0.1 14,5 and 12. Constructor that takes 3 parameters for each side. d. Create the triangle t3 with sides equals the sum corresponding sides of Print the type of triangles t1, t2, f. Print which one is right triangle. ii) Constructor that takes one t1 and t2. parameter to create an equilateral triangle. e. c. Define a method that returns the and t3 erimeter of the triangle. d. Define a method that returns the area of the triangle. (you might need to google it looking for the formula e. Define getters/setters of all f. Define the toString method to side 1: value, side 2: value, side 3: value attributes. present the object in the form: g. Define a method that returns the type of the triangle. A triangle might be: equilateral, Isosceles, or Scalene. h. Define a method that returns whether the triangle is right triangle or not. Make sure to have valid triangle objects all times, i.e., in case a change will make the triangle invalid, your code must not apply the change

Explanation / Answer

import java.util.*;

import java.lang.Math.*;

class Triangle{

public double s;

public double firstside,secondside,thirdside;

public double getfirstside(){

return firstside;

}

public void setfirstside(double firstside){

this.firstside=firstside;

}

public double getsecondside(){

return secondside;

}

public void setsecondside(double secondside){

this.secondside=secondside;

}

public double getthirdside(){

return thirdside;

}

public void setthirdside(double thirdside){

this.thirdside=thirdside;

}

Triangle(){

firstside=0.1;

secondside=0.1;

thirdside=0.1;

}

Triangle(double x,double y,double z){

firstside=x;

secondside=y;

thirdside=z;

}

Triangle(double x){

firstside=secondside=thirdside=x;

}

public double perimeter(){

return firstside+secondside+thirdside;

}

public double area(){

s=(firstside+secondside+thirdside)/2;

double k=Math.sqrt((s*(s-firstside)*(s-secondside)*(s-thirdside)));

return k;

}

public void typeoftriangle(){

if((firstside==secondside && secondside==thirdside)){

System.out.println("This is equilateral triangle");

}

else if(firstside==secondside || secondside==thirdside || firstside==thirdside){

System.out.println("this is isosceles triangle");

}

else{

System.out.println("this is Scalene triangle");

}

}

public void rightangledtriangle(){

if(firstside>secondside && firstside>thirdside)

{

if((firstside*firstside)==(secondside*secondside)+(thirdside*thirdside))

System.out.println("It is a right-angled triangle");

else

System.out.println("It is not a right-angled triangle");

}

if(secondside>thirdside && secondside>firstside)

{

if((secondside*secondside)==(thirdside*thirdside)+(firstside*firstside))

System.out.println("It is a right-angled triangle");

else

System.out.println("It is not a right-angled triangle");

}

if(thirdside>firstside && thirdside>secondside)

{

if((thirdside*thirdside)==(firstside*firstside)+(secondside*secondside))

System.out.println("It is a right-angled triangle");

else

System.out.println("It is not a right-angled triangle");

}

}

}

class Test{

public static void main(String args[]){

double side1,side2,side3;

Triangle t1=new Triangle(4,3,5);

Triangle t2=new Triangle(14,5,12);

System.out.println(t1.firstside);

System.out.println(t1.secondside);

System.out.println(t1.thirdside);

side1=t1.firstside+t2.firstside;

side2=t1.secondside+t2.secondside;

side3=t1.thirdside+t2.thirdside;

Triangle t3=new Triangle(side1,side2,side3);

t1.typeoftriangle();

t2.typeoftriangle();

t3.typeoftriangle();

t1.rightangledtriangle();

t2.rightangledtriangle();

t3.rightangledtriangle();

}

}