Download presentation
Presentation is loading. Please wait.
Published byΣπύρο Ζάχος Modified over 6 years ago
1
What If? Write a program that prompts for the names and locations of 1000 employees. Store the information in the program for later use.
2
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?
3
Arrays 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
4
Array Example char [] lastname = new char[100]; lastname[0] = ‘H’;
lastname[1] = ‘a’; lastname[2] = ‘\0’; System.out.println( lastname[0]);
5
Array Example int [] values = new int[15]; values[0] = 150;
System.out.println( values[0]); values[3] = values[0] + 6;
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;
7
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(); } sum = sum + numArray[offset]; average = sum / ARRAY_SIZE;
8
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();
9
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
10
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); } static void changeZero(int [] passedArray) passedArray[0] = 1000;
11
Initializing Arrays char [] cArray3 = {'a', 'b', 'c'};
int [] iArray = {1, 2, 3, 4}; double [] dArray = {3.4, 5.67e4};
12
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;
13
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
14
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
15
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
16
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())
17
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++; }
18
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; }
19
Other Class Methods boolean equals( objType comObj) void display()
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
20
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)
21
Final Exam 2 Hours 200 Points 35% of Grade Must Take to Pass Course
22
Need to Know for Final Everything Through Exam 2 Plus: Arrays Methods
Classes Constructors Accessor Methods Mutator Methods toString() and equals() Methods
23
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)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.