Scanner new scanner system out string nose nest Response by the doing following:
ID: 3786025 • Letter: S
Question
Scanner new scanner system out string nose nest Response by the doing following: Ask the user with entered in and Height If the user doesn't enter a valid with or Height trace of the shape call the method below and print If the user doesn't enter a valid with the customized massage thanks john for creating a square. public Static String Gat Message return Tanks %5 for creating a %5 public static string determination String shape is 5% area String shape square if (x 1 = y) {Shape = Rectangle} System out printed (message, shape, (x * y));Explanation / Answer
//StatementExpression.java
import java.util.Scanner;
public class StatementExpression
{
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
System.out.print("What is your name? ");
String name = s.next();
System.out.println("Hello, "+ name);
int width, height;
while(true)
{
System.out.println("Enter width: ");
width = s.nextInt();
if(width < 0)
System.out.println("Invalid input!");
else
break;
}
while(true)
{
System.out.print("Enter height: ");
height = s.nextInt();
if(height < 0)
System.out.print("Invalid input!");
else
break;
}
String shape = DetermineShape(width,height);
System.out.printf(GetCompleteMessage(), name, shape);
}
public static String GetCompleteMessage()
{
return "Thanks, %s, for creating a %s!";
}
public static String DetermineShape(int x, int y)
{
String message = "Shape is %s with area %d%n";
String shape = "Square";
if(x!=y) { shape = "Rectangle"; }
System.out.printf(message,shape,(x*y));
return shape;
}
}
/*
Output:
What is your name? John
Hello, John
Enter width: 4
Enter height: -2
Invalid input!
Enter height: 5
Shape is Rectangle with area 20
Thanks, John, for creating a Rectangle!
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.