advanced computer science lesson 4: reviewing loops and arrays reading user input
TRANSCRIPT
Advanced Computer Science
Lesson 4: Reviewing Loops and ArraysReading 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
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)
While Loop Review
Syntax of a While Loopwhile (condition) {
statements}
Exampleint i = 0;while (i < 3) {
System.out.println (“Rule #” + i);i = i+1;
}
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);}
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
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,
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];
Arrays
Example: An Array of Integers
Values
Index
0 1 2 3 4 … n-1
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 };
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?
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 = ??
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.
Homework Review
Put problems on the board
Review Solutions Document
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();
Problems
• P6.9 / P6.6