Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
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: type variableName[] = 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]; values[0] = 150; values[1] = 78; values[2] = 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; } 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; 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(); Passing Arrays to Methods • Pass Array Name into Method int [] intArray = new int[100]; loadArray(intArray); • Accept into Method as an Array with No Set Size void loadArray( int [] passedArray ) • Changes to Array in Method *will* Update Original Array public static void main(String [] args) { final int ASIZE = 5; int [] intArray= new int[ASIZE]; for(int i = 0; i < ASIZE; i++) intArray[i] = i; System.out.println( intArray[0]); changeZero(intArray); System.out.println( intArray[0]); } static void changeZero(int [] passedArray) { passedArray[0] = 1000; } Initializing Arrays char [] cArray3 = {'a', 'b', 'c'}; int [] iArray = {1, 2, 3, 4}; double [] dArray = {3.4, 5.67e4}; Two Dimensional Arrays char [][] cArray = new char[10][20]; int [][] iArray = new int[100][50]; cArray[0][0] = ‘a’; cArray[0][1] = ‘b’; cArray[9][19] = ‘x’; iArray[0][0] = 99; iArray[1][5] = 135; iArray[99][49] = 0; Math Class • • • • • • • • • • Contains Typical Math Methods In Package java.lang (i.e., automatically imported) Access using Math.math_thing Math.PI; Math.E (log e = 1) double pow (double base, double exp) double sqrt(double number) int round(double number) int abs( int number) min(number1,number2) and max(number1, number2) double random(); //random number between 0.0 and 1.0 Tokenizing/Parsing • Taking Apart a String and Separating It into Smaller Strings (i.e., “tokens”) • Usually, Tokens are Items Separated by Whitespace • Tokens Can Be Defined with Different Separators • Parsing Takes Tokens and Applies Some Manipulation of Those Tokens StringTokenizer Class import java.util.*; … String bigString = “This is a sentence.”; StringTokenizer st = new StringTokenizer(bigString); while (st.hasMoreTokens()) { System.out.println(st.nextToken()); } // Displays This, is, a, and sentence. one // per line Static Variables and Methods • static means “in class” methods and variables • static variable: one per class (not one per object) • static method: no invoking object (invoke with className.method()) Static Variable Example public class MyClass { … private final int STARTID = 1; private int IDNum = -1; //above are one per object private static int nextIDNum = STARTID; //above is one per class public MyClass() { … IDNum = nextIDNum; nextIDNum++; } Static Method Example public class MyClass { … private final int STARTID = 1; private int IDNum = -1; //above are one per object private static int nextIDNum = STARTID; //above is one per class … public static int GetNextID() { // Called by MyClass.GetNextID() return nextIDNum; Other Class Methods • boolean equals( objType comObj) – compares invoking object with parameter of same type. Returns true if equal, false if not. • void display() – displays object to monitor • String toString() – converts each attirbute to String and returns String Employee Class Review • Create a Class Employee • Employee.java – – – – – Employee Attributes (data members) Constructor Accessor Methods Mutator Methods toString() and equals() • tryEmployee.java (Client Test Program) Final Exam • • • • 2 Hours 200 Points 35% of Grade Must Take to Pass Course Need to Know for Final • Everything Through Exam 2 • Plus: – Arrays – Methods – Classes • • • • Constructors Accessor Methods Mutator Methods toString() and equals() Methods Final Exam • 200 Points Total – 35% of Final Grade • 90 points Matching or Short Answer or Multiple Choice (Terminology, Operators, JAVA Basics) • 30 Points Program Output • 80 Points Programming (Full Programs)