Advanced Computer Science Lesson 4: Reviewing Loops and Arrays Reading User Input.
-
Upload
damian-phillips -
Category
Documents
-
view
214 -
download
2
Transcript of Advanced Computer Science Lesson 4: Reviewing Loops and Arrays Reading User Input.
![Page 1: Advanced Computer Science Lesson 4: Reviewing Loops and Arrays Reading User Input.](https://reader036.fdocuments.us/reader036/viewer/2022083006/56649f315503460f94c4c249/html5/thumbnails/1.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022083006/56649f315503460f94c4c249/html5/thumbnails/2.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022083006/56649f315503460f94c4c249/html5/thumbnails/3.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022083006/56649f315503460f94c4c249/html5/thumbnails/4.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022083006/56649f315503460f94c4c249/html5/thumbnails/5.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022083006/56649f315503460f94c4c249/html5/thumbnails/6.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022083006/56649f315503460f94c4c249/html5/thumbnails/7.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022083006/56649f315503460f94c4c249/html5/thumbnails/8.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022083006/56649f315503460f94c4c249/html5/thumbnails/9.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022083006/56649f315503460f94c4c249/html5/thumbnails/10.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022083006/56649f315503460f94c4c249/html5/thumbnails/11.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022083006/56649f315503460f94c4c249/html5/thumbnails/12.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022083006/56649f315503460f94c4c249/html5/thumbnails/13.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022083006/56649f315503460f94c4c249/html5/thumbnails/14.jpg)
Homework Review
Put problems on the board
Review Solutions Document
![Page 15: Advanced Computer Science Lesson 4: Reviewing Loops and Arrays Reading User Input.](https://reader036.fdocuments.us/reader036/viewer/2022083006/56649f315503460f94c4c249/html5/thumbnails/15.jpg)
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.](https://reader036.fdocuments.us/reader036/viewer/2022083006/56649f315503460f94c4c249/html5/thumbnails/16.jpg)
Problems
• P6.9 / P6.6