Question

Write a Java program that prompts the user to input length and width of a shape. If length is equal to width, call a method t
solve it with Java please
0 0
Add a comment Improve this question Transcribed image text
Answer #1

input code:

output:

Enter the width:4 Enter the length:4 Area of the square is 16 ... Program finished with exit code o Press ENTER to exit conso

code:

import java.util.*;
public class Main
{
/*make method for calculate the square area*/
public static int shape(int l)
{
return l*l;
}
/*make method for calculate the rectangle area*/
public static int shape(int w,int l)
{
return w*l;
}
   public static void main(String[] args) {
   /*make Scanner class object for take input from user*/
   Scanner sc=new Scanner(System.in);
   /*take input from user*/
       System.out.print("Enter the width:");
       int width=sc.nextInt();
       System.out.print("Enter the length:");
       int length=sc.nextInt();
       int area;
       /*check width and length is same or not*/
       if(width==length)
       {
       /*call method*/
area=shape(length);
/*print area*/
System.out.println("Area of the square is "+area);
       }
   else
   {
   /*call method*/
   area=shape(width,length);
   /*print Area*/
   System.out.println("Area of the rectangle is "+area);
   }
   }
}

Add a comment
Know the answer?
Add Answer to:
solve it with Java please Write a Java program that prompts the user to input length...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • For a sample of n=6 lightbulbs. The probability of a broken bulb is 9%. What is...

    For a sample of n=6 lightbulbs. The probability of a broken bulb is 9%. What is the probability of exactly two broken bulbs? Round your final answer to 4 decimal places For the toolbar, press ALT+F10 (PC) or ALT+FN+F10 (Mac). TT T Arial . 3 (12pt) - T E E-S... Qi The probability to find anything is

  • Write a Java program that prompts user to select one of the five shapes(Rectangle, Square, Triangle,...

    Write a Java program that prompts user to select one of the five shapes(Rectangle, Square, Triangle, Circle, and Parallelogram), then calculate area of the shape,must have input validation and uses more of java library like math class, wrapper class, string methods, use formatted output with the printf method. Finally, create a pseudo-code statement and flowchart

  • Its java class pratice  problem (Geometry: point in a rectangle?) Write a program that prompts the user...

    Its java class pratice  problem (Geometry: point in a rectangle?) Write a program that prompts the user to enter a point (x, y) and checks whether the point is within the rectangle centered at (0, 0) with width 10 and height 5.  For example, (2, 2) is inside the rectangle and (6, 4) is outside the rectangle. (Hint: A point is in the rectangle if its horizontal distance to (0, 0) is less than or equal to10 / 2 and...

  • In Java, write a program that prompts the user to input a sequence of characters and...

    In Java, write a program that prompts the user to input a sequence of characters and outputs the number of vowels. You will need to write a method to return a value called isAVowel which will return true if a given character is a vowel and returns false if the character is not a vowel. A second method, called isAConstant, should return true if a given character is a constant and return false if the character is not a constant....

  • Write a complete java program that prompts the user for their name and two numbers. The...

    Write a complete java program that prompts the user for their name and two numbers. The correct java methods and parameters must be passed to find the length of the name entered and return “TRUE” if the sum of numbers is even, or FALSE if the sum of numbers is odd: Notes included in the program Sample Program Please enter a name and I will tell you the length of the name entered John Then length of the name John...

  • In Java Exercise #2: (Java) Design and implement a program (name it ComputeAreas) that defines four...

    In Java Exercise #2: (Java) Design and implement a program (name it ComputeAreas) that defines four methods as follows: Method squareArea (double side) returns the area of a square. Method rectangleArea (double width, double length) returns the area of a rectangle. Method circleArea (double radius) returns the area of a circle. Method triangleArea (double base, double height) returns the area of a triangle. In the main method, test all methods with different input value read from the user. Document your...

  • Write a Java console application that prompts the user to enter the radius of a circle,...

    Write a Java console application that prompts the user to enter the radius of a circle, then prints its radius, diameter, circumference, and area. Write a JavaFX GUI application to do the same calculation, and draw the circle. The Console Output Enter the radius of the circle: 1.2 The radius is 1.2 The diameter is 2.4 The circumference is 7.5398223686155035 The area is 4.523893421169302 Write and document your program per class coding conventions. Add an instance variable double radius. Generate...

  • (NEED FULL CODE PLEASE)write a java program class to calculate and display the areas of 10 rectan...

    (NEED FULL CODE PLEASE)write a java program class to calculate and display the areas of 10 rectangles using arrays. * input validation sizes must be between 0-50 inches output to be displayed on the screen and send to an output file. *the output must appear in the following format. rectangle length width area 1    2.0    3.0    6.0 2 - - - - 10 display the rectangle with largest area. rectangle1 area= display the rectangle with smallest area. rectangle2 area=

  • Write a program to display the area of a rectangle after accepting its length and width...

    Write a program to display the area of a rectangle after accepting its length and width from the user by means of the following functions: getLength – ask user to enter the length and return it as double getWidth – ask user to enter the width and return it as double getArea – pass width and length, compute area and return area displayArea – pass area and display it in this function. Expected Output: (i) Enter the length: 29 Enter...

  • Please write in Java Language Write an abstract class Shape with an attribute for the name...

    Please write in Java Language Write an abstract class Shape with an attribute for the name of the shape (you'll use this later to identify a particular shape). Include a constructor to set the name of the shape and an abstract calculateArea method that has no parameters and returns the area of the shape. Optionally, include a constructor that returns the name of the shape and its area (by calling the calculateArea method). Write a Rectangle class that inherits from...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT