In: Computer Science
In Java for beginners:
The math teacher at Garfield High School, Mr. Escalante, is looking for new ways to help his students learn algebra. They are currently studying the line equation ( y = m * x + b ). Mr. Escalante wants to be able to give his students a phone app that they can use to study for the midterm exam. This is what he'd like the program to do:
The user will be presented with the option to study in one of 3 modes, or to quit the program:
In each mode, all of the given values should be randomly generated integers between -100 and +100. This means that the correct answer must be calculated by the program.
Once the student selects a mode, the program will continue to present randomly generated questions until the student has correctly answered 3 questions in a row. If the student attempts more than 3 questions in a particular mode, a hint about how to solve the problem should be given before the next question is presented.
After the student has correctly answered 3 questions in a row, an overall score (the number of questions answered correctly divided by the total number of questions attempted) should be displayed, and the menu is presented again.
DIRECTIONS
Your program must
This is a three-part staged assignment. You must complete part 1 with at least 80% success before you can begin part 2, and the same with part 3. After you've completed and submitted each part, you can proceed to the next part by selecting the drop-down arrow next to the assignment title. You will need to complete all parts to receive full credit.
---------------------------------------------------------------------------------------------
Part 1. Solve for y.
Write a program with a that displays a randomly generated problem that asks the user to solve for the y variable, takes input from the user, and prints "correct" if the user answered correctly and prints "incorrect" if not. Your main should give one problem and then exit. Use one or more methods to produce this behavior. Example interactions: Given: m = 10 x = 3 b = 2 What is the value of y? 32 Correct! Given: m = 6 x = 9 b = 3 What is the value of y? 52 Sorry, that is incorrect. The answer is 57.
Part 2. Solve for y, m and b.
Add 2 choices that behave similarly to your part 1 program, but have the user solve for m and b instead. These choices should also randomly generate problems, and print if the user answers correctly. Your main program should display a menu asking which type of question to ask: Select 1 to Solve for Y, 2 to Solve for M, 3 to Solve for B and 4 to quit. When the user selects the question type, one question should be asked, then the user should return to the menu. This should repeat until the user selects quit, then the program should exit. Use one or more methods and loops to produce this behavior.
Part 3.Three in a Row with Hints.
Update your program so that each problem type mode will repeatedly ask questions until the user gets 3 correct answers in a row. If the attempts more than 3 questions in a particular mode, the program should provide a hint on how to solve problems of this type. After the student has correctly answered 3 questions in a row, an overall score (the number of questions answered correctly divided by the total number of questions attempted) should be displayed, and the menu is presented again.
Part 1:
Code:
import java.util.Random;
import java.util.Scanner;
public class solveForY {
public static void main(String[] args) {
Random rn = new Random();
Scanner sc = new
Scanner(System.in);
// randomly generating values
between -100 and 100
int x = rn.nextInt(200) -
100;
int m = rn.nextInt(200) -
100;
int b = rn.nextInt(200) - 100;
// value of y
int yCorrect = m*x + b;
// user response
int yUser;
// prompting user for
input
System.out.print("Given: m = "+m+"
x = "+x+" b = "+b+" What is the value of y? ");
yUser = sc.nextInt();
// check the answer
check(yUser, yCorrect);
}
// method to check and print user response
public static void check(int user, int correct)
{
if (user == correct)
{
System.out.println("Correct!");
}
else
{
System.out.println("Sorry, that is incorrect. The answer is
"+correct+".");
}
}
}
OUTPUT:
Part 2:
NOTE: I've assumed that inputs and outputs are integers only, so if the value of m is 0.08 it'll be rounded to 0.
Code:
import java.util.Random;
import java.util.Scanner;
public class Part2 {
public static void main(String[] args) {
Random rn = new Random();
Scanner sc = new
Scanner(System.in);
int x, y, m, b, user;
while (true)
{
// getting user
choice
System.out.print("\nSelect 1 to Solve for Y, 2 to Solve for M, 3 to
Solve for B and 4 to quit. ");
int choice =
sc.nextInt();
if (choice ==
1)
{
x = rn.nextInt(200) - 100;
m = rn.nextInt(200) - 100;
b = rn.nextInt(200) - 100;
y = m*x + b;
// prompting user for input
System.out.print("\nGiven: m = "+m+" x = "+x+" b
= "+b+" What is the value of y? ");
user = sc.nextInt();
check(user, y);
}
else if (choice
== 2)
{
x = rn.nextInt(200) - 100;
y = rn.nextInt(200) - 100;
b = rn.nextInt(200) - 100;
m = (y - b)/x;
// prompting user for input
System.out.print("\nGiven: y = "+y+" x = "+x+" b
= "+b+" What is the value of m? ");
user = sc.nextInt();
check(user, m);
}
else if (choice
== 3)
{
x = rn.nextInt(200) - 100;
y = rn.nextInt(200) - 100;
m = rn.nextInt(200) - 100;
b = y - (m*x);
// prompting user for input
System.out.print("\nGiven: y = "+y+" x = "+x+" m
= "+m+" What is the value of b? ");
user = sc.nextInt();
check(user, b);
}
else if (choice
== 4)
{
break; // break out of loop
}
else
{
System.out.println("\nInvalid Choice!");
}
}
}
// method to check and print user response
public static void check(int user, int correct)
{
if (user == correct)
{
System.out.println("Correct!");
}
else
{
System.out.println("Sorry, that is incorrect. The answer is
"+correct+".");
}
}
}
OUTPUT:
Part 3:
Code:
import java.util.Random;
import java.util.Scanner;
public class three {
public static void main(String[] args) {
Random rn = new Random();
Scanner sc = new
Scanner(System.in);
int x, y, m, b, user;
while (true)
{
int correct =
0;
int
num_of_questions = 0;
int
total_questions = 0;
// getting
user choice
System.out.print("\nSelect 1 to Solve for Y, 2 to Solve for M, 3 to
Solve for B and 4 to quit. ");
int choice =
sc.nextInt();
if (choice ==
1)
{
while (correct < 3)
{
x = rn.nextInt(200) -
100;
m = rn.nextInt(200) -
100;
b = rn.nextInt(200) -
100;
y = m*x + b;
// prompting user for
input
System.out.print("\nGiven: m
= "+m+" x = "+x+" b = "+b+" What is the value of y? ");
user = sc.nextInt();
boolean isCorrect =
check(user, y);
if (isCorrect)
{
correct++;
num_of_questions = 0;
}
else // this will make sure
that user answers correctly in a row
{
correct =
0;
num_of_questions++;
}
total_questions++;
if (num_of_questions >
3)
{
hintForY();
num_of_questions = 0;
}
}
printScore(total_questions, correct);
}
else if (choice
== 2)
{
while (correct < 3)
{
x = rn.nextInt(200) -
100;
y = rn.nextInt(200) -
100;
b = rn.nextInt(200) -
100;
m = (y - b)/x;
// prompting user for
input
System.out.print("\nGiven: y
= "+y+" x = "+x+" b = "+b+" What is the value of m? ");
user = sc.nextInt();
boolean isCorrect =
check(user, m);
if (isCorrect)
{
correct++;
num_of_questions = 0;
}
else // this will make sure
that user answers correctly in a row
{
correct =
0;
num_of_questions++;
}
total_questions++;
if (num_of_questions >
3)
{
hintForM();
num_of_questions = 0;
}
}
printScore(total_questions, correct);
}
else if (choice
== 3)
{
while (correct < 3)
{
x = rn.nextInt(200) -
100;
y = rn.nextInt(200) -
100;
m = rn.nextInt(200) -
100;
b = y - (m*x);
// prompting user for
input
System.out.print("\nGiven: y
= "+y+" x = "+x+" m = "+m+" What is the value of b? ");
user = sc.nextInt();
boolean isCorrect =
check(user, b);
if (isCorrect)
{
correct++;
num_of_questions = 0;
}
else // this will make sure
that user answers correctly in a row
{
correct =
0;
num_of_questions++;
}
total_questions++;
if (num_of_questions >
3)
{
hintForB();
num_of_questions = 0;
}
}
printScore(total_questions, correct);
}
else if (choice
== 4)
{
break; // break out of loop
}
else
{
System.out.println("\nInvalid Choice!");
}
}
}
// method to check and print user response
public static boolean check(int user, int correct)
{
if (user == correct)
{
System.out.println("Correct!");
return
true;
}
else
{
System.out.println("Sorry, that is incorrect. The answer is
"+correct+".");
return
false;
}
}
// methods for printing hints
public static void hintForY() {
System.out.println("\nHint:
Multiply the values of m and x, then add b.");
}
public static void hintForM() {
System.out.println("\nHint:
Subtract the value of b from y, then divide the answer by
x");
}
public static void hintForB() {
System.out.println("\nHint:
Multiply the values of m and x, then subtract it from y.");
}
// method to print score
public static void printScore(int attempt, int
correct) {
System.out.println("\nScore is
"+correct+"/"+attempt+" = "+correct/attempt);
}
}
OUTPUT: