Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class admin {
- private Random rand = new Random();
- static ArrayList<String> trivia = new ArrayList<String>();// Will be unedited just to access the questions.txt
- private final int row = 10;
- private final int col = 5;
- private String[][] game = new String[row][col];
- public admin() {
- }
- public void addQandA(String q, String ca, String wa1, String wa2, String wa3) {// adds the questions and answers from game class into the arraylist
- trivia.add(q);
- trivia.add(ca);
- trivia.add(wa1);
- trivia.add(wa2);
- trivia.add(wa3);
- }
- public int databaseSize() {
- return trivia.size() / 5;
- }
- public String displayQuestion(int n) {
- return trivia.get(n * 5);
- }
- public String displayCorAns(int n) {
- return trivia.get((n * 5) + 1);
- }
- public String displayWrongOne(int n) {
- return trivia.get((n * 5) + 2);
- }
- public String displayWrongTwo(int n) {
- return trivia.get((n * 5) + 3);
- }
- public String displayWrongThree(int n) {
- return trivia.get((n * 5) + 4);
- }
- //removes a question and its answers with a user input of n
- public static void removeQuestion(int n) {
- trivia.remove(n * 5);
- trivia.remove(n * 5);
- trivia.remove(n * 5);
- trivia.remove(n * 5);
- trivia.remove(n * 5);
- }
- public void printDatabase(int i) {
- int questionNum = i + 1;
- System.out.println("Question " + questionNum + ":\n\tQuestion: " + this.displayQuestion(i));
- System.out.println("\tCorrect Answer: " + this.displayCorAns(i));
- System.out.println("\tWrong Answer 1: " + this.displayWrongOne(i));
- System.out.println("\tWrong Answer 2: " + this.displayWrongTwo(i));
- System.out.println("\tWrong Answer 3: " + this.displayWrongThree(i));
- }
- public static boolean isNumeric(String strNum) {
- try {
- Integer.parseInt(strNum);
- } catch (NumberFormatException | NullPointerException nfe) {
- return false;
- }
- return true;
- }
- public void createGame() {// takes 10 random question and answers from the array list and adds it to the 2 dim array
- for (int i = 0; i < row; i++) {
- game[i][0] = displayQuestion(i);
- game[i][1] = displayCorAns(i);
- game[i][2] = displayWrongOne(i);
- game[i][3] = displayWrongTwo(i);
- game[i][4] = displayWrongThree(i);
- }
- }
- public void randAnswers() {
- ArrayList<String> triviaTemp = new ArrayList<>(4);
- ArrayList<String> triviaTemp1 = new ArrayList<>();
- ArrayList<String> triviaTemp2 = (ArrayList<String>)trivia.clone();
- for (int i = 0; i < this.databaseSize(); i++) {
- //Shuffles answers
- triviaTemp.clear();
- triviaTemp.add(this.displayCorAns(i));
- triviaTemp.add(this.displayWrongOne(i));
- triviaTemp.add(this.displayWrongTwo(i));
- triviaTemp.add(this.displayWrongThree(i));
- Collections.shuffle(triviaTemp);
- triviaTemp1.add(triviaTemp.get(0));
- triviaTemp1.add(triviaTemp.get(1));
- triviaTemp1.add(triviaTemp.get(2));
- triviaTemp1.add(triviaTemp.get(3));
- triviaTemp2.set((i * 5) + 1, triviaTemp1.get(i * 4));
- triviaTemp2.set((i * 5) + 2, triviaTemp1.get(i * 4 + 1));
- triviaTemp2.set((i * 5) + 3, triviaTemp1.get(i * 4 + 2));
- triviaTemp2.set((i * 5) + 4, triviaTemp1.get(i * 4 + 3));
- }
- this.createGame();//creates 2 dim game array
- }
- public void display2dArray() {
- for (int row = 0; row < game.length; row++) {
- for (int column = 0; column < game[row].length; column++) {
- System.out.print(game[row][column] + "\t");
- }
- System.out.println();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement