// File: BogusListTester2.java // A simple test class for the BogusList2 class. // To demonstrate "information hiding," this test class is identical to // the previous version (that tested the BogusList class) import lists.BogusList2 ; import java.util.Random ; public class BogusListTester2 { public static void listPositives(BogusList2 newList, BogusList2 oldList) { // Copy all positive ints from oldList to newList oldList.setIterator() ; // point cursor to head of oldList while ( oldList.more() ) // while more items on oldList... { // get current item and advance iterator to next item int currentItem = oldList.next() ; if ( currentItem > 0 ) // if positive number... newList.append(currentItem) ; // ...append to newList } } public static void main(String [] args) { BogusList2 aList = new BogusList2(), bList = new BogusList2() ; Random r = new Random() ; // append 10 random ints in range -99..99 to aList for (int i = 1 ; i <= 10 ; i++) aList.append( r.nextInt() % 100 ) ; System.out.println("Printing the \"A\" list:\n" + aList) ; // copy positive ints to bList listPositives (bList, aList) ; System.out.println("Printing the \"B\" list:\n" + bList) ; // is 37 on bList? if ( bList.contains(37) ) System.out.println(37 + " IS on the \"B\" list\n") ; else System.out.println(37 + " is NOT on the \"B\" list\n") ; } } /* program output: Printing the "A" list: -77 -83 86 48 79 -36 -95 83 5 82 Printing the "B" list: 86 48 79 83 5 82 37 is NOT on the "B" list */