Results 1 to 4 of 4
Thread: Help With Objects
- 12-12-2012, 01:59 PM #1
Member
- Join Date
- Dec 2012
- Posts
- 8
- Rep Power
- 0
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!"); } } }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?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"); } }
Thanks!
KreekLast edited by Kreek; 12-12-2012 at 02:07 PM.
- 12-12-2012, 04:28 PM #2
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; } }
- 12-12-2012, 05:02 PM #3
Member
- Join Date
- Dec 2012
- Posts
- 8
- Rep Power
- 0
Re: Help With Objects
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?
- 12-12-2012, 05:11 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,458
- Rep Power
- 16
Similar Threads
-
Simulating objects selecting objects
By Asef in forum New To JavaReplies: 7Last Post: 10-27-2012, 07:05 PM -
Create objects. What happens to objects on close
By rru96 in forum New To JavaReplies: 1Last Post: 10-13-2012, 09:46 AM -
Noob question - Create objects using objects as parameters
By pantaloc in forum New To JavaReplies: 12Last Post: 04-29-2012, 02:55 PM -
How to map 2 different objects together?
By einnhann in forum New To JavaReplies: 5Last Post: 12-22-2009, 09:42 AM -
read txt file,with some records, create objects and store objects in tables of a db.
By stamv in forum JDBCReplies: 1Last Post: 01-22-2009, 04:25 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks