160 likes | 277 Views
CSc2310 tutoring session, week 7 Fall, 2012. Using arrays Case study: PhoneDirectory. Haidong Xue. 5:30pm—8:30pm 10/9/2012 and 10/10/2012. CSc2310 Tutoring Time: 5:30pm-8:30pm Tutor: Haidong Xue Website: http://www.cs.gsu.edu/~hxue1/csc2310_Tutoring/index.html
E N D
CSc2310 tutoring session, week 7Fall, 2012 • Using arrays • Case study: PhoneDirectory HaidongXue 5:30pm—8:30pm 10/9/2012 and 10/10/2012
CSc2310 Tutoring • Time: 5:30pm-8:30pm • Tutor: HaidongXue • Website: http://www.cs.gsu.edu/~hxue1/csc2310_Tutoring/index.html There are 2 sections: 1. Review • Using arrays • Case study: PhoneDirectory 2. Q&A • Answer your questions about java programming
Using Arrays • Array is a collection of variables in the same type • They are accessed by index • Index start from 0 They are very useful when you need to deal with a collection of data.
Using Arrays • Create an array DataType[] ArrayReference = new DataType[Size] e.g. int[] ages = new int[100];
Using Arrays • Access variables in a created array ArrayReference[Index] // it is equal to a variable in the same type • e.g. int[] ages = new int[3]; ages[0] = 19; ages[1] = 22; ages[2] = 23; System.out.println(“The age of the second person is ” + ages[1]);
Case Study: PhoneDirectory • The PhoneDirectory program will store names and telephone numbers. • It supports 2 operations: • Entering new names and numbers • Looking up existing names
Case Study: PhoneDirectory User Interface • A command line menu • Users always choose from a - Add a new phone number f - Find a phone number q - Quit
Case Study: PhoneDirectory Database • Use a PhoneRecordobject to store a name-number pair • Use a PhoneRecord array as the database
Case Study: PhoneDirectory =================================================== Phone directory commands: a - Add a new phone number f - Find a phone number q - Quit f Enter name to look up: b 0 records were found. =================================================== Phone directory commands: a - Add a new phone number f - Find a phone number q - Quit f Enter name to look up: a Adam 678-908-3276 Apple Woods 443-332-4423 2 records were found. =================================================== Phone directory commands: a - Add a new phone number f - Find a phone number q - Quit q Phone directory commands: a - Add a new phone number f - Find a phone number q - Quit a Enter new name: Adam Enter new phone number: 678-908-3276 A phone record is added to database. ================================ Phone directory commands: a - Add a new phone number f - Find a phone number q - Quit a Enter new name: Apple Woods Enter new phone number: 443-332-4423 A phone record is added to database. A use case:
Case Study: PhoneDirectory • Get your IDE ready, let’s code it!
Case Study: PhoneDirectory The frame: // Using an array as the database final int DB_SIZE = 100; // the capacity of the database PhoneRecord[] database = new PhoneRecord[DB_SIZE]; intnumRecords = 0; // the current number of records Scanner s = new Scanner(System.in); while(true){ // Commands System.out.println("Phone directory commands:\n" + " a - Add a new phone number\n" + " f - Find a phone number\n" + " q - Quit\n"); // Get a command String command = s.nextLine(); // According to the user's command, add, search or quit if( command.equalsIgnoreCase("a")){/*TODO-A*/} // Command is "a" else if( command.equalsIgnoreCase("f")){/*TODO-B*/} // Command is "f" else if( command.equalsIgnoreCase("q")) {/*TODO-C*/} // Command is "q" else{} // Command is illegal } s.close(); With this frame, we then need to finish 4 components: PhoneRecord class TODO-A TODO-B TODO-C
Case Study: PhoneDirectory • PhoneDirectory Class class PhoneRecord{ private String name; private String number; // Constructor public PhoneRecord(String personName, String phoneNumber){ name = personName; number = phoneNumber; } // Returns the name stored in the record public String getName(){return name;} // Returns the phone number stored in the record public String getNumber(){return number;} }
Case Study: PhoneDirectory // Command is "a" if( command.equalsIgnoreCase("a")){/*TODO-A*/} if( command.equalsIgnoreCase("a")){ if( numRecords < database.length){ System.out.print("Enter new name: "); String name = s.nextLine().trim(); System.out.print("Enter new phone number: "); String number = s.nextLine().trim(); database[numRecords] = new PhoneRecord(name, number); numRecords++; System.out.println("A phone record is added to database."); } else{ System.out.print("The database is full; the record is not added."); } }
Case Study: PhoneDirectory // Command is "f" else if( command.equalsIgnoreCase("f")){/*TODO-B*/} else if( command.equalsIgnoreCase("f")){ // Command is "f" System.out.print("Enter name to look up: "); String key = s.nextLine().trim().toLowerCase(); intcounter=0; for( inti=0; i<numRecords; i++){ PhoneRecordr = database[i]; if( r.getName().toLowerCase().startsWith(key)){ System.out.println(r.getName() + " " + r.getNumber() ); counter++; } } System.out.println(counter + " records were found."); }
Case Study: PhoneDirectory // Command is "q" else if( command.equalsIgnoreCase("q")) {/*TODO-C*/} else if( command.equalsIgnoreCase("q")){ // Command is "q" System.out.println("Bye!"); break; } Finished code can be found at: http://www.cs.gsu.edu/~hxue1/csc2310_Tutoring/
Please let me know your questions. I will be here till 8:30pm