943,565 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 1543
  • Java RSS
Apr 30th, 2008
0

java code help 2;;

Expand Post »
instruction for quiz
Next, write a class Quiz that represents a quiz consisting of true/false questions. This class should have exactly two instance variables: a one-dimensional array of TFQuestions that stores all the true/false questions of the quiz, and an int variable that records the number of true/false questions in the array (because the array may contain unused cells). This class should provide the following public methods:

* public Quiz(int capacity): this constructor initializes the two instance variables by creating an array of TFQuestions of size capacity and setting the number of true/false questions to zero.
* public boolean add(String questionStmt, boolean correctAnswer): this method creates a TFQuestion object representing a true/false question with statement questionStmt and correct answer correctAnswer, inserts this question into the array, and returns true. However, if the array is already full, then no TFQuestion object will be created and added to the array, and this method should return false instead.
* public double play(): this method allows the user to take the quiz once. It should display a true/false question on the screen, prompt the user to enter the answer for that question in the form "t" (for true) or "f" (for false), then display the next question, and so on. The questions should be displayed in the same order that they were added to the array. Finally, this method should return the score as the percentage of questions that have been answered correctly. Note that if the array contains no true/false questions, then this method should display the sentence "This quiz has no questions!" and simply return 100%.

please help me

so far i got code for tfquestion because someone helped me in this site
Java Syntax (Toggle Plain Text)
  1. public class TFQuestion {
  2. String tfQuestion;
  3. boolean cAnswer;
  4.  
  5.  
  6. public TFQuestion(String tfQtion, boolean tfvalue)
  7. {
  8.  
  9. tfQuestion = tfQtion;
  10.  
  11. cAnswer = tfvalue;
  12. }
  13.  
  14. public String toString()
  15. {
  16. return tfQuestion;
  17. }
  18.  
  19. public boolean checkAnswer(boolean userAnswer)
  20. {
  21. return userAnswer == cAnswer;
  22. }
  23. }
And I am trying to make another code which is quiz, this one connected from tfquestion and also i have to make quizdriver so i have to make 3 classes tfquestion, quiz, quizdriver. But i really want to ask is about quiz.I couldn't ask about quizdriver yet, because I didn't do it yet...

here is my code for quiz
Java Syntax (Toggle Plain Text)
  1. public class Quiz
  2. {
  3. private TFQuestion[] Quiz;
  4. private int count;
  5.  
  6. public Quiz(int capacity)
  7. {
  8. capacity = 20;
  9. Quiz = new TFQuestion[capacity];
  10. count = 0;
  11. }
  12. public boolean add (String QuestionSTMT, boolean Answer)
  13. {
  14. if (count >= quiz.length)
  15. return false;
  16.  
  17. else
  18. quiz[count] = new TFQuestion (QuestionSTMT, tfvalue);
  19. count++;
  20. return true;
  21.  
  22. }
  23. public double play()
  24. {
  25. Scanner keyboard = new Scanner (System.in);
  26.  
  27. for (int i = 0; i < quiz.length; i++) {
  28. System.out.println(quiz[i]);
  29. System.out.println("Enter the answer for this question with 't' for true and 'f'" +
  30. " for false.");
  31. char answer = keyboard.nextChar();
  32. double numQuestions = ((double) i);
  33.  
  34. if (answer == correctAnswer)
  35. double numCorrect++;
  36. }
  37. double percentage = (numCorrect/numQuestions) * 100.0;
  38. return percentage;
  39. }
  40.  
  41.  
  42. }

the quiz code it self uses array, too...
I really not sure about code in boolean add and double play
please help me
and thx to the repliers for tfquestion code
Last edited by ~s.o.s~; May 3rd, 2008 at 12:52 pm. Reason: Added code tags, learn to use them; last warning.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
comjisu33 is offline Offline
5 posts
since Apr 2008
May 3rd, 2008
0

Re: java code help 2;;

What is the problem ? What's not working ?
Reputation Points: 254
Solved Threads: 74
Practically a Posting Shark
thekashyap is offline Offline
804 posts
since Feb 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Who can teach me combine these 2 class?
Next Thread in Java Forum Timeline: java





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC