Results 1 to 4 of 4
  1. #1
    Kreek is offline Member
    Join Date
    Dec 2012
    Posts
    8
    Rep Power
    0

    Default Help With Objects

    Hello everyone! I've recently been learning and trying to grasp the concept of OOP, by just simply writing programs that involves objects and constructors. I've pretty much finished this program, except I'm not sure as to how to access a private variable in it. Here's the code:

    Java Code:
    import java.util.Scanner;
    public class TextBasedGameQuestions {
        private String question, correctAnswer, userAnswer, choiceA, choiceB, choiceC;
        private int ammountCorrect;
        private Scanner input = new Scanner(System.in);
        public TextBasedGameQuestions(String q, String a, String cA, String cB,
                String cC) {
            question = q;
            correctAnswer = a;
            choiceA = cA;
            choiceB = cB;
            choiceC = cC;
        }
        public void printQuestion() {
            System.out.println(question);
            System.out.println(choiceA);
            System.out.println(choiceB);
            System.out.println(choiceC);
            userAnswer = input.next();
        }
        public String checkAnswer() {
            if (userAnswer.equalsIgnoreCase(correctAnswer)) {
                ammountCorrect++;
                return "Correct!";
            }
            else {
                return "Incorrect!";
            }
        }            
        public void checkNerdLevels() {
            if (ammountCorrect == 0) {
                System.out.println("Your nowhere near being a nerd.");
            }
            else if (ammountCorrect > 0 && ammountCorrect <= 3) {
                System.out.println("Your on your way to being a nerd!");
            }
            else if (ammountCorrect > 4 && ammountCorrect <= 7) {
                System.out.println("Your a nerd in training. Keep studying your Klingon!");
            }
            else if (ammountCorrect > 8 && ammountCorrect <= 9) {
                System.out.println("Your so much of a nerd you can speak fluently in java script!");
            }
            else if (ammountCorrect == 10) {
                System.out.println("Your so much of a nerd you give everyone the vulcan hand signal as you walk by on the street! Live long and prosper!");
            }
        }
    }
    Java Code:
    public class TextBasedGameTester {
        public static void main(String [] args) {
            TextBasedGameQuestions q1 = new TextBasedGameQuestions("How many bits are in a byte?", "B", "A: 4", "B: 8", "C: 16");
            q1.printQuestion();
            String result1 = q1.checkAnswer();
            System.out.println(result1 + "\n");
            
            TextBasedGameQuestions q2 = new TextBasedGameQuestions("Computers calculate numbers in what mode?", "C", "A: Decimal", "B: Octal", "C: Binary");
            q2.printQuestion();
            String result2 = q2.checkAnswer();
            System.out.println(result2 + "\n");
            
            TextBasedGameQuestions q3 = new TextBasedGameQuestions("RAM stands for what?", "A", "A: Random Access Memory", "B: Really Annoying Machine", "C: Read A Manual");
            q3.printQuestion();
            String result3 = q3.checkAnswer();
            System.out.println(result3 + "\n");
            
            TextBasedGameQuestions q4 = new TextBasedGameQuestions("ROM stands for what?", "B", "A: Royal Ontario Museum", "B: Read Only Memory", "C: Read on Monday");
            q4.printQuestion();
            String result4 = q4.checkAnswer();
            System.out.println(result4 + "\n");
             
            TextBasedGameQuestions q5 = new TextBasedGameQuestions("Which of the following is NOT a programming language?", "C", "A: Basic", "B: Java", "C: Knuth");
            q5.printQuestion();
            String result5 = q5.checkAnswer();
            System.out.println(result5 + "\n");
            
            TextBasedGameQuestions q6 = new TextBasedGameQuestions("What does WWW stand for?", "A", "A: World Wide Web", "B: World Wide Wrestling", "C: Wacky, Wild, Wonderful");
            q6.printQuestion();
            String result6 = q6.checkAnswer();
            System.out.println(result6 + "\n");
            
            TextBasedGameQuestions q7 = new TextBasedGameQuestions("Which is NOT an internet protocol?", "C", "A: HTTP", "B: FTP", "C: STP");
            q7.printQuestion();
            String result7 = q7.checkAnswer();
            System.out.println(result7 + "\n");
            
            TextBasedGameQuestions q8 = new TextBasedGameQuestions("Which was NOT an early mainframe computer?", "C", "A: ENIAC", "B: UNIVAC", "C: BRANIAC");
            q8.printQuestion();
            String result8 = q8.checkAnswer();
            System.out.println(result8 + "\n");
            
            TextBasedGameQuestions q9 = new TextBasedGameQuestions("What does CPU stand for?", "B", "A: Cute People United", "B: Central Processing Unit", "C: Computer Power User");
            q9.printQuestion();
            String result9 = q9.checkAnswer();
            System.out.println(result9 + "\n");
            
            TextBasedGameQuestions q10 = new TextBasedGameQuestions("Which of these is NOT a computer?", "C", "A: Macintosh", "B: Acorn", "C: Paseo");
            q10.printQuestion();
            String result10 = q10.checkAnswer();
            System.out.println(result10 + "\n");
            
            
        }
    }
    Now the output and point of the program works correctly. It's simply a program that asks the user 10 questions. However, I am trying to get the program to count how many questions you get correctly, throughout the 10 questions. Then with if statements, see if the amount correct is between say 0-3, 4-7, etc. Anyone know how I would do that correctly here?

    Thanks!
    Kreek
    Last edited by Kreek; 12-12-2012 at 02:07 PM.

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: Help With Objects

    To access private fields, you need to write accessor methods, for example:

    Java Code:
    public class SomeClass{
        //Private field
        private int age;
    
        //accessor methods:
        public int getAge(){
            return age;
        }
    
        public void setAge(int age){
            this.age = age;
        }
    }

  3. #3
    Kreek is offline Member
    Join Date
    Dec 2012
    Posts
    8
    Rep Power
    0

    Default Re: Help With Objects

    Quote Originally Posted by quad64bit View Post
    To access private fields, you need to write accessor methods, for example:

    Java Code:
    public class SomeClass{
        //Private field
        private int age;
    
        //accessor methods:
        public int getAge(){
            return age;
        }
    
        public void setAge(int age){
            this.age = age;
        }
    }
    Ok but how would I call the method? Wouldn't I have to use one of the objects such as q10 and then call upon the checKNerdLevels() method such as q10.checkNerdLevels();. However, wouldn't that reset the ammountCorrect variable to 0, since it isn't used in the actual object?

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,798
    Rep Power
    19

    Default Re: Help With Objects

    Your checkNerdLevels is in the wrong place, I would say.
    Your TextBasedGameQuestions class is actual a single Question, so having a method that calculates how many questions are correct in it is not really correct.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Simulating objects selecting objects
    By Asef in forum New To Java
    Replies: 7
    Last Post: 10-27-2012, 07:05 PM
  2. Create objects. What happens to objects on close
    By rru96 in forum New To Java
    Replies: 1
    Last Post: 10-13-2012, 09:46 AM
  3. Replies: 12
    Last Post: 04-29-2012, 02:55 PM
  4. How to map 2 different objects together?
    By einnhann in forum New To Java
    Replies: 5
    Last Post: 12-22-2009, 09:42 AM
  5. Replies: 1
    Last Post: 01-22-2009, 04:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •