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

Slides:



Advertisements
Similar presentations
CS102--Object Oriented Programming Discussion 2: (programming strategy in java) – Two types of tasks – The use of arrays Copyright © 2008 Xiaoyan Li.
Advertisements

Road Map Introduction to object oriented programming. Classes
What If? Write a program that prompts for the names and locations of 1000 employees. Store the information in the program for later use.
Fundamental Programming Structures in Java: Strings.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
MSc IT Programming Methodology (2). MODULE TEAM Dr Aaron Kans Dr Sin Wee Lee.
Warm-Up: April 21 Write a loop (type of your choosing) that prints every 3 rd number between 10 and 50.
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Classes and Methods Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2014.
Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
The for Loop Syntax: for ( expression1 ; condition ; expression2 ) statement ; for ( expression1 ; condition ; expression2 ) { statement ; } Same as: expression1.
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
Chapter 4: Control Structures II
OBJECTS FOR ORGANIZING DATA -- As our programs get more sophisticated, we need assistance organizing large amounts of data. : array declaration and use.
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
Announcements Final Exam:TBD. public static void main(String [] args) { final int ASIZE = 5; int [] intArray= new int[ASIZE]; for(int i = 0; i < ASIZE;
Chapter 6. else-if & switch Copyright © 2012 Pearson Education, Inc.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 9 GEORGE KOUTSOGIANNAKIS Copyright: 2014 Illinois Institute of Technology- George Koutsogiannakis 1.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
Chapter 3A Strings. Using Predefined Classes & Methods in a Program To use a method you must know: 1.Name of class containing method (Math) 2.Name of.
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
Announcements Quiz 1 Next Monday. int : Integer Range of Typically –2,147,483,648 to 2,147,483,647 (machine and compiler dependent) float : Real Number.
Files Review For output to a file: –FileOutputStream variable initialized to filename (String) and append/not append (boolean) –PrintWriter variable initialized.
Announcements Final Exam: TBD. Static Variables and Methods static means “in class” methods and variables static variable: one per class (not one per.
CS 116 Lecture 1 John Korah Contains content provided by George Koutsogiannakis & Matt Bauer.
Java Classes Introduction. Contents Introduction Objects and Classes Using the Methods in a Java Class – References and Aliases Defining a Java Class.
Exam 2 EXAM 2 Thursday!!! 25% of Final Grade Know: loops, switch/case Files Input failure (e.g. scan.hasNextInt())
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
(Dreaded) Quiz 2 Next Monday.
GC211 Data structure Lecture 3 Sara Alhajjam.
Chapter 7 User-Defined Methods.
Yanal Alahmad Java Workshop Yanal Alahmad
Intro To Classes Review
CS116 OBJECT ORIENTED PROGRAMMING II LECTURE 1 Part II
Chapter 5: Control Structures II
SELECTION STATEMENTS (1)
An Introduction to Java – Part I
Control Statement Examples
Initializing Arrays char [] cArray3 = {'a', 'b', 'c'};
Exercise on Java Basics
Classes Variables That Are Not of a Built-in Type Are Objects
An Introduction to Java – Part I, language basics
Outline Writing Classes Copyright © 2012 Pearson Education, Inc.
Know for Quiz Everything through Last Week and Lab 7
Exam 2 EXAM 2 Thursday!!! 25% of Final Grade
Announcements Exam 1 Thursday 50 minutes 10 % of Total Grade
File Review Declare the File Stream Object Name
Arrays Syntax: type variableName[size];
Control Statements Loops.
Announcements Exam 1 Thursday Everything through Lab 5 50 minutes
Arrays of Objects // The following does NOT create memory for
Building Java Programs
OBJECT ORIENTED PROGRAMMING I LECTURE 9 GEORGE KOUTSOGIANNAKIS
OBJECT ORIENTED PROGRAMMING II LECTURE 13_2 GEORGE KOUTSOGIANNAKIS
Building Java Programs
Outline Anatomy of a Class Encapsulation Anatomy of a Method
Control Statements Loops.
Building Java Programs
Names of variables, functions, classes
Consider Write a program that prompts a user to enter the number of students and then, their names and grades. The program will then outputs the average.
Objects with ArrayLists as Attributes
What If? Write a program that prompts for the names and locations of 1000 employees. Store the information in the program for later use.
First Semester Review.
Announcements Exam 2 Lecture Grades Posted on blackboard
Presentation transcript:

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]; 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; 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(); } 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); } 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) 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

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)