Quote (Kellye @ Apr 14 2014 09:05pm)
It's not that it is not working. It's that I need to do it object oriented. The questions for the trivia game are unspecified and coming from a text file. I will post my github so you can read the instructions and maybe better understand
the idea of what I am looking for.
The code from github has since been deleted and am reworking. It seemed a bit too procedural.
https://github.com/green-brad/triviagameYou want to think object oriented? It is actually pretty easy. Easiar, and more intuitive than procedural in my opinion. Because we do it intuitively.
What is an object? It is an abstract model of something. An object contains
fields which define what they
are and
methods which define what the object can
do.
Let's define some objects.
Your trivia game.
Quote
Write a trivia game that asks two players a set of multiple choice questions.
Players will take turns answering multiple choice questions. When a player
answers correctly, that player gets 1 point. The player does not lose points
for answering incorrectly. After all of the questions are answered, display
the number of points each player earned and declare a winner.
I identified 3 things we can encapsulate as objects above:
Game
Player
Question
Let's abstract them out further.
What is a game? What does it have? What does it do?
A Game has
-Players
- Questions
A Game does
-selects a player to ask a question (game logic, turn based)
-determines a winner
What is a Player? What does it have? What does it do?
A player has
-A name (identifier)
-points
A player does
-answers a question
What is a Question? What does it have? What does it do?
A question has
- multiple choices
-an answer
A question does
-determine if it was answered correctly (a stretch, but go with it)
So can we wrap these objects up?
Code
public class Question {
//a question has question text
private String text;
//a question has multiple choices
private String choices;
//a question has an answer
private String answer;
public Question(String theText, String theChoices, String theAnswer)
{
text = theText;
choices = theChoices;
answer = theAnswer;
}
//a question can determine if its answer is matched
public boolean isCorrect(String guess)
{
return guess.equals(answer);
}
//a question is represented by its text and choices
public String toString()
{
return text + "\n" + choices;
}
}
Code
import java.util.Scanner;
public class Player {
//a player has a name
private String name;
private int points;
public Player(String theName)
{
//set up this player with a name and 0 points
name = theName;
points = 0;
}
//players can answer questions
public String answerQuestion(Question q)
{
Scanner in = new Scanner(System.in);
System.out.println(q);
String answer = in.nextLine();
return answer;
}
//players accumulate points
public void addPoint()
{
points++;
}
//players tell how many points they have
public int getPoints()
{
return points;
}
//players are represented by their name
public String toString()
{
return name;
}
}
Code
public class Game {
//a game has players
private Player[] players;
//a game has questions
private Question[] questions;
public Game()
{
//when a game is created, it must intialize its players and questions
//create 2 players
players = new Player[]{ new Player("Bob"), new Player("Mary")};
//create 5 questions
questions = new Question[] {new Question("What color is the sky?", "A. Blue, B. Red, C. Yellow","A"),
new Question("What color is the grass?", "A. Blue, B. Red, C. Green","C"),
new Question("What color is the Sun?", "A. Blue, B. Yellow, C. Red","B"),
new Question("What color is the Moon?", "A. White, B. Red, C. Yellow","A"),
new Question("What color is the dirt?", "A. Blue, B. Brown, C. Yellow","B")
};
}
//a game asks each player a question until all questions are asked
public void playGame()
{
String answer;
for(int q = 0; q < questions.length; q++)
{
for(int p = 0; p < players.length; p++)
{
//Ask first player a question and judge his answer
System.out.println(players[p] + " please answer this question:");
answer = players[p].answerQuestion(questions[q]);
if(questions[q].isCorrect(answer))
players[p].addPoint();
}
}
if(players[0].getPoints() > players[1].getPoints())
System.out.println(players[0] + " wins the game.");
else if(players[0].getPoints() < players[1].getPoints())
System.out.println(players[1] + " wins the game.");
else
System.out.println("It was a tie");
}
}
Objects.