473,387 Members | 1,535 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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!!
Mar 29 '07 #1
7 1245
r035198x
13,262 8TB
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.
Mar 29 '07 #2
JosAH
11,448 Expert 8TB
So basically you have a list of answers and one of them is the correct answer.
Simply build a little class for it:
Expand|Select|Wrap|Line Numbers
  1. public class Answers {
  2.    private List<String> answers;
  3.    private int correct;
  4.  
  5.    public Answers(List<String> answers, int correct) {
  6.       this.answers= answers;
  7.       this.correct= correct;
  8.    }
  9.  
  10.    public List<String> getAnswers() { return answers; }
  11.    public String getCorrectAnswer() { return answers.get(correct); }
  12.    public int getCorrectIndex() { return correct; }
  13. }
I'm sure you can figure out the details by yourself.

kind regards,

Jos
Mar 29 '07 #3
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???
Mar 29 '07 #4
So basically you have a list of answers and one of them is the correct answer.
Simply build a little class for it:
Expand|Select|Wrap|Line Numbers
  1. public class Answers {
  2.    private List<String> answers;
  3.    private int correct;
  4.  
  5.    public Answers(List<String> answers, int correct) {
  6.       this.answers= answers;
  7.       this.correct= correct;
  8.    }
  9.  
  10.    public List<String> getAnswers() { return answers; }
  11.    public String getCorrectAnswer() { return answers.get(correct); }
  12.    public int getCorrectIndex() { return correct; }
  13. }
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???
Mar 29 '07 #5
JosAH
11,448 Expert 8TB
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
Mar 29 '07 #6
Thanks alot jos...
Mar 29 '07 #7
JosAH
11,448 Expert 8TB
Thanks alot jos...
You're welcome of course. Another tip: if you really want to play it safe you
can store a Collections.unmodifiableList() so noone will be able to change your
answers list from this proposed class.

kind regards,

Jos
Mar 29 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Nikita A. Visnevski | last post by:
Hi everyone, I am rather new to Java beans. Just picked up a book last night and started reading about them. I am building an application that allows a user to define objects with dynamic...
7
by: alsemgeest | last post by:
Hi, I'd like to change the properties of the default form design. Every form I need to change many settings. Especially the following properties (I try to translate them well from Dutch): * make...
48
by: Chad Z. Hower aka Kudzu | last post by:
A few of you may recognize me from the recent posts I have made about Indy <http://www.indyproject.org/indy.html> Those of you coming to .net from the Delphi world know truly how unique and...
5
by: Rod | last post by:
About two weeks ago I had an accident and have broken my left elbow and left wrist. For doing things like Word or e-mail (I use Outlook for) I have been using Microsoft's speech recognition and...
2
by: Andrew S. Giles | last post by:
OK, Ive run my head into this wall for too long. I need help. I am developing an applicaiton in C# to present a user with a GUI to specify a configurable list of machines that he wants to listen...
12
by: Noel | last post by:
Hello, I'm currently developing a web service that retrieves data from an employee table. I would like to send and retrieve a custom employee class to/from the webservice. I have currently coded...
1
by: Learner | last post by:
Hi there, I have designed a three tier arichitecture(presentation, business and datalayer). I have used Shared keyword in all my DL methods inorder to make them available in my BL. I have...
3
by: yoyojava | last post by:
here go to this link: http://staff.beaumont.k12.tx.us/jchauvi/CS2/CS2.html ..... then click on PROJECT:Bruin Grocery ... and i am done with everything except for step 5 and 6 the search methods.. i...
1
by: javabeginner123 | last post by:
i have a java prob, and i have to solve it fast, but i'm just getting to know it, so plz help me solve it with full code completed, thanks so much. the prob is to create a monter fight and there is...
0
by: akshaycjoshi | last post by:
I am reading a book which says Even though unboxed value types don't have a type object pointer, you can still call virtual methods (such as Equals, GetHashCode, or ToString) inherited or...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.