import java.util.Scanner;
public class InteractiveRectangle
{
public static void main(String[] args)
{
int height = readPositiveInteger("Enter the height");
int width = readPositiveInteger("Enter the width");
printRectangleDetails(height, width);
}
/**
* Read in an integer and return its value
* @param the prompt to be shown to the user
*/
public static int readInteger(String prompt)
{
Scanner scan = new Scanner(System.in);
System.out.println(prompt);
while (!scan.hasNextInt()) // while non-integers are present...
{
scan.next(); //...read and discard input, then prompt again
System.out.println ("Bad input. Enter an integer");
}
int firstInteger = scan.nextInt();
return firstInteger;
}
/**
* Read in an integer greater than 0 and return its value
* @ param the prompt to be shown to the user
*/
public static int readPositiveInteger(String prompt)
{
int input = 0;
Scanner scan = new Scanner(System.in);
boolean first = false;
while (!scan.hasNextInt())
{
int quantity = scan.nextInt();
if (quantity > 0)
{
first = true;
quantity = scan.nextInt();
}
else if (quantity <= 0)
{
System.out.println("Bad input. Enter a positive integer.");
}
}
return input;
}
/**
* Returns the area of a rectangle
* @param height the height of the rectangle
* @param width the width of the rectangle
*/
public static int area (int height, int width)
{
return height * width;
}
/**
* Returns the perimeter of a retangle
* @param height the height of the rectangle
* @param width the width of the rectangle
*/
public static int perimeter (int height, int width)
{
return (height * 2) + (width * 2);
}
/**
* Prints the area, the perimeter, the length and the width
* @param heigth the height of the rectangle
* @param width the width of the rectangle
*/
public static void printRectangleDetails (int height, int width)
{
System.out.println("The height is " + height);
System.out.println("The width is " + width);
System.out.println("The area is " + area(height, width));
System.out.println("The perimeter is " + perimeter(height, width));
}
}
This is my program so far, but I have some problems with the readPositiveInteger method and I also have to write a method that asks the user if he wants another rectangle typing "y" if he wants it and "n" if he doesn't using a while loop. Thank you for