I need this done in java Problem 1 (name this Lab5_Problem1) Use a nested, multi
ID: 672305 • Letter: I
Question
I need this done in java
Problem 1 (name this Lab5_Problem1)
Use a nested, multiple-selection if structure for this problem. McCormick is conducting a focus group and will pay people $30 to attend. It will be at 5 p.m. on Wednesday, March 10 in Hunt Valley. Once you determine that a person can attend, next determine if they are 18 or older; if not, they are not eligible to participate. If they are, then determine if they are married. If they are, they are eligible to participate. Otherwise, they can't.
A key to setting up your nested if logic is to design things so that once you reach an "N" answer, you don't keep asking the person any more questions. Once they answer no, you've determined that they're not eligible, so don't annoy them by asking more questions when you already know they don't meet the criteria.
Specifics:
Use a set of three nested, multiple-selection if tests to code this problem. It must be three layers deep. See the general information above for the order to ask your questions.
Each if test must prompt for input and take three courses of action as it validates the data entered by the user. Valid entries are Yy and Nn, and a third possibility: bad data. Three possibilities.
Use a String as your input capture variable. Reuse the input-capture variable since. The example below uses sIn.
First convert the input value to uppercase; then your logic can then test for Y or N. Anything besides YyNn is invalid data. Here's how each input dialog would go:
Prompt with the question.
Store the input into the String input-capture variable:
sIn = cin.next();
Convert the value to uppercase:
sIn = sIn.toUpperCase();
There are three possible outcomes when you evaluate sIn, once converted to uppercase:
If Y proceed to your next question.
If N they’re done—you don't need to continue, because they're not a candidate for this taste testing session. Tell them you're sorry and end the interrogation.
If they typed anything besides YyNn display an error message and stop.
The pseudocode looks like this:
PROMPT for available on this date Y or N
GET input
CONVERT to uppercase
IF available
PROMPT for 18 or older Y or N
GET INPUT
CONVERT to uppercase
IF 18 or older
PROMPT for married Y or N
GET INPUT
CONVERT to uppercase
IF Y for married
DISPLAY Eligible
ELSE IF N for invalid data or not married
DISPLAY not eligible
ELSE
DISPLAY error (other than Y/y/N/n)
END IF
ELSE IF N for not 18 or older
DISPLAY not eligible
ELSE
DISPLAY error (other than Y/y/N/n)
END IF
ELSE IF N for not available
DISPLAY not eligible message
ELSE
DISPLAY error (other than Y/y/N/n)
END IF
Each of the three nested cases should look something like this:
if (sIn.equals("Y"))
{
// Statements
}
else if (sIn.equals("N"))
{
// Statements
}
else
{
// Error message for invalid data
}
Use only ONE variable sIn for your input capture. Due to the way you will nest the logic, you won't need any more variables than this since you can reuse the same input-capture variable for each of the three user-supplied answers.
In this problem, there is no distinct input, processing, calculation, or output section: everything is kind of jumbled together within the if code. You don't need to use all your "starter code" for this problem; just provide decent comments in lieu of the standard sections.
Valid responses will be Y or y, N or n, so if the user types anything else, it will be an invalid answer.
The following are three examples of how the dialog might play out; the user responses are in bold and highlighted. Use the verbiage provided for the prompts when you write your program:
Here is a sample. Since they answered Y to all the questions, they are eligible.
Are you available March 10? Y or N: Y
Are you 18 or older? Y or N: Y
Are you married? Y or N: Y
Congratulations. You are eligible.
Here is another example. In this one, they answer N to the 18 or over question, so the question about being married is not shown at all.
Are you available March 10? Y or N: Y
Are you 18 or older? Y or N: N
Sorry. You are not eligible.
Here is a final example. The person answer's X to the first question, which is invalid data (Y or N is asked for):
Are you available March 10? Y or N: X
Error: Invalid response. Y or N is required.
Explanation / Answer
Please find the required solution:
import java.util.Scanner;
public class TestClass {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Are you available March 10? Y or N:");
String text = input.next();
if (text.equals("Y")) {
System.out.print("Are you 18 or older? Y or N:");
text = input.next();
if (text.equals("Y")) {
System.out.print("Are you married? Y or N:");
text = input.next();
if (text.equals("Y")) {
System.out.println("Congratulations. You are eligible.");
} else if (text.equals("N")) {
System.out.println("Sorry you not eligible");
} else {
System.out
.println("Error: Invalid response. Y or N is required.");
}
} else if (text.equals("N")) {
System.out.println("Sorry you not eligible");
} else {
System.out
.println("Error: Invalid response. Y or N is required.");
}
} else if (text.equals("N")) {
System.out.println("Sorry you not eligible");
} else {
System.out.println("Error: Invalid response. Y or N is required.");
}
input.close();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.