I recently completed a "fun" program where it asks the user questions and outputs answers. I submitted it and my professor said all was great, except he wanted me to implement methods into the program. I have NO idea how o do the methods. Can someone use my code to give me an example or something, or at the very least help me figure it out?

He told me to use a for loop instead of a do while, and told me to use this method.

boolean gotItRight;
gotItRight = doQuestion("Q: Polar bears...",
                   "A: ...", "A: ...", TRUE_NUM);
if(gotItRight) count++;
gotItRight = doQuestion("Q: This quiz...",
                    "A: ...", "A: ...", FALSE_NUM);
if(gotItRight) count++;

This is my program code:

import java.util.*;
public class Fun {
    /**
     * I thought of doing a trvia question type program.
     * The questions I entered, I thought were interesting. 
     * For being a pretty pathetic programmer, I would say I did a good job.
     * It's not up to me though. So...
     * 
     * Please have mercy on my soul.
     */

    public static void main(String args[]) throws Exception {
    
        // THE BEST CONSTANTS IN THE WORLD.
        final String TRUE = "True";
        final int TRUE_NUM = 1;
        
        final String FALSE = "False";
        final int FALSE_NUM = 2;
        
        int input;

        //Displays the welcoming message.
        System.out.println("Anwser the following trivia questions with a true/false input. Since I am");
        System.out.println("a horrible programmer, you will have to enter 1 for True and 2 for False.");
        System.out.println("If you still do not understand, here is a table-thingy to clarify.");
        
        //Displays the TRUE/FALSE table-thingy.
        System.out.printf(" %d. %-20s\n", TRUE_NUM, TRUE);
        System.out.printf(" %d. %-20s\n", FALSE_NUM, FALSE);
        
        //These are the trivia questions with the answers if they entered True or False.
        Scanner scanner = new Scanner(System.in);
        
        int count = 0;
        
        do {
            System.out.println("Q: Polar Bears live in Antarctica."); 
            input = scanner.nextInt();
            if  (input == TRUE_NUM) {
                System.out.println("A: Wow, you're pretty pathetic. Polar Bears live in the North Pole.\n");
                break;
            } else if (input == FALSE_NUM) {
                System.out.println("A: Okay, you're right. But stop searching on Google.\n");
                count = count + 1;
                break;
            } else if (input != TRUE_NUM || input != FALSE_NUM); { 
                System.out.println("Invalid input birdbrain!\n");
                continue;
            }
        }
        while(true);
        
        do {
            System.out.println("Q: The Hundred Years War lasted 100 years."); 
            input = scanner.nextInt();
            if (input == TRUE_NUM) {
                System.out.println("A: Anyone with a half a brain would know it lasted 116 years.\n");
                break;
            } else if (input == FALSE_NUM) {
                System.out.println("A: That's right. But if you don't stop searching on Google, I swear...\n");
                count = count + 1;
                break;
            } else if (input != TRUE_NUM || input != FALSE_NUM); { 
                System.out.println("Invalid input birdbrain!\n");
                continue;
            }
        }
        while(true);
        
        do {    
            System.out.println("Q: This quiz sucks so far, doesn't it?"); 
            input = scanner.nextInt();
            if (input == TRUE_NUM) {
                System.out.println("A: Let's see you do better chump.\n");
                count = count + 1;
                break;
            } else if (input == FALSE_NUM) {
                System.out.println("A: No sweet-talking.");
                count = count + 1;
                break;
            } else if (input != TRUE_NUM || input != FALSE_NUM); { 
                System.out.println("Invalid input birdbrain!\n");
                continue;
            }
        }
        while(true);        
        
        do {    
            System.out.println("Q: 22 people failed the IT 108 Midterm.\n");
            input = scanner.nextInt();
            if (input == TRUE_NUM) {
                System.out.println("A: WRONG! 23 people failed the midterm and most likely cried in the process.\n");
                break;
            } else if (input == FALSE_NUM) {
                System.out.println("A: The midterm was hard wasn't it? 22 other people would agree with you.\n");
                count = count + 1;
                break;
            } else if (input != TRUE_NUM || input != FALSE_NUM); { 
                System.out.println("Invalid input birdbrain!\n");
                continue;
            } 
        }
        while(true);        
        
        do {    
            System.out.println("Q: Christopher Colombus was Spanish."); 
            input = scanner.nextInt();
            if (input == TRUE_NUM) {
                System.out.println("A: You really are stupid. He was Italian. Now, go back read up on your history.\n");
                break;
            } else if (input == FALSE_NUM) {
                System.out.println("A: Yes, you are right. Stop Googling.\n");
                count = count + 1;
                break;
            } else if (input != TRUE_NUM || input != FALSE_NUM); { 
                System.out.println("Invalid input birdbrain!\n");
                continue;
            }   
        }
        while(true);        
        
        do {    
            System.out.println("Q: Ohio was the 17th state to join the Union.");
            input = scanner.nextInt();
            if (input == TRUE_NUM) {
                System.out.println("A: Yeah, but Ohio is boring.\n");
                count = count + 1;
                break;
            } else if (input == FALSE_NUM) {
                System.out.println("A: Wrong. Ohio was the 17th state to join the Union. Stop being dumb.\n");
                break;
            } else if (input != TRUE_NUM || input != FALSE_NUM); { 
                System.out.println("Invalid input birdbrain!\n");
                continue;
            }
        }
        while(true);        
        
        do {   
            System.out.println("Q: George Washington was the first Continental President.");
            input = scanner.nextInt();
            if (input == TRUE_NUM) {
                System.out.println("A: Trick question, but you're wrong. John Hanson was the first Contintental");
                System.out.println("President.\n");
                break;
            } else if (input == FALSE_NUM) {
                System.out.println("A: I'm actually surprised yuo know the answer. I'm sure the INTERWEBZ had"); 
                System.out.println("nothing to do with it. Right?\n");
                count = count + 1;
                break;
            } else if (input != TRUE_NUM || input != FALSE_NUM); { 
                System.out.println("Invalid input birdbrain!\n");
                continue;
            } 
        }
        while(true);        
        
        do {    
            System.out.println("Q: GMU's mascot is Gunston."); 
            input = scanner.nextInt();
            if (input == TRUE_NUM) {
                System.out.println("A: Gunston has bene retired. You go to this school, and don't know");
                System.out.println("anything about it. How patheic.\n");
                break;
            } else if (input == FALSE_NUM) {
                System.out.println("A: You've got Patriot Pride!...That sounded lame.\n");
                count = count + 1;
                break;
            } else if (input != TRUE_NUM || input != FALSE_NUM); { 
                System.out.println("Invalid input birdbrain!\n");
                continue;
            }
        }
        while(true);        
        
        do {    
            System.out.println("Q: Elephants are not scared of field mice.");
            input = scanner.nextInt();
            if (input == TRUE_NUM) {
                System.out.println("A: You didn't watch any cartoons growing up? Had a dull life I guess.\n");
                break;
            } else if (input == FALSE_NUM) {
                System.out.println("A: Yes, it is true, Elephants are scared of mice. Cartoons can depict");
                System.out.println("reality sometimes.\n");
                count = count + 1;
                break;
            } else if (input != TRUE_NUM || input != FALSE_NUM); { 
                System.out.println("Invalid input birdbrain!\n");
                continue;
            }
        }
        while(true);        
        
        do {    
            System.out.println("Q: The Blue Whale is the largest fish in the world."); 
            input = scanner.nextInt();
            if (input == TRUE_NUM) {
                System.out.println("A: YOU REALLY ARE STUPID! WHALES ARE MAMMALS!\n");
                break;
            } else if (input == FALSE_NUM) {
                System.out.println("A: Okay, you're smarter than the average 108 student.\n");
                count = count + 1;
                break;
            } else if (input != TRUE_NUM || input != FALSE_NUM); { 
                System.out.println("Invalid input birdbrain!\n");
                continue;
            }
        }
        while(true);        
        
        do {    
            System.out.println("Q: Algae produces most of Earth's oxygen."); 
            input = scanner.nextInt();
            if (input == TRUE_NUM) {
                System.out.println("A: Yes, Algae produces about 75% of Earth's oxygen.\n");
                count = count + 1;
                break;
            } else if (input == FALSE_NUM) {
                System.out.println("A: No, you're wrong. Algae produces 75% of Earth's oxygen.\n");
                break;
            } else if (input != TRUE_NUM || input != FALSE_NUM); { 
                System.out.println("Invalid input birdbrain!\n");
                continue;
            }
        }
        while(true);        
        
        do {   
            System.out.println("Q: Humans only have two nostrils"); 
            input = scanner.nextInt();
            if (input == TRUE_NUM) {
                System.out.println("A: Nope. Just because you can't see them, doesn't mean they don't exist.");
                System.out.println("We have 4 nostrils.\n");
                break;
            } else if (input == FALSE_NUM) {
                System.out.println("A: STOP SEARCHING ON GOOGLE!\n");
                count = count + 1;
                break;
            } else if (input != TRUE_NUM || input != FALSE_NUM); { 
                System.out.println("Invalid input birdbrain!\n");
                continue;
            }
        }
        while(true);
        
        do {    
            System.out.println("Q: Chickens can live two years without it's head."); 
            input = scanner.nextInt();
            if (input == TRUE_NUM) {
                System.out.println("A: Yes, chickens can live without their heads if their jugular is still intact.\n");
                count = count + 1;
                break;
            } else if (input == FALSE_NUM) {
                System.out.println("A: Nope. Chickens can live about two years without it's head if you force");
                System.out.println("feed it and it's jugular is still intact.\n");
                break;
            } else if (input != TRUE_NUM || input != FALSE_NUM); { 
                System.out.println("Invalid input birdbrain!\n");
                continue;
            }
        }
        while(true);        
        
        do {    
            System.out.println("Q: This quiz has gotten more interesting."); 
            input = scanner.nextInt();
            if (input == TRUE_NUM) {
                System.out.println("A: I'm actually quite flattered.\n");
                count = count + 1;
                break;
            } else if (input == FALSE_NUM) {
                System.out.println("A: Listen nitwit, I tried hard on this.\n");
                count = count + 1;
                break;
            } else if (input != TRUE_NUM || input != FALSE_NUM); { 
                System.out.println("Invalid input birdbrain!\n");
                continue;
            }
        }
        while(true);        
        
        do {    
            System.out.println("Q: More than half the class will fail this semester."); 
            input = scanner.nextInt();
            if (input == TRUE_NUM) {
                System.out.println("A: Talk about being a pessimistic buzz-kill.\n");
                count = count + 1;
                break;
            } else if (input == FALSE_NUM) {
                System.out.println("A: That's attitude you should have. Although, it is highly unlikely...\n");
                count = count + 1;
                break;
            } else if(input != TRUE_NUM || input != FALSE_NUM); {
                System.out.println("Invalid input birdbrain!\n");
                continue;
            } 
        }
        while(true);        
        
        do {   
            System.out.println("Q: I will get an 'A' on this program.");
            input = scanner.nextInt();
            if (input == TRUE_NUM) {
                System.out.println("A: Okay, I know it sucks. But thanks for the false hope.\n");
                count = count + 1;
                break;
            } else if (input == FALSE_NUM) {
                System.out.println("A: Due to this, I dislike you greatly, but you're probably right.\n");
                count = count + 1;
                break;
            } else if (input != TRUE_NUM || input != FALSE_NUM); { 
                System.out.println("Invalid input birdbrain!\n");
                continue;
            }
        }
        while(true);        
       
        //Displays the number of correct answers to the user.
        System.out.println("Out of 16 questions, your number of correct answers: " +count);

        // Displays end message
        System.out.println("I hope you enjoyed my attempt at making an interesting program.\n");
    }
}

I'm not asking you guys to do my HW, but I just want to know how to implement the method so I can get started.

Recommended Answers

All 2 Replies

make methods which contains the question a method for each question
and return true if correct and false if wrong and other method to calculate the result

Look at the suggested code. It's calling a method, passing a question, two answers, and a last param showing which answer is correct.
You need to create the method with those 4 parameters so it shows the Q and the A's, gets the reply, and finally returns true or false depending on whether the user got it right or not. Then you can just call that method for each actual question without repeating all the code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.