CS 106 Introduction to Computer Science I 10 / 04 / 2006 Instructor: Michael Eckmann.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Chapter 41 Variables and JSP Control Structures JavaServer Pages By Xue Bai.
CS 106 Introduction to Computer Science I 02 / 18 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 09 / 25 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 09 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 04 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 01 / 30 / 2008 Instructor: Michael Eckmann.
Chapter 4 - Control Structures: Part 1 Outline 4.4Control Structures 4.5The if Selection Structure 4.6The if/else Selection Structure 4.7The while Repetition.
CS 106 Introduction to Computer Science I 09 / 14 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 25 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 11 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 27 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 22 / 2008 Instructor: Michael Eckmann.
Loops – While, Do, For Repetition Statements Introduction to Arrays
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
CS 106 Introduction to Computer Science I 02 / 20 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 09 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 03 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 08 / 2010 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 09 / 18 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 19 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 23 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 09 / 28 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 09 / 11 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
CSM-Java Programming-I Spring,2005 Control Flow Lesson - 3.
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
Lecture 12 Instructor: Craig Duckett ARRAYS. Announcements Assignment 3 Assignment 3 Revision Assignment 4 (and Final Exam) GRADED! RETURNED! Woot! NEXT.
Arrays (Part 1) Computer Science Erwin High School Fall 2014.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
JAVA Tokens. Introduction A token is an individual element in a program. More than one token can appear in a single line separated by white spaces.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
ARRAYS 1 TOPIC 8 l Array Basics l Arrays and Methods l Programming with Arrays Arrays.
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
Computer Programming 12 Mr. Jean April 24, The plan: Video clip of the day Upcoming Quiz Sample arrays Using arrays More about arrays.
CPS120: Introduction to Computer Science Decision Making in Programs.
CS 106 Introduction to Computer Science I 01 / 31 / 2007 Instructor: Michael Eckmann.
An Introduction to Java – Part 1 Dylan Boltz. What is Java?  An object-oriented programming language  Developed and released by Sun in 1995  Designed.
Logic Our programs will have to make decisions in terms of what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if.
 The if statement and the switch statement are types of conditional/decision controls that allow your program.  Java also provides three different looping.
Primitive data Week 3. Lecture outcomes Primitive data – integer – double – string – char – Float – Long – boolean Declaration Initialisation Assignments.
Introduction to Java Java Translation Program Structure
August 6, 2009 Data Types, Variables, and Arrays.
CS 106 Introduction to Computer Science I 01 / 26 / 2007 Instructor: Michael Eckmann.
1-Dec-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
CS 106 Introduction to Computer Science I 09 / 26 / 2007 Instructor: Michael Eckmann.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
CS 106 Introduction to Computer Science I 02 / 01 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
Arrays-. An array is a way to hold more than one value at a time. It's like a list of items.
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
An Introduction to Java – Part 1 Erin Hamalainen CS 265 Sec 001 October 20, 2010.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
BIT115: Introduction to Programming
CS 106 Introduction to Computer Science I 09 / 10 / 2007 Instructor: Michael Eckmann.
CS0007: Introduction to Computer Programming The for Loop, Accumulator Variables, Seninel Values, and The Random Class.
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS 1 Copyright: 2015 Illinois Institute of Technology_ George Koutsogiannakis.
CS 106 Introduction to Computer Science I 01 / 24 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 08 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 10 / 2007 Instructor: Michael Eckmann.
© 2004 Pearson Addison-Wesley. All rights reserved October 5, 2007 Arrays ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS
Arrays in Java.
Comparing Python and Java
Presentation transcript:

CS 106 Introduction to Computer Science I 10 / 04 / 2006 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS Fall 2006 Today’s Topics Comments and/or Questions? let's go over my solution to the Dates.java program from last lab. for loop review do while loop review nested loops labelled break and continue start discussion of arrays

for loops (what happens when?) for ( expression1; expression2; expression3 ) statement or compound statement example: int y = 0; for ( int x = 10; x >= 1; x-- ) { System.out.println(“x = “ + x); y += x; } System.out.println(“y = “ + y); Michael Eckmann - Skidmore College - CS Spring 2006

for loops for ( expression1; expression2; expression3 ) { statements } Order of operations: 1. expression1 executes first (and only once) 2. expression2 executes and if the result is false, program control goes to the code after the right curly brace 3. if the result is true, then the statements within the curly braces of the for loop execute in order then expression3 executes (which usually alters the value of the loop variable.) Then “goto” 2 above. Michael Eckmann - Skidmore College - CS Fall (if 2 was true)

Nested loops The body of a loop can contain another loop. We say that the inner loop is nested inside (or within) the outer loop. It is important to understand that the inner loop executes fully (all its iterations) within each iteration of the outer loop. For example, if the inner loop of a nested loop always iterates 10 times and the outer loop always iterates 5 times --- the code within the inner loop (its body) will end up executing a total of 50 times. Michael Eckmann - Skidmore College - CS Fall 2005

Nested loop - what prints? public class Printing { public static void main(String args[]) { for ( int i = 1; i <= 10; i++) { for ( int j = 1; j<=5; j++) { } System.out.println(); } Michael Eckmann - Skidmore College - CS Fall 2005

Nested loop - what prints? public class Printing2 { public static void main(String args[]) { int i = 1, j = 1; for ( ; i <= 10; i++) { for ( ; j<=5; j++) { } System.out.println(); } Michael Eckmann - Skidmore College - CS Fall 2005

Nested loop - what prints? public class Printing3 { public static void main(String args[]) { int i = 1, j = 1; while (i <= 10) { while ( j<=5) { j++; } System.out.println(); i++; } Michael Eckmann - Skidmore College - CS Fall 2005

Nested loop - what prints? public class Printing4 { public static void main(String args[]) { int i = 1; while (i <= 10) { int j = 1; while ( j<=5) { j++; } System.out.println(); i++; } Michael Eckmann - Skidmore College - CS Fall 2005

Pay attention to control variable The lesson is, to pay attention to the control variables. Does the inner loop's control variable get reset each time through the outer loop?, etc. Michael Eckmann - Skidmore College - CS Fall 2005

More Nesting example on page 144 Michael Eckmann - Skidmore College - CS Fall 2005

break; and continue; What does break do? Where are all the places break is allowed? What does continue do? Where are all the places continue is allowed? Michael Eckmann - Skidmore College - CS Fall 2005

labeled break & continue statements labeled break statements within nested structures (while, for, do while or switch structure) –A label can be given to a block of code. A block of code is defined as code enclosed in a pair of curly braces. –The labeled break statement can specify the block to “jump out of.” Non-labeled break statements could only “jump out of” the immediately enclosed structure, whereas, labeled break statements can “jump totally out of” a set of nested structures. Michael Eckmann - Skidmore College - CS Fall 2005

labeled break & continue statements labeled continue statements within nested structures (while, for, or do while structure) –A label can be given to a structure. –The labeled continue statement causes execution to skip the code in the enclosing structures and continues with the next iteration of the structure with the specified label. Michael Eckmann - Skidmore College - CS Fall 2005

labeled break & continue statements Let’s look at an example of how to label a block and nest some loops so that we can do a labeled break; Michael Eckmann - Skidmore College - CS Fall 2005

Arrays A data structure of related data items of the same type They store this data in contiguous memory locations Each data item in an array is an element. Arrays are denoted with brackets. When you want to select one element of the array, use its index in brackets. Array indices start at 0. When we speak of arrays: array means the full array, and array element is one element of the array. Michael Eckmann - Skidmore College - CS Fall 2005

Arrays Arrays can be of any type (user-defined class types, Java API class types, or primitive types.) Arrays of primitive types: –Might use an array of integers to store test scores. (better than creating individual variables of type int for reasons which should become clear.) –Might use an array of doubles to store a list of temperatures. Arrays of Java API class types: –Might use an array of Strings to store a list of people’s names. Michael Eckmann - Skidmore College - CS Fall 2005

Arrays (declaring and allocating) Arrays, like any variables need to be declared. Arrays also need to have memory allocated for a specific number of elements. Example: int test_scores[]; // declares an array of ints test_scores = new int[ 36 ]; // the line above allocates enough memory for 36 ints Michael Eckmann - Skidmore College - CS Fall 2005

Arrays (declaring and allocating) Arrays can be declared and allocated in one line. Example: // declares an array of ints & allocates enough memory for 36 ints int test_scores[] = new int[ 36 ]; Michael Eckmann - Skidmore College - CS Fall 2005

Arrays (allocating and initializing) When arrays are allocated, the array elements are all initialized to the following values: Arrays of numeric primitive types (byte, short, long, int, float, double, char) have their elements initialized to 0. Arrays of booleans (the only other primitive type) have their elements initialized to false. Arrays of any non-primitive data type, (classes in the Java API or user-defined classes) have their elements initialized to null. Michael Eckmann - Skidmore College - CS Fall 2005

Arrays (declaring, allocating and initializing) Arrays can be declared, allocated and initialized to user defined values all in one line. Example: int test_scores[] = { 130, 120, 111, 44, 181, 183, 160, 165, 148, 147, 146, 153, 139, 150, 153, 122, 144, 148, 149, 161, 144, 155, 132, 158, 151, 129, 148, 144, 143, 165, 121, 141, 149, 148, 180, 150 }; // declares an array of ints & allocates enough memory for 36 ints and // initializes each element to the value listed Michael Eckmann - Skidmore College - CS Fall 2005

Arrays In our example of the test_scores array, each element of this array is an int. So, we can refer to say the 10th element of the array by test_scores[9] and this is an int, so we can use it anywhere an int can be used. REMEMBER: Array indices start at 0 (zero) and end at the (size of the array – 1). –E.g. Indices for an array of size 36, go from 0 to 35. Array values can be set one element at a time by supplying the index like so: // stores the value 130 in the 0 element of the array test_scores test_scores[0] = 130; // stores the value 120 in the 1 element of the array test_scores test_scores[1] = 120; // etc … Michael Eckmann - Skidmore College - CS Fall 2005

Arrays Each array knows its own length, use the array name followed by.length to get access to its length. In our case for the test_scores example, the length is 36 (0 to 35.) Arrays are easily manipulated with loops. Let’s look at an example. Michael Eckmann - Skidmore College - CS Fall 2005

Array code example // Code to populate (store values into the elements of) an array int test_scores[] = new int[ 36 ]; String user_input; for (int i=0; i < test_scores.length; i++) { user_input = JOptionPane.showInputDialog( “Enter an integer”); test_scores[i] = Integer.parseInt(user_input); } Michael Eckmann - Skidmore College - CS Fall 2005

Array code example // Code to find the lowest element of an array of ints. int least = test_scores[0]; for (int i=0; i < test_scores.length; i++) { if (test_scores[i] < least) least = test_scores[i]; } System.out.println("The lowest value in the array is: " + least); Michael Eckmann - Skidmore College - CS Fall 2005

Array code example Let's write a program to find the average test score. What info do we need to compute the average score? Michael Eckmann - Skidmore College - CS Fall 2005

Example of an array of Strings String peoples_names[] = new String[10]; // The above line declares and allocates space for 10 Strings. // The Strings then can have their values set like: peoples_names[0] = “Jerry Garcia"; peoples_names[1] = “Bob Weir"; // etc. Michael Eckmann - Skidmore College - CS Fall 2005

Example of an array of Strings Just like you'd expect, not only String literals are allowed to be assigned, we could assign another String variable value to one of the elements of the peoples_names array. String peoples_names[] = new String[10]; for (int i=0; i < peoples_names.length; i++) { String user_input_string = JOptionPane.showInputDialog("Enter a name"); peoples_names[i] = user_input_string; } Michael Eckmann - Skidmore College - CS Fall 2005

.length of an array VS..length() method of String class Understand the difference between –.length (the variable accessible for Arrays to tell us how many elements the array has) –.length() (the method in class String to find out how many characters are in the String.) e.g. String peoples_names[] = new String[10]; // peoples_names.length --- this is the length of the array which is 10 here. peoples_names[0] = “Jerry Garcia”; // peoples_names[0].length() --- this is how many characters are in // the String peoples_names[0] --- which is 12 Michael Eckmann - Skidmore College - CS Fall 2005

.length of an array VS..length() method of String class String peoples_names[] = new String[10]; peoples_names[0] = “Jerry Garcia”; System.out.println( “The number of elements of the array peoples_names is:” + peoples_names.length); System.out.println( “The number of characters in the 0 element of the array is:” + peoples_names[0].length()); // so what #’s will these print? Michael Eckmann - Skidmore College - CS Fall 2005

Arrays of Strings So, let’s write a for loop that will populate an array of Strings based on user input. What will the for loop’s control variable be initialized to? What will the condition to stop the loop be? What will we increment the control variable by? Michael Eckmann - Skidmore College - CS Fall 2005