Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program which asks the user to enter the length and width of a rectangle

ID: 3811657 • Letter: W

Question

Write a program which asks the user to enter the length and width of a rectangle and computes the area. The program MUST include the following methods: readLength: This method asks the user for the length of the rectangle, and returns the length. readwidth: This method asks the user for the width of the rectangle and returns the width. compute Area: This method takes the length and width as input and returns the computed area of the rectangle. display solution: This method displays the solution to the problem. You will need to think very carefully about how these methods should work together to find the solution. Input Validation: The length must be positive. The width must be positive. Requirements: All four of the required methods must be implemented, and all must be correctly used within your program to get credit.

Explanation / Answer

import java.io.*;
import java.util.*;
class Test
{
int l,w;
int readLength()
{
Scanner console=new Scanner(System.in);
System.out.println("enter length of a rectangle");
l=console.nextInt();
return l;
}
int readWidth()
{
Scanner console=new Scanner(System.in);
System.out.println("enter width of a rectangle");
w=console.nextInt();
return w;
}
int computeArea()
{
return l*w;
}
void displaySolution()
{
System.out.println("the area of the rectangle is"+(l*w));
}
}
public class Rect
{
public static void main(String args[])
{
int length,width,area;
Test t=new Test();
length=t.readLength();
width=t.readWidth();
area=t.computeArea();
t.displaySolution();
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote