Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSc2310 tutoring session, week 7 Fall, 2012 Haidong Xue 5:30pm—8:30pm 10/9/2012 and 10/10/2012 -Using arrays -Case study: PhoneDirectory.

Similar presentations


Presentation on theme: "CSc2310 tutoring session, week 7 Fall, 2012 Haidong Xue 5:30pm—8:30pm 10/9/2012 and 10/10/2012 -Using arrays -Case study: PhoneDirectory."— Presentation transcript:

1 CSc2310 tutoring session, week 7 Fall, 2012 Haidong Xue 5:30pm—8:30pm 10/9/2012 and 10/10/2012 -Using arrays -Case study: PhoneDirectory

2 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

3 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.

4 Using Arrays Create an array DataType[] ArrayReference = new DataType[Size] e.g. int[] ages = new int[100];

5 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]);

6 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

7 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

8 Case Study: PhoneDirectory Database Use a PhoneRecord object to store a name- number pair Use a PhoneRecord array as the database

9 Case Study: PhoneDirectory A use case: 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. =================================================== 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

10 Case Study: PhoneDirectory Get your IDE ready, let’s code it!

11 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]; 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 class 2.TODO-A 3.TODO-B 4.TODO-C

12 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;} }

13 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."); }

14 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."); }

15 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/ http://www.cs.gsu.edu/~hxue1/csc2310_Tutoring/

16 Please let me know your questions. I will be here till 8:30pm


Download ppt "CSc2310 tutoring session, week 7 Fall, 2012 Haidong Xue 5:30pm—8:30pm 10/9/2012 and 10/10/2012 -Using arrays -Case study: PhoneDirectory."

Similar presentations


Ads by Google