Question

In: Computer Science

Must implement a simple program that asks the user for a width and a height, storing...

Must implement a simple program that asks the user for a width and a height, storing them as double values. Afterwards, you compute area and perimeter as if the shape were a rectangle. Output the results. Then, you compute the area and perimeter as if the shape were a right triangle and you have been given the base and height.

Area and Perimeter Calculator Enter width: 3

Enter height: 5

If your shape is a rectangle, its area is 15.0 Its perimeter is 16.0

If your shape is a triangle, its area is 7.5

If it is a right triangle, its perimeter is 8.0 + square root of 34.0

Needs to be in Java

Solutions

Expert Solution

Explanation:

Here is the code which has the Scanner object and takes the width and height from the user.

Then it prints the area and perimeter of rectangle and triangle.

Code:

import java.util.Scanner;

public class Main
{
   public static void main(String[] args) {
      
       Scanner sc = new Scanner(System.in);
      
       System.out.println("Area and Perimeter Calculator");
       System.out.print("Enter width: ");
       double width = sc.nextDouble();
       System.out.print("Enter height: ");
       double height = sc.nextDouble();
      
       System.out.println("If your shape is a rectangle, its area is "+(width*height));
       System.out.println("Its Perimeter is "+(2*(width+height)));
       System.out.println("If your shape is a triangle, its area is "+((width*height)/2.0));
       System.out.println("If it is a right triangle, its perimeter is "+(width+height)+" + square root of "+(width*width + height*height));
      
      
   }
}

output:

PLEASE UPVOTE IF YOU FOUND THIS HELPFUL!

PLEASE COMMENT IF YOU NEED ANY HELP!


Related Solutions

You must implement a simple program that asks the user for three values, a, b, and...
You must implement a simple program that asks the user for three values, a, b, and c, which you should store as double values. Using these numbers, you compute the value of the two solutions to the quadratic equation, assuming that the equation is of the form: ax^2 + bx + c = 0 Complete the description of the program given above so that your output looks as close to the following sample output as possible. In this sample, the...
Write a program checkerboard3x3.cpp that asks the user to input width and height and prints a...
Write a program checkerboard3x3.cpp that asks the user to input width and height and prints a checkerboard of 3-by-3 squares. (It should work even if the input dimensions are not a multiple of three.) . Don't use function. Example 1: Input width: 16 Input height: 11 Shape: *** *** *** *** *** *** *** *** *** *** *** * *** *** * *** *** * *** *** *** *** *** *** *** *** *** *** *** * *** *** *...
Write a simple MIPS program that asks the user to input a string and then a...
Write a simple MIPS program that asks the user to input a string and then a character. The program should then count how many times that character appears in the string and print out that value. Please use comments.
Task G. Checkerboard (3x3) Write a program checkerboard3x3.cpp that asks the user to input width and...
Task G. Checkerboard (3x3) Write a program checkerboard3x3.cpp that asks the user to input width and height and prints a checkerboard of 3-by-3 squares. (It should work even if the input dimensions are not a multiple of three.) Don't use function. Please explain in detail and make it simple. And be clear with the {}. Thank you in advance. Example 1: Input width: 16 Input height: 11 Shape: *** *** *** *** *** *** *** *** *** *** *** *...
Selection Sort Programmatically Implement in C++ the necessary program that does the following: Asks the user...
Selection Sort Programmatically Implement in C++ the necessary program that does the following: Asks the user and gets at least 5 whole numbers as user input from the keyboard and stores them in an array Displays the numbers from the array on the screen Sorts the numbers in the array using SELECTION SORT Algorithm Displays the sorted numbers on the screen from the array Save your code file as "yourLastname_Firstname_SelectionSort.cpp" and submit your .cpp file. NOTE: This assignment needs only...
Bubble Sort Programmatically Implement in C++ the necessary program that does the following: Asks the user...
Bubble Sort Programmatically Implement in C++ the necessary program that does the following: Asks the user and gets at least 5 whole numbers as user input from the keyboard and stores them in an array Displays the numbers from the array on the screen Sorts the numbers in the array using BUBBLE SORT Algorithm Displays the sorted numbers on the screen from the array Save your code file as "yourLastname_Firstname_BubbleSort.cpp" and submit your .cpp file. NOTE: This assignment needs only...
Write a java simple document retrieval program that first asks the user to enter a single...
Write a java simple document retrieval program that first asks the user to enter a single term query, then goes through two docuements named doc1.txt and doc2.txt (provided with assignment7) to find which document is more relevant by calculating the frequency of the query term in each document. Output the conclusion to a file named asmnt7output.txt in the following format (as an example for query “java” and “problem”). The percentages in parenthese are respective supporting frequencies. java: doc1(6.37%) is more...
Write a program that asks the user for an integer. The program checks and prints to...
Write a program that asks the user for an integer. The program checks and prints to the screen whether the number is prime or not. For example, if user enters 17, the program should print “17 is prime”; if the user enters 20, the program should print “20 is not prime”. please do it with a “ while Loop”, Thanks..
Design a program that asks the user for a number and the program computes the factorial...
Design a program that asks the user for a number and the program computes the factorial of that number and displays the result . Implement with two different modules - one that uses a for loop and one that uses a while loop Grading Rubrick Program Compiles Cleanly  syntax errors25 pts-5 per errorProgram runs without runtime errors ( validation)run-time errors 25 pts-5 per errorProgram give correct answersLogic errors30 pts-5 per errorProgram is repeatableNot repeatable5 pts-5 ptsProgram is well modularizedBarely Modularized10 pts-...
Python: Write a program that asks the user for the name of a file. The program...
Python: Write a program that asks the user for the name of a file. The program should display the contents of the file line by line.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT