Need help.... get. methods...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • thesingh
    New Member
    • Mar 2007
    • 4

    Need help.... get. methods...

    Hi there,

    New to java but i have to jump into the deep end of things...

    I have created an arraylist that stores answers for questions and answers for a quiz im doing.

    public ArrayList storeAnswer3 ()
    {
    ans3.add("1995" );
    ans3.add("1996" );//answer
    ans3.add("1997" );
    ans3.add("1998" );

    return ans3;
    }

    What i need to do is declear that "1996" is the right answer. I been trying to look into get methods but to no success.

    Can somebody help me???

    Thanks in advance for any one who can help me!!
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by thesingh
    Hi there,

    New to java but i have to jump into the deep end of things...

    I have created an arraylist that stores answers for questions and answers for a quiz im doing.

    public ArrayList storeAnswer3 ()
    {
    ans3.add("1995" );
    ans3.add("1996" );//answer
    ans3.add("1997" );
    ans3.add("1998" );

    return ans3;
    }

    What i need to do is declear that "1996" is the right answer. I been trying to look into get methods but to no success.

    Can somebody help me???

    Thanks in advance for any one who can help me!!
    How do you plan on using it? I still don't get your question.

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      So basically you have a list of answers and one of them is the correct answer.
      Simply build a little class for it:
      Code:
      public class Answers {
         private List<String> answers;
         private int correct;
      
         public Answers(List<String> answers, int correct) {
            this.answers= answers;
            this.correct= correct;
         }
      
         public List<String> getAnswers() { return answers; }
         public String getCorrectAnswer() { return answers.get(correct); }
         public int getCorrectIndex() { return correct; }
      }
      I'm sure you can figure out the details by yourself.

      kind regards,

      Jos

      Comment

      • thesingh
        New Member
        • Mar 2007
        • 4

        #4
        sorry....

        Ok this is what i need...

        I need to declare what the right answer is so when you click on the right answer in the interface it will show a pop up message saying " thats right".

        I have a interface class and in that i have these codes


        private JLabel xField = new JLabel();
        private JButton yField = new JButton();
        private JButton zField = new JButton();
        private JButton aField = new JButton();
        private JButton bField = new JButton();
        {
        ans = q.storeQuestion ();
        ans1 = q.storeAnswer() ;

        cat1 = ans.get(0);
        xField.setText( cat1.get(0));
        yField.setText( ans1.get(0));
        zField.setText( ans1.get(1));
        aField.setText( ans1.get(2));
        bField.setText( ans1.get(3));
        }

        these are label and buttons which are showing the strings of the questions and answers which are located in another class...

        What i need to do is define what the right answer is so when you click on that button it will come up with a message...

        Is that any clearer???

        Comment

        • thesingh
          New Member
          • Mar 2007
          • 4

          #5
          Originally posted by JosAH
          So basically you have a list of answers and one of them is the correct answer.
          Simply build a little class for it:
          Code:
          public class Answers {
             private List<String> answers;
             private int correct;
          
             public Answers(List<String> answers, int correct) {
                this.answers= answers;
                this.correct= correct;
             }
          
             public List<String> getAnswers() { return answers; }
             public String getCorrectAnswer() { return answers.get(correct); }
             public int getCorrectIndex() { return correct; }
          }
          I'm sure you can figure out the details by yourself.

          kind regards,

          Jos

          Thanks..

          I am very poor at java.. but il have a go.. would i place this in a new class???

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by thesingh
            Thanks..

            I am very poor at java.. but il have a go.. would i place this in a new class???
            Yep, sure; never be afraid to craft a new class or interface instead of knitting
            more and more stuff to an already existing class. It'll keep your code base clean.

            kind regards,

            Jos

            Comment

            • thesingh
              New Member
              • Mar 2007
              • 4

              #7
              Thanks alot jos...

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                Originally posted by thesingh
                Thanks alot jos...
                You're welcome of course. Another tip: if you really want to play it safe you
                can store a Collections.unm odifiableList() so noone will be able to change your
                answers list from this proposed class.

                kind regards,

                Jos

                Comment

                Working...