You are on page 1of 20

CSCI 1100 November 2013 Project Part 2 Names and IDs: Negin Sauermann Section: Bonnie or Denis

Due Date: Presentation on December 3rd


1 2 Declaration: Please complete this declaration This document is entirely my Yes/no own work. I obtained help to complete this Yes/no. Give Detail? document. Asked a friend about how to implement exception handling through try and catch.

This document contains some material copied or cut and pasted from the internet or another document or file or program.

Yes/no. Give detail? Anything explaining the other programs has a link to them. I added a reference link for the tutorials on exception handling and classes.

Your task is to complete this report using Word and JGrasp and to submit the complete word document on Moodle. Acknowledge any help that you obtained from friends, the Learning Centre or the TAs in the table above.

CSCI 1100, Project, page 1

Introduction My Program
There are a vast variety of programs available on the internet to help children with arithmetic skills. In part 1, I compiled three programs that have stood out through the criterias of how interactive they are, how simple and straightforward the program is, as well as the replay value. I used this as a basis for my own program to help children with arithmetic skills. I stressed in my program that continuous play and replay value are key to understanding and learning arithmetic skills. I used many methods, classes, for loops, while loops, arrays, timer, and try catch exception handling to complete my program. Prior to explaining the program, it must be noted that the work was not split as I decided to work on it individually.

Program Methods
Methods were used extensively to ensure that the program did not have a lot of repeating unnecessary code. It also helped organize my program more efficiently and through this program I truly learned how effective methods were due to the sheer size of the program and how it could have been much larger and redundant. I will be going through these methods in a bit more depth as they essentially explain most of my program.
public int getAnswer(char type,int num1, int num2)

This method was used to generate the correct answers which would compare with the users input. It uses if statements to check between the arithmetic type. The return type was set as int as my entire program deals solely with int in regards to the questions and answers (more of this will be explained in getDivisible method.)
if(type =='+') //addition { return num1 + num2; } else if(type == '-'){ //subtraction return num1 - num2; } else if(type == '/'){ //division return num1 / num2; } else { } } public int compareAnswer(int userAnswer, int answer){ //multiplication return num1*num2;

This would compare the users answer with the method getAnswer and keep a score count.
if (userAnswer == answer ) return 1; //increments score by 1 else return 0; //if user answer is incorrect score is not incremented

CSCI 1100, Project, page 2

public int getDivisibleNumber(int number,int level)

This is one of the more important methods as it is crucial to the functionality of my program. I had decided to run the arithmetic questions and answers solely using integers. This is due to doubles specifically in division asking for very specific inputs from the user such as 3.33333- . This method works as follows:
int divisibleNum; while(true) //using evenly divisible numbers (factors) due to dealing with integers { divisibleNum = randomGenerator.nextInt(10 * level) + 1; if(number % divisibleNum == 0) // if true, divisible number is a factor of number//evenly divisible return divisibleNum;

