Lesson no.6 (Simple Quiz Game in j2me)

Hi guys,
i hope you are doing well with the courses. As the theory has importance. Well, More then that practical has more importance.
Always remember intrest +hardwork=success. Try Try and at last you will do it. :)
Well, Today i will give small example of simple quiz game. We will develop it by parts by parts.
Whenever you are making softwares or anything first make some startegies. You all may know the meaning of the word algorithm. A succesfull programmer always works with steps following algorithm. Algorithm can be explained as the simple rough sketch of the program. You can write something that you are going to make in your copy. Write the features that you will include and bla bla. Suppose you are making a software with 10 forms. You may be confused anywhere in the middle with the form name, declaration, actions. But if you write the name of forms and other basic logics of your program as a rough in copy and put it in front of you while programming. This makes very less chances of confusion and errors. You can really save your time too. Remember a suceesful programmer does this. ;)
Well, That was the tips. Accordingly i said the importance of algorithm we will also work in this projects with some steps.
About the project:
The project which we are going to create is a simple quiz game. When we open it it will ask for username. After input and ok it will take to next form page. Next form page will include some messages like ” Hi, $username welcome…..bla bla..” then after this page includes start command and there will be about 10 forms with each quiz questions. If user make answer correct then he will get next question. If user makes answer incorrect then the game ends and it takes to another form which contains the points he got throughout the game and suggestion according to the point.
We will make like levels. If user inputsall answer correct then some good messages bla bla….
That was about the game. I also make every time algorithm while making any programs. I tried to submit that rough sketch here too but i cant today. that will be submitted in some days a scanned page image that may help giving concepts on how to make quite big programs with less confusions..
The thing i would like to say it always divide and make some parts for your program which you will work into it..Donot try to do all the task at a time. That will create lots of confusions and you will get depressed. Always remember smartness also got lot of value in programming world. No matter How you did it but the thing matter is You did or not. I divided this projects in some section.
Today we will do the first section the main part of the program.
1> 10 forms, 10 Questions.
2> We will make username input
3> We will make to display username in other forms.
4> We will make points
5> We will handle wrong answer and print the obtained points.
6> We will add some images.
And we will do lot more with this so you can learn some intresting things that can be done with codings.
today only we will do our first part. I will submit the codes here. The codes that i will be submitting are already disccused. Whenever i will add new syntax etc then i will be explaining it.
Still you can raise questions anytime if you face any problems. Lets do new things in an intresting way. :d

Hi,
Accordingly now i completed the first part. I am giving the code here: The code looks like this

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.midlet.*;

/**
* @author pradeep
*/

public class quiz extends MIDlet implements CommandListener {

private Display display;

//form1 intialize
private Form form1;

String q1 =”Which are max99 features?\n”;
private String q1ans1=”1. sms\n”;
private String q1ans2=”2. chat\n”;
private String q1ans3=”3. All of the above\n”;
private Command q1ans11;
private Command q1ans12;
private Command q1ans13;
//form2
private Form form2;
String q2 =”J2me course started by?\n”;
private String q2ans1=”1. Max99\n”;
private String q2ans2=”2. mig33\n”;
private String q2ans3=”3. None of the above\n”;
private Command q2ans11;
private Command q2ans12;
private Command q2ans13;
//form3
private Form form3;
String q3 =”Url of max99 is\n”;
private String q3ans1=”1. http://www.lovingnepal.com\n”;
private String q3ans2=”2. http://www.max99.mobi\n”;
private String q3ans3=”3. None of the above\n”;
private Command q3ans11;
private Command q3ans12;
private Command q3ans13;
//form4
private Form form4;
String q4 =”Which software is used to make javaME applications?\n”;
private String q4ans1=”1. Internetbeans\n”;
private String q4ans2=”2. Netbeans\n”;
private String q4ans3=”3. Nobeans\n”;
private Command q4ans11;
private Command q4ans12;
private Command q4ans13;
//form5
private Form form5;
String q5 =”Best feature of max99?\n”;
private String q5ans1=”1. Gifts\n”;
private String q5ans2=”2. Vidoe call\n”;
private String q5ans3=”3. None of above\n”;
private Command q5ans11;
private Command q5ans12;
private Command q5ans13;
//form6
private Form form6;
String q6 =”Congrats you made all correct\n”;

public quiz (){
display = Display.getDisplay(this);
//form1 declaration

form1 = new Form(“Welcome”);
q1ans11 = new Command(“1″, 1, 1);
q1ans12 = new Command(“2″, 1, 2);
q1ans13 = new Command(“3″, 1, 3);

form1.append(q1);
form1.append(q1ans1);
form1.append(q1ans2);
form1.append(q1ans3);
form1.addCommand(q1ans11);
form1.addCommand(q1ans12);
form1.addCommand(q1ans13);
form1.setCommandListener(this);
//form2
form2 = new Form(“Welcome to 2nd question”);
q2ans11 = new Command(“1″, 1, 1);
q2ans12 = new Command(“2″, 1, 2);
q2ans13 = new Command(“3″, 1, 3);

form2.append(q2);
form2.append(q2ans1);
form2.append(q2ans2);
form2.append(q2ans3);
form2.addCommand(q2ans11);
form2.addCommand(q2ans12);
form2.addCommand(q2ans13);
form2.setCommandListener(this);
//form3
form3 = new Form(“Welcome to 3rd question”);
q3ans11 = new Command(“1″, 1, 1);
q3ans12 = new Command(“2″, 1, 2);
q3ans13 = new Command(“3″, 1, 3);

form3.append(q3);
form3.append(q3ans1);
form3.append(q3ans2);
form3.append(q3ans3);
form3.addCommand(q3ans11);
form3.addCommand(q3ans12);
form3.addCommand(q3ans13);
form3.setCommandListener(this);
//form4
form4 = new Form(“Welcome to 4th question”);
q4ans11 = new Command(“1″, 1, 1);
q4ans12 = new Command(“2″, 1, 2);
q4ans13 = new Command(“3″, 1, 3);

form4.append(q4);
form4.append(q4ans1);
form4.append(q4ans2);
form4.append(q4ans3);
form4.addCommand(q4ans11);
form4.addCommand(q4ans12);
form4.addCommand(q4ans13);
form4.setCommandListener(this);
//form5
form5 = new Form(“Welcome to 5th question”);
q5ans11 = new Command(“1″, 1, 1);
q5ans12 = new Command(“2″, 1, 2);
q5ans13 = new Command(“3″, 1, 3);

form5.append(q5);
form5.append(q5ans1);
form5.append(q5ans2);
form5.append(q5ans3);
form5.addCommand(q5ans11);
form5.addCommand(q5ans12);
form5.addCommand(q5ans13);
form5.setCommandListener(this);
//form6
form6 = new Form(“Congrats”);

form6.append(q6);

}

public void startApp() {
display.setCurrent(form1);

}

public void pauseApp() {
}

public void destroyApp(boolean unconditional) {
}

public void commandAction(Command c, Displayable d) {
//form1 actions
if (c==q1ans13)
{
display.setCurrent(form2);
}

if (c==q2ans11)
{
display.setCurrent(form3);
}
if (c==q3ans12)
{
display.setCurrent(form4);
}
if (c==q4ans12)
{
display.setCurrent(form5);
}
if (c==q5ans11)
{
display.setCurrent(form6);
}
}

}

This was our first target of the application. Now we did it. every codes here are already disscussed but also you can raise the questions in any erros and problems. Gradually we will develop it and make it more better according to our task list.
Well, In the code everything is old but the new thing is:

String q6 =”Congrats you made all correct\n”;

In all string message i used “\n” . Well some html developers may know the work of <br/>. this has same function. It breaks the line after the text so that other text can be appended throughly.
Well, According to task list we need to make 10 forms but i mentioned project with only 5 forms. I hope you all will be trying to make rest 5 forms. Nothing hard just copy paste and modify. Then you’ve done it!
The upper code may get some problem so please download the source java file from link below:
http://www.lovingnepal.com/quiz.java
You want to test the application before copy paste, modify?? Download it.
http://www.lovingnepal.com/quiz.jar
Keep on developing the project. If cant dont worry i am just looking at you and guide ;)

Hi again,
Well everyone may have done the first part very well.
According to our task list. Today we will make username input and print it in next page at startup.

2> We will make username input

ater we will print name at score page too but for now .Now just modify the codes In ths way:

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.*;

/**
* @author pradeep
*/

public class quiz extends MIDlet implements CommandListener {

private Display display;
private Form inputpage;

private TextField username;
private Command startgame;
//form1 intialize
private Form form1;

String q1 =”Which are max99 features?\n”;
private String q1ans1=”1. sms\n”;
private String q1ans2=”2. chat\n”;
private String q1ans3=”3. All of the above\n”;
private Command q1ans11;
private Command q1ans12;
private Command q1ans13;
//form2
private Form form2;
String q2 =”J2me course started by?\n”;
private String q2ans1=”1. Max99\n”;
private String q2ans2=”2. mig33\n”;
private String q2ans3=”3. None of the above\n”;
private Command q2ans11;
private Command q2ans12;
private Command q2ans13;
//form3
private Form form3;
String q3 =”Url of max99 is\n”;
private String q3ans1=”1. http://www.lovingnepal.com\n”;
private String q3ans2=”2. http://www.max99.mobi\n”;
private String q3ans3=”3. None of the above\n”;
private Command q3ans11;
private Command q3ans12;
private Command q3ans13;
//form4
private Form form4;
String q4 =”Which software is used to make javaME applications?\n”;
private String q4ans1=”1. Internetbeans\n”;
private String q4ans2=”2. Netbeans\n”;
private String q4ans3=”3. Nobeans\n”;
private Command q4ans11;
private Command q4ans12;
private Command q4ans13;
//form5
private Form form5;
String q5 =”Best feature of max99?\n”;
private String q5ans1=”1. Gifts\n”;
private String q5ans2=”2. Vidoe call\n”;
private String q5ans3=”3. None of above\n”;
private Command q5ans11;
private Command q5ans12;
private Command q5ans13;
//form6
private Form form6;
String q6 =”Congrats you made all correct\n”;

public quiz (){
display = Display.getDisplay(this);
inputpage = new Form(“username”);

username =new TextField(“Enter Your name;”, “”, 50, TextField.ANY);
inputpage.append(username);
startgame = new Command(“Start game”, 1, 1);
inputpage.addCommand(startgame);
inputpage.setCommandListener(this);

//form1 declaration

form1 = new Form(“Welcome to 1st question”);
q1ans11 = new Command(“1″, 1, 1);
q1ans12 = new Command(“2″, 1, 2);
q1ans13 = new Command(“3″, 1, 3);

form1.append(q1);
form1.append(q1ans1);
form1.append(q1ans2);
form1.append(q1ans3);
form1.addCommand(q1ans11);
form1.addCommand(q1ans12);
form1.addCommand(q1ans13);
form1.setCommandListener(this);
//form2
form2 = new Form(“Welcome to 2nd question”);
q2ans11 = new Command(“1″, 1, 1);
q2ans12 = new Command(“2″, 1, 2);
q2ans13 = new Command(“3″, 1, 3);

form2.append(q2);
form2.append(q2ans1);
form2.append(q2ans2);
form2.append(q2ans3);
form2.addCommand(q2ans11);
form2.addCommand(q2ans12);
form2.addCommand(q2ans13);
form2.setCommandListener(this);
//form3
form3 = new Form(“Welcome to 3rd question”);
q3ans11 = new Command(“1″, 1, 1);
q3ans12 = new Command(“2″, 1, 2);
q3ans13 = new Command(“3″, 1, 3);

form3.append(q3);
form3.append(q3ans1);
form3.append(q3ans2);
form3.append(q3ans3);
form3.addCommand(q3ans11);
form3.addCommand(q3ans12);
form3.addCommand(q3ans13);
form3.setCommandListener(this);
//form4
form4 = new Form(“Welcome to 4th question”);
q4ans11 = new Command(“1″, 1, 1);
q4ans12 = new Command(“2″, 1, 2);
q4ans13 = new Command(“3″, 1, 3);

form4.append(q4);
form4.append(q4ans1);
form4.append(q4ans2);
form4.append(q4ans3);
form4.addCommand(q4ans11);
form4.addCommand(q4ans12);
form4.addCommand(q4ans13);
form4.setCommandListener(this);
//form5
form5 = new Form(“Welcome to 5th question”);
q5ans11 = new Command(“1″, 1, 1);
q5ans12 = new Command(“2″, 1, 2);
q5ans13 = new Command(“3″, 1, 3);

form5.append(q5);
form5.append(q5ans1);
form5.append(q5ans2);
form5.append(q5ans3);
form5.addCommand(q5ans11);
form5.addCommand(q5ans12);
form5.addCommand(q5ans13);
form5.setCommandListener(this);
//form6
form6 = new Form(“Congrats”);

form6.append(q6);

}

public void startApp() {
display.setCurrent(inputpage);

}

public void pauseApp() {
}

public void destroyApp(boolean unconditional) {
}

public void commandAction(Command c, Displayable d) {
//form1 actions

if (c==startgame)
{
display.setCurrent(form1);
}
if (c==q1ans13)
{
display.setCurrent(form2);
}

if (c==q2ans11)
{
display.setCurrent(form3);
}
if (c==q3ans12)
{
display.setCurrent(form4);
}
if (c==q4ans12)
{
display.setCurrent(form5);
}
if (c==q5ans11)
{
display.setCurrent(form6);
}
}

}

Nothing more i added here. :)
Download it from here..
http://www.lovingnepal.com/quizmodified.java
Try modifying it. We wll learn next part i.e, points in next tutorial. Never be back in questioning.
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.*;

/**
* @author pradeep
*/

public class quiz extends MIDlet implements CommandListener {

int score=0;
private Display display;
private Form inputpage;
String lastscore = (new Integer(score)).toString();
private TextField username;
private Command startgame;
//form1 intialize
private Form form1;

String q1 =”Which are max99 features?\n”;
private String q1ans1=”1. sms\n”;
private String q1ans2=”2. chat\n”;
private String q1ans3=”3. All of the above\n”;
private Command q1ans11;
private Command q1ans12;
private Command q1ans13;
//form2
private Form form2;
String q2 =”J2me course started by?\n”;
private String q2ans1=”1. Max99\n”;
private String q2ans2=”2. mig33\n”;
private String q2ans3=”3. None of the above\n”;
private Command q2ans11;
private Command q2ans12;
private Command q2ans13;
//form3
private Form form3;
String q3 =”Url of max99 is\n”;
private String q3ans1=”1. http://www.lovingnepal.com\n”;
private String q3ans2=”2. http://www.max99.mobi\n”;
private String q3ans3=”3. None of the above\n”;
private Command q3ans11;
private Command q3ans12;
private Command q3ans13;
//form4
private Form form4;
String q4 =”Which software is used to make javaME applications?\n”;
private String q4ans1=”1. Internetbeans\n”;
private String q4ans2=”2. Netbeans\n”;
private String q4ans3=”3. Nobeans\n”;
private Command q4ans11;
private Command q4ans12;
private Command q4ans13;
//form5
private Form form5;
String q5 =”Best feature of max99?\n”;
private String q5ans1=”1. Gifts\n”;
private String q5ans2=”2. Vidoe call\n”;
private String q5ans3=”3. None of above\n”;
private Command q5ans11;
private Command q5ans12;
private Command q5ans13;
//form6
private Form form6;
String q6 =”Congrats you made all correct\n”;

public quiz (){
display = Display.getDisplay(this);
inputpage = new Form(“username”);

username =new TextField(“Enter Your name;”, “”, 50, TextField.ANY);
inputpage.append(username);
startgame = new Command(“Start game”, 1, 1);
inputpage.addCommand(startgame);
inputpage.setCommandListener(this);

//form1 declaration

form1 = new Form(“Welcome to 1st question”);
q1ans11 = new Command(“1″, 1, 1);
q1ans12 = new Command(“2″, 1, 2);
q1ans13 = new Command(“3″, 1, 3);

form1.append(q1);
form1.append(q1ans1);
form1.append(q1ans2);
form1.append(q1ans3);
form1.addCommand(q1ans11);
form1.addCommand(q1ans12);
form1.addCommand(q1ans13);
form1.setCommandListener(this);
//form2
form2 = new Form(“Welcome to 2nd question”);
q2ans11 = new Command(“1″, 1, 1);
q2ans12 = new Command(“2″, 1, 2);
q2ans13 = new Command(“3″, 1, 3);

form2.append(q2);
form2.append(q2ans1);
form2.append(q2ans2);
form2.append(q2ans3);
form2.addCommand(q2ans11);
form2.addCommand(q2ans12);
form2.addCommand(q2ans13);
form2.setCommandListener(this);
//form3
form3 = new Form(“Welcome to 3rd question”);
q3ans11 = new Command(“1″, 1, 1);
q3ans12 = new Command(“2″, 1, 2);
q3ans13 = new Command(“3″, 1, 3);

form3.append(q3);
form3.append(q3ans1);
form3.append(q3ans2);
form3.append(q3ans3);
form3.addCommand(q3ans11);
form3.addCommand(q3ans12);
form3.addCommand(q3ans13);
form3.setCommandListener(this);
//form4
form4 = new Form(“Welcome to 4th question”);
q4ans11 = new Command(“1″, 1, 1);
q4ans12 = new Command(“2″, 1, 2);
q4ans13 = new Command(“3″, 1, 3);

form4.append(q4);
form4.append(q4ans1);
form4.append(q4ans2);
form4.append(q4ans3);
form4.addCommand(q4ans11);
form4.addCommand(q4ans12);
form4.addCommand(q4ans13);
form4.setCommandListener(this);
//form5
form5 = new Form(“Welcome to 5th question”);
q5ans11 = new Command(“1″, 1, 1);
q5ans12 = new Command(“2″, 1, 2);
q5ans13 = new Command(“3″, 1, 3);

form5.append(q5);
form5.append(q5ans1);
form5.append(q5ans2);
form5.append(q5ans3);
form5.addCommand(q5ans11);
form5.addCommand(q5ans12);
form5.addCommand(q5ans13);
form5.setCommandListener(this);
//form6
form6 = new Form(“Congrats”);

form6.append(q6);

}

public void startApp() {
display.setCurrent(inputpage);

}

public void pauseApp() {
}

public void destroyApp(boolean unconditional) {
}

public void commandAction(Command c, Displayable d) {
//form1 actions

if (c==startgame)
{
score= score+1;
display.setCurrent(form1);
}
if (c==q1ans13)
{
score= score+1;
display.setCurrent(form2);
}

if (c==q2ans11)
{
score= score+1;
display.setCurrent(form3);
}
if (c==q3ans12)
{
score= score+1;
display.setCurrent(form4);
}
if (c==q4ans12)
{
score= score+1;
display.setCurrent(form5);
}
if (c==q5ans11)
{
String congrats=”Your total points is\n”;

form6.append(congrats);
form6.append(lastscore);
display.setCurrent(form6);

}

}

}

Here one syntax looks like new. But the logic was already given:Everything is just like we practised before. Here new thing is:

String lastscore = (new Integer(score)).toString();

[/quote]
this syntax helps to pass the value of score to string so that we can append/print in the forms.
So nothing is difficult. Just give your time along with the sense then everything goes perfect.
download code from below:
http://lovingnepal.com/j2me/quiz.java
Try always to modify and add something new and nice with the same ideas.
Best of luck! ;)

Author: pradeep

Share This Post On

Submit a Comment