Task #1: Define 3 global int variables called totalCorrect , totalIncorrect, and
ID: 3725643 • Letter: T
Question
Task #1:
Define 3 global int variables called totalCorrect, totalIncorrect, and timesTable, and initialize them to 0.
Hint: public static …;
In main, ask the user for his/her first name and last name. You can store each in a separate String variable. Print out “Hello “and the initials of their first and last name. name (Hint: use charAt(0) to get the first initial of each variable. ) Then, call 2 methods: processTimesTable() and displaySummary();
Task #2:
In the processTimesTable() method, define the following local variables:
int correctAnswer, userAnswer
Generate a random number between 2 and 12, and store it in the timesTable global variable. Create a loop that will ask the user to practice their times table 12 times, multiplying the timesTable by the
loopCounter (1 – 12).
Inside the loop, ask the user “what is “+ timesTable + “ times “ + loopCounter. Store the user’s answer in the local variable called userAnswer. Calculate the product of the loop counter and the times table, and store the product in the local variable called correctAnswer. Then, compare userAnswer to the correctAnswer. If they answered the problem correctly, add 1 to the global variable called totalCorrect, otherwise add 1 to the global variable called total incorrect. Tell the user if they got the answer right or wrong.
Task #3:
In the displaySummary() method, print the following messages:
“There were 12 multiplication problems for the “ + timesTable + “ times table.”
“The number of problems answered correctly were: ” + totalCorrect
Define a local variable called percentCorrect, and calculate it by using the following formula:
(totalCorrect/ (totalCorrect + totalIncorrect) ) * 100
“The percent answered correctly is: ” + percentCorrect + “%”
Explanation / Answer
import java.util.Scanner;
import java.util.Random;
class Main {
public static int totalCorrect, totalIncorrect, timesTable;
public static void processTimesTable()
{
int correctAnswer, userAnswer;
Random rn = new Random();
Scanner sc = new Scanner(System.in);
timesTable = rn.nextInt((11))+2;
for(int loopCounter = 1; loopCounter<=12; loopCounter++)
{
System.out.printf("What is %d times %d ? ",timesTable, loopCounter);
userAnswer = sc.nextInt();
correctAnswer = loopCounter * timesTable;
if(correctAnswer == userAnswer)
{
System.out.println("Correct ");
totalCorrect++;
}
else
{
System.out.println("Incorrect ");
totalIncorrect++;
}
}
}
public static void displaySummary()
{
System.out.println("There were 12 multiplication problems for the " + timesTable + " times table. The number of problems answered correctly were: "+ totalCorrect);
double percentCorrect = (totalCorrect/ (double)(totalCorrect + totalIncorrect) ) * 100;
System.out.println("The percent answered correctly is: " + percentCorrect + "%");
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String firstname, lastname;
System.out.print("Enter your first name: ");
firstname = sc.nextLine();
System.out.print("Enter your last name: ");
lastname = sc.nextLine();
System.out.printf("Hello %c%c ",firstname.charAt(0),lastname.charAt(0));
processTimesTable();
displaySummary();
}
}
/* SAMPLE OUTPUT
Enter your first name: Chegg
Enter your last name: India
Hello CI
What is 7 times 1 ? 7
Correct
What is 7 times 2 ? 14
Correct
What is 7 times 3 ? 23
Incorrect
What is 7 times 4 ? 28
Correct
What is 7 times 5 ? 35
Correct
What is 7 times 6 ? 42
Correct
What is 7 times 7 ? 49
Correct
What is 7 times 8 ? 53
Incorrect
What is 7 times 9 ? 12
Incorrect
What is 7 times 10 ? 70
Correct
What is 7 times 11 ? 77
Correct
What is 7 times 12 ? 84
Correct
There were 12 multiplication problems for the 7 times table.
The number of problems answered correctly were: 9
The percent answered correctly is: 75.0%
*/
// Hit thumbs up if you are happy with answer.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.