Trivia Game - random set of 10 questions from list of 100 questions

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 1436 Views - Last Post: 30 November 2012 - 09:53 AM

#1 mattlyons  Icon User is offline

  • D.I.C Regular

Reputation: 6
  • View blog
  • Posts: 301
  • Joined: 10-September 09

Trivia Game - random set of 10 questions from list of 100 questions

Posted 09 November 2012 - 09:33 AM

I have a working trivia game app that just goes straight through the questions I have programmed. Each question has it's own class. What I want is the user to start a game and answer 10 randomly selected questions and then the game be over. I cannot figure out how to only allow them to answer 10 questions instead of all 100.

Not sure what code you would like to see so I will just post my main menu class.

Any help is appreciated and thank you in advanced!

MainMenu.java
package com.example.test;

import java.util.Random;
import com.example.test.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.onclickListener;
 
public class MainMenu extends Activity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mainmenumain);
		
		Button button1 = (Button)findViewById(R.id.startBtn);
		Button button2 = (Button)findViewById(R.id.categoriesBtn);
		Button button3 = (Button)findViewById(R.id.highscoresBtn);
		Button button4 = (Button)findViewById(R.id.aboutBtn);
		
		button1.setonclickListener(new onclickListener() {	 
			public void onclick(View arg0) {
			    Intent intent1 = new Intent(MainMenu.this, Q1.class);
                startActivity(intent1);   
			}
		});
		
		button2.setonclickListener(new onclickListener() {	 
			public void onclick(View arg0) {
			    Intent intent2 = new Intent(MainMenu.this, Categories.class);
                startActivity(intent2);   
			}
		});
		
		button3.setonclickListener(new onclickListener() {	 
			public void onclick(View arg0) {
			    Intent intent3 = new Intent(MainMenu.this, Highscores.class);
                startActivity(intent3);   
			}
		});
		
		button4.setonclickListener(new onclickListener() {	 
			public void onclick(View arg0) {
			    Intent intent4 = new Intent(MainMenu.this, About.class);
                startActivity(intent4);   
			}
		});
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Trivia Game - random set of 10 questions from list of 100 questions

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10563
  • View blog
  • Posts: 39,087
  • Joined: 27-December 08

Re: Trivia Game - random set of 10 questions from list of 100 questions

Posted 09 November 2012 - 09:37 AM

Quote

Each question has it's own class.

That doesn't seem like good design to me. How about a single Question class and a List<Question> instead? Or at the very least, MultipleChoiceQuestion, ShortAnswerQuestion, etc.
Was This Post Helpful? 2
  • +
  • -

#3 mattlyons  Icon User is offline

  • D.I.C Regular

Reputation: 6
  • View blog
  • Posts: 301
  • Joined: 10-September 09

Re: Trivia Game - random set of 10 questions from list of 100 questions

Posted 09 November 2012 - 10:09 AM

Hmm, interesting. All my questions are the same -- 4 possible answer multiple choice. So you are saying a better design would to be put all 100+ questions in one class? I guess I can worry about how to pull the 10 random questions after I try to put all the questions in one class.

Thanks Mac!
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10563
  • View blog
  • Posts: 39,087
  • Joined: 27-December 08

Re: Trivia Game - random set of 10 questions from list of 100 questions

Posted 09 November 2012 - 10:11 AM

I'm saying design a single Question class to model a single Question:
class Question{

     private String query;
     private String[] answers;
     private int correctAnswer;
}



Then store a List<Question>, where each Question in the list represents a single Trivia Question. Pick 10 of those at Random.
Was This Post Helpful? 0
  • +
  • -

#5 slehmann101  Icon User is offline

  • D.I.C Head

Reputation: 21
  • View blog
  • Posts: 146
  • Joined: 30-November 11

Re: Trivia Game - random set of 10 questions from list of 100 questions

Posted 09 November 2012 - 10:11 AM

couldn't you make one class, then put it into either an array or a list and then randomly access them using the random number class to generate a random number which would correspond to the question location?
Was This Post Helpful? 1
  • +
  • -

#6 mattlyons  Icon User is offline

  • D.I.C Regular

Reputation: 6
  • View blog
  • Posts: 301
  • Joined: 10-September 09

Re: Trivia Game - random set of 10 questions from list of 100 questions

Posted 09 November 2012 - 11:35 AM

Does the Question.java class need to be something like this?:

public class Question extends Activity { 
    private String query;  
    private String[] answers;  
    private String correctAnswer;

    ArrayList<Question> q = new ArrayList<Question>(100);
    q.add("First letter of alphabet?", ["a", "b", "c", "d"], "d");
    q.add...  //Add all 100 question, answers and correct answers here...
}



One more question -- would this class have an question.xml file? If it did, I am not sure what would go into it. I think I am a little confused now with what each .java and .xml class needs to be with this restructuring I am trying to do.
Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10563
  • View blog
  • Posts: 39,087
  • Joined: 27-December 08

Re: Trivia Game - random set of 10 questions from list of 100 questions

Posted 09 November 2012 - 11:39 AM

A Question is not an Activity. Think back to good software development practices. Your Activity subclass should store a List<Question>, and it's different than the individual Question class. Plus, it's not good practice to have a single Question object store a List<Question>.
Was This Post Helpful? 0
  • +
  • -

#8 mattlyons  Icon User is offline

  • D.I.C Regular

Reputation: 6
  • View blog
  • Posts: 301
  • Joined: 10-September 09

Re: Trivia Game - random set of 10 questions from list of 100 questions

Posted 09 November 2012 - 11:50 AM

Yea it has been a while so I am trying to remember back as best I can. Been out of college for a couple of years and trying to get back into it now.

I think what is confusing me is when I was in college we only did Java computer programs. Now I am trying to do a mobile app and the "Activities" are giving me a fit. Does every .java file require a .xml file?

I was thinking of creating a simple ArrayList of Question objects with 3 parameters each (question, list of 4 possible answers, and then the correct string answer). I wanted to do all of this in one class/activity called Question.java. Is that sounding right? If so, does I need a .xml to correspond to this Question.java file?
Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10563
  • View blog
  • Posts: 39,087
  • Joined: 27-December 08

Re: Trivia Game - random set of 10 questions from list of 100 questions

Posted 09 November 2012 - 11:59 AM

Quote

I wanted to do all of this in one class/activity called Question.java. Is that sounding right?

I've already answered that- no. On top of that, we've already outlined a better design.

I believe you only need to include Activities in your Android manifest file.
Was This Post Helpful? 0
  • +
  • -

#10 mattlyons  Icon User is offline

  • D.I.C Regular

Reputation: 6
  • View blog
  • Posts: 301
  • Joined: 10-September 09

Re: Trivia Game - random set of 10 questions from list of 100 questions

Posted 09 November 2012 - 12:15 PM

In my OP I was looking for coding help and now after your help I see my structure is incorrect and I no longer need coding help but an overall big picture help.

All of our posts seem to be confusing me more and more. I am trying to piece together all of your helpful tips from each of your posts but I cannot seem to put it together into a big picture of what needs to be done/restructured. Would you mind giving me a brief big picture of the classes/subclasses I am going to need and what each will need to do to get this done part of the app done?

I appreciate your patience with me so far! :)
Was This Post Helpful? 0
  • +
  • -

#11 TheGDeveloper  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 93
  • Joined: 22-September 09

Re: Trivia Game - random set of 10 questions from list of 100 questions

Posted 09 November 2012 - 05:19 PM

make a class for the question that holds the question, and array of possible answers and an int as a pointer to the correct answer
public class Question{
public Question( String question, String ans1, String ans2, String ans3, String ans4, int correctAns) {
this.quastion = quastion;
this.answers = new answers[4];
this.answers[0] = ans1;
this.answers[1] = ans2;
this.answers[2] = ans3;
this.answers[3] = ans4;
this.correctAns = correctAns
}
}



then make a list of question and put questions in it
ArrayList<Question> qlist = new ArrayList<Question>();
qlist.add( new Question( "q", "ans1", "ans2", "ans3", "ans3", 0 );



and then make an algorithm to pick randomly 10 of these questions it is easy
Was This Post Helpful? 3
  • +
  • -

#12 mattlyons  Icon User is offline

  • D.I.C Regular

Reputation: 6
  • View blog
  • Posts: 301
  • Joined: 10-September 09

Re: Trivia Game - random set of 10 questions from list of 100 questions

Posted 13 November 2012 - 10:41 AM

Thank you for your response. It helped and I made some progress. Now I am having trouble just tieing everything together so the app actually runs. Right now I have 4 files that I am trying to get to work together:

- Main menu (MainMenu.java)
- The new Question class that holds all questions (Question.java)
- The activity class for the first of the 10 random questions (Q1.java)
- The xml file for Q1.java (q1main.xml)

Below is the code for all of them. When the app starts I click on the Start button and the app crashes. Below is also the LogCat output.

I (think) was able to successfully generate an ArrayList of 10 random questions. I am just having a hard time of how to make the 1st Question in the 10 randomly generated questions to appear in an activity after the Start button is pressed on the main menu.

MainMenu.java
package com.example.test;

import java.util.Random;
import com.example.test.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.onclickListener;
 
public class MainMenu extends Activity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mainmenumain);
		
		Button button1 = (Button)findViewById(R.id.startBtn);
		Button button2 = (Button)findViewById(R.id.categoriesBtn);
		Button button3 = (Button)findViewById(R.id.highscoresBtn);
		Button button4 = (Button)findViewById(R.id.aboutBtn);
		
		button1.setonclickListener(new onclickListener() {	 
			public void onclick(View arg0) {
			    Intent intent1 = new Intent(MainMenu.this, Q1.class);
                startActivity(intent1);   
			}
		});
		
		button2.setonclickListener(new onclickListener() {	 
			public void onclick(View arg0) {
			    Intent intent2 = new Intent(MainMenu.this, Categories.class);
                startActivity(intent2);   
			}
		});
		
		button3.setonclickListener(new onclickListener() {	 
			public void onclick(View arg0) {
			    Intent intent3 = new Intent(MainMenu.this, Highscores.class);
                startActivity(intent3);   
			}
		});
		
		button4.setonclickListener(new onclickListener() {	 
			public void onclick(View arg0) {
			    Intent intent4 = new Intent(MainMenu.this, About.class);
                startActivity(intent4);   
			}
		});
	}
}



Question.java
package com.example.test;

import java.util.ArrayList;
import java.util.Random;

public class Question {
	
	private String question;
	private String[] answers;
	private int correctAns;
	private Random randomGenerator = new Random();
	
	public static ArrayList<Question> tenQs = new ArrayList<Question>(10);
	
	public Question(String question, String ans1, String ans2, String ans3, String ans4, int correctAns) {
		
		this.question = question;
		this.answers = new String[4];
		this.answers[0] = ans1;
		this.answers[1] = ans2;
		this.answers[2] = ans3;
		this.answers[3] = ans4;
		this.correctAns = correctAns;
		
		ArrayList<Question> qList = new ArrayList<Question>(100);
		
		qList.add(new Question("q1?", "1", "2", "3", "4", 0));
		qList.add(new Question("q2?", "a", "b", "c", "d", 1));
		qList.add(new Question("q3?", "z", "y", "x", "w", 2));
		qList.add(new Question("q4?", "q", "w", "e", "r", 3));
		qList.add(new Question("q6?", "t", "y", "h", "f", 0));
		qList.add(new Question("q7?", "t", "y", "h", "f", 0));
		qList.add(new Question("q8?", "t", "y", "h", "f", 0));
		qList.add(new Question("q9?", "t", "y", "h", "f", 0));
		qList.add(new Question("q10?", "t", "y", "h", "f", 0));
		qList.add(new Question("q11?", "t", "y", "h", "f", 0));
		qList.add(new Question("q12?", "t", "y", "h", "f", 0));
		qList.add(new Question("q13?", "t", "y", "h", "f", 0));
		qList.add(new Question("q14?", "t", "y", "h", "f", 0));
		qList.add(new Question("q15?", "t", "y", "h", "f", 0));
	
		//Method with algorithm for pulling 10 q's at a time from qList.
        for(int i = 0; i < 10; i++) {
        	int index = randomGenerator.nextInt(qList.size());
        	tenQs.add(qList.get(index));
        }
	}
	
	public static ArrayList getArrayList() {
	     return tenQs;
	}
}



Q1.java
package com.example.test;

import java.util.ArrayList;

import com.example.test.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.view.View.onclickListener;

public class Q1 extends Activity {
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.q1main);

		Button button1 = (Button)findViewById(R.id.q1b1);
		Button button2 = (Button)findViewById(R.id.q1b2);
		Button button3 = (Button)findViewById(R.id.q1b3);
		Button button4 = (Button)findViewById(R.id.q1b4);
		
		ArrayList newTenQs = null;
		newTenQs = Question.getArrayList();
		TextView tv = (TextView)findViewById(R.id.q1tv);
		tv.setText(newTenQs.get(0).toString());
				
		button1.setonclickListener(new onclickListener() {	 
			public void onclick(View arg0) {
			    Intent intent1 = new Intent(Q1.this, Q2.class);
                startActivity(intent1);   
			}
		});
		
		button2.setonclickListener(new onclickListener() {	 
			public void onclick(View arg0) {
			    Intent intent2 = new Intent(Q1.this, Q2.class);
                startActivity(intent2);   
			}
		});
		
		button3.setonclickListener(new onclickListener() {	 
			public void onclick(View arg0) {
			    Intent intent3 = new Intent(Q1.this, Q2.class);
                startActivity(intent3);   
			}
		});
		
		button4.setonclickListener(new onclickListener() {	 
			public void onclick(View arg0) {
			    Intent intent4 = new Intent(Q1.this, Q2.class);
                startActivity(intent4);   
			}
		});
	}
}



q1main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"  >

    <TextView
        android:id="@+id/q1tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    
    <Button
        android:id="@+id/q1b1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Able"  />

    <Button
        android:id="@+id/q1b2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Cain"  />

    <Button
        android:id="@+id/q1b3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Eve"  />

    <Button
        android:id="@+id/q1b4"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Adam"  />

</LinearLayout>



LogCat
11-08 17:32:06.022: W/dalvikvm(2092): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
11-08 17:32:06.092: E/AndroidRuntime(2092): FATAL EXCEPTION: main
11-08 17:32:06.092: E/AndroidRuntime(2092): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test/com.example.test.Q1}: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
11-08 17:32:06.092: E/AndroidRuntime(2092): 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
11-08 17:32:06.092: E/AndroidRuntime(2092): 	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
11-08 17:32:06.092: E/AndroidRuntime(2092): 	at android.app.ActivityThread.access$600(ActivityThread.java:130)
11-08 17:32:06.092: E/AndroidRuntime(2092): 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
11-08 17:32:06.092: E/AndroidRuntime(2092): 	at android.os.Handler.dispatchMessage(Handler.java:99)
11-08 17:32:06.092: E/AndroidRuntime(2092): 	at android.os.Looper.loop(Looper.java:137)
11-08 17:32:06.092: E/AndroidRuntime(2092): 	at android.app.ActivityThread.main(ActivityThread.java:4745)
11-08 17:32:06.092: E/AndroidRuntime(2092): 	at java.lang.reflect.Method.invokeNative(Native Method)
11-08 17:32:06.092: E/AndroidRuntime(2092): 	at java.lang.reflect.Method.invoke(Method.java:511)
11-08 17:32:06.092: E/AndroidRuntime(2092): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-08 17:32:06.092: E/AndroidRuntime(2092): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-08 17:32:06.092: E/AndroidRuntime(2092): 	at dalvik.system.NativeStart.main(Native Method)
11-08 17:32:06.092: E/AndroidRuntime(2092): Caused by: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
11-08 17:32:06.092: E/AndroidRuntime(2092): 	at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
11-08 17:32:06.092: E/AndroidRuntime(2092): 	at java.util.ArrayList.get(ArrayList.java:304)
11-08 17:32:06.092: E/AndroidRuntime(2092): 	at com.example.test.Q1.onCreate(Q1.java:29)
11-08 17:32:06.092: E/AndroidRuntime(2092): 	at android.app.Activity.performCreate(Activity.java:5008)
11-08 17:32:06.092: E/AndroidRuntime(2092): 	at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
11-08 17:32:06.092: E/AndroidRuntime(2092): 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
11-08 17:32:06.092: E/AndroidRuntime(2092): 	... 11 more
11-08 17:32:06.582: D/dalvikvm(2092): GC_CONCURRENT freed 140K, 3% free 8273K/8519K, paused 112ms+24ms, total 449ms


Was This Post Helpful? 0
  • +
  • -

#13 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10563
  • View blog
  • Posts: 39,087
  • Joined: 27-December 08

Re: Trivia Game - random set of 10 questions from list of 100 questions

Posted 13 November 2012 - 11:34 AM

Quote

public static ArrayList<Question> tenQs = new ArrayList<Question>(10)

Having this in your Question class is a very bad design, as we've tried to tell you. There should be a separate class to store things like the Questions, number correct, number missed, etc.

This is the design we've been pushing you towards.
class Question{
   private String[] answers;
   private int correctAnswer;
   private String query;
}

class Quiz{
  
    private List<Question> questions;
    private int numCorrect; 
    private int numAttempted;
    private List<Question> missedQuestions;
}

class QuestionView extends LinearLayout{

    private RadioButton[] choices;
    private EditText questionLabel;

    public QuestionView(Activity act, String[] choices, String question){
        //code
    }

    public int getSelectedAnswer(){
        //code
    }
}



Quote

ArrayList newTenQs = null;

And don't ignore the generics. They're there for a reason.
Was This Post Helpful? 1
  • +
  • -

#14 mattlyons  Icon User is offline

  • D.I.C Regular

Reputation: 6
  • View blog
  • Posts: 301
  • Joined: 10-September 09

Re: Trivia Game - random set of 10 questions from list of 100 questions

Posted 16 November 2012 - 12:09 PM

Hey guys, thanks for all the help. I cannot seem to figure out how to do it this way so I will just go with my first structure in my opening post and randomly pick a set of predefined 10 questions where each question is its own class. Later when I get this app working fully I will come back to this and try to restructure it to how you guys were telling me to.

Thank you for your time!
Was This Post Helpful? 0
  • +
  • -

#15 TheGDeveloper  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 93
  • Joined: 22-September 09

Re: Trivia Game - random set of 10 questions from list of 100 questions

Posted 18 November 2012 - 06:28 AM

this is not a good idea. Just download a book about data structures and algorithms and spend 2-3 weak to learn how to program properly.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2