Write a simple Java program code that can be used to capture rectangle dimensions from thekeyboard and then it should compute the area of the rectangle and then display the result on thescreen
Question
Write a simple Java program code that can be used to capture rectangle dimensions from thekeyboard and then it should compute the area of the rectangle and then display the result on thescreen
Solution
Sure, here is a simple Java program that captures rectangle dimensions from the keyboard, computes the area, and displays the result on the screen:
import java.util.Scanner;
public class RectangleArea {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the length of the rectangle:");
double length = scanner.nextDouble();
System.out.println("Enter the width of the rectangle:");
double width = scanner.nextDouble();
double area = length * width;
System.out.println("The area of the rectangle is: " + area);
}
}
Here's how it works:
-
We import the Scanner class, which is used to get user input.
-
We create a new instance of the Scanner class.
-
We ask the user to enter the length of the rectangle and store the input in the
lengthvariable. -
We ask the user to enter the width of the rectangle and store the input in the
widthvariable. -
We calculate the area of the rectangle by multiplying the length and width.
-
Finally, we print out the area of the rectangle.
Similar Questions
Write a program to calculate the circumference and area of a rectangle. Read the required values from the keyboard. Your output must be dislpayed in a user-friendly manner.
Area of a RectangleCreate a Java program that calculates the area of a rectangle using variables to store the length and width.Print out the result.Constraints:NAExample:Input:5.04.0Output:20.0
Write a program to print the area of a rectangle by creating a class named 'Area' having two methods. First method named as 'setDim' takes length and breadth of rectangle as parameters and the second method named as 'getArea' returns the area of the rectangle. Length and breadth of rectangle are entered through keyboard.
Write a VB Program that calculates the area of a rectangle give that the area = lengthmultiplied by width.
Create a method display() that display the length, width, perimeter and area of an object created using an instantiation on rectangle class.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.