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...
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-...
Write a program that asks the user for an angle, entered in radians. The program should...
Write a program that asks the user for an angle, entered in radians. The program should then display the sine, cosine, and tangent of the angle. (Use the sin, cos, and tan library functions to determine these values.) The output should be displayed in fixed-point notation, rounded to four decimal places of precision Take your previous Angle Calculator program and modify it to do a table of trig values. The columns will be: Degrees, Sine, Cosine, Tangent,. And the rows...
Write a program IN JAVA that asks the user for a number. The program should check...
Write a program IN JAVA that asks the user for a number. The program should check the number to ensure that it is valid (if not, the user should enter a valid number to continue.) The program should print out all of the prime numbers from 2 up to the number, with up to 10 numbers per line. (Recall: A prime number is a number that is only divisible by itself and 1.) The code should ask the user if...
Program should be written in Java a) Write a program that asks the user to enter...
Program should be written in Java a) Write a program that asks the user to enter the approximate current population of India. You should have the computer output a prompt and then YOU (as the user should enter the population.)  For testing purposes you may use the value of 1,382,000,000 from August 2020. Assume that the growth rate is 1.1% per year. Predict and print the predicted population for 2021 and 2022. The printout should include the year and the estimated...
Tail of a File, C++ Program. write a program that asks the user for the name...
Tail of a File, C++ Program. write a program that asks the user for the name of a text file. The program should display the last 10 lines, or all lines if less than 10. The program should do this using seekg Here is what I have so far. #include<iostream> #include<fstream> #include<string> using namespace std; class File { private:    fstream file;    string name; public:    int countlines();    void printlines(); }; int File::countlines() {    int total =...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT