Write a program in java that determines how long a human user can assign dish du
ID: 3785663 • Letter: W
Question
Write a program in java that determines how long a human user can assign dish duties among the children in a family before making a mistake. Use Console I/O (Scanner and System.out) for all input/output. Follow the directions carefully and be particularly careful to write methods when directed, instead of stuffing main() with code that should be elsewhere.
Write a method that takes user input to create a list of the names of the children in the family. This method must be able to create a list of any number of names, including zero. You can ask the user first for the number of children, and then use a for loop to take that number of names from input and add them to the list, but there are several other possible approaches. This method must return a reference to the list. Do not give the list class scope.
In main(), write a loop that calls the method described above, keeping a reference to the list it returns, and then offers a menu with three options: Quit, Hard Problem, and Easy Problem. The user must be able to repeat this exercise as many times as he or she wants to, providing a new list of names each time, and the program must not terminate until the user chooses to quit.
Implement the hard and easy problems in separate methods, not inside main(). Each of these methods will take a List of Strings as its only parameter and return an int.
In the main loop, use a switch based on the user's input to call the method that corresponds to the user's choice of hard or easy problem. Send a reference to the List of names in the method call.
Within the method for each problem, use a loop that increments the day by one for each iteration, and use a modulo operation to determine which child is responsible for dish duties each day. The Three Stooges example in the review lecture may be helpful. Get the name using the index and List's get(int index) method.
For the easy problem, construct Strings that contain the day number (starting with 0) and the name of the child who should do the dishes. For day 0, the String should be similar to "Day 0: Bob must do the dishes." For the hard problem, just get the name of the child who is responsible for the dishes on each day.
For the easy problem, output the correct String and require the user to copy the whole String back at the console prompt.
For the hard problem, just provide the prompt "input: " and test whether the String input by the user is identical to the name of the child.
For each iteration of the loop, test the input to see if it matches the correct answer. Use a boolean variable that is set to true if the input is correct and set to false when the user makes an error. The loop test should fail when the value of this boolean is false. Don't use a break() statement for this.
The method should return an int, which represents the number of times the user provided the correct input. Print this result form to the main
Explanation / Answer
import java.util.List;
import java.util.ArrayList;
import java.util.Scanner;
public class Test {
public static List userInput() {
List<String> list = new ArrayList<String>();
Scanner scanner = new Scanner(System.in);
System.out.println("enter how many childrens you want to add");
int no = scanner.nextInt();
for (int i = 1; i <=no; i++) {
if (no != 0) {
System.out.println("enter the children name");
String name = scanner.nextLine();
list.add(name);
}
}
return list;
}
public static void main(String[] args) {
Scanner scanner1 = new Scanner(System.in);
List list = userInput();
int i = 1;
while (i == 1) {
System.out.println("choose any of these");
System.out.println("for new list type newlist");
System.out.println("for easy problem type easy");
System.out.println("for hard problem type hard");
System.out.println("for Quit type quit");
String word = scanner1.next();
switch (word.trim()) {
case "easy":
int no = easy(list);
System.out.println("the names in easy problem list is " + no);
break;
case "newlist":
userInput();
break;
case "hard":
int no1 = hard(list);
System.out.println("the names in hard problem list is " + no1);
break;
case "quit":
System.exit(0);
break;
default:
System.out.println("choose right option");
break;
}
}
}
private static int hard(List list) {
int count = 0;
for (int i = 0; i < list.size(); i++) {
count++;
}
return count;
}
private static int easy(List list) {
int count = 0;
for (int i = 0; i < list.size(); i++) {
count++;
}
return count;
}
}
output
enter how many childrens you want to add
3
enter the children name
lisa
enter the children name
david
enter the children name
john
choose any of these
for new list type newlist
for easy problem type easy
for hard problem type hard
for Quit type quit
easy
the names in easy problem list is 3
choose any of these
for new list type newlist
for easy problem type easy
for hard problem type hard
for Quit type quit
quit
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.