Question:
Java Code Help Math.Random?
Ryroan
2013-01-21 13:08:27 UTC
I have a code I need (I am a school student and before you say figure it out on your own, stop while you can.) and I am Stuck. I need to make 4 modifications to this code and I can't seem to do any of them.

The I am Attempting to Make:

/* Name: Math.Random Mods
Author: Ryroan
Date: January 21st 2013 */


import javax.swing.*;

public class Summative2
{
public static void main (String args[])
{

int value;
int value2,Answer;
int value3;
int value4;
int counter;
int input;
String word;
String word2;
String output = "", output1 = "";

JOptionPane.showMessageDialog (null, "Welcome. Press OK to begin", "Random Numbers Game", JOptionPane.QUESTION_MESSAGE);
for (int counter2 = 1 ; counter2 <= 10 ; counter2++)

{

value = 1 + (int) (Math.random () * 9);
value2 = 1 + (int) (Math.random () * 9);

do

{
output = value + " * " + value2;
output1 = JOptionPane.showInputDialog ("What is " + output + "?");
input = Integer.parseInt (output1);
Answer = value*value2;
value3 = 1 + (int) (Math.random() * 4);
{

switch(value3)
{
case 1:
word = "Awesome";
break;

case 2:
word = "Perfect";
break;
case 3:
word = "Flawless";
break;
case 4:
word = "YOU'RE UNSTOPPABLE";
break;
default:
break;
}
}

value4 = 1 + (int) (Math.random() * 3);
{
switch(value4)
{
case 1:
word2 = "Try Again";
break;

case 2:
word2 = "Sorry";
break;
case 3:
word2 = "Don't give up.";
break;
case 4:
word2 = "Durrrrrr";
break;
default:
break;

}
}

if (input != Answer)
JOptionPane.showMessageDialog (null, "NOPE", "FAIL", JOptionPane.ERROR_MESSAGE);

else
JOptionPane.showMessageDialog (null, "CORRECT", "PASS", JOptionPane.INFORMATION_MESSAGE);

}
while (input != Answer);
}

System.exit (0);
}
}


What The Mods need to be:
Modification #1: Modify the program from assignment 13.Random Numbers so the various comments are printed for each correct answer and each incorrect answer as follows:

Responses to a correct answer
Very good!
Excellent!
Nice work!
Keep up the good work!

Responses to an incorrect answer
No. Please try again.
Wrong. Try once more.
Don’t give up.
Durrrrrr.

Use random-number generation to choose a number from 1 to 4 that will be used to select an appropriate response to each answer. Use a switch structure to issue the responses.
More sophisticated computer-aided instruction systems monitor the student’s performance over a period of time. The decision to begin a new topic is often based on the student’s success with previous topics.

Modification #2: Modify the program to count the number of correct and incorrect responses typed by the student. After the student types 10 answers, your program should calculate the percentage of correct responses. If the percentage is lower than 75%, print “Please ask your instructor for extra help” and reset the program.

Modification #3: Modify the program to allow the user to enter a grade-level capability. A grade level of 1 means to use only single-digit numbers in the problems, and a grade level of 2 means to use numbers as large as two digits.

Modification #4: Modify the program to allow to user to pick the type of arithmetic problems he or she wishes to study. An option of 1 means addition problems only, 2 means subtraction problems only, 3 means multiplication problems only, 4 means division problems only and 5 means to intermix randomly problems of all these types


All help is very much appreciated.
Three answers:
Robert
2013-01-21 14:34:55 UTC
Your work is good, and I did not delete anything (rather, added things ... mainly up the top where you get // Mod ... code). Good luck with it all. This is third and last take (had to delete code this time) ... so you will have to reinstate your switch statement method and your Mod 4 instructions (hint: in welcome window you could ask about Mod4 and Mod3 aspects)!



/* Name: Math.Random Mods

Author: Ryroan

Date: January 21st 2013 */





import javax.swing.*;



public class Summative2

{

public static void main (String args[])

{

// Mod 1 below

String[] okwords = {"Very good!", "Excellent!", "Nice work!", "Keep up the good work!"};

String[] wrongwords = {"No. Please try again.", "Wrong. Try once more.", "Don’t give up.", "Durrrrrr."};



// Mod 2 below

Double acnt=0.0;

Double okpreper=0.0;

String qualitysuffix = "";



// Mod 3,4 below

int typeofq = 0;

int gradeofquestion = 0;

String[] oper = {"", "+", "-", "*", "/", "+-*/"};

String functionalitysuffix = " (prefix your answer with + to increase the difficulty of questions, suffix with sign + or * or / or - or ? to limit your answer to these types of questions)";



int value;

int value2,Answer;

int value3;

int value4;

int counter;

int input;

String word;

String word2;

String output = "", output1 = "";



JOptionPane.showMessageDialog (null, "Welcome. Press OK to begin", "Random Numbers Game", JOptionPane.QUESTION_MESSAGE);

for (int counter2 = 1 ; counter2 <= 10 ; counter2++)



{

if (gradeofquestion == 0) {

value = 1 + (int) (Math.random () * 9);

value2 = 1 + (int) (Math.random () * 9);

} else {

value = (1 + (int) (Math.random () * 9)) + (1 + (int) (Math.random () * 9)) * 10;

value2 = (1 + (int) (Math.random () * 9)) + (1 + (int) (Math.random () * 9)) * 10;

}



do



{

if (typeofq == 5) typeofq = - (1 + (int) (Math.random () * 4));

if ((Math.abs(typeofq) == 2 || Math.abs(typeofq) == 4) && value < value2) {

Answer = value2;

value2 = value;

value = Answer;

}

if (Math.abs(typeofq) == 1) {

Answer = value + value2;

} else if (Math.abs(typeofq) == 2) {

Answer = value - value2;

} else if (Math.abs(typeofq) == 3) {

Answer = value * value2;

} else if (Math.abs(typeofq) == 4) {

Answer = value / value2;

value = Answer * value2;

} else {

Answer = value * value2;

}

output = value + " " + (oper[Math.abs(typeofq)] + "*").substring(0,1) + " " + value2 + functionalitysuffix;

if (typeofq < 0) typeofq = 5;

output1 = JOptionPane.showInputDialog ("What is " + output + "?");

if (("@" + output1).indexOf("@+") != (0 - 1)) {

gradeofquestion = 1;

functionalitysuffix = " (suffix with sign + or * or / or - or ? to limit your answer to these types of questions)";

if (output1.indexOf("-") != (0 - 1)) {

typeofq = 2;

} else if (output1.indexOf("*") != (0 - 1)) {

typeofq = 3;

} else if (output1.indexOf("/") != (0 - 1)) {

typeofq = 4;

} else if (output1.indexOf("?") != (0 - 1)) {

typeofq = 5;

} else if (output1.substring(1).indexOf("+") != (0 - 1)) {

typeofq = 1;

}

} else if (output1.indexOf("+") != (0 - 1)) {

typeofq = 1;

} else if (output1.indexOf("-") != (0 - 1)) {

typeofq = 2;

} else if (output1.indexOf("*") != (0 - 1)) {

typeofq = 3;

} else if (output1.indexOf("/") != (0 - 1)) {

typeofq = 4;

} else if (output1.indexOf("?") != (0 - 1)) {

typeofq = 5;

}

input = Integer.parseInt (output1.replace("+","").replace("*", "").replace("-", "").replace("?", "").replace("/", ""));

//Answer = value*value2;



acnt = acnt + 1.0;

if (input != Answer) {

if (acnt >= 9.999 && ((okpreper / acnt) * 100.0) < 74.999) {

qualitysuffix = " Please ask your instructor for extra help";

} else {

qualitysuffix = "";

}

JOptionPane.showMessageDialog (null, wrongwords[(int) (Math.random() * 4)], "FAIL", JOptionPane.ERROR_MESSAGE);

} else {

okpreper = okpreper + 1.0;

if (acnt >= 9.999 && ((okpreper / acnt) * 100.0) < 74.999) {

qualitysuffix = " Please ask your instructor for extra help";

} else {

qualitysuffix = "";

}

JOptionPane.showMessageDialog (null, okwords[(int) (Math.random() * 4)], "PASS", JOptionPane.INFORMATION_MESSAGE);

}

if (qualitysuffix != "") JOptionPane.showMessageDialog (null, qualitysuffix, "Random Numbers Game", JOptionPane.QUESTION_MESSAGE);



}

while (input != Answer);

}



System.exit (0);

}

}
anonymous
2016-03-09 06:46:05 UTC
This is plain Java source code. The import statements simply refer to other packages/classes that are referenced in the program. "The "public class ArtPart rxtends JApplet{" defines the class (name and type (Applet). The rest of the code is missing. The line "boolean loa" should have a semi-colon at the end of the line. Having this semi-colon missing will surely give you compilation errors. Once the statement has been corrected it will define a variable (loa) which can have the value either true or fals and you are able to set this value any time while the applet is running. At the end of the class, there should be a closing curly brace to match the opening one at the end of JApplet{ ---> } Unless you have left out the rest of the code deliberatly this piece of code won't actually do anything.
anonymous
2016-06-06 06:07:07 UTC
Working for property is a great deal and not a good deal of people get to do it even if they want this.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...