Results 1 to 1 of 1
  1. #1
    RSH
    RSH is offline Member
    Join Date
    Jun 2012
    Posts
    6
    Rep Power
    0

    Default 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:

    Java Code:
    ArrayList <Questions> QuestionsArrayList = new ArrayList <Questions>();
    I then add a random selection of unique questions:

    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;
    				}
    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:
    for(int i=0; i<100; i++){
        QuestionsArrayList.get(i).printQuestion();
    }
    Can anybody help?
    Last edited by RSH; 09-12-2012 at 04:05 PM. Reason: Added code tags.

Similar Threads

  1. Problem printing ArrayList
    By pamm in forum New To Java
    Replies: 2
    Last Post: 09-13-2011, 06:17 PM
  2. Replies: 9
    Last Post: 08-19-2011, 12:59 AM
  3. About printing Arraylist on JSP page....
    By vaibhavspawar in forum Advanced Java
    Replies: 0
    Last Post: 08-13-2010, 06:52 AM
  4. ArrayList printing
    By tommyyyy in forum New To Java
    Replies: 4
    Last Post: 03-20-2009, 04:33 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •