{}
run-icon
Main.java
//The Triangle Base Java Program //Teacher who is learning Java: Andy Goldstein //First, create a Scanner object. This will allow keyboard input from the user. import java.util.Scanner; //Second, write Java statements to prompt the user for the area and height of the triangle. //I need to first create the class declaration public class TriangleBaseCalculator { //Next I need to create the main method public static void main(String[] args) { //Now I need to create a Scanner object to read input from the keyboard Scanner input = new Scanner(System.in); //Now I need to create variables to store the area, height and base of the triangle double area; double height; double base; //I want to print a message welcoming the user to the Triangle Base Calculator System.out.println("---Welcome to the Triangle Base Calculator---"); System.out.println("You have arrived just in time!"); System.out.println(""); //Skipping a line to create some space //Now I want to ask the user for input for the area of the triangle System.out.println("Enter the area of the triangle you want to calculate: "); //Now I want to assign the user's answer to the area variable I created area = input.nextDouble(); //Now I want to ask the user for input for the height of the triangle System.out.println("Enter the height of the triangle you want to calculate: "); //Now I want to assign the user's answer to the height variable I created height = input.nextDouble(); //Good so far. //Next, I want to calculate the base of the triangle, based on the input entered by the user. The formula to calculate the area of a triangle is 1/2(base*height). So the base would be b = 2a/h. Let's do this! //I need to make sure the height is not equal to 0, since you cannot divide by zero. I'm going to work this into a conditional statement if (height > 0) { //I want to calculate and show the base of the triangle base = (2 * area) / height; System.out.println("The base of the triangle is: " + base); } else { //I want to tell the user that they can't have 0 or a negative for the height. System.out.println("Warning Will Robinson! Danger! Danger! The height cannot be 0 or less than 0!!!!"); } //Now I want to close the Scanner object so that I don't take up system resources input.close(); } }
Output