CSC 220 Code Exercise 2, Summer 2009

Notre Dame University–Louaize · Programming in Java I · 10 pages

All CSC 220 files
CSC 220 Code Exercise 2, Summer 2009, page 1

Stuck on a question? A tutor can walk you through it.

CSC 220 Code Exercise 2, Summer 2009, page 2 CSC 220 Code Exercise 2, Summer 2009, page 3 CSC 220 Code Exercise 2, Summer 2009, page 4 CSC 220 Code Exercise 2, Summer 2009, page 5 CSC 220 Code Exercise 2, Summer 2009, page 6 CSC 220 Code Exercise 2, Summer 2009, page 7 CSC 220 Code Exercise 2, Summer 2009, page 8 CSC 220 Code Exercise 2, Summer 2009, page 9 CSC 220 Code Exercise 2, Summer 2009, page 10

More CSC 220 files

Show the text of this file
public class List { String [ ] listItems ; // Array to hold list items int numItems ; // number of items currently in list int currentPos ; // state variable for iteration public List ( ) // Default Constructor // Result: An empty list object has been instantiated with room // for 100 items { numItems = 0 ; listItems = new String [ 100 ] ; currentPos = 0 ; } public List ( int maxItems ) // Constructor // Result: An empty list object has been instantiated with room // for maxItems items { numItems = 0 ; listItems = new String [ maxItems ] ; currentPos = 0 ; } public void saveString(String e) { listItems[numItems]=e ; numItems++ ; } public boolean isFull ( ) // Returns true if no room to add a component; false otherwise { return ( numItems == listItems.length ) ; } public boolean isEmpty ( ) // Returns true if no components in the list; false otherwise { return ( numItems == 0 ) ; } public int length ( ) // Returns the number of components in the list { return numItems ; } public void insert ( String item ) // Result: If the list is not full, puts item in the last position in // the list; otherwise list is unchanged. { if ( !this.isFull( ) ) { listItems [ numItems ] = item ; numItems++ ; } } public boolean isThere ( String item ) // Returns true if item is in the list; false otherwise { int index = 0 ; while ( index < numItems && listItems[ index ].compareTo(item) != 0 ) index++ ; return ( index < numItems ) ; } public void delete ( String item ) // Result: Removes item from the list if it is there; otherwise // list is unchanged. { int index = 0 ; boolean found = false ; while ( index < numItems && !found ) { if ( listItems[index].compareTo( item ) == 0 ) found = true; else index++; } // if item found, shift remainder of list up to delete item if ( found ) { for ( int count = index ; count < numItems - 1; count++ ) listItems [ count ] = listItems [ count + 1 ] ; numItems-- ; } } public void resetList ( ) // The iteration is initialized by setting currentPos to 0 { currentPos = 0; } public String getNextItem ( ) // Returns item at the curentPos positon; increments currentPos; // resets currentPos to first item after last item is returned. // Assumes: no transformers invoked since last call. { String next = listItems[ currentPos ] ; if ( currentPos == numItems - 1 ) currentPos = 0 ; else currentPos++; return next ; } public void selectSort ( ) // Arranges list into ascending order using straight selection sort { String temp ; int passCount ; int sIndex ; int minIndex ; // index of minimum so far for ( passCount = 0 ; passCount < numItems - 1 ; passCount++ ) { minIndex = passCount ; // find index of smallest of // listItems [ passCount ] . . . listItems[ numItems-1 ] for ( sIndex = passCount + 1 ; sIndex < numItems ; sIndex++ ) if ( listItems [ sIndex ].compareTo( listItems [ minIndex ] ) < 0 ) minIndex = sIndex ; temp = listItems [ minIndex ] ; // swap listItems [ minIndex ] = listItems [ passCount ] ; listItems [ passCount ] = temp ; } } public boolean isTherefast ( String item ) // Assumes: List items are in ascending order // Returns true if item is in the list; false otherwise. // Uses Binary Search algorithm. { int first = 0; int last = numItems - 1 ; int middle ; boolean found = false ; while ( last >= first && !found ) { middle = ( first + last ) / 2 ; // INDEX OF MIDDLE ELEMENT if ( item.compareTo( listItems [ middle ] ) == 0 ) found = true ; // ITEM HAS BEEN FOUND else if ( item.compareTo( listItems [ middle ] )< 0 ) last = middle - 1 ; // LOOK IN FIRST HALF NEXT else first = middle + 1 ; // LOOK IN SECOND HALF NEXT } return found ; } } import java.io.IOException; public class System { public static void main(String[] args) throws IOException { WriteRead s = new WriteRead(); s.work() ; } } import java.io.* ; public class WriteRead { public void work() throws IOException { BufferedReader br ; PrintWriter pw ; PrintWriter pw2 ; String currline ; List l ; int temp ; l= new List(); pw = new PrintWriter(new FileWriter ("output.txt")) ; br = new BufferedReader( new FileReader ("data.txt")) ; pw2= new PrintWriter(new FileWriter ("outputsorted.txt")); currline = br.readLine() ; while (currline!=null) { temp=currline.indexOf(" "); currline=currline.substring(0,temp); l.saveString(currline) ; pw.println(currline) ; currline=br.readLine(); } l.selectSort(); for (int a=0 ;a<l.listItems.length && l.listItems[a]!=null ;a++ ) { pw2.println(l.listItems[a]) ; } pw.close(); pw2.close(); br.close(); } } CarloMahfouz kpwgvwiognwog ElieHarfouche swnvgsjnkvnk Christelle s.,f vlsfnlvn Dani smflvksmlvns ElieBoms skvlnsvnsn Joseph snvlnslvnsnv