Question

In Java, create a program to determine the perimeter and area of a rectangle. Write a...

In Java, create a program to determine the perimeter and area of a rectangle. Write a rectangle class with a separate runner class. Allow the user to input the rectangle length and width. All should be tested in a runner. You need: file input using scanner, instance variables, constructors, minimum of 3 objects and 3 methods.

Example:

Please enter the length of the rectangle (in inches): 6

Please enter the width of the rectangle (in inches): 10

The area of the rectangle is 60 inches. The perimeter is 32 inches. Would you like to run another (y/n)?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hey,

Note: Brother in case of any queries, just comment in box I would be very happy to assist all your queries

import java.util.*;
class Rectangle
{
private double l,w;
public Rectangle(double l,double w)
{
this.l=l;
this.w=w;
}
public double getArea()
{
return l*w;
}
public double getPeri()
{
return 2*(l+w);
}
public void printInfo()
{
System.out.println("The area of the rectangle is "+getArea()+" inches. ");
System.out.println("The perimeter is "+getPeri()+" inches.");
}
  
}
public class Runner {

public static void main (String [ ] args)
  
{
Scanner sc=new Scanner(System.in);
char choice='y';
while(choice!='n'&&choice!='N')
{
System.out.println("Please enter the length of the rectangle (in inches): ");
double l,w;
l=sc.nextDouble();
System.out.println("Please enter the width of the rectangle (in inches): ");
w=sc.nextDouble();
  
Rectangle r=new Rectangle(l,w);
r.printInfo();
System.out.println("Would you like to run another (y/n)?: ");
choice=sc.next().charAt(0);
  
}

}

}

Kindly revert for any queries

Thanks.

Add a comment
Know the answer?
Add Answer to:
In Java, create a program to determine the perimeter and area of a rectangle. Write a...
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
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