What%20If? - PowerPoint PPT Presentation

About This Presentation
Title:

What%20If?

Description:

What If? Write a program that prompts for the names and locations of 1000 employees. Store the information in the program for later use. – PowerPoint PPT presentation

Number of Views:53
Avg rating:3.0/5.0
Slides: 24
Provided by: Preferr749
Learn more at: http://www.cs.iit.edu
Category:

less

Transcript and Presenter's Notes

Title: What%20If?


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
  • 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 char100
  • lastname0 H
  • lastname1 a
  • lastname2 \0
  • System.out.println( lastname0)

5
Array Example
  • int values new int15
  • values0 150
  • values1 78
  • values2 16
  • System.out.println( values0)
  • values3 values0 6

6
Array Example
  • final int ARRAY_SIZE 100
  • int offset
  • int numArray new int ARRAY_SIZE
  • for(offset 0 offset lt ARRAY_SIZE offset)
  • numArrayoffset 0
  • for(offset 0 offset lt numArray.length
    offset)
  • numArrayoffset offset

7
Array Example
final int ARRAY_SIZE 10 int offset, sum 0,
average int numArray new intARRAY_SIZE f
or(offset 0 offset lt ARRAY_SIZE offset)
System.out.print( Enter Score offset
) numArrayoffset scan.nextInt() for(of
fset 0 offset lt ARRAY_SIZE offset) sum
sum numArrayoffset 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 lt MAXEMP i)
  • employeesi new Employee()

9
Passing Arrays to Methods
  • Pass Array Name into Method
  • int intArray new int100
  • 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 intASIZE
  • for(int i 0 i lt ASIZE i)
  • intArrayi i
  • System.out.println( intArray0)
  • changeZero(intArray)
  • System.out.println( intArray0)
  • static void changeZero(int passedArray)
  • passedArray0 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 char1020
  • int iArray new int10050
  • cArray00 a
  • cArray01 b
  • cArray919 x
  • iArray00 99
  • iArray15 135
  • iArray9949 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)
  • 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)
Write a Comment
User Comments (0)
About PowerShow.com