In: Computer Science
Write in JAVA eclipse program (comment in every line):
class for calculating an area of a room. The program MUST ask for the length and the width of the room.
Make sure you write the methods to get the width and length and print the area.
Please find below code in java and don't forget to give a Like.
Please refer below screenshot for code and output.
Code:
import java.util.Scanner;//importing scanner to take input from
user
public class Main
{
public static void main(String[] args) {
double area;//taking all varibles datatype as
double
double length,width;
//calling length method that takes input
//method returns lenght value from user and store in
length variable
length=length();
//calling width function and storing in width
variable
width=width();
//calculating area by multiplying both
area=length*width;
//calling print method to print the area
print(area);
}
//creating a length method of type double since it
returns double value
public static double length(){
//creating a input varible and will be using it to
take input
Scanner input=new Scanner(System.in);
System.out.print("Enter the length of the room:
");
//this this take the input from user and store it in
lenght varible
double length=input.nextDouble();
//returning length to main
return length;
}
//this is same as above length method
public static double width(){
Scanner input=new Scanner(System.in);
System.out.print("Enter the width of the room:
");
double width=input.nextDouble();
return width;
}
//method print will take double value as argument and
print the area
public static void print(double area){
//this statement prints the output
System.out.println("Area of the room: "+area);
}
}
Output and screenshot: