Question

In: Computer Science

This problem will give you hands-on practice with the following programming concepts: • All programming structures...

This problem will give you hands-on practice with the following programming concepts: • All programming structures (Sequential, Decision, and Repetition) • Methods • Random Number Generation (RNG) Create a Java program that teaches people how to multiply single-digit numbers. Your program will generate two random single-digit numbers and wrap them into a multiplication question. The program will provide random feedback messages. The random questions keep generated until the user exits the program by typing (-1). For this problem, multiple methods must be used. You can’t put all the code in one method. For example, you can create a void method to generate a random message for correct answers and another method to generate a message for incorrect answers. You can also create a method to generate the random questions, etc. Name your class as Q4Multiplication.java The sample run below shows you the behavior of this program:

For this question, I see an answer already posted on site but when I was studying it, I found out that the question says use multiple methods, Everything in the posted answer is crammed into the main method and so how do I break it into multiple methods??

Solutions

Expert Solution

Following is the program for the same in JAVA

import java.util.Random;
import java.util.Scanner;

public class Q4Multiplication {
   public static int randomNumber() {
       Random rand = new Random();
       int n = rand.nextInt(9) + 1;
       return n;
   }
   // Matching whether the solution matches with input
   public static void matchAnswerWithSolution(int userInput, int solution) {
       if(userInput == solution) {
           System.out.println("Correct answer");
       }else {
           System.out.println("Incorrect answer" + "\nCorrect answer is: " + (solution));
       }
   }
   // Taking the input
   public static int scanInt() {
       Scanner scan = new Scanner(System.in);
       return scan.nextInt();
   }
   // Printing the question by getting the random numbers
   // and returning the solution of multiplication from the function
   public static int printQuestion() {
       int randomNumber1 = randomNumber();
       int randomNumber2 = randomNumber();
       System.out.println("Enter the multiplication of: " + randomNumber1 + " and " + randomNumber2);
       return randomNumber1*randomNumber2;
   }
   // Main function
   public static void main(String args[]) {
       boolean enter = true;
       while(enter) {
           int solution = printQuestion();
           int userInput = scanInt();
           if(userInput == -1) {
               enter = false;
               break;
           }else {
               matchAnswerWithSolution(userInput, solution);
           }
       }
   }
}

Following is the snippet of the output.

That was a nice question to answer
Friend, If you have any doubts in understanding do let me know in the comment section. I will be happy to help you further.
Thanks


Related Solutions

Question Objective: The objective of this lab exercise is to give you practice in programming with...
Question Objective: The objective of this lab exercise is to give you practice in programming with one of Python’s most widely used “container” data types -- the List (commonly called an “Array” in most other programming languages). More specifically you will demonstrate how to: Declare list objects Access a list for storing (i.e., writing) into a cell (a.k.a., element or component) and retrieving (i.e., reading) a value from a list cell/element/component Iterate through a list looking for specific values using...
In this assignment, you will practice solving a problem using object-oriented programming and specifically, you will...
In this assignment, you will practice solving a problem using object-oriented programming and specifically, you will use the concept of object aggregation (i.e., has-a relationship between objects). You will implement a Java application, called MovieApplication that could be used in the movie industry. You are asked to implement three classes: Movie, Distributor, and MovieDriver. Each of these classes is described below. The Movie class represents a movie and has the following attributes: name (of type String), directorsName (of type String),...
Programming Assignment #3: SimpleFigure and CirclesProgram Description:This assignment will give you practice with value...
Programming Assignment #3: SimpleFigure and CirclesProgram Description:This assignment will give you practice with value parameters, using Java objects, and graphics. This assignment has 2 parts; therefore you should turn in two Java files.You will be using a special class called DrawingPanel written by the instructor, and classes called Graphics and Color that are part of the Java class libraries.Part 1 of 2 (4 points)Simple Figure, or your own drawingFor the first part of this assignment, turn in a file named...
Hands-On Assignments are designed to foster independent thinking and problem solving programming skills. This activity is...
Hands-On Assignments are designed to foster independent thinking and problem solving programming skills. This activity is closely guided by the course instructor. Students are encourages to ask instructor any questions related to this challenge. Similar to the course Exercises, this activity also has a Base Code. The initially given Base Code to Start with this exercise: --------------Start---------------- guess.py   (also, see starters in the course FILES/StudentFiles/Exercise Starter Files) --------------End ---------------- Prompt Task to Accomplish The Base Code implements “guessing” of one number....
The purpose of this C++ programming assignment is to practice using an array. This problem is...
The purpose of this C++ programming assignment is to practice using an array. This problem is selected from the online contest problem archive, which is used mostly by college students worldwide to challenge their programming ability and to prepare themselves for attending programming contests such as the prestige ACM International Collegiate Programming Contest. For your convenience, I copied the description of the problem below with my note on the I/O and a sample executable. Background The world-known gangster Vito Deadstone...
Background Hands-On Assignments are designed to foster independent thinking and problem solving programming skills. This activity...
Background Hands-On Assignments are designed to foster independent thinking and problem solving programming skills. This activity is closely guided by the course instructor. Students are encourages to ask instructor any questions related to this challenge. Similar to the course Exercises, this activity also has a Base Code. The initially given Base Code to Start with this assignment: This assignment is based on the project 6.9 of the course book. --------------Start---------------- buttondemo.py      breezypythongui.py --------------End ---------------- --------------------------------------- Prompt Task to Accomplish...
Objective: The purpose of this programming assignment is to practice using STL containers. This problem is...
Objective: The purpose of this programming assignment is to practice using STL containers. This problem is selected from the online contest problem archive (Links to an external site.), which is used mostly by college students world wide to challenge their programming ability and to prepare themselves for attending programming contests such as the prestige ACM International Collegiate Programming Contest (Links to an external site.). For your convenience, I copied the description of the problem below with my note on the...
Summarize the strategic hands for problem-solving, and give an example of the meaning of each one...
Summarize the strategic hands for problem-solving, and give an example of the meaning of each one which of the following best summarizes the strategic hand there may be more than one strategy and provide an example of the meaning A there may be several strategies to solving the problem, however only one of them will find the correct answer. For example, there is more than one strategy to stop exit squared plus 3X +2 = 0 however, only one of...
The assignment: C++ program or Java You need to use the following programming constructs/data structures on...
The assignment: C++ program or Java You need to use the following programming constructs/data structures on this assignment. 1. A structure named student which contains:   a. int ID; student id; b. last name; // as either array of char or a string c. double GPA;    2. An input file containing the information of at least 10 students. 3. An array of struct: read the student information from the input file into the array. 4. A stack: you can use the...
Expected Return and Standard Deviation / This problem will give you some practice calculating measures of...
Expected Return and Standard Deviation / This problem will give you some practice calculating measures of prospective portfolio performance. There are two assets and three states of the economy State of Economy Probability of State of Economy Rate of Return If State Occurs Stock A Stock B Recession .20 -.15 .20 Normal .50 .20 .30 Boom .30 .60 .40 Expected Returns : Stock A = (.20 * -.15) + (0.50 * .20) + (.30 * .60) = .25 Stock B...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT