1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
vichka [17]
4 years ago
15

For this project you will write a Java program that will run a simple math quiz. Your program will generate two random integers

between 1 and 20 and then ask a series of math questions. Each question will be evaluated as to whether it is the right or wrong answer. In the end a final score should be reported for the user. (See below for how to generate random numbers).Following the instructions from Closed Lab 01, create a new folder named FunWithBranching and a new Java program in that folder named FunWithBranching.java for this assignment.Sample Ouptut This is a sample transcript of what your program should do. Values in the transcript in BOLD show the user inputs. So in this transcript the user inputs 33, Jeremy, 24, -16, 80 and 0 - the rest is output from the program.Enter a random number seed: 33Enter your name: JeremyHello Jeremy!Please answer the following questions:4 + 20 = 24Correct!4 - 20 = -16Correct!4 * 20 = 80Correct!4 / 20 = 0Correct!4 % 20 = 4Correct!You got 5 correct answers!That's 100.0%!Your code will behave differently based on the random number seed you use and the answers provided by the user. Here is a second possible execution of this code. As before, values in BOLD are provided by the user - in this case 54, Bob, 8, 32, 8 and 1 - the rest is the output of the program when you run it.Enter a random number seed: 54Enter your name: BobHello Bob!Please answer the following questions:20 + 12 = 8Wrong!The correct answer is: 3220 - 12 = 32Wrong!The correct answer is: 820 * 12 = 8Wrong!The correct answer is: 24020 / 12 = 1Correct!20 % 12 = 8Correct!You got 2 correct answers!That's 40.0%!

Computers and Technology
1 answer:
ElenaW [278]4 years ago
3 0

Answer:

Here is the JAVA program:

import java.util.Scanner;   //to accept input from user

public class FunWithBranching {   //class name

   public static void main(String[] args) {  //start of main function

   Scanner input = new Scanner(System.in);  //creates Scanner object

   System.out.print("Enter your name: "); //prompts user to enter name

   String userName = input.nextLine();  //stores the name

   System.out.print("Welcome " + userName + "! Please answer the following questions:\n");   //prompts user to answer the questions

   int randomOperand1 =  (int)(20 * Math.random()) + 1;  //generates random number for operand 1 (1-20)

   int randomOperand2 =  (int)(20 * Math.random()) + 1;  //generates random number for operand 2 (1-20)

   int randomAdd = randomOperand1 + randomOperand2;  //performs addition of two random numbers and stores result in randomAdd

   int randomMul = randomOperand1 * randomOperand2;  //performs multiplication of two random numbers

   int randomDiv = randomOperand1 / randomOperand2;  //performs division of two random numbers

   int randomMod = randomOperand1 % randomOperand2;  //performs modulo of two random numbers

   int correctAns = 0;  //stores number of correct answers

   System.out.print(randomOperand1 + " + " + randomOperand2 + " = ");  //displays random number + random number

   int AdditionGuess = input.nextInt();   //reads the answer of addition from user and stores it in AdditionGuess

   if (AdditionGuess == randomOperand1 + randomOperand2) {  //if the user answer is correct

   System.out.println("Correct!");  //displays correct

   correctAns++;  //adds 1 to the count of correctAns every time the user gives correct answer of addition

 } else {  //if user answer is incorrect

   System.out.println("Wrong!");  //displays wrong

   System.out.println("The correct answer is " + randomAdd);   }  //displays the correct answer of addition

   System.out.print(randomOperand1 + " * " + randomOperand2 + " = ");  //displays random number * random number  

   int MultiplicationGuess = input.nextInt();   //reads the answer of multiplication from user and stores it in MultiplicationGuess

 if (MultiplicationGuess == randomOperand1 * randomOperand2) {  //if the user answer is correct

     System.out.println("Correct!");  //displays correct

     correctAns++;  //adds 1 to the count of correctAns every time the user gives correct answer of multiplication

 }else{  //if user answer is incorrect

       System.out.println("Wrong!");  //displays wrong

       System.out.println("The correct answer is " + randomMul);   }  //displays the correct answer of multiplication

   System.out.print(randomOperand1 + " / " + randomOperand2 + " = ");  //displays random number / random number  

   int DivisionGuess = input.nextInt();   //reads the answer of division from user and stores it in DivisionGuess

   if (DivisionGuess == randomOperand1 / randomOperand2) {  //if the user answer is correct

       System.out.println("Correct!");  //displays correct

       correctAns++;   //adds 1 to the count of correctAns every time the user gives correct answer of division

       }else{  //if user answer is incorrect

           System.out.println("Wrong!");  //displays wrong

           System.out.println("The correct answer is " + randomDiv);     }  //displays the correct answer of division

      System.out.print(randomOperand1 + " % " + randomOperand2 + " = ");  //displays random number % random number  

       int ModGuess = input.nextInt();  //reads the answer of modulo from user and stores it in ModGuess

           if (ModGuess == randomOperand1 % randomOperand2) {  //if the user answer is correct

           System.out.println("Correct!");  //displays correct

           correctAns++;   //adds 1 to the count of correctAns every time the user gives correct answer of modulo

           }else{  //if user answer is incorrect

               System.out.println("Wrong!");  //displays wrong

               System.out.println("The correct answer is " + randomMod);    }  //displays the correct answer of modulo

double percentage = correctAns * 25;   //computes percentage

System.out.println("You got " + correctAns + " correct answers.");   //display number of correct answers given by user

System.out.println("That's " + percentage + "%!");         }  } //displays percentage

Explanation:

The program is well explained in the comments mentioned with each line of code. The screenshot of the output is attached.

You might be interested in
What are some things all boomers say.
lilavasa [31]

Answer: the last one ;)

Explanation:

6 0
3 years ago
Read 2 more answers
Code written by computer programmers is translated to binary code, so computers can understand the instructions. True or False
san4es73 [151]

Answer:

True

Explanation:

High-level languages such as Java, C++, Ruby, Python, etc need to be translated into binary code so a computer can understand it, compile and execute them.

Machine language is the only language that is directly understood by the computer because it is written in binary code. But writing codes in Machine Language is tiring, tedious, and obsolete as no one really uses it anymore.

7 0
3 years ago
In a particular factory, a team leader is an hourly paid production worker who leads a small team. In addition to hourly pay, te
Ray Of Light [21]

Answer:

#include <iostream>

#include <iomanip>

#include <string>

using namespace std;

class Employee

{

private:

string name; // Employee name

string number; // Employee number

string hireDate; // Hire date

public:

Employee()

{ name = ""; number = ""; hireDate = ""; }

Employee(string aName, string aNumber, string aDate)

{ name = aName; number = aNumber; hireDate = aDate; }

void setName(string n)

{ name = n; }

void setNumber(string num)

{ number = num; }

void setHireDate(string date)

{ hireDate = date; }

string getName() const

{ return name; }

string getNumber() const

{ return number; }

string getHireDate() const

{ return hireDate; }

};

class ProductionWorker : public Employee

{

private:

int shift; // The worker's shift

double payRate; // The worker's hourly pay rate

public:

ProductionWorker() : Employee()

{ shift = 0; payRate = 0.0; }

ProductionWorker(string aName, string aNumber, string aDate,

int aShift, double aPayRate) : Employee(aName, aNumber, aDate)

{ shift = aShift; payRate = aPayRate; }

void setShift(int s)

{ shift = s; }

void setPayRate(double r)

{ payRate = r; }

int getShiftNumber() const

{ return shift; }

string getShiftName() const

{ if (shift == 1)

return "Day";

else if (shift == 2)

return "Night";

else

return "Invalid";

}

double getPayRate() const

{ return payRate; }

};

class TeamLeader : public ProductionWorker

{

private:

double monthlyBonus; // Monthly bonus amount

double requiredTraining; // Required training hours

double completedTraining; // Completed training hours

public:

};

void displayInfo(ProductionWorker);

void displayInfo(TeamLeader);

int main()

{

ProductionWorker pw("John Jones", "123", "1/1/2006", 2, 18.00);

displayInfo(pw);

/* remove the comment to enable

TeamLeader leader("John Jones", "123", "1/1/2006", 2, 18.00,

500.0, 20.0, 12.5);

displayInfo(leader);

*/

return 0;

}

void displayInfo(ProductionWorker e)

{

cout << setprecision(2) << fixed << showpoint;

cout << "Name: "

<< e.getName() << endl;

cout << "Employee number: "

<< e.getNumber() << endl;

cout << "Hire date: "

<< e.getHireDate() << endl;

cout << "Shift: "

<< e.getShiftName() << endl;

cout << "Shift number: "

<< e.getShiftNumber() << endl;

cout << "Pay rate: "

<< e.getPayRate() << endl;

}

void displayInfo(TeamLeader e)

{

/* enable this section to print

cout << setprecision(2) << fixed << showpoint;

cout << "Name: "

<< e.getName() << endl;

cout << "Employee number: "

<< e.getNumber() << endl;

cout << "Hire date: "

<< e.getHireDate() << endl;

cout << "Shift: "

<< e.getShiftName() << endl;

cout << "Shift number: "

<< e.getShiftNumber() << endl;

cout << "Pay rate: "

<< e.getPayRate() << endl;

cout << "Monthly bonus: $"

<< e.getMonthlyBonus() << endl;

cout << setprecision(1);

cout << "Required training hours: "

<< e.getRequiredTraining() << endl;

cout << "Completed training hours: "

<< e.getCompletedTraining() << endl;

*/

}

Hope it helps.

8 0
3 years ago
what will be the Trade discount and Invoice Amount if the List Price is $400.00 and the Trade Discount Rate is 85%
V125BC [204]

Answer:

$340

Explanation:

you have to multiply $400 bu 85% to get $340

4 0
3 years ago
Write some general rules used while chatting​
Tpy6a [65]

Answer:

Well,

Explanation:

Well, this topic is a very broad one and I'm not exactly sure what kind of information you' like provided, but I got a few ideas in mind.

While chatting online, a big letdown for most people is if you curse and especially if you do it excsessivley. Doing so will make you seem toxic and most people would just try to avoid you.

Talking about toxicity, being a toxic person online is petty and quite pathetic. Gloating and raising your ego won't make you any more of a liekable person online, or even in the real world.

Last but not least, another general rule of online chatting is to make sure you know who you're talking too. Talking to strangers online can be very dangerous, in some cases leading to things like pedophilia etc.

3 0
3 years ago
Other questions:
  • Look at the following array definition:
    11·1 answer
  • Crowdsourcing is:
    5·1 answer
  • Hw to gain more knowledge ​
    6·1 answer
  • Which of the following is the java comparison operator for "not equal to"
    10·1 answer
  • Define the missing method. licenseNum is created as: (100000 * customID) + licenseYear. Sample output: Dog license: 77702014
    14·1 answer
  • ZeroIt is a function that takes one argument and returns no value. The argument is a pointer to int. The function stores the val
    15·1 answer
  • Write a program that inputs a time from the console. The time should be in the format "HH:MM AM" or "HH:MM PM". Hours may be one
    6·1 answer
  • Write a simple hello world program in python 3
    8·2 answers
  • Why do designers use tonal shading?
    10·2 answers
  • . Write the advantages and disadvantages of CLI?
    14·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!