content-nid-6030563 1/courses)001256-01-210T-T-O5L-T044 17rencp Problem 2: Cows
ID: 3735422 • Letter: C
Question
content-nid-6030563 1/courses)001256-01-210T-T-O5L-T044 17rencp Problem 2: Cows go M00! [15pt] A Kindergarden teacher wants to test her students to determine whether they know what sounds different animals make by asking them a question and giving them a series of pos- sibilities. For example a sample question is: Enter the sound that a duck makenquack.moo.ruff: She asks you to create a program which tests the students. The program must: Display a message and instructions on how to use the program . Have at least 10 different animals Must give the students 3 chances to get the question correct If they get it right then display a message (saying something positive) . If they exceed 3 tries give them the answer (providing positive support) Questions must be randomized (choose a random animal for each question) There must be three possible sounds to choose from (in a random order) . Two of the incorrect sounds must be randomly chosen for each new question . Must repeat until quit is inputted as an answer. . Must output a message when done which includes the number of questions attempted and the percentage correct . All output should be formatted (neat and orderly) . All output must be age (0-10) appropriate Hint: the functions randiO and randperm) may be useful. Make sure to email your code to your lab instructor so that we can play your game. A sample output of the code is shown on the following page. LGExplanation / Answer
CODE:
public class MainCLass {
//Map of Animal Name to Sound
private static HashMap<String,String> animalSoundMap ;
static ArrayList<String> animalNameList = new ArrayList<>();
static ArrayList<String> animalSoundList = new ArrayList<>();
static final String GAME_RULES ="Answer the question as best possible as possible type quit when finished";
static final String STRING_TO_QUIT_QUIZ = "quit";
static final String WRONG_ANSWER_TEXT ="*** Sorry. You Got it wrong";
static final String RIGHT_ANSWER_TEXT ="*** Correct! You Got it right!";
static final String TRY_AGAIN ="Try Again!";
static final String WRONG_ANSWER_AFTER_EXAUSTING_ATTEMPTS ="*** Sorry. No.";
static String []QUESTION_FORMAT ={"A ",""," goes (" ,"",") :"};
static final String RESULT_BOUNDARY_LINE="==========================================";
static final String NUMBER_OF_QUESTIONS_STRING= "Number of Questions : ";
static final String PERCENTAGE= "Percentage (%) : ";
static final String[] CORRECT_ANSWER ={"A ",""," goes ","" };
public static void main(String[] args) {
configureAnimalsSound();
System.out.println(GAME_RULES);
Scanner scan = new Scanner(System.in);
int numOfQuestionsAttempted=0;
int numOfCorrectAnswers=0;
Random randomNum = new Random();
int min =0;
int max= 9;
boolean isanyQuestionAttempted = false;
String inputFromUser = "";
while(!hasUserQuit(inputFromUser)){
int questionNumber =min + randomNum.nextInt(max);
int numOfWrongsAllowed = 3;
String randomQuestion = createRandomQuestion(questionNumber);
while(numOfWrongsAllowed >0){
System.out.print(randomQuestion);
inputFromUser = scan.next();
if(inputFromUser.equalsIgnoreCase(STRING_TO_QUIT_QUIZ)){
break;
}
isanyQuestionAttempted = true;
if(isAnswerCorrect(questionNumber,inputFromUser)){
System.out.println(RIGHT_ANSWER_TEXT);
numOfCorrectAnswers++;
break;
}else if (numOfWrongsAllowed >1){
System.out.println(WRONG_ANSWER_TEXT +" "+TRY_AGAIN);
}else{
CORRECT_ANSWER[1] = animalNameList.get(questionNumber);
CORRECT_ANSWER[3] = animalSoundList.get(questionNumber);
System.out.println(WRONG_ANSWER_TEXT +" "+Arrays.asList(CORRECT_ANSWER).toString());
}
numOfWrongsAllowed--;
}
numOfQuestionsAttempted ++;
}
if(isanyQuestionAttempted){
System.out.println("No Questions attempted");
}else{
double percent = calculatePercentage(numOfCorrectAnswers,numOfQuestionsAttempted);
System.out.println(getResult(numOfQuestionsAttempted,percent));
}
}
public static boolean isAnswerCorrect(int questionNumber, String answer){
return animalSoundList.get(questionNumber).equalsIgnoreCase(answer);
}
public static double calculatePercentage(int correctQuestionsCount, int totalQuestionsCount){
return (double) correctQuestionsCount/totalQuestionsCount * 100;
}
public static String getResult(int totalQuestionsAttempted, double perCentageForCorrect){
String result = new String(RESULT_BOUNDARY_LINE+" ");
result = result.concat(NUMBER_OF_QUESTIONS_STRING+ totalQuestionsAttempted+" ");
result = result.concat(PERCENTAGE+ perCentageForCorrect +" ");
result = result.concat(" "+RESULT_BOUNDARY_LINE+" ");
return result.toString();
}
public static String pickAnyRandomAnimalSound(int index){
Random randomNum = new Random();
int answerIndex1 = 0+ randomNum.nextInt(9);
while(answerIndex1 == index){
answerIndex1 = 0+ randomNum.nextInt(9);
}
int answerIndex2 = 0+ randomNum.nextInt(9);
while (answerIndex2 == index || answerIndex2 == answerIndex1){
answerIndex2 = 0+ randomNum.nextInt(9);
}
Set<String> optionsArraySet = new HashSet<>();
StringBuffer optionsList = new StringBuffer("");
optionsArraySet.add(animalSoundList.get(answerIndex1));
optionsArraySet.add(animalSoundList.get(index));
optionsArraySet.add(animalSoundList.get(answerIndex2));
for(String item : optionsArraySet){
optionsList = optionsList.append(item +"/");
}
return optionsList.replace(optionsList.length()-1,optionsList.length(),"").toString();
}
public static String createRandomQuestion(int index){
String animal = animalNameList.get(index);
String optionsList = pickAnyRandomAnimalSound(index);
return pickRandomAnimalAndSound(animal,optionsList);
}
public static String pickRandomAnimalAndSound(String animalName, String optionsList){
QUESTION_FORMAT[1] = animalName;
QUESTION_FORMAT[3] = optionsList;
String result = new String();
for(String item : Arrays.asList(QUESTION_FORMAT)){
result = result.concat(item);
}
return result;
}
public static boolean hasUserQuit(String inputFromUser ){
return inputFromUser.equalsIgnoreCase(STRING_TO_QUIT_QUIZ);
}
public static void configureAnimalsSound(){
//Add Animals
animalNameList.add("cow");
animalSoundList.add("moo");
animalNameList.add("duck");
animalSoundList.add("quack");
animalNameList.add("dog");
animalSoundList.add("bark");
animalNameList.add("cat");
animalSoundList.add("meow");
animalNameList.add("donkey");
animalSoundList.add("hee-haw");
animalNameList.add("lion");
animalSoundList.add("roar");
animalNameList.add("deer");
animalSoundList.add("growl");
animalNameList.add("bee");
animalSoundList.add("buzz");
animalNameList.add("crow");
animalSoundList.add("cah");
animalNameList.add("pig");
animalSoundList.add("grunt");
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.