In: Computer Science
For my class the first step of our homework was to make a project known as "TriangleTester" to input the lengths of a triangle and have them calculate the area and perimeter. Now the 2nd step is to split the project into two classes, where "TriangleTester" asks for the user's input and "Triangle" uses a getInput() method to recieve the input and uses recursion to get the input and do the calculations for the area and perimeter.
This is for java software
Here is the first step code.
import java.util.Scanner;
public class TriangleTester {
public static void main(String[] args)
{
double side1,side2,side3;
Scanner input = new Scanner(System.in);
System.out.print("Enter lengths of sides of the triangle: ");
side1 = input.nextDouble();
side2 = input.nextDouble();
side3 = input.nextDouble();
if ((checkValidity(side1, side2, side3))==1) {
double perimeter = side1 + side2 + side3;
double area = 0;
double s = (side1 + side2 + side3)/2;
area = Math.sqrt(s*(s - side1)*(s - side2)*(s - side3));
System.out.println("The perimeter of the triangle is " + perimeter
+ ".");
System.out.println("The Area of the triangle is " + area +
".");
} else {
System.out.println("Those sides do not specify a valid triangle.");
}
input.close();
}
// Function to calculate for validity
private static int checkValidity(double a, double b, double
c)
{
// check condition
if (a + b <= c || a + c <= b || b + c <= a)
return 0;
else
return 1;
}
}
Triangle.java :-
class Triangle {
/*
Creating instance for side1,side2 and side3 of Triangle.
*/
double side1,side2,side3;
/*
method to receive input.
*/
public void getInput(double side1,double side2,double side3) {
/*
initialize instances of Triangle .
*/
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}
// function to calculate and return area of triangle.
public double getArea() {
/*
calculate and return area of Triangle.
*/
double area = 0;
double s = (side1 + side2 + side3)/2; // calculate value of s.
area = Math.sqrt(s * (s-side1) * (s-side2) * (s-side3));
return area; // return area.
}
// function to calculate and return perimeter of triangle.
public double getPerimeter() {
// calculate perimeter of Triangle.
double perimeter = side1 + side2 + side3;
return perimeter; // return perimeter of Triangle.
}
}
TriangleTester.java :-
import java.util.Scanner;
public class TriangleTester {
public static void main(String[] args) {
double side1,side2,side3;
Scanner input = new Scanner(System.in);
System.out.print("Enter lengths of sides of the triangle: ");
side1 = input.nextDouble();
side2 = input.nextDouble();
side3 = input.nextDouble();
if ((checkValidity(side1, side2, side3))==1) {
// Creating instance of Triangle class.
Triangle triangle = new Triangle();
// call getInput() method of Triangle class
// to send user input.
triangle.getInput(side1,side2,side3);
// call getPerimeter() method of Triangle class to get perimeter of Triangle.
// and print the result
System.out.println("The perimeter of the triangle is "+triangle.getPerimeter());
// call getArea() method of Triangle class to get area of Triangle.
// and print the result
System.out.println("The area of the triangle is "+triangle.getArea());
} else {
System.out.println("Those sides do not specify a valid triangle.");
}
input.close();
}
// Function to calculate for validity
private static int checkValidity(double a, double b, double c) {
// check condition
if (a + b <= c || a + c <= b || b + c <= a)
return 0;
else
return 1;
}
}
Sample Output :-
Please refer to the Screenshot of the code given below to understand indentation of the code.
Screenshot of Triangle.java :-
Screenshot of TriangleTester.java :-