Question

In: Computer Science

Using Java, write a program named MyAngles that will prompt the user for the measure of...

Using Java, write a program named MyAngles that will prompt the user for the measure of the three sides of a triangle and then reports the measurement of each interior angle of the triangle and the area of the triangle.

Solutions

Expert Solution

To solve this question take input of the first three sides of the triangle. Make sure to input valid triangle sides. First to calculate angle we can use Law of cosine the cos() function to calculate the angle on each vertex. For the area just use the heron's formula to calculate the area.

I have included the necessary comments that will help you understand the solution.

import java.util.Scanner;
class MyAngles {
    public static void main(String[] args) {
        double a,b,c;
        double area,resArea;
        Scanner scan = new Scanner(System.in);
        System.out.println("Provide valid triangle sides value");
        System.out.print("Enter the three sides of triangle(use space seperated value): ");
        // Input sides
        a = scan.nextDouble();
        b = scan.nextDouble();
        c = scan.nextDouble();
        // Angle calculation
        double angleAtA = Math.acos((b*b + c*c - a*a)/(2*b*c));
        double angleAtB = Math.acos((a*a + c*c - b*b)/(2*a*c));
        double angleAtC = Math.acos((a*a + b*b - c*c)/(2*a*b));
        angleAtA = Math.toDegrees(angleAtA);
        angleAtB = Math.toDegrees(angleAtB);
        angleAtC = Math.toDegrees(angleAtC);
        System.out.println("The angle at A: " + Math.round(angleAtA *10)/10.0);
        System.out.println("The angle at B: " + Math.round(angleAtB * 10)/10.0);
        System.out.println("The angle at C: " + Math.round(angleAtC * 10)/10.0);
        // Area calculation
        area = (a+b+c)/2.0d;
        resArea = Math.sqrt(area* (area - a) * (area - b) * (area - c));
        System.out.println("Area of Triangle = " + resArea);
    }
}

The output will as follows:

I hope you have understood the solution. If you like the solution kindly upvote it.


Related Solutions

Write a Java program named, MultiTable, (MultiTable.java), with the following tasks: Prompt user to input the...
Write a Java program named, MultiTable, (MultiTable.java), with the following tasks: Prompt user to input the maximum number (as integer) Store a multiplication table for all combinations (with some specific eliminations) of value 0 through the maximum number (being entered) into a 2-D array Write a method named printTable to print out the MulitTable, with the following header, public static void printTable(int[][] multitable) In printTable(), when the value of the MultiTable is an odd number, print out Z Must use...
Write a Java program named, TicketSale, (TicketSale.java) with the following tasks: Prompt user to input the...
Write a Java program named, TicketSale, (TicketSale.java) with the following tasks: Prompt user to input the number of Adult tickets to purchase Prompt user to input the number of Children tickets to purchase Prompt user to input the number of Senior tickets to purchase Write a method named, ticketCost(), which will be invoked by main() with statement similar to: cost = ticketCost( adults, children, senior ); Ticket costs structure: $15.00 for each adult $10.00 for each child $5.00 for each...
Using Java! Write a program that ask prompt the user to enter a dollar amount as...
Using Java! Write a program that ask prompt the user to enter a dollar amount as double. Then, calculate how many quarters, dimes, nickels and pennies are in the dollar amount. For example: $2.56 = 10 quarters, 1 dime, 1 nickel and 1 cent. Print all of the values. Hint: Use Modulus operator and integer division when necessary.
Write by hand a full java program that will prompt the user for the length and...
Write by hand a full java program that will prompt the user for the length and width of a square, calculate and display the area of that square rounded to the nearest tenth with an appropriate message.
IN JAVA PROGRAMMING Write a complete Java program to do the following: a) Prompt the user...
IN JAVA PROGRAMMING Write a complete Java program to do the following: a) Prompt the user to enter the name of the month he/she was born in (example: September). b) Prompt the user to enter his/her weight in pounds (example: 145.75). c) Prompt the user to enter his/her height in feet (example: 6.5). d) Display (print) a line of message on the screen that reads as follows: You were born in the month of September and weigh 145.75 lbs. and...
Answer in JAVA Write a program that would prompt the user to enter an integer. The...
Answer in JAVA Write a program that would prompt the user to enter an integer. The program then would displays a table of squares and cubes from 1 to the value entered by the user. The program should prompt the user to continue if they wish. Name your class NumberPowers.java, add header and sample output as block comments and uploaded it to this link. Use these formulas for calculating squares and cubes are: square = x * x cube =...
JAVA Write a java program that will sum all positive number. Prompt the user to enter...
JAVA Write a java program that will sum all positive number. Prompt the user to enter numbers and add all positive numbers. The program will not add negative numbers and the program will stop when you enter ‘0’.   Enter a number> 25 Enter a number> 9 Enter a number> 5 Enter a number> -3 Enter a number> 0 Sum = 39
I need a java code Write a simple program to prompt the user for a number...
I need a java code Write a simple program to prompt the user for a number between 1 and 12 (inclusive) and print out the corresponding month. For example:   The 12th month is December. Use a java "switch" statement to convert from the number (1-12) to the month. Also use a "do while" loop and conditional checking to validate that the number entered is between 1 and 12 inclusive and if not, prompt the user again until getting the correct...
Write a java program that prompt the user to insert last term’s courses and their individual...
Write a java program that prompt the user to insert last term’s courses and their individual mark and store them in an appropriate multidimensional array.
Write a Java program called Decision that includes a while loop to prompt the user to...
Write a Java program called Decision that includes a while loop to prompt the user to enter 5 marks using the JOptionPane statement and a System. Out statement to output a message inside the loop highlighting a pass mark >= 50 and <= 100. Any other mark will output a messaging stating it’s a fail.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT