csc2310 tutoring session, week 7 fall, 2012 haidong xue 5:30pm—8:30pm 10/9/2012 and 10/10/2012...
TRANSCRIPT
CSc2310 tutoring session, week 7Fall, 2012
Haidong Xue
5:30pm—8:30pm10/9/2012 and 10/10/2012
- Using arrays- Case study: PhoneDirectory
• CSc2310 Tutoring• Time: 5:30pm-8:30pm• Tutor: Haidong Xue• 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 arrayDataType[] ArrayReference = new DataType[Size]
e.g.int[] ages = new int[100];
Using Arrays
• Access variables in a created arrayArrayReference[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 numberf - Find a phone numberq - Quit
Case Study: PhoneDirectory
Database• Use a PhoneRecord object to store a name-
number pair• Use a PhoneRecord array as the database
Case Study: PhoneDirectoryA use case:Phone directory commands: a - Add a new phone number f - Find a phone number q - Quit
aEnter new name: AdamEnter new phone number: 678-908-3276A phone record is added to database.================================Phone directory commands: a - Add a new phone number f - Find a phone number q - Quit
aEnter new name: Apple WoodsEnter new phone number: 443-332-4423A phone record is added to database.
===================================================Phone directory commands: a - Add a new phone number f - Find a phone number q - Quit
fEnter name to look up: b0 records were found.===================================================Phone directory commands: a - Add a new phone number f - Find a phone number q - Quit
fEnter name to look up: aAdam 678-908-3276Apple Woods 443-332-44232 records were found.===================================================Phone directory commands: a - Add a new phone number f - Find a phone number q - Quitq
Case Study: PhoneDirectory• Get your IDE ready, let’s code it!
Case Study: PhoneDirectoryThe frame:
// Using an array as the databasefinal int DB_SIZE = 100; // the capacity of the databasePhoneRecord[] database = new PhoneRecord[DB_SIZE];int numRecords = 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:1. PhoneRecord class2. TODO-A3. TODO-B4. 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();
int counter=0; for( int i=0; i<numRecords; i++){ PhoneRecord r = 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