Exercise 1 NumberListTesterjava Class NumberList implements

Exercise 1:      NumberListTester.java

Class NumberList implements a simple list of Integers, using an ArrayList. NumberListTester generates 10 random, positive, 2-digit ints, and adds them to the list. It then calls method printList which prints the items on the list.

Run the program until you understand what it does.

1.Write the body of the method printReversed, which prints the list items in reverse order. Add a statement in main to call printReversed after the call to printList.

2.Write the body of the method printEveryOther, which prints every other item on the list, beginning with the first one.   In main, replace the statement that calls printReversed with one that calls printEveryOther.

3.Write the body of the method printEvens, which prints all the even-numbered ints on the list.   In main, replace the statement that calls printEveryOther with one that calls printEvens.

CODE is here: http://users.cis.fiu.edu/~crahn/COP2210/LabActivitySheets/Lab%2011/NumberListTester.java

Solution

NumberListTester.java

import java.util.ArrayList ;
import java.util.Random ;

/**
* A class to provide practice using ArrayLists
*/
class NumberList
{
   // instance var\'s
   private ArrayList<Integer> aList ;       // a list of Integer objects

   /**
   * Creates a NumberList object.
   */  
   public NumberList()
   {
       aList = new ArrayList<Integer>() ;   // creates an empty list
   }
  
   /**
   * Adds a number to the list.
   * @param number the number to be added to the list
   */
   public void add(int number)
   {
       aList.add(number) ;       // calls add method of ArrayList class
   }
  
   /**
   * Prints the numbers stored in aList.
   */
   public void printList()
   {
       System.out.println( \"The numbers on the list: \" ) ;
      
       // for each number on the list, from 1st to last...
       for (int i = 0 ; i < aList.size() ; i++)
       {
           int number = aList.get(i) ;               // get the number
           System.out.print(number + \" \") ;       // print it
       }
       System.out.println(\"\ \") ;
   }
  
   /**
   * Prints the numbers stored in aList, in reverse order.
   */
public void printReversed()
   {
       System.out.println( \"The numbers on the list, in reverse order: \" ) ;
       // write your code here
       for(int i=aList.size()-1 ; i>=0; i--){
           System.out.print(aList.get(i)+\" \");
       }
           System.out.println();
      
   }
  
   /**
   * Prints every other number stored in aList, starting with the first one.
   */
   public void printEveryOther()
   {
       System.out.println( \"Starting with the first, every other number: \" ) ;
       // write your code here
       for (int i = 0 ; i < aList.size() ; i++)
       {
           int number = aList.get(i) ;               // get the number
           System.out.print(number + \" \") ;       // print it
       }
       System.out.println(\"\ \") ;
      
      
   }

   /**
   * Prints all the even-numbered ints stored in aList.
   */
   public void printEvens()
   {
       System.out.println( \"The even numbers on the list: \" ) ;
       // write your code here
       for (int i = 0 ; i < aList.size() ; i++)
       {
           int number = aList.get(i) ;               // get the number
           if(number % 2 == 0){
           System.out.print(number + \" \") ;       // print it
           }
       }
       System.out.println(\"\ \") ;
      
      
   }

/**
   * Copies all ints that are 50 or greater from aList to bigList.
   */
   public void splitList()
   {
       // write your code here
      
          
   }
}

public class NumberListTester
{
   public static void main (String [] args)
   {
       Random r = new Random() ;
       NumberList list = new NumberList() ;
      
       // populate the list with 10 random 2-digit ints (10 to 99)
       for (int i = 1 ; i <= 10 ; i++)
       {
           int next = r.nextInt(90) + 10 ;
           // call the \"add\" method of the NumberList class
           list.add( next ) ;              
       }

       // print the aList
       list.printList() ;
    list.printReversed();
       list.printEveryOther();
       list.printEvens();
}
}

Exercise 1: NumberListTester.java Class NumberList implements a simple list of Integers, using an ArrayList. NumberListTester generates 10 random, positive, 2-d
Exercise 1: NumberListTester.java Class NumberList implements a simple list of Integers, using an ArrayList. NumberListTester generates 10 random, positive, 2-d
Exercise 1: NumberListTester.java Class NumberList implements a simple list of Integers, using an ArrayList. NumberListTester generates 10 random, positive, 2-d

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site