Question

In: Computer Science

In Java for beginners: The math teacher at Garfield High School, Mr. Escalante, is looking for...

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:

  1. Solve for the value of y, given the values for m, x and b.
  2. Solve for the value of m, given the values for y, x and b.
  3. Solve for the value of b, given the values for y, m and x.

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

  • define and use custom methods (functions)
  • use if statements
  • use while loops

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.

Solutions

Expert Solution

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:


Related Solutions

Question 6 A high school math teacher believes that male and female students who graduated from...
Question 6 A high school math teacher believes that male and female students who graduated from the school perform equally well on SAT math test. She randomly chooses 10 male students and 10 female students who graduated from this school. The following are the SAT math scores of the 20 students: Male: 23, 30, 27, 29, 22, 34, 36, 28, 28, 31 Female: 22, 33, 30, 28, 28, 31, 34, 25, 29, 21 Give a 95% confidence interval for the...
Question 6 (1 point) A high school math teacher believes that male and female students who...
Question 6 (1 point) A high school math teacher believes that male and female students who graduated from the school perform equally well on SAT math test. She randomly chooses 10 male students and 10 female students who graduated from this school. The following are the SAT math scores of the 20 students: Male: 23, 30, 27, 29, 22, 34, 36, 28, 28, 31 Female: 22, 33, 30, 28, 28, 31, 34, 25, 29, 21 Give a 95% confidence interval...
Question 5 (1 point) A high school math teacher believes that male and female students who...
Question 5 (1 point) A high school math teacher believes that male and female students who graduated from the school perform equally well on SAT math test. She randomly chooses 10 male students and 10 female students who graduated from this school. The following are the SAT math scores of the 20 students: Male: 23, 30, 27, 29, 22, 34, 36, 28, 28, 31 Female: 22, 33, 30, 28, 28, 31, 34, 25, 29, 21 Test the teacher's claim. What...
A school district developed an after-school math tutoring program for high school students. To assess the...
A school district developed an after-school math tutoring program for high school students. To assess the effectiveness of the program, struggling students were randomly selected into treatment and control groups. A pre-test was given to both groups before the start of the program. A post-test assessing the same skills was given after the end of the program. The study team determined the effectiveness of the program by comparing the average change in pre- and post-test scores between the two groups....
Mr. Conway is an elementary school teacher who is a follower of Carl Rogers and believes...
Mr. Conway is an elementary school teacher who is a follower of Carl Rogers and believes that unconditional positive regard leads to psychological health and positive behavior. Mr. Conway obtains a sample of 40 children from other teachers' classrooms. At his school and through home observation, he categorizes each child as receiving unconditional positive regard 1) frequently, 2) sometimes and 3) rarely. He then has each child's teacher classify the child as behavior problem or not. The following summarizes the...
A high school teacher is interested to compare the average time for students to complete a...
A high school teacher is interested to compare the average time for students to complete a standardized test for three different classes of students.        The teacher collects random data for time to complete the standardized test (in minutes) for students in three different classes and the dataset is provided below.     The teacher is interested to know if the average time to complete the standardized test is statistically the same for three classes of students. Use a significance level...
Lying to a teacher. One of the questions in a survey of high school students asked...
Lying to a teacher. One of the questions in a survey of high school students asked about lying to teachers. The following table gives the number of students who said that they lied to a teacher as least once during the past year, classified by sex: Sex Sex Lied as least once Male Female Yes 3,228 10,295 No 9,659 4,620 A. Add the marginal totals to the table B. Calculate appropriate percents to describe the results of this question C....
Andy teaches high school math for the Metro School District. In 2018, she has incurred the...
Andy teaches high school math for the Metro School District. In 2018, she has incurred the following expenses associated with her job: Noncredit correspondence course on history $ 800 Teaching cases for classroom use $1,900 Andy’s employer does not provide any funding for the correspondence course or teaching cases. Use an available tax service to identify the amount she can deduct, if any, as a “For AGI” deduction and the amount, if any, as a “From AGI” deduction. Prepare a...
A teacher was interested in the mathematical ability of graduating high school seniors in her state....
A teacher was interested in the mathematical ability of graduating high school seniors in her state. She gave a 32-item test to a random sample of 15 seniors with the following results: mean = 1270, and standard deviation = 160. Find the 95% confidence interval and write a sentence describing what it means. Show your work.
Dolores used to work as a high school teacher for $40,000 per year but quit in...
Dolores used to work as a high school teacher for $40,000 per year but quit in order to start her own catering business. To invest in her factory, she withdrew $20,000 fromher savings, which paid 5 percent interest, and borrowed $30,000 from her uncle, whom she pays 4 percent interest per year. Last year she paid $25,000 for ingredients and had revenue of $60,000. She asked Louis the accountant and Greg the economist to calculate her profit for her. Explicit...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT