I have a code that works fine but I'd like to break it up into three different classes. I'm not good with returning and instantiating classes.
I've written a pseudocode based on my understanding of creating classes but honestly don't know how to break it down.
here's my pseudocode
/************************************************************************
*BEGIN Create Test
* Set num correct to 0
* FOR (Each question to generate)
* create single digit random number
* create second single digit random number
* display equation and prompt user for answer
* IF (user's answer is correct)
* display "correct" message
* add 1 num correct
* ELSE
* display "incorrect message
* END IF
* END FOR
* Calculate test score
* Return test score
*END Create Test
*************************************************************************/
/********************************************************
*
* BEGIN Display Menu
* Display user menu
* prompt user for selection
* return user selection
* END Display Menu
*
*********************************************************/
/************************************************************************
*
*BEGIN Main
* call menu method to return user's choice menuInput for user
* WHILE menuInput is not QUIT
* Set number correct to 0
* Call method to create test and return test score
* Clear screen
* Display test score
* call menu method to return user's choice menuInput for user
* END IF
*END Main
**************************************************************************/
import java.util.Random;
public class MathTest
{
public static void main (String [] args)
{
//local constants
final int RANGE = 10; //# of diff values to create
final int NUM_QUESTIONS = 5;
final int QUIT = 2;
//local variables
int num1; // 1st number
int num2; // 2nd number
int answer; // answer to the addition problem
int userSelection; // user selects to take test or to quit
int numCorrect; // number of correct answers
float testScore; // test score of user in percentage
Random gen = new Random(); // Random class to generate random numbers
Library myLib = new Library ();
//Display test menu and input user choice
System.out.print("\n\n Simple Math Tests Made Easy\n\n" +
" 1. Take a Test\n" +
" 2. Quit\n\n" +
" Enter Choice: ");
userSelection = Keyboard.readInt();
//WHILE (user selection is take test)
while(userSelection != QUIT)
{
//Clear the screen
myLib.clrscr();
//Init number correct to 0
numCorrect = 0;
//FOR (Each of the 5 questions)
for (int count = 0; count < NUM_QUESTIONS; count++)
{
//Generate two random singal digit numbers
num1 = gen.nextInt(RANGE);
num2 = gen.nextInt(RANGE);
//Display the equation and input user answer
System.out.print("\n\n\n\n\t\t\t\t\t\t\t\t" +
num1 + " + " + num2 + " = ");
answer = Keyboard.readInt();
//IF (answer is correct)
if (answer == num1 + num2)
{
// Display Correct
System.out.println ( " CORRECT " );
//Add 1 to number correct
numCorrect++;
}
// ELSE Display answer is Incorrect
else
{
System.out.println ( " INCORRECT " );
}
//Pause the Screen
myLib.pause();
//Clear the screen
myLib.clrscr();
}
// calculate the test score average
testScore = ( (float) numCorrect / NUM_QUESTIONS) * 100;
//Output the results
System.out.print("\n\n RESULTS OF THE TEST \n\n" +
" " +
" " +
" %" + testScore );
System.out.print("\n\n ");
//Pause the Screen
myLib.pause();
//Clear the screen
myLib.clrscr();
//Display test menu and input user choice
System.out.print("\n\n Simple Math Tests Made Easy\n\n" +
" 1. Take a Test\n" +
" 2. Quit\n\n" +
" Enter Choice: ");
userSelection = Keyboard.readInt();
}
} //end main method
} //end MathTest
I've written a pseudocode based on my understanding of creating classes but honestly don't know how to break it down.
here's my pseudocode
/************************************************************************
*BEGIN Create Test
* Set num correct to 0
* FOR (Each question to generate)
* create single digit random number
* create second single digit random number
* display equation and prompt user for answer
* IF (user's answer is correct)
* display "correct" message
* add 1 num correct
* ELSE
* display "incorrect message
* END IF
* END FOR
* Calculate test score
* Return test score
*END Create Test
*************************************************************************/
/********************************************************
*
* BEGIN Display Menu
* Display user menu
* prompt user for selection
* return user selection
* END Display Menu
*
*********************************************************/
/************************************************************************
*
*BEGIN Main
* call menu method to return user's choice menuInput for user
* WHILE menuInput is not QUIT
* Set number correct to 0
* Call method to create test and return test score
* Clear screen
* Display test score
* call menu method to return user's choice menuInput for user
* END IF
*END Main
**************************************************************************/