Sunday, February 26, 2017

Java Implementation ! Challenge 2 : Simple Substraction Quiz

What's up guys welcome back to my tutorial blog. Hope you guys enjoy learn programming from my blog these entire time. By the way in this new series from my blog I'm gonna show you guys the implemantation of Java programming languange. This time, I'm gonna show you guys how to make a simple substraction quiz program.

So this program you're about to learn is gonna ask the user to start the quiz. Then showing the simple math quiz of substraction.In this simple quiz, the user is gonna answer 5 questions. Also, this program is gonna show the user the total correct and incorrect answer that they made with the time they take to answer all the questions.

Please notice that in this Tutorial Series, I'm gonna use Notepad++ instead of eclipse, I also suggest you guys to do the same thing. Because whenever you guys are using an IDE like eclipse, you will find your error syntax faster than it should. While learning programming, the more time you spend on it, the more you will get used to it. I want you guys to spend more time in coding, because that's what great programmers did. As usual, I'm gonna show you guys the program first, I want you guys to read it carefully, then I'm gonna explain each part of the codes. Here we go.




In case if the image isn't loaded :
========================================================================

import java.util.Scanner;
class SimpleQuiz{
public static void main (String args[]){
Scanner input = new Scanner (System.in);

System.out.println("Welcome to the simple substraction quiz !");
System.out.println("Make your way to the fastest !");
System.out.println("Start ? (1 to start/0 to close)");
int userinput = input.nextInt();
long startTime = System.currentTimeMillis();

if(userinput == 1){
System.out.println("answer these questions below as fast as you can ! \n\n");
int correct = 0;
int incorrect = 0;
int i = 1;
while (i <=5){
int num1 = (int)(Math.random()*10);
int num2 = (int)(Math.random()*10);

if(num1<num2){
int temp = num1;
num1 = num2;
num2 = temp;
}

System.out.println("How much is "+num1+" - "+num2+" = ?\n");
int answer = input.nextInt();

if(num1-num2==answer){
correct++;
System.out.println("Correct !");
}

else{
incorrect++;
System.out.println("Correct !");
}
i++;
System.out.println();

}

long endTime = System.currentTimeMillis();
long testTime = (int)(endTime-startTime)/1000;

System.out.println("Total correct answer   : "+correct);
System.out.println("Total incorrect answer : "+incorrect);
System.out.println("TIME       : "+testTime+" seconds");
}

else if (userinput==0)
System.out.println("okay then, Good Bye !");

else
System.out.println("Just answer 0 or 1 -_-");

  }
}


========================================================================

You guys still remember how to compile a source code from notepad right ? If not, just take it easy. You still can check it out in Part 2 . After finish with compiling, I want you guys to run the program by typing "java SimpleQuiz" in the command line (Command Prompt) to run the program.
Here is the results :


Now, Let's Read the Codes !

As usual guys, I'm not gonna explain what I think not necessary to be explained. That's because I've explained it in the couple of tutorials before.

Line 8-10 :

There you can see this program ask the user to start the SimpleQuiz program and create an input variable on the 9th line.The 10th Line will create a Time variable (I've explained about System.currentTimeMillis() in Challenge 1 before ) as soon as the user input '1'.

Then I make three if-else statement there that would excecuted statements inside its body depend on the user's input. If the user input is equal '1' ,then the SimpleQuiz program is gonna start, if user input is equal '0', then the SimpleQuiz program will not start. Else, the program will tell the user to only input 1 or 0 as the input then close the program

Line 14-15 :
On these lines, are the declaration of two variables which will play as the counter of the correct and incorrect answers.

Line 17 :
There is a While loop there as a control variable that will do statements inside it 5 iterations. Iterations is a single time when a loop statements are being excecuted. In this case, the statements inside will give the user a question in each iteration.

Line 18-19:
On these Lines, The program makes 2 random number in one digit format in integer datatype. How is that possible ? The reason is because the Math.random() is a syntax that will create a random value between 0,0 until <1. Then why do we have to multiply it with 10 ?. For example, the number that created randomly is 0,5, if we multiply it with 10, then the created number will equals 5, and 5 is a one digit number which is the exactly type of values that we want.

Line 21-24:
These lines also has an important role.That's because I will make a statement in Line 30 about the result, which will have to be the same as the user input if the user wanna gain the correct answer being count. Technically, these line is actually a prevent action in case the first number is smaller than the second number. These lines pf syntax will exchange the value between the first number and the second number. Because we don't want the result is smaller than 0 (negative number).

Line 39 :
Remember guys, whenever you created a loop, don't forget to give the increment operators with it. Because if not, your programs are gonna loop forever if the condition is always true.

Line 44:
It's a variable that will save a value of times. I mean the time exactly when the user are finished with the simple quiz questions.

Line 45:
I've explained about System.currentTimeMillis()  before in Challenge 1. I'm just gonna explain it simpy here. So the System.currentTimeMillis() is a methiod in System class that will show the time since 1 January 1970 until now in milisecond format. So on the 10th Line is the time since 1 January 1970 until the user start the program and the 45th line is the time since 1 January 1970 until the user finish with the quiz each in milisecond format which is why if we want it in second format, we will have to divide it with 1000.

Line 47-49 :
These lines will show the user the correct and the incorrect answer they make, also the time they spend to finish the quiz.

Challenge :

I want you guys to rewrite the codes above without cheating or peeking the codes above. Understand the code, don't memorize it ! Close or Minimize your browser first then go to your text editor to rewrite the codes. That's it for now guys I hope you guys are enjoying it. Thank you guys for visiting my blog and see you on the next Challenge Series ! Good Luck guys !

Beat my time with the same score if you can !

No comments:

Post a Comment