Anonymous Anonymous - 3 months ago
202 0

An IQ level test program that outputs an IQ level message based on users' scores.

Java

codeplus

//Made by khay
//Codeplus Academy

//This program asks users some particular questions
//And outputs an IQ level message based on users' scores

import java.util.Scanner;

public class IqTest {
    
    static Scanner userInput = new Scanner(System.in);
    static String subjectAttempt = "", givenAnswer = "";
    private static String correctAnswer = "";
    
    public static void main(String[] args) {
        if(testAttempt()) {
            String option;
            int totalScore = 0;
            displayMenu();
            option = option();
            
            while(true) {
                if(option.equals("1") && !subjectAttempt.contains(option)) {
                    totalScore += aptitudeScore();
                    System.out.println("Score recorded.");
                    pause(1); //Pauses for 1 second
                    
                } else if(option.equals("2") && !subjectAttempt.contains(option)) {
                    totalScore += englishScore();
                    System.out.println("Score recorded.");
                    pause(1);
                    
                } else if(option.equals("3") && !subjectAttempt.contains(option)) {
                    totalScore += mathScore();
                    System.out.println("Score recorded.");
                    pause(1);
                    
                } else if(option.equals("4") && !subjectAttempt.contains(option)) {
                    totalScore += gkScore();
                    System.out.println("Score recorded.");
                    pause(1);
                    
                } else if(option.equals("5")) {
                    System.out.println("Total marks: " + totalScore);
                    break;
                } else {
                    System.out.println("You have already attempted this question,"
                            + " please choose another option.");
                    pause(2);
                }
                
                if(subjectAttempt.contains("1234")) {
                   
                    System.out.println("\nAll questions answered. Enter "
                            + "5 to exit and see results: ");
                    option = option();
                    while(!option.equals("5")) {
                        System.out.println("Enter 5 to exit and see results.");
                        option = option();
                    }
                    continue;
                }
                    
                displayMenu();
                option = option();
            }
            
            System.out.println("Bonus points earned: " + bonusPoints(totalScore));
            System.out.println("Total score out of 50: " + totalScoreOver50
        (totalScore, bonusPoints(totalScore)));
            iqMessage(totalScoreOver50(totalScore, bonusPoints(totalScore)));
        
        
        } else {
            System.out.println("You have exceeded the number of attempts allowed.");
        }    
    } 
    
    public static void pause(int value) {
        try {
                Thread.sleep(value * 1000);
            } catch(InterruptedException ex) {
                Thread.currentThread().interrupt();
            }
    }
    
    // Checks if IQ test has been attempted more than once
    public static boolean testAttempt() {
        String numbers = "0123456789";
        String checkAttempt;
        System.out.println("How many times have you attempted the IQ test? "
                + "Enter 0 if no attempt yet.");
        checkAttempt = userInput.next();
        while(!numbers.contains(checkAttempt)) {
            System.out.println("Please enter a valid number");
            checkAttempt = userInput.next();   
        }
        if(checkAttempt.equals("0") || checkAttempt.equals("1")) {
            return true;
        } else {
            return false;
        }
    }
    
    public static void displayMenu() {
        System.out.println("\n1. Aptitude \n2. English \n3. Math "
                + "\n4. GK \n5. Exit \nPlease enter an option in the range "
                + "1-5\n");
    }
    
    public static int aptitudeScore() {
        int score = 0;
        subjectAttempt += "1";
        correctAnswer = "100";
        System.out.println("\nAptitude Question \nThere are two examinations "
                + "rooms A and B. If 10 students are sent from A to B, then the"
                + " number of students in each room is the same. If 20 "
                + "candidates are sent from B to A, then the number of students"
                + " in A is double the number of students in B. The number "
                + "of students in room A is:"); 
        givenAnswer = userInput.next().toLowerCase();
        if(correctAnswer.equals(givenAnswer)) {
            score += 10;
            return score;
        } else {
            return score;
        } 
    }
    
    public static int englishScore() {
        int score = 0;
        subjectAttempt += "2";
        correctAnswer = "snake";
        System.out.println("\nEnglish Question \n"
                + "Which one of these animals is least like the other four?\n"
                + "BEAR - SNAKE - COW - DOG - TIGER"); 
        givenAnswer = userInput.next().toLowerCase();
        if(correctAnswer.equals(givenAnswer)) {
            score += 10;
            return score;
        } else {
            return score;
        } 
    }
    
    public static int mathScore() {
        int score = 0;
        subjectAttempt += "3";
        correctAnswer = "3";
        System.out.println("\nMath Question\nIf \na + b = 76 "
                    + "\na - b = 38 \na / b = ...?"); 
        givenAnswer = userInput.next().toLowerCase();
        if(correctAnswer.equals(givenAnswer)) {
            score += 10;
            return score;
        } else {
            return score;
        } 
    }
    
    public static int gkScore() {
        int score = 0;
        subjectAttempt += "4";
        correctAnswer = "1960";
        System.out.println("\nGeneral Knowledge Question"
                    + "\nWhat year did Nigeria gain independence?");
        givenAnswer = userInput.next().toLowerCase();
        if(correctAnswer.equals(givenAnswer)) {
            score += 10;
            return score;
        } else {
            return score;
        } 
    }
    
    public static int bonusPoints(int totalScore) {
        if (totalScore == 10 || totalScore == 0) {
            return 0;
        } else if(totalScore == 20) {
            return 2;
        } else if(totalScore == 30) {
            return 5;
        } else {
            return 10;
        }
    }
    
    public static int totalScoreOver50(int totalScore, int bonusPoint) {
        return totalScore + bonusPoint;
    }
    
    public static void iqMessage(int totalScoreOver50) {
        if(totalScoreOver50 == 0) {
            System.out.println("You need to re-attempt the test.");
        } else if(totalScoreOver50 == 10) {
            System.out.println("Your IQ level is below average.");
        } else if(totalScoreOver50 == 22) {
            System.out.println("Your IQ level is average.");
        } else if(totalScoreOver50 == 35) {
            System.out.println("You are intelligent.");
        } else {
            System.out.println("You are a genius.");
        }
    }
    
    public static String option() {
        String allowedOptions = "12345";
        String option = userInput.next();
        while(allowedOptions.indexOf(option) < 0) {
            System.out.println("Please enter a valid option: ");
            option = userInput.next();
        }
        return option;
    }
}
Comments