All the questions generated from the divisible section will be evenly divisible by checking if the numbers contain factors allowing for the user to only have to input divisible numbers.
public int getQuestion (int level,char type){

For this method I used arrays and randomGenerator (from a class, which will be explained in the next section) to generate questions based on the level selection by the user. In this method, score count is also kept. The method works as follows:
int[]num1 = new int[10]; int[]num2 = new int[10]; int[]answers = new int[10]; for (int i = 0; i < answers.length; i++){ //generates numbers from 1-10 * level (if level is 3 then 3-30) //math.pow just increases min value at levels so some questions don't repeat per level if(type != '/'){ num1[i] = randomGenerator.nextInt(10 * level) + ((int)Math.pow(2, level) - 1); num2[i] = randomGenerator.nextInt(10 * level) + ((int)Math.pow(2, level) - 1); answers[i] = getAnswer(type, num1[i], num2[i]); //checks answer from method getAnswer } else{ num1[i] = randomGenerator.nextInt(10 * level) + 1; //generates 10 random integers from 1-10 * the level (if level 3 then 3-30) num2[i] = getDivisibleNumber(num1[i], level); answers[i] = num1[i] / num2[i]; }

Most of the program is quite self-explanatory. I multiplied the questions initial numbers by the level the user selected. Therefore, if the user selected 3 the questions numbers range would change from 1-10 to 3-30. Math.pow was used to increase the min value of numbers so that some questions would not potentially repeat in higher levels. -1 was used to shift the number range from 0-9 to 1-10. However, none of this is used for the divisible section with the exception of multiplying values by the level input. This is due to the getDivisibleNumber method and how the numbers must stay factors as explained in the previous method explanation. I used a for loop to go through the array to compare the userAnswer with the compareAnswer method.

CSCI 1100, Project, page 3

for (int i = 0; i < answers.length; i++){ //loop for println questions System.out.println((i+1) + " : " + num1[i] + " " + type + " " + num2[i] + " = " ); //outputs first question, type used for (+,-.*,/) userAnswer = keyboard.nextInt(); //gets users answer and assigns it to int variable answer if ( compareAnswer(userAnswer, answers[i]) == 1 ){ //checks users answer with the correct answer at index 'i' using compareAnswer method System.out.println("You are correct!"); score++; //score increased by 1 } else System.out.println("Incorrect!" + answers [i]); //score will not increase

public int reviewQuestions(int score, int time, int level) This method is quite extensive as it covers some of continuous play and feedback. The method essentially checks the users score using an if statement such as: if(score == 0){ if (level == 1){ System.out.println("GLaDOS Suggests: review your questions!"); newLevel = 0; //keep users level the same if at lowest level } else if (level == 2){ System.out.println("GLaDOS Suggests: go down a level!"); newLevel = -1;//used to decrease users level } else if(level == 3){ System.out.println("GLaDOS Suggests: go down a level!"); newLevel = -1;//used to decrease users level } newLevel = -1;//used to decrease users level }

This works for all levels and scores. If the score is 0 and the level is anything higher than 1 the program will force the user to go down a level. This works similarly if the score is 5 or lower except it gives the user the option whether they want to decrease in level or not.
System.out.print("Do you wish to go down a level? (Y or N): "); String choice = keyboard.nextLine(); if(choice.equalsIgnoreCase("Y")) newLevel = -1; else newLevel = 0;

If the score is 8-6 the console will suggest going up a level using the same procedure as asking if they wish to go down a level. Finally, if the score is anything higher than 8 the program will force the user to go up a level unless the level is already 3, then it will prompt the user if they would like to keep playing.
else if(score > 8 && level == 3){ System.out.println("GLaDOS Suggests: I have no advice for you, you're pretty much as smart as me!");

CSCI 1100, Project, page 4

newLevel = 0; } System.out.println("Your total score is: " + score + "\n");

Program Continuous Play:


Alongside the reviewQuestions method that forces or asks a player to go down or a up a level, a while loop was used to ensure for continuous play which is what I find the most effective part of arithmetic programs as found in all 3 programs I reviewed.
while(play == true) { System.out.println("Your current level is: " + level); level += questionMaker.getQuestion(level, type); if(level == 3){ System.out.print("Do you want to play again?(Y or N) "); userInput = keyboard.nextLine(); if(userInput.equalsIgnoreCase("N") || userInput.equalsIgnoreCase("No")) play = false; } } System.out.println("Good bye!");

I used a Boolean to check for if play is true or not. While play is true, getQuestion is called. The program will never stop unless the level is 3, in the case the console will prompt the user if they would like to play again or not.

Program Extras Classes:


I was not familiar with the concept of classes so it took me awhile to understand the concept and use behind them.
public class Questions { Random randomGenerator; Scanner keyboard; public Questions() { randomGenerator = new Random(); keyboard = new Scanner(System.in); }

This class was used to avoid unnecessary code repetition. I used global variables in the class so I would not have to declare them over and over in my code. I also used the class to group all question related code (generating, answering, comparing, and creating.) As a result, my main class is just running the loop around the methods and the exception handling. Therefore, it was very easy to navigate through my code.

Program Extras Timer and Feedback:

CSCI 1100, Project, page 5

The start timer was declared at the beginning of getQuestions method. It is essentially used to time how long the child will take to solve a level of the questions.
int startTime = (int)System.currentTimeMillis();

The end timer was declared at the end of the getQuestions method. I did this so it would record the entire time the user took to answer the questions.
int endTime = (int)System.currentTimeMillis();

At the end of each 10 questions of a level, in the reviewQuestions method alongside the score count; the console will print suggestions feedback based on how long the child took.
if (time <= 30) //30 seconds System.out.println("Time: You're really fast! You did it in: " + time + " seconds!\n"); else if (time <= 60) //1 minute System.out.println("Time: You're average! You did it in: " + time + " seconds!\n"); else if (time <= 300) //5 minutes System.out.println("Time: You're kind of slow! Consider reviewing. You did it in: " + time + " seconds!\n"); else System.out.println("Time: Are you even there? You did it in: " + time + " seconds!\n");

Program Extras Exception Handling:


public static char checkType(String userInput) throws Exception{

This was a pretty tricky method as I tried to use Exception handling for the first time using try and catch; I had a fellow computer science student explain to me the process of try and catch. I used a char as my return value for the method getAnswer. The program works as follows:
if(userInput.equals("+") || userInput.equalsIgnoreCase("Addition") || userInput.equalsIgnoreCase("Add")) return '+'; else if(userInput.equals("-") || userInput.equalsIgnoreCase("Subtraction") || userInput.equalsIgnoreCase("Minus") || userInput.equalsIgnoreCase("Subtract")) return '-'; else if(userInput.equals("*") || userInput.equalsIgnoreCase("x") || userInput.equalsIgnoreCase("multiply") || userInput.equalsIgnoreCase("multiplication")) return '*'; else if(userInput.equals("/") || userInput.equalsIgnoreCase("Divide") || userInput.equalsIgnoreCase("Division")) return '/'; else{ throw new Exception("Error in input.");

CSCI 1100, Project, page 6

This method basically checks for different types of inputs the user may input when asking what type of arithmetic equation they wish to try. It accounts for different inputs such as + instead of Add or Addition instead of +. IgnoreCase is used for all cases as well. The most interesting part of the method however, is the else statement which is throwing the exception.
try{ type = checkType(userInput); } catch(Exception e) { System.out.println(e.getMessage() + " Defaulting to addition."); type = '+'; }

The catch will return a thrown error message if the user inputs anything that is not in the method such as say for example z. By using exception handling, the console will print an error and then default the program to addition. Another example of exception handling is when the user does not input a number(int) for the questions:
for (int i = 0; i < answers.length; i++){ //loop for println questions System.out.println((i+1) + " : " + num1[i] + " " + type + " " + num2[i] + " = " ); //outputs first question, type used for (+,-.*,/) try{ userAnswer = Integer.parseInt(keyboard.nextLine()); //gets users answer and assigns it to int variable answer }catch (Exception e) { System.out.println(e.getMessage() + " Defaulting answer to 0."); userAnswer = 0; } if ( compareAnswer(userAnswer, answers[i]) == 1 ){ //checks users answer with the correct answer at index 'i' using compareAnswer method System.out.println("You are correct!"); score++; //score increased by 1 } else System.out.println("Incorrect! The correct answer is: " + answers [i]); //score will not increase }

The program will print yet another error message and default the answer to 0. Integer.parseInt takes the input (e.g. if its a z) it will change it into a number. If its a number it will just keep it as that number.

CONCLUSION:
In conclusion, Ive learned quite a bit from this project. One thing I would do differently in the future is possibly add a gui or something slightly more interactive like a game in the example of the Math Invaders game I reviewed. Rather, I focused more on replay value, continuity, and exception handling instead. I learned how to use exception handling with the use of try and catch which I will revisit many times in the future as it is quite a useful feature. As well, I further practiced using methods and am much more comfortable with them and have a deeper appreciation for them. Classes are another aspect that I

CSCI 1100, Project, page 7

was introduced to and I find them extremely helpful in organizing large programs such as this project. The one feature I truly took from the other programs I reviewed is replay quality. I find that practicing over and over again is the most effective way to learn arithmetic skills. By adding a timer and a score, the user will understand how well they are doing and what areas they lacked in. The program also adjusts itself to how well the user is doing by either pushing them down or up a level or asking them if they wantr to switch levels depending on how well or poorly they are doing in that section. The program in a sense is somewhat cruel in that it does not end unless the user reaches level 3 with a score of 9 or higher. However, its quite entertaining as the user will constantly compete with oneself. Another aspect Id hope to work on in the future is possible adding a rival AI, a leaderboard or a way to graph your progress. I hope to return to this project one day and further improve it. PROGRAM Part 1 of the Project: This are can be reference to note what good and bad qualities I gathered from these programs to create my own program. PROGRAM Hippo Hotel: The first program is called Hippo Hotel Addition and Subtracting Volume 1 by SmartTutor. This program is accessible directly through the URL and uses flash. As a result, the program is quite interactive. It attempts to teach children the fundamental skills of counting forward and backwards as well as subtracting and adding. All the children must know how to do is click and follow clear instructions. The program is not confusing at all and the instructions will not be overwhelming for children. Essentially, the child will be given a list of room doors and floors. Each floor and room in the hotel is assigned a number. At first, the child will be asked to count how many rooms by simply clicking on each room number and the narrator will count alongside the child. Then, the child will be asked how many floors is between floor a and b and will simply need to click an answer out of a selection of results. The narrator will either commend the child or ask them to try again. There is also a small story revolving around the Hippo working at the hotel making the context easily approachable and fun for children. As well, the program is thoroughly animated and quite colorful.
Unfortunately, the game does not possess much replay qualities. After the first runt through there is no incentive to replay the game for further practice. A lack of a scoreboard or story progression is the basis for this. As a result, I would rank this program 3rd due to the lack of replay value, the use of simple instructions, how interactive it is and the use of visuals and animations to keep the child entertained. Source: http://www.smarttutor.com/free-resources/free-math-lessons/kindergarten-math/ Program: http://www2.smarttutor.com/player/swf/STA_add_subt_LK_V1_T1a.swf

PROGRAM Math Invaders Game: The second program is coined Math Invaders Game by kidsnumbers.com. The premise of this game was initially interesting. It takes the familiar approach of the famous Space Invaders game but adds arithmetic equations to the program. The game is quite fast paced and it follows simple addition concepts. There is also a point system or scoreboard to reward the child, allowing the game to have replay value unlike Hippo Hotel. Initially, the program was quite confusing. You must use the keyboard left and right arrows to move the ship. Spacebar is used to fire the number. The child must match the number on the alien with the number on the side for the alien to dissolve. If the aliens break the houses the game is over. Over time the aliens will continuously shoot at the houses so speed is the context of the game. This is a great premise due to the fact that the game has replay qualities. The child will feel inclined to reach a high score by completing the arithmetic equations at a faster pace. However, how one can change the shots initially number was not clear. In addition to using the left and right and spacebar keys the child must press the numbers from 0-9 to change the initial number of the shot.

CSCI 1100, Project, page 8

The initial number begins at 7 and it took me several tries to understand how to change the number. Unfortunately, this program lacks clear instructions and it would be handy to have them available at the beginning of the game or on the sidebar. Aside from that, the program is more enjoyable than Hippo Hotel and provides replay value which Hippo Hotel also lacks. I would rank this program 2nd due to the replay value and interactive qualities. However, the instruction was quite lacking and the game may be too fast paced initially. The game would also benefit from a level system allowing for the game to gradually progress in how fast paced it is. Lastly, the games visuals are quite lacking and unpolished and may deter children from using it. Source: http://www.kidsnumbers.com/addition.php Program: http://www.kidsnumbers.com/sunny-bunny-addition.php

PROGRAM: Sunny Bunny The last program is called Sunny Bunny and is also created by kidsnumbers.com. The premise of the game is similar to that of Pacman. The player must navigate through a small maze picking up carrots and progress to the next level. However, rather than having ghosts such as in Pacman to avoid, the child must avoid vegetables with the wrong answer on them. At the top of the screen an equation will show such as 0+2=?. In the map, randomly generated answers that move will appear. A right answer will also appear and the player must avoid the incorrect answer and pick up the correct answer for bonus points. Also, at the beginning of the game the instructions are shown and extremely straightforward and easy to understand unlike Math Invaders. The child must simply navigate using the left, right, down, and up arrow keys. There is also replay value in the game through the use of levels and a point system. Overall, this game is ranked 1st due to its polished visuals, straightforward instructions, and replay qualities. Source: http://www.kidsnumbers.com/addition.php Program: http://www.kidsnumbers.com/math-invaders-gamer.php

REFERENCES: I used online tutorials for Classes and exceptional handling in addition to lessons from a classmate. "Classes." The Java Tutorials. Java Oracle, n.d. Web. 3 Dec. 2013. <http://docs.oracle.com/javase/tutorial/java/javaOO/classes.html>. "Lesson: Exceptions." The Java Tutorials . Java Oracle, n.d. Web. 3 Dec. 2013. <http://docs.oracle.com/javase/tutorial/essential/exceptions/>.

APPENDIX:
import java.util.Random; //imports Random for randomly generated question import java.util.Scanner; //imports Scanner for user keyboard input public class Questions { //not going to be in a method //all classes need a constructor //a constructor is a method with the exact same name as the class itself, with no return type //global variables, can't assign values

CSCI 1100, Project, page 9

//used for efficiency so I don't have to declare them in every method Random randomGenerator; Scanner keyboard; /* * Questions class is used to generate new questions * All questions will be dealt in integers to avoid precision error with user input * in relation to using doubles. */ public Questions() { randomGenerator = new Random(); //creates new random number generator keyboard = new Scanner(System.in); //creates a new scanner called keyboard } public int getAnswer(char type,int num1, int num2) //method used to generate correct answers alongside user's typing choice { if(type =='+') //addition { return num1 + num2; } else if(type == '-'){ //subtraction return num1 - num2; } else if(type == '/'){ //division return num1 / num2; } else { } } public int compareAnswer(int userAnswer, int answer){ //compares userAnswer with correct answer, and keeps score count if (userAnswer == answer ) return 1; //increments score by 1 else return 0; //if user answer is incorrect score is not incremented } //return a number thats evenly divisible public int getDivisibleNumber(int number,int level) { int divisibleNum; while(true) //using evenly divisible numbers (factors) due to dealing with integers { divisibleNum = randomGenerator.nextInt(10 * level) + 1; if(number % divisibleNum == 0) // if true, divisible number is a factor of number//evenly divisible return divisibleNum; } } public int getQuestion (int level,char type){ //method used to generate questions based on level //make arrays of 10 spaces for 10 questions int[]num1 = new int[10]; //first number int[]num2 = new int[10]; //second number int[]answers = new int[10]; //answer for (int i = 0; i < answers.length; i++){ //goes through array //multiplication return num1*num2;

CSCI 1100, Project, page 10

if(type != '/'){ //generates numbers from 1-10 * level (if level is 3 then 3-30) //math.pow just increases min value at levels so some questions don't repeat per level num1[i] = randomGenerator.nextInt(10 * level) + ((int)Math.pow(2, level) - 1); //using -1 shifts the number range by 1 (eg 0-9 becomes 1-10) num2[i] = randomGenerator.nextInt(10 * level) + ((int)Math.pow(2, level) - 1); answers[i] = getAnswer(type, num1[i], num2[i]); //checks answer from method getAnswer } else{ num1[i] = randomGenerator.nextInt(10 * level) + 1; //generates 10 random integers from 1-10 * the level (if level 3 then 3-30) num2[i] = getDivisibleNumber(num1[i], level); //gets divisible num2 from method getDivisible answers[i] = num1[i] / num2[i]; //checks for correct answer } } int userAnswer = 0; //for user's answer to compare with getAnswer int score = 0; //used to keep track of score of the user //before we start asking questions, start a timer int startTime = (int)System.currentTimeMillis(); //gives the current system time(computer time) in milliseconds for (int i = 0; i < answers.length; i++){ //loop for println questions System.out.println((i+1) + " : " + num1[i] + " " + type + " " + num2[i] + " = " ); //outputs first question, type used for (+,-.*,/) try{ userAnswer = Integer.parseInt(keyboard.nextLine()); //gets users answer and assigns it to int variable answer }catch (Exception e) { System.out.println(e.getMessage() + " Defaulting answer to 0."); userAnswer = 0; } if ( compareAnswer(userAnswer, answers[i]) == 1 ){ //checks users answer with the correct answer at index 'i' using compareAnswer method System.out.println("You are correct!"); score++; //score increased by 1 } else System.out.println("Incorrect! The correct answer is: " + answers [i]); //score will not increase }

//stop timer, and give total time spent to user int endTime = (int)System.currentTimeMillis(); int totalTimeTaken = endTime - startTime; totalTimeTaken = totalTimeTaken / 1000; //convert milliseconds to seconds return reviewQuestions(score,totalTimeTaken,level);

CSCI 1100, Project, page 11

//reviews the users end score and determines if they should increase or decrease in level public int reviewQuestions(int score, int time, int level) { int newLevel = 0; //if score is 0 always decrease level if possible if(score == 0) { if (level == 1) { System.out.println("GLaDOS Suggests: review your questions!"); newLevel = 0; //keep users level the same if at lowest level } else if (level == 2) { System.out.println("GLaDOS Suggests: go down a level!"); newLevel = -1;//used to decrease users level } else if(level == 3) { System.out.println("GLaDOS Suggests: go down a level!"); newLevel = -1;//used to decrease users level } newLevel = -1;//used to decrease users level } //if score is 5 or less than 5 ask the user if they wish to decrease in level if possible else if (score <= 5) { if(level == 1) System.out.println("GlaDOS Suggests: consider reviewing!"); else if (level == 2) System.out.println("GLaDOS Suggests: consider reviewing or go down a level!"); else if (level == 3) System.out.println("GLaDOS: consider reviewing or go down a level!"); System.out.print("Do you wish to go down a level? (Y or N): "); String choice = keyboard.nextLine(); if(choice.equalsIgnoreCase("Y")) newLevel = -1; else newLevel = 0; } //if the user between 8 and 6 ask user if they wish to go up a level else if (score <= 8) { if(level == 1) System.out.println("GLaDos Suggests: decent work! Keep practicing!"); else if (level == 2) System.out.println("GLaDOS Suggests: not bad! Keep practicing!"); else if (level == 3) System.out.println("GLaDOS Suggests: great work! Keep practicing!"); System.out.print("Do you wish to go up a level? (Y or N): "); String choice = keyboard.nextLine(); if(choice.equalsIgnoreCase("Y")) newLevel = 1; else newLevel = 0;

CSCI 1100, Project, page 12

} //if user score is 9 or 10 force user to go up a level if possible else if(score > 8 && (level == 2 || level == 1)) { System.out.println("GlaDOS Suggests: Go up a level!"); newLevel = 1; } else if(score > 8 && level == 3) { System.out.println("GLaDOS Suggests: I have no advice for you, you're pretty much as smart as me!"); newLevel = 0; } System.out.println("Your total score is: " + score + "\n"); //prints out total time they took + additional comment if (time <= 30) //30 seconds System.out.println("Time: You're really fast! You did it in: " + time + " seconds!\n"); else if (time <= 60) //1 minute System.out.println("Time: You're average! You did it in: " + time + " seconds!\n"); else if (time <= 300) //5 minutes System.out.println("Time: You're kind of slow! Consider reviewing. You did it in: " + time + " seconds!\n"); else System.out.println("Time: Are you even there? You did it in: " + time + " seconds!\n"); return newLevel; //returns 0, 1 or -1 depending on if they go up a level, down a level, or stay the same } }

import java.util.Scanner; public class Project { public static void main (String[]args){ boolean play = true; //using boolean for while loop, default set to true so that loop can commence int level, lastLevel = -1; char type; //used to convert user input for arithmetic type to a char type String userInput; Scanner keyboard = new Scanner(System.in); Questions questionMaker = new Questions(); //makes a questions object so we can use it to make questions in questions.java/class System.out.println("What level do you wish to play? Select from 1 to 3: "); level = Integer.parseInt(keyboard.nextLine()); //using nextLine and then parse to turn it into a number so it doesn't break the next input if( level > 3){ //if user inputs anything higher than 3, program will default level to level 3 System.out.println("Level 3 is the highest level. Defaulting to level 3."); level = 3; }

CSCI 1100, Project, page 13

else if(level < 1){ //if user inputs anything lower than 1, program will default level to level 1 System.out.println("Level 1 is lowest level. Defaulting to level 1."); level = 1; } System.out.println("What type of arithmetic equation to you wish to do? \nOptions: addition(+), subtraction(-), multiplication(*), division(/). "); userInput = keyboard.nextLine(); //gets user input, using string so that user can input word, part of the word, or a symbol /*try & catch used for exception handling, if anything in the try block fails the catch block will print * an error statement and then default the type the user inputed to addition */ try{ type = checkType(userInput);//gets method check type }catch(Exception e) //e is "Error in Input" { System.out.println(e.getMessage() + " Defaulting to addition."); //get message used to print error message in 'e' type = '+'; //defaults type selection to addition } System.out.println(); //blank while(play == true) { //while play is true, getQuestion is called which allows the user to play through the randomly generated questions

System.out.println("Your current level is: " + level); // for getting new level after user completes questions, used for continuous play lastLevel = level; level += questionMaker.getQuestion(level, type); if(level == 3 && lastLevel == 3) // { System.out.print("Do you want to play again?(Y or N) "); userInput = keyboard.nextLine(); if(userInput.equalsIgnoreCase("N") || userInput.equalsIgnoreCase("No")) //ignoreCase used for error handling for user input play = false; //if user input 'N' or 'No' the program will stop } } System.out.println("Good bye!"); } //checks user input to select proper arithmetic type //accounts for multiple user input for arithmetic type and out of range input public static char checkType(String userInput) throws Exception{ if(userInput.equals("+") || userInput.equalsIgnoreCase("Addition") || userInput.equalsIgnoreCase("Add")) return '+'; else if(userInput.equals("-") || userInput.equalsIgnoreCase("Subtraction") || userInput.equalsIgnoreCase("Minus") || userInput.equalsIgnoreCase("Subtract")) return '-'; else if(userInput.equals("*") || userInput.equalsIgnoreCase("x") || userInput.equalsIgnoreCase("multiply") || userInput.equalsIgnoreCase("multiplication")) return '*'; else if(userInput.equals("/") || userInput.equalsIgnoreCase("Divide") || userInput.equalsIgnoreCase("Division")) return '/';

CSCI 1100, Project, page 14

else { throw new Exception("Error in input."); }

} }

OUTPUT:
Test 1
What level do you wish to play? Select from 1 to 3: 1 What type of arithmetic equation to you wish to do? Options: addition(+), subtraction(-), multiplication(*), division(/). add Your current level is: 1 1 : 4 + 5 = 9 You are correct! 2 : 7 + 1 = 8 You are correct! 3 : 2 + 10 = 12 You are correct! 4 : 1 + 10 = 11 You are correct! 5 : 8 + 2 = 10 You are correct! 6 : 1 + 4 = 5 You are correct! 7 : 5 + 3 = 8 You are correct! 8 : 7 + 4 = 11 You are correct! 9 : 9 + 4 = 13 You are correct! 10 : 6 + 3 = 9 You are correct! GlaDOS Suggests: Go up a level! Your total score is: 10 Time: You're really fast! You did it in: 16 seconds! Your current level is: 2 1 : 10 + 22 = 32 You are correct! 2 : 12 + 11 = 23 You are correct! 3 : 14 + 15 =

CSCI 1100, Project, page 15

29 You are correct! 4 : 13 + 17 = 30 You are correct! 5 : 16 + 4 = 20 You are correct! 6 : 10 + 6 = 16 You are correct! 7 : 18 + 11 = 29 You are correct! 8 : 21 + 12 = 33 You are correct! 9 : 8 + 6 = 14 You are correct! 10 : 20 + 18 = 38 You are correct! GlaDOS Suggests: Go up a level! Your total score is: 10 Time: You're really fast! You did it in: 22 seconds! Your current level is: 3 1 : 12 + 7 = 19 You are correct! 2 : 36 + 13 = 39 Incorrect! The correct answer is: 49 3 : 18 + 31 = 49 You are correct! 4 : 33 + 30 = 63 You are correct! 5 : 10 + 22 = 32 You are correct! 6 : 12 + 18 = 30 You are correct! 7 : 33 + 10 = 43 You are correct! 8 : 15 + 21 = 36 You are correct! 9 : 25 + 26 = 51 You are correct! 10 : 18 + 9 = 27 You are correct! GLaDOS Suggests: I have no advice for you, you're pretty much as smart as me! Your total score is: 9 Time: You're average! You did it in: 34 seconds! Do you want to play again?(Y or N) n

CSCI 1100, Project, page 16

Good bye!

Test 2
What level do you wish to play? Select from 1 to 3: 3 What type of arithmetic equation to you wish to do? Options: addition(+), subtraction(-), multiplication(*), division(/). poop Error in input. Defaulting to addition. Your current level is: 3 1 : 16 + 32 = 3 Incorrect! The correct answer is: 2 : 30 + 24 = 4 Incorrect! The correct answer is: 3 : 13 + 10 = 56 Incorrect! The correct answer is: 4 : 11 + 15 = 8 Incorrect! The correct answer is: 5 : 22 + 21 = 9 Incorrect! The correct answer is: 6 : 34 + 30 = 10 Incorrect! The correct answer is: 7 : 15 + 31 = 20 Incorrect! The correct answer is: 8 : 19 + 21 = 3 Incorrect! The correct answer is: 9 : 31 + 36 = 4 Incorrect! The correct answer is: 10 : 21 + 10 = 6 Incorrect! The correct answer is: GLaDOS Suggests: go down a level! Your total score is: 0

48 54 23 26 43

64 46 40 67 31

Time: You're really fast! You did it in: 9 seconds! Your current level is: 2 1 : 20 + 22 = 7 Incorrect! The correct answer is: 42 2 : 8 + 13 = p For input string: "p" Defaulting answer to 0. Incorrect! The correct answer is: 21 3 : 3 + 13 = 16 You are correct! 4 : 4 + 18 = 22 You are correct! 5 : 5 + 9 = 14 You are correct!

CSCI 1100, Project, page 17

6 : 22 + 15 = 37 You are correct! 7 : 3 + 5 = 8 You are correct! 8 : 19 + 14 = 0 Incorrect! The correct answer is: 33 9 : 14 + 17 = p For input string: "p" Defaulting answer to 0. Incorrect! The correct answer is: 31 10 : 22 + 10 = q For input string: "q" Defaulting answer to 0. Incorrect! The correct answer is: 32 GLaDOS Suggests: consider reviewing or go down a level! Do you wish to go down a level? (Y or N): n Your total score is: 5 Time: You're average! You did it in: 36 seconds! Your current level is: 2 1 : 10 + 20 = 30 You are correct! 2 : 18 + 19 = 37 You are correct! 3 : 22 + 5 = 27 You are correct! 4 : 10 + 10 = 20 You are correct! 5 : 7 + 17 = 24 You are correct! 6 : 18 + 3 = 21 You are correct! 7 : 22 + 20 = 42 You are correct! 8 : 3 + 18 = 0 Incorrect! The correct answer is: 21 9 : 3 + 20 = qw For input string: "qw" Defaulting answer to 0. Incorrect! The correct answer is: 23 10 : 10 + 13 = poop For input string: "poop" Defaulting answer to 0. Incorrect! The correct answer is: 23 GLaDOS Suggests: not bad! Keep practicing! Do you wish to go up a level? (Y or N): y Your total score is: 7 Time: You're really fast! You did it in: 29 seconds! Your current level is: 3 1 : 24 + 27 = 0

CSCI 1100, Project, page 18

Incorrect! The correct answer is: 51 2 : 33 + 30 = 0 Incorrect! The correct answer is: 63 3 : 19 + 15 = 0 Incorrect! The correct answer is: 34 4 : 29 + 30 = 0 Incorrect! The correct answer is: 59 5 : 8 + 8 = 0 Incorrect! The correct answer is: 16 6 : 34 + 16 = 0 Incorrect! The correct answer is: 50 7 : 29 + 8 = 0 Incorrect! The correct answer is: 37 8 : 28 + 28 = 0 Incorrect! The correct answer is: 56 9 : 25 + 24 = 0 0Incorrect! The correct answer is: 49 10 : 24 + 24 = Incorrect! The correct answer is: 48 GLaDOS Suggests: go down a level! Your total score is: 0 Time: You're really fast! You did it in: 9 seconds! Your current level is: 2 1 : 20 + 5 = 0 Incorrect! The correct answer is: 2 : 22 + 11 = 0 Incorrect! The correct answer is: 3 : 22 + 21 = 0 Incorrect! The correct answer is: 4 : 21 + 13 = 0 Incorrect! The correct answer is: 5 : 19 + 15 = 0 Incorrect! The correct answer is: 6 : 6 + 18 = 0 Incorrect! The correct answer is: 7 : 22 + 5 = 0 Incorrect! The correct answer is: 8 : 11 + 8 = 0 Incorrect! The correct answer is: 9 : 14 + 5 = 0 Incorrect! The correct answer is: 10 : 5 + 10 = 0 Incorrect! The correct answer is: GLaDOS Suggests: go down a level!

25 33 43 34

34 24 27 19 19

15

CSCI 1100, Project, page 19

Your total score is: 0 Time: You're really fast! You did it in: 6 seconds! Your current level is: 1 1 : 5 + 10 =

Test 3
What level do you wish to play? Select from 1 to 3: 3 What type of arithmetic equation to you wish to do? Options: addition(+), subtraction(-), multiplication(*), division(/). division Your current level is: 3 1 : 16 / 2 = 8 You are correct! 2 : 15 / 5 = 3 You are correct! 3 : 29 / 1 = 20 Incorrect! The correct answer is: 29 4 : 27 / 1 = 27 You are correct! 5 : 29 / 1 = 29 You are correct! 6 : 16 / 4 = 4 You are correct! 7 : 28 / 2 = 14 You are correct! 8 : 25 / 25 = 1 You are correct! 9 : 14 / 7 = 2 You are correct! 10 : 9 / 1 = 9 You are correct! GLaDOS Suggests: I have no advice for you, you're pretty much as smart as me! Your total score is: 9 Time: You're average! You did it in: 33 seconds! Do you want to play again?(Y or N) n Good bye!

CSCI 1100, Project, page 20

You might also like