advanced computer science lesson 4: reviewing loops and arrays reading user input

16
Advanced Computer Science Lesson 4: Reviewing Loops and Arrays Reading User Input

Upload: damian-phillips

Post on 05-Jan-2016

214 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Advanced Computer Science Lesson 4: Reviewing Loops and Arrays Reading User Input

Advanced Computer Science

Lesson 4: Reviewing Loops and ArraysReading User Input

Page 2: Advanced Computer Science Lesson 4: Reviewing Loops and Arrays Reading User Input

Agenda

• Review Key Topics From Last 2 Sessions• Review Loops– Simple Loop Example

• Review Arrays– Simple Array Example

• Review Homework #3: Marathon Runners• Reading User Input• Example Problems

Page 3: Advanced Computer Science Lesson 4: Reviewing Loops and Arrays Reading User Input

Lesson 2 Review• Describe Variable Types. Be able to specify the Name,

Range of Values, Amount of Memory Used for each of the major types.– Describe how to define and set the value of a variable

• State the syntax for implementing a method. • Demonstrate the use of a method that accepts

parameters and returns a value. – Identify the scope of variables in the method and outside of

the method• State the syntax of an if / else statement. Name the

Comparison operators. Given a series of sample conditions, evaluate the outcome (True / False)

Page 4: Advanced Computer Science Lesson 4: Reviewing Loops and Arrays Reading User Input

While Loop Review

Syntax of a While Loopwhile (condition) {

statements}

Exampleint i = 0;while (i < 3) {

System.out.println (“Rule #” + i);i = i+1;

}

Page 5: Advanced Computer Science Lesson 4: Reviewing Loops and Arrays Reading User Input

For Loop Review

Syntax of a for Loopfor (initialization; condition; update) {statements}

Examplefor (int i = 0; i < 3; i=i+1) {

System.out.println (“Rule #” + i);}

Page 6: Advanced Computer Science Lesson 4: Reviewing Loops and Arrays Reading User Input

Loop Statements

BREAK (break): Exits out of the loop and continues with the code AFTER the loop

CONTINUE: Exits out of the loop and continues with the code at the BEGINNING of the loop

Page 7: Advanced Computer Science Lesson 4: Reviewing Loops and Arrays Reading User Input

Programming Test

1: Write a piece of code that will print 100 numbers in order, starting at 1

2: Write a piece of code that will print 100 even numbers in order, starting at 2,

Page 8: Advanced Computer Science Lesson 4: Reviewing Loops and Arrays Reading User Input

Array Review

An array is an indexed list of values, of any typeSyntax: TYPE [] name = new TYPE [SIZE];Example: int [ ] values = new int [5];

Example using a variable to specify the size:

int size = 12;int [] values = new int [size];

Page 9: Advanced Computer Science Lesson 4: Reviewing Loops and Arrays Reading User Input

Arrays

Example: An Array of Integers

Values

Index

0 1 2 3 4 … n-1

Page 10: Advanced Computer Science Lesson 4: Reviewing Loops and Arrays Reading User Input

Array Initialization

Curly braces can be used to initialize an array.It can ONLY be used when you declare the variable.

int[] values = { 12, 24, -23, 47 };

Page 11: Advanced Computer Science Lesson 4: Reviewing Loops and Arrays Reading User Input

Accessing Arrays

To access the elements of an array, use the [] operator

values [iIndex] = X;

EG int[] values = {12, 24, -32, 47}; values [3] = 99; //What is in the array? int x = values[1] + 10; // what is X?

Page 12: Advanced Computer Science Lesson 4: Reviewing Loops and Arrays Reading User Input

The length variable

Each array has a length variable built-in that contains the length of the array.

EGint[] values = new int [12];int size = values.length; // size = 12int[] values2 = {1,2,3,4,5};int size2 = values2.length; //size2 = ??

Page 13: Advanced Computer Science Lesson 4: Reviewing Loops and Arrays Reading User Input

Array Programming Test

1: Write a piece of code that will generate 100 random numbers and put them into an array. Use the method Math.random() (Google this if you need the syntax).

2. Write a METHOD that will take the array created above as an input, and return the AVERAGE of the numbers.

Page 14: Advanced Computer Science Lesson 4: Reviewing Loops and Arrays Reading User Input

Homework Review

Put problems on the board

Review Solutions Document

Page 15: Advanced Computer Science Lesson 4: Reviewing Loops and Arrays Reading User Input

Reading Input From A User

1. Create an object that captures input from the console

Scanner sConsole = new Scanner (System.in);

2. Read data and put it into a StringString sInputText = sConsole.next();

Page 16: Advanced Computer Science Lesson 4: Reviewing Loops and Arrays Reading User Input

Problems

• P6.9 / P6.6