Friday, 27 September 2013

C++ design suggestions

C++ design suggestions

I made a program in Java a while back that is essentially a math quiz, it
randomly generates simple math problems based on user input. Now, I'm
teaching myself C++ and I was just wondering, based on efficiency and
organization, what is the best way to convert this program. For example
should my methods be classes, should they stay in one class, ect. Any help
on this would be much appreciated! This is my current Java code
public class MathQuiz {
Scanner s = new Scanner(System.in);
int numberOne;
int numberTwo;
int actualSolution;
int userSolution;
int numberOfQuestions;
int counter = 1;
int temp;
int randomOperation;
int totalCorrect;
int totalIncorrect;
long startTime;
long endTime;
long totalTime;
double percent;
String operation;
String done;
public void mathQuizzer(){
System.out.println("Welcome to Math Quiz!");
System.out.println("What kind of questions would you like to answer?");
System.out.println("Enter + for Addition");
System.out.println("Enter - for Subtraction");
System.out.println("Enter * for Multiplication");
System.out.println("Enter / for Division");
System.out.println("Enter m for Mix");
operation = s.next();
System.out.println("How many questions would you like to answer? ");
numberOfQuestions = s.nextInt();
System.out.println();
startTime = System.currentTimeMillis();
while(counter <= numberOfQuestions){
if(operation.equals("+")){
System.out.println("Question " + counter + ":");
createAdditionEquation();
checkSolution();
counter++;
}
else if(operation.equals("-")){
System.out.println("Question " + counter + ":");
createSubtractionEquation();
checkSolution();
counter++;
}
else if(operation.equals("*")){
System.out.println("Question " + counter + ":");
createMultiplicationEquation();
checkSolution();
counter++;
}
else if(operation.equals("/")){
System.out.println("Question " + counter + ":");
createDivisionEquation();
checkSolution();
counter++;
}
else if (operation.equals("m")){
System.out.println("Question " + counter + ":");
createMixEquation();
checkSolution();
counter++;
}
else{
System.out.println("Sorry invalid operation");
System.exit(0);
}
}
endTime = System.currentTimeMillis();
report();
}
public void createAdditionEquation(){
numberOne = (int)(Math.random() * (100 + 1));
numberTwo = (int)(Math.random() * (100 + 1));
actualSolution = numberOne + numberTwo;
System.out.println(numberOne + " + " + numberTwo + "= ?");
}
public void createSubtractionEquation(){
numberOne = (int)(Math.random() * (100 + 1));
numberTwo = (int)(Math.random() * (100 + 1));
if(numberOne >= numberTwo){
actualSolution = numberOne - numberTwo;
}
else{
numberOne = temp;
numberOne = numberTwo;
numberTwo = temp;
actualSolution = numberOne - numberTwo;
}
System.out.println(numberOne + " - " + numberTwo + "= ?");
}
public void createMultiplicationEquation(){
numberOne = (int)(Math.random() * (10 + 1));
numberTwo = (int)(Math.random() * ((20 - 1) + 1));
actualSolution = numberOne * numberTwo;
System.out.println(numberOne + " * " + numberTwo + "= ?");
}
public void createDivisionEquation(){
numberOne = (int)(Math.random() * (100 + 1));
numberTwo = (int)(1 +(Math.random() * (10 - 1)));
if((numberOne >= numberTwo) && (numberOne%numberTwo == 0)){
System.out.println(numberOne + " / " + numberTwo + "= ?");
actualSolution = numberOne / numberTwo;
}
else{
createDivisionEquation();
}
}
public void createMixEquation(){
randomOperation = (int)(Math.random() * (5 - 1));
System.out.println("Random Operation: " + randomOperation);
if(randomOperation == 0){
createAdditionEquation();
}
else if(randomOperation == 1){
createSubtractionEquation();
}
else if(randomOperation == 2){
createMultiplicationEquation();
}
else
createDivisionEquation();
}
public void checkSolution(){
System.out.println("Answer = ");
userSolution = s.nextInt();
System.out.println();
if(userSolution == actualSolution){
System.out.println("Correct!");
System.out.println("#########################");
System.out.println();
totalCorrect++;
}
else if(userSolution != actualSolution){
System.out.println("Sorry, wrong answer. The correct solution is:
" + actualSolution);
System.out.println("#########################");
System.out.println();
totalIncorrect++;
}
}
public void report(){
percent = ((double) totalCorrect / numberOfQuestions) * 100;
totalTime = (endTime - startTime) / 1000;
System.out.println("Report:");
System.out.println("Questions Answered: " + numberOfQuestions);
System.out.println("Total Correct: " + totalCorrect);
System.out.println("Total Incorrect: " + totalIncorrect);
System.out.println("Percent: " + percent + "%");
System.out.println("Time taken: " + totalTime + " seconds");
}
}
It should be noted that I know this program isn't optimized, and I'm okay
with that for the time being

No comments:

Post a Comment