Results 1 to 1 of 1
Thread: Help Printing an ArrayList
- 09-12-2012, 04:20 AM #1
Member
- Join Date
- Jun 2012
- Posts
- 6
- Rep Power
- 0
Help Printing an ArrayList
Hey guys,
I'm working on a Practice Test program where I store 200 objects (a mixture of multiplication, subtraction, and addition questions) and then print them out twice - once without the answers and once with the answers.
I created an abstract Questions class from which subclasses Multiplication, Addition, and Subtraction, all inherit. In Questions class I have the following two print methods:
Java Code:public void printQuestion() { System.out.println(num1 + " " + operator + " " + num2 + " = "); } public void printQuestionWithAnswer() { System.out.println(num1 + " " + operator + " " + num2 + " = " + solution); }
The variables are all defined by a computeAnswer() method within the subclasses. In my main I created an ArrayList:
I then add a random selection of unique questions:Java Code:ArrayList <Questions> QuestionsArrayList = new ArrayList <Questions>();
Now, I would like to print them out once without answers and once with the answers. I imagine I will use something like the following, but I haven't been able to figure it out completely:Java Code:Random rand2 = new Random(); for(int j = 0; j<199; j++){ int k = rand2.nextInt(3)+1; if (k == 1){ boolean i; while(i = false){ Addition a = new Addition(); if (!QuestionsArrayList.contains(a)){ QuestionsArrayList.add(a); i = true; } else i = false; } } if (k == 2){ boolean i; while(i = false){ Subtraction s = new Subtraction(); if (!QuestionsArrayList.contains(s)){ QuestionsArrayList.add(s); i = true; } else i = false; } } if (k == 3){ boolean i; while(i = false){ Multiplication m = new Multiplication(); if (!QuestionsArrayList.contains(m)){ QuestionsArrayList.add(m); i = true; } else i = false; }
Can anybody help?Java Code:for(int i=0; i<100; i++){ QuestionsArrayList.get(i).printQuestion(); }Last edited by RSH; 09-12-2012 at 04:05 PM. Reason: Added code tags.
Similar Threads
-
Problem printing ArrayList
By pamm in forum New To JavaReplies: 2Last Post: 09-13-2011, 06:17 PM -
Help with printing an arrayList. printing the first element twice
By kingkongjaffa in forum New To JavaReplies: 9Last Post: 08-19-2011, 12:59 AM -
About printing Arraylist on JSP page....
By vaibhavspawar in forum Advanced JavaReplies: 0Last Post: 08-13-2010, 06:52 AM -
ArrayList printing
By tommyyyy in forum New To JavaReplies: 4Last Post: 03-20-2009, 04:33 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks