Click here to Skip to main content
15,884,099 members
Home / Discussions / Java
   

Java

 
Questionweb services Pin
benjamin yap5-Aug-09 17:47
benjamin yap5-Aug-09 17:47 
QuestionHelp needed with a source code Pin
Ogunjimi Adewale5-Aug-09 12:06
Ogunjimi Adewale5-Aug-09 12:06 
AnswerRe: Help needed with a source code Pin
fly9045-Aug-09 23:30
fly9045-Aug-09 23:30 
QuestionCORRECTION FOR MA CODE: Dr Phils Personality test Pin
Ogunjimi Adewale6-Aug-09 9:57
Ogunjimi Adewale6-Aug-09 9:57 
AnswerRe: CORRECTION FOR MA CODE: Dr Phils Personality test Pin
fly9046-Aug-09 10:10
fly9046-Aug-09 10:10 
GeneralRe: CORRECTION FOR MA CODE: Dr Phils Personality test Pin
Ogunjimi Adewale7-Aug-09 10:09
Ogunjimi Adewale7-Aug-09 10:09 
GeneralRe: CORRECTION FOR MA CODE: Dr Phils Personality test Pin
fly9047-Aug-09 11:39
fly9047-Aug-09 11:39 
GeneralRe: CORRECTION FOR MA CODE: Dr Phils Personality test Pin
fly9047-Aug-09 11:56
fly9047-Aug-09 11:56 
You may be interested in having a look at one of my first ever programs in Java which was a quiz, similar to this, which tallied up someones lifestyle. I have changed a couple of variable names and changed the questions but it is still the same code. Have a read of it to see a more generic approach.

DrPhilQuiz.java
import java.util.ArrayList;
import java.util.Scanner;

public class DrPhilQuiz extends ArrayList<Question> {

	private static final long serialVersionUID = 4452314777184430591L;
	
	public DrPhilQuiz()
	{
		setQuestions();
	}
	
	private void setQuestions()
	{
		this.add(
				new Question(
						"When do you feel the happiest?",
						new AnswerOption("In the morning.", 2),
						new AnswerOption("In the afternoon / earliy evening.", 4),
						new AnswerOption("Late at night.", 6)
				)
		);
		this.add(
				new Question(
						"When do you feel the happiest?",
						new AnswerOption("In the morning.", 2),
						new AnswerOption("In the afternoon.", 2),
						new AnswerOption("In the earliy evening.", 2),
						new AnswerOption("Late at night.", 2)
				)
		);
	}
	
	public void startQuiz()
	{
		int count = 1, score = 0;
		Scanner input = new Scanner(System.in);
		for (Question q : this)
		{
			System.out.println(count + ") " +q.getQuestion());
			System.out.println(q.getAnswers());
			
			while (!q.setAnswer(input.nextLine().charAt(0)))
				System.out.println("Invalid choice");
			
			score += q.getScore();
			
			count++;
		}
		input.close();
		
		System.out.println("\nYou have finished the quiz!");
		System.out.println("You Scored " + score);
	}

	public static void main(String[] args)
	{
		DrPhilQuiz dpq = new DrPhilQuiz();
		dpq.startQuiz();
	}
}


Question.java
public class Question {

	private String question = "";
	private AnswerOption[] answers = null;

	private char answer = 'a';
	
	public Question(String q, AnswerOption... a)
	{
		question = q;
		answers = a;
	}
	
	public String getQuestion()
	{
		return question;
	}
	public String getAnswers()
	{
		String ret = "";
		for (int i = 0; i < answers.length; i++)
			ret += (char)(i + 97) + ") " + answers[i].getAnswerOption() + "\n";
		return ret;
	}
	public boolean setAnswer(char a)
	{
		if(97 <= a && a < (97 + answers.length))
		{
			answer = a;
			return true;
		}
		return false;
	}
	public int getScore()
	{
		return answers[answer - 97].getAnswerOptionValue();
	}
}

class AnswerOption
{
	private String answer = "";
	private int value = 0;
	
	public AnswerOption(String a, int v)
	{
		answer = a;
		value = v;
	}
	
	public String getAnswerOption()
	{
		return answer;
	}
	public int getAnswerOptionValue()
	{
		return value;
	}
}


Outputs
1) When do you feel the happiest?
a) In the morning.
b) In the afternoon / earliy evening.
c) Late at night.

c
2) When do you feel the happiest?
a) In the morning.
b) In the afternoon.
c) In the earliy evening.
d) Late at night.

a

You have finished the quiz!
You Scored 8


If at first you don't succeed, you're not Chuck Norris.

Generalthanks Pin
Ogunjimi Adewale8-Aug-09 11:17
Ogunjimi Adewale8-Aug-09 11:17 
QuestionBottom toolbar like facebook application Pin
sangeethanarayan5-Aug-09 1:04
sangeethanarayan5-Aug-09 1:04 
AnswerRe: Bottom toolbar like facebook application Pin
fly9045-Aug-09 2:20
fly9045-Aug-09 2:20 
QuestionJ2me: wireless tookit error. Pin
udombat4-Aug-09 21:24
udombat4-Aug-09 21:24 
AnswerRe: J2me: wireless tookit error. Pin
carlospc19705-Aug-09 4:38
professionalcarlospc19705-Aug-09 4:38 
GeneralRe: J2me: wireless tookit error. Pin
udombat5-Aug-09 5:18
udombat5-Aug-09 5:18 
GeneralRe: J2me: wireless tookit error. Pin
carlospc19705-Aug-09 5:47
professionalcarlospc19705-Aug-09 5:47 
GeneralRe: J2me: wireless tookit error. Pin
udombat5-Aug-09 6:52
udombat5-Aug-09 6:52 
GeneralRe: J2me: wireless tookit error. Pin
carlospc19705-Aug-09 7:05
professionalcarlospc19705-Aug-09 7:05 
QuestionASP.Net with Java Windows Pin
PDTUM4-Aug-09 17:02
PDTUM4-Aug-09 17:02 
QuestionMozilla Firefox Pin
TCSJoe3-Aug-09 18:29
TCSJoe3-Aug-09 18:29 
AnswerRe: Mozilla Firefox Pin
fly9044-Aug-09 7:29
fly9044-Aug-09 7:29 
Questionout of heap Exception Pin
shammasi31-Jul-09 20:24
shammasi31-Jul-09 20:24 
AnswerRe: out of heap Exception Pin
42774803-Aug-09 20:08
42774803-Aug-09 20:08 
GeneralRe: out of heap Exception Pin
shammasi3-Aug-09 20:51
shammasi3-Aug-09 20:51 
QuestionFingerprint Reader Pin
-=spike=-30-Jul-09 19:26
-=spike=-30-Jul-09 19:26 
GeneralRe: Fingerprint Reader Pin
Kujtim Hyseni31-Jul-09 2:50
Kujtim Hyseni31-Jul-09 2:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.