110 likes | 273 Views
What If?. Write a program that prompts for the names and locations of 1000 employees. Store the information in the program for later use. Program for Previous. String empName1, empName2, empName3,… String empLocation1, empLocation2, … System.out.print( “Enter employee name 1:”);
E N D
What If? • Write a program that prompts for the names and locations of 1000 employees. Store the information in the program for later use.
Program for Previous String empName1, empName2, empName3,… String empLocation1, empLocation2, … System.out.print( “Enter employee name 1:”); empName1 = scan.next(); System.out.print(“Enter employee name 2:”); empName2 = scan.next(); … //Can we use a loop?
Arrays • Syntax: typevariableName[] = new type [size]; • Syntax: type [ ] variableName = new type [size]; • Memory Is Set Aside for size Items of type • Each Variable Location in Array Is Accessed by Offsetting into the Array with an Integer Expression • Legitimate Offsets Are 0 to size-1
Array Example char [] lastname = new char[100]; lastname[0] = ‘H’; lastname[1] = ‘a’; lastname[2] = ‘\0’; System.out.println( lastname[0]);
Array Example int [] values = new int[15]; int i = 0; values[i] = 150; values[i + 1] = 78; i = 2; values[i] = 16; System.out.println( values[0]); values[3] = values[0] + 6;
Array Example final int ARRAY_SIZE = 100; int offset; int [] numArray = new int [ARRAY_SIZE]; for(offset = 0; offset < ARRAY_SIZE; offset++) { numArray[offset] = 0; } for(offset = 0; offset < numArray.length; offset++) { numArray[offset] = offset; }
Arrays of Objects // The following does NOT create memory for // objects, it only makes a blueprint array Employee [] employees = new Employee [MAXEMP]; // Must create memory and call constructor for // each single member of aray of objects for (i = 0; i < MAXEMP; i++) employees[i] = new Employee();
Array Example final int ARRAY_SIZE = 10; int offset, sum = 0, average; int [] numArray = new int[ARRAY_SIZE]; for(offset = 0; offset < ARRAY_SIZE; offset++) { System.out.print( “Enter Score ” + offset + “ : “); numArray[offset] = scan.nextInt(); } for(offset = 0; offset < ARRAY_SIZE; offset++) { sum = sum + numArray[offset]; } average = sum / ARRAY_SIZE;
Project • Posted on blackboard under Tasks • Due Wednesday of Finals week at Noon CDT • Hand in to your TA (NOT me!) • .java for Client Program (source code), • .java for Class (source code), • .txt (Data file runwaycs115.txt), • .doc (Analysis) • Pseudocode!!!!! Design before Coding!!!! • Start *TODAY*!!!!
Project Do’s • Start this week (read project at least) • Write Pseudocode/Design FIRST • Start Small, Test Often, Add Little by Little • Backup Versions Often • Review All cs115 Concepts – Do Well on Final Exam
Don’t’s • No Working Together • Write your own Code • Checked by Supercomputer • May be Called in to Discuss Program • Last Term – 10 Investigated, 7 0s, 1 probation • Eleven terms (94 investigated, 64 received 0s)