Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Advanced Computer Science Lesson 4: Reviewing Loops and Arrays Reading User Input."— Presentation transcript:

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

2 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

3 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)

4 While Loop Review Syntax of a While Loop while (condition) { statements } Example int i = 0; while (i < 3) { System.out.println (“Rule #” + i); i = i+1; }

5 For Loop Review Syntax of a for Loop for (initialization; condition; update) { statements } Example for (int i = 0; i < 3; i=i+1) { System.out.println (“Rule #” + i); }

6 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

7 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,

8 Array Review An array is an indexed list of values, of any type Syntax: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];

9 Arrays Example: An Array of Integers Values Index 01234… n-1

10 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 };

11 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?

12 The length variable Each array has a length variable built-in that contains the length of the array. EG int[] values = new int [12]; int size = values.length; // size = 12 int[] values2 = {1,2,3,4,5}; int size2 = values2.length;//size2 = ??

13 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.

14 Homework Review Put problems on the board Review Solutions Document

15 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 String String sInputText = sConsole.next();

16 Problems P6.9 / P6.6


Download ppt "Advanced Computer Science Lesson 4: Reviewing Loops and Arrays Reading User Input."

Similar presentations


Ads by Google