Question

In: Computer Science

For my class the first step of our homework was to make a project known as...

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;
}

}

Solutions

Expert Solution

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 :-


Related Solutions

Please finish this code and make it work. This is my homework and my professor wont...
Please finish this code and make it work. This is my homework and my professor wont allow me to change the code in main, it's a set of huge numbers need to sort by radixsort with bit operation. #include <iostream> using namespace std; void radixLSD_help(int *items, int length, int bit) {    // – Count number of items for each bucket.    // – Figure out where each bucket should be stored (positions // of the first and last element...
Hi, I just started my first nursing class and our question of the week is: Module...
Hi, I just started my first nursing class and our question of the week is: Module 2 The community health RN is caring for a family with a child who has significant developmental delays. The child is 9-years-old and exhibits the development of a 6-month old infant. She can move her extremities spontaneously, hold her head up and cry out occasionally. She has a gastrostomy tube for her medications and she receives continuous tube feeding via pump. She was discharged...
A Class assessment is based on Three Examinations, Homework and a Final Project. The weight for...
A Class assessment is based on Three Examinations, Homework and a Final Project. The weight for each category is as follows: Ex 1, Ex 2 and Ex 3 Weight 45% of the overall grade, Homework Weight 25% and Final Project Weight 30%. Write a C# program ( Console or GUI ) that prompts the user to enter the three examinations ( ex1, ex 2, and ex 3), homework and final project grades then calculate and display the overall grade along...
First, create a project that creates an ordered linked list class (use the code provided, make...
First, create a project that creates an ordered linked list class (use the code provided, make sure to update the insert function so that it maintains an ordered list). To this class, add a function that prints the list in REVERSE order. Making use of recursion is the easiest and best way to do this, but certainly not the only way. Submit a .zip of your entire project.
Project Integration Management is the first step in Project Execution. What does it entail?
Project Integration Management is the first step in Project Execution. What does it entail?
Suppose when searching for a large prime, our first step is to sieve by eliminating the...
Suppose when searching for a large prime, our first step is to sieve by eliminating the first n primes. How large should n be to speed up our search by a factor of 10? This requires careful thought and inventiveness before doing a computation. How to approach this: Eliminating all odd numbers speeds up our program by a factor of 2, since we've eliminated half of the choices that are composite. If we also immediately disqualify multiples of 3, then...
I can't seem to make my inheritance to work properly between my parent class GameCharacter and...
I can't seem to make my inheritance to work properly between my parent class GameCharacter and the child classes hero and villain. Here's my code: import java.sql.SQLOutput; public class Main { public static void main(String[] args) { GameCharacter me = new GameCharacter("King Arthur", 5); GameCharacter tree = new GameCharacter("Tall Tree", 5); GameCharacter enemy = new GameCharacter("Monster Bug", 10); System.out.println(); System.out.println("\n~~~ Game Characters Created ~~~"); System.out.println(tree); System.out.println(me); System.out.println(enemy); System.out.println("\n~~~ The Bug Has Been Attacked ~~~"); me.attack(enemy); System.out.println(tree); System.out.println(me); System.out.println(enemy); System.out.println("\n~~~ The...
Let's assume our class represents a normal population with a known mean of 90 and population...
Let's assume our class represents a normal population with a known mean of 90 and population standard deviation 2. There are 100 students in the class. a. Construct the 95% confidence interval for the population mean. b. Interpret what this means. c. A few students have come in. Now we cannot assume normality and we don't know the population standard deviation. Let the sample mean = 90 and sample standard deviation = 3. Let's make the sample size 20. We...
Let's assume our class represents a normal population with a known mean of 95 and population...
Let's assume our class represents a normal population with a known mean of 95 and population standard deviation 3. There are 120 students in the class. a. Construct the 95% confidence interval for the population mean. b. Interpret what this means. c. A few students have come in. Now we cannot assume normality and we don't know the population standard deviation. Let the sample mean = 90 and sample standard deviation = 2. Let's make the sample size 20. We...
Let's assume our class represents a normal population with a known mean of 90 and population...
Let's assume our class represents a normal population with a known mean of 90 and population standard deviation 2. There are 100 students in the class. a. Construct the 95% confidence interval for the population mean. b. Interpret what this means. c. A few students have come in. Now we cannot assume normality and we don't know the population standard deviation. Let the sample mean = 90 and sample standard deviation = 3. Let's make the sample size 20. We...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT