Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.

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

Week 8 - Monday.  What did we talk about last time?  StdAudio.
Week 7: Input and Output 1.  Now we are going to talk a little bit about output  You have a lot of experience with System.out.println() and System.out.print()
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Week 5: Loops 1.  Repetition is the ability to do something over and over again  With repetition in the mix, we can solve practically any problem that.
Week 7 - Friday.  What did we talk about last time?  Array examples.
PHP Functions and Control Structures. 2 Defining Functions Functions are groups of statements that you can execute as a single unit Function definitions.
CS 106 Introduction to Computer Science I 02 / 18 / 2008 Instructor: Michael Eckmann.
Week 9: Methods 1.  We have written lots of code so far  It has all been inside of the main() method  What about a big program?  The main() method.
Computer Science 1620 Loops.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
Loops – While, Do, For Repetition Statements Introduction to Arrays
1 CS 177 Week 15 Recitation Slides Review. Announcements Final Exam on Sat. May 8th  PHY 112 from 8-10 AM Complete your online review of your classes.
CS 106 Introduction to Computer Science I 10 / 04 / 2006 Instructor: Michael Eckmann.
Week 8: Audio Processing 1.  Light and sound are both transmitted in waves 2.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
The switch Statement, DecimalFormat, and Introduction to Looping
TODAY’S LECTURE Review Chapter 2 Go over exercises.
Games and Simulations O-O Programming in Java The Walker School
Week 7 - Wednesday.  What did we talk about last time?  Introduction to arrays  Lab 6.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Week 5 - Wednesday.  What did we talk about last time?  Exam 1!  And before that?  Review!  And before that?  if and switch statements.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Week 6 - Wednesday.  What did we talk about last time?  Exam 1 post-mortem  Recursive running time.
Week 4 - Wednesday.  What did we talk about last time?  if statements  else statements  Nested selection statements.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
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.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
1 Arrays An array is a collection of data values, all of which have the same type. The size of the array is fixed at creation. To refer to specific values.
PHP Conditional Statements Conditional statements in PHP are used to perform different actions based on different conditions. Conditional Statements Very.
Compound Statements If you want to do more than one statement if an IF- else case, you can form a block of statements, or compound statement, by enclosing.
CPS120: Introduction to Computer Science Decision Making in Programs.
Chapter 2 Functions and Control Structures PHP Programming with MySQL 2 nd Edition.
CSc2310 tutoring session, week 8 Fall, 2012 Haidong Xue 5:30pm—8:30pm 11/06/2012 and 11/07/2012 -Test 3 Study Guide.
Week 3 - Wednesday.  What did we talk about last time?  Other C features  sizeof, const  ASCII table  printf() format strings  Bitwise operations.
Introduction to Java Java Translation Program Structure
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.
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
Week 7 - Friday.  What did we talk about last time?  Array examples  Sound.
Week 8 - Friday.  What did we talk about last time?  Static methods.
Week 6 - Monday.  What did we talk about last time?  while loop examples  Lab 5.
Arrays Chapter 6. Objectives learn about arrays and how to use them in Java programs learn how to use array parameters and how to define methods that.
Week 9 - Wednesday.  What did we talk about last time?  2D arrays  Queen attacking pawn example  Started Game of Life.
Week 7 - Wednesday.  What did we talk about last time?  Introduction to arrays  Lab 6.
Week 10 - Wednesday.  What did we talk about last time?  Method example  Roulette simulation  Types in Java.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
Week 10 - Friday.  What did we talk about last time?  References and primitive types  Started review.
Week 6 - Friday.  What did we talk about last time?  Loop examples.
Midterm Review Tami Meredith. Primitive Data Types byte, short, int, long Values without a decimal point,..., -1, 0, 1, 2,... float, double Values with.
Week 3 - Friday.  What did we talk about last time?  Preprocessor directives  Other C features  sizeof, const  ASCII table  printf() format strings.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS 1 Copyright: 2015 Illinois Institute of Technology_ George Koutsogiannakis.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Today… Preparation for doing Assignment 1. Invoking methods overview. Conditionals and Loops. Winter 2016CMPE212 - Prof. McLeod1.
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
Arrays Chapter 7.
Information and Computer Sciences University of Hawaii, Manoa
Week 3 - Friday CS222.
Week 7 - Friday CS 121.
Week 7 - Wednesday CS 121.
Week 8 - Friday CS 121.
Week 8 - Monday CS 121.
Week 15 – Wednesday CS 121.
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
CS 177 Week 15 Recitation Slides
Starting Out with Java: From Control Structures through Objects
CISC124 Labs start this week in JEFF 155. Fall 2018
Review for Midterm 3.
Week 7 - Monday CS 121.
Presentation transcript:

Week 15 – Wednesday

 What did we talk about last time?  Review up to Exam 1

 Making choices with if statements  Basics  Having if bodies with more than one line  Using else blocks  Nesting if statements  Using switch statements

The if part Any boolean expression Any single executable statement if( condition ) statement;

Two different outcomes if( condition ) statement1; else statement2;

if( condition ) { statement1; statement2; … statementn; } A whole bunch of statements

if( condition1 ) { statement1; if( condition2 ) { if( condition3 ) statement2; … }

 The most common condition you will find in an if is a comparison between two things  In Java, that comparison can be:  == equals  != does not equal  < less than  <= less than or equal to  > greater than  >= greater than or equal to

switch( data ) { case value1: statements 1; case value2: statements 2; … case valuen: statements n; default: default statements; }

int data = 3; switch( data ) { case 3: System.out.println("Three"); case 4: System.out.println("Four"); break; case 5: System.out.println("Five"); } Both "Three" and "Four" are printed The break is optional The default is optional too

1. The data that you are performing your switch on must be either an int, a char, or a String 2. The value for each case must be a literal 3. Execution will jump to the case that matches 4. If no case matches, it will go to default 5. If there is no default, it will skip the whole switch block 6. Execution will continue until it hits a break

 Allow us to repeatedly execute code  Care must be taken to run exactly the right number of times  Not too many  Not too few  Not an infinite number  Not zero (unless that’s what should happen)  Loops come in three flavors:  while loops  for loops  do-while loops

 Used when you don’t know how many times a loop will run  Runs as long as the condition is true  Syntax: while( condition ) { //statements //braces not needed for single //statement }

 Used when you do know how many times a loop will run  Still runs as long as the condition is true  Syntax: for(initialize; condition; increment) { //statements //braces not needed for single //statement }

 Used infrequently, mostly for input  Useful when you need to guarantee that the loop will run at least once  Runs as long as the condition is true  Syntax: do { //statements //braces not needed for single //statement } while( condition );

 Infinite loops  Almost infinite loops (with overflow or underflow)  Fencepost errors (off by one)  Skipping loops entirely  Misplaced semicolon

 An array is a homogeneous, static data structure  Homogeneous means that everything in the array is the same type: int, double, String, etc.  Static (in this case) means that the size of the array is fixed when you create it

 To declare an array of a specified type with a given name :  Example with a list of type int :  Just like any variable declaration, but with [] type[] name; int[] list;

 When you declare an array, you are only creating a variable that can hold an array  At first, it holds nothing, also know as null  To use it, you have to create an array, supplying a specific size:  This code creates an array of 100 int s int[] list; list = new int[100]; int[] list; list = new int[100];

 You can access an element of an array by indexing into it, using square brackets and a number  Once you have indexed into an array, that variable behaves exactly like any other variable of that type  You can read values from it and store values into it  Indexing starts at 0 and stops at 1 less than the length list[9] = 142; System.out.println(list[9]); list[9] = 142; System.out.println(list[9]);

 When you instantiate an array, you specify the length  You can use its length member to find out int[] list = new int[42]; int size = list.length; System.out.println("List has " + size + " elements"); //prints 42 int[] list = new int[42]; int size = list.length; System.out.println("List has " + size + " elements"); //prints 42

 To declare a two dimensional array, we just use two sets of square brackets ( [][] ):  Doing so creates a variable that can hold a 2D array of int s  As before, we still need to instantiate the array to have a specific size: int [][] table; table = new int[5][10];

 StdDraw is a library of Java code developed by the authors of another textbook  StdDraw allows you to draw output on the screen easily  You can draw points, lines, and polygons in various colors  You can clear and resize the drawing area and even save the results  StdDraw is not standard Java that everyone uses, but it’s a nice tool for graphics

 The simplest things you can draw with StdDraw are lines and points  The first thing you should be aware of is that the canvas is drawn like Quadrant I of a Cartesian plane (0,0) (0,1)(1,1) (1,0)

 The following methods can be used to draw lines and points MethodUse void line(double x0, double y0, double x1, double y1) Draw a line from (x0,y0) to (x1,y1) void point(double x, double y) Draw a point at (x,y)

 Here are some methods for drawing circles and squares and setting the color for doing so: MethodUse void circle(double x, double y, double r) Draw a circle centered at (x,y) with radius r void filledCircle(double x, double y, double r) Draw a filled circle centered at (x,y) with radius r void square(double x, double y, double r) Draw a square centered at (x,y) with edges 2r void filledSquare(double x, double y, double r) Draw a filled square centered at (x,y) with edges 2r void setPenColor(Color c) Start drawing with color c

 Now, you can define your own colors  But StdDraw provides 13 presets  For example, to make something magenta, you would use the value StdDraw.MAGENTA BLACKBLUECYANDARK_GRAYGRAY GREENLIGHT_GRAYMAGENTAORANGEPINK REDWHITEYELLOW

 Audio data on Windows machines is sometimes stored in a WAV file  A WAV file is much simpler than an MP3 because it has no compression  Even so, it contains two channels (for stereo) and can have many different sample rates and formats for recording sound  The StdAudio class lets you read and write a WAV file easily and always deal with a single array of sound, sampled at 44,100 Hz

 Everything you’d want to do with sound:  To do interesting things, you have to manipulate the array of samples  Make sure you added StdAudio.java to your project before trying to use it MethodUse static double[] read(String file) Read a WAV file into an array of double s static void save(String file, double[] input) Save an array of double s (samples) into a WAV file static void play(String file) Play a WAV file static void play(double[] input) Play an array of double s (samples)

 Let’s load a file into an array:  If the song has these samples:  Perhaps samples will contain: String file = "song.wav"; double[] samples = StdAudio.read(file); String file = "song.wav"; double[] samples = StdAudio.read(file);

 Static methods allow you to break your program into individual pieces that can be called by each other repeatedly  Advantages:  More modular programming ▪ Break a program into separate tasks ▪ Each task could be assigned to a different programmer  Code reusability ▪ Use code over and over ▪ Even from other programs (like Math.sqrt() ) ▪ Less code (and error) duplication  Improved readability ▪ Each method can do a few, clear tasks ▪ Well named method are self-documenting

 A method takes in 0 or more parameters and returns 0 or 1 values  A method that doesn’t return a value is declared as a void method  Definition syntax: public static type name( type arg1, type arg2, … ) { //statements //braces are needed for single //statement }

 Proper syntax for calling a static method gives first the name of the class that the method is in, a dot, the name of the method, then the arguments  If the method is in the same class as the code calling it, you can leave off the Class. part  If it is a value returning method, you can store that value into a variable of the right type Class.name(arg1, arg2, arg3);

 No connection between the two different x ’s and y ’s xy public static int add(int x, int y){ xy int z = x + y; // return z; } xy public static int add(int x, int y){ xy int z = x + y; // return z; } int a = 10; int x = 3; 5a int y = add( 5, a ); //y contains 15 now int a = 10; int x = 3; 5a int y = add( 5, a ); //y contains 15 now

 When a method is called, the arguments passed into the method are copied into the parameters  The names for the values inside the method can be different from the names outside of the method  Methods cannot change the values of the arguments on the outside for primitive types  Methods can change the values inside of arrays and sometimes inside of object types

int a = 1; for( int i = 1; i < 1000; i *= 2 ) { for( int j = 0; j < 3; j++ ) { System.out.print(a + " "); a++; } int a = 1; for( int i = 1; i < 1000; i *= 2 ) { for( int j = 0; j < 3; j++ ) { System.out.print(a + " "); a++; }

 This is an image generated with StdDraw  It contains 100 line segments drawn in a spiral  Each is 90% as long as the last one  How would you code it?

public static int jolly( int n ) { int barnacle = 0; for( int i = 1; i <= n; i++ ) barnacle += roger( i ); return barnacle; } public static int roger( int flag ) { return flag * flag; } public static int jolly( int n ) { int barnacle = 0; for( int i = 1; i <= n; i++ ) barnacle += roger( i ); return barnacle; } public static int roger( int flag ) { return flag * flag; }

char[][] stuff = new char[2][13]; for( int i = 0; i < 2; i++ ) for( int j = 0; j < 13; j++ ) { stuff[i][j] = (char)((13*i + j) + 'a'); } char[][] stuff = new char[2][13]; for( int i = 0; i < 2; i++ ) for( int j = 0; j < 13; j++ ) { stuff[i][j] = (char)((13*i + j) + 'a'); }

 Review after Exam 2  Consider visiting CodingBat.com for Java practice  Lab 15

 Finish Project 5  Due Friday before midnight  Study for Final Exam  2:30 - 5:30pm, Thursday, 12/10/2015 (CS121B)  11:00am - 2:00pm, Monday, 12/07/2015 (CS121C)