February 16 - 20, 2009 CSE 113 B.

Slides:



Advertisements
Similar presentations
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Advertisements

IAT 800 Lab 1: Loops, Animation, and Simple User Interaction.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
Loops – While, Do, For Repetition Statements Introduction to Arrays
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
Looping Yong Choi School of Business CSU, Bakersfield.
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Loops III Lecture 19, Wed Mar
CSE 113 Week 5 February , Announcements  Module 2 due 2/15  Exam 3 is on 2/15  Module 3 due 2/22  Exam 4 is on 2/25  Module 4 due 2/29.
The If/Else Statement, Boolean Flags, and Menus Page 180
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Copyright © Texas Education Agency, Computer Programming For Loops.
COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.
COMP More About Classes Yi Hong May 22, 2015.
CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05.
Lecture 10 Instructor: Craig Duckett. Assignment 2 Revision TONIGHT DUE TONIGHT Wednesday, August 5 th Assignment 3 NEXT DUE NEXT Monday, August 10 th.
CS140: Intro to CS An Overview of Programming in C by Erin Chambers.
Repetition Statements.  Often it is necessary to repeat statements many times  Java has two ways of doing this  while statements  for statements.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
New Tools And Workshop Mod & For Loops. Modulo Calculates the remainder (remember long division?) % Examples: 7 % 3 10 % 2 2 % 3 evaluates to 1 evaluates.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
Computer Science Department LOOPS. Computer Science Department Loops Loops Cause a section of your program to be repeated a certain number of times. The.
TOPIC 6 MODIFYING PICTURES USING LOOPS 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and.
Repetition Statements while and do while loops
February ,  2/16: Exam 1 Makeup Papers Available  2/20: Exam 2 Review Sheet Available in Lecture  2/27: Lab 2 due by 11:59:59pm  3/2:
Loops Robin Burke IT 130. Outline Announcement: Homework #6 Conditionals (review) Iteration while loop while with counter for loops.
Often being different. Control flow By default Java (and therefore Processing) executes lines of a program one after the other –Doesn’t matter what happened.
1.1: Objects and Classes msklug.weebly.com. Agenda: Attendance Let’s get started What is Java? Work Time.
Loops. More Flow of Control  Sometimes we want to do something many times.  Don’t have to write all the steps out multiple times.  Use a LOOP – control.
CS0007: Introduction to Computer Programming The for Loop, Accumulator Variables, Seninel Values, and The Random Class.
Loops ( while and for ) CSE 1310 – Introduction to Computers and Programming Alexandra Stefan 1.
Follow up from lab See Magic8Ball.java Issues that you ran into.
Barbara Ericson Georgia Tech Sept 2005
Information and Computer Sciences University of Hawaii, Manoa
Chapter 9 Repetition.
Chapter 5: Structured Programming
Exam 2 Review.
Chapter 4 Repetition Statements (loops)
Manipulating Pictures, Arrays, and Loops part 2
Manipulating Pictures, Arrays, and Loops part 2
Topic 6 Modifying Pictures Using Loops
Loop Structures.
Review If you want to display a floating-point number in a particular format use The DecimalFormat Class printf A loop is… a control structure that causes.
Incrementing ITP © Ron Poet Lecture 8.
Computation as an Expressive Medium
CS 106A, Lecture 7 Parameters and Return
Chapter 5 Repetition.
Manipulating Pictures, Arrays, and Loops part 2
Manipulating Pictures, Arrays, and Loops part 2
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Functions BIS1523 – Lecture 17.
Outline Altering flow of control Boolean expressions
LRobot Game.
Chapter 9 Control Structures.
An Introduction to Java – Part I, language basics
CSE 113 A February , 2009.
Testing and Repetition
Lab5 PROGRAMMING 1 Loop chapter4.
Manipulating Pictures, Arrays, and Loops
February , 2009 CSE 113 B.
Language Constructs Construct means to build or put together. Language constructs refers to those parts which make up a high level programming language.
Seating “chart” Front - Screen rows Back DOOR.
Manipulating Pictures, Arrays, and Loops
February 2 - 6, 2009 CSE 113 B.
In this class, we will cover:
PROGRAM FLOWCHART Iteration Statements.
The for-statement.
CSC1401 Manipulating Pictures 2
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
Loops CGS3416 Spring 2019 Lecture 7.
Presentation transcript:

February 16 - 20, 2009 CSE 113 B

Announcements 2/16: Exam 1 Makeup Papers Available 2/20: Exam 2 Review Sheet Available in Lecture 2/27: Lab 2 due by 11:59:59pm 3/2: Exam 2 3/4: Go over Exam 2 3/6: Exam 2 Makeup

Methods for CSE113Picture class Method that takes in a pixel location and returns the color of that pixel. public java.awt.Color getColorAtPixel(int x, int y) { //Get the pixel at the location (x,y) Pixel pixel = _pic.getPixel(x,y); //Get the color from that pixel java.awt.Color color = pixel.getColor(); //return the color to the caller of the method return color; }

Comments Comments are text inserted into the program for humans to read. Comments are ignored by the compiler. Comments can be of two types: // starts a to-the-end-of-line comment /* is the start of a multi-line comment Which ends with */ DrJava colors comments green to differentiate them from the rest of the code.

Method for CSE113Picture class Write a method that takes in a pixel location and a color and sets the color of that pixel to the color passed in as the parameter. public void setColorAtPixel(int x, int y, java.awt.Color color) { //Get the pixel at the location (x,y) Pixel pixel = _pic.getPixel(x,y); //Set the color of that pixel pixel.setColor(color); }

Repetition If we want the computer to do something over and over for us, we need to use repetition. Different programming languages implement repetition in different ways. Most languages have loops as a way to implement repetition. Java has several different types of loops, one of them is the for-loop.

For-loop Syntax for(initialization; boolean expression; increment) { //loop body } The parts in black above are necessary syntax. The other parts are the names of the sections of the loop syntax. Let’s take a closer look at how a loop is executed in the machine and the different sections of the loop.

Code executed before the for keyword Loop Execution This the code that has been executed before we want to loop Code executed before the for keyword Then we initialize the loop counter – create a variable to help us keep track of how many times we’ve looped Initialization true false A boolean expression is one that evaluates to true or false. Depending on the evaluation, we will do two different things. This expression controls whether we keep looping or stop looping. Boolean Expression

Loop Execution - continued true false Boolean Expression This is the part of the code that is executed over and over again. loop body Increment our loop counter (usually by 1) to tell us that we have executed the loop one more time. Increment This is the code in the program after the loop is finished. Go back to the boolean expression to see if we need to execute the loop again Execute the rest of the code in program

Loop that prints out “Hello” five times for (int count = 1; count <= 5; count++) { System.out.println(“Hello”); } We typed this in at the interactions pane and saw the five “Hello”s on the screen. Remember that count++ is a shortcut way of writing count = count + 1

We can make a method to execute that loop public void printHello5Times() { for(int count = 1; count <= 5; count++) { System.out.println("Hello"); } Now we can call this method and see the Hellos on the screen.

How about letting the caller specify how many times to print hello? public void printHelloNTimes(int numberOfTimes) { for(int count = 1; count <= numberOfTimes; count++){ System.out.println("Hello"); } Notice the parameter that will tell us how many times Hello should be printed. When we call this method, we need to pass in the actual number of times to print Hello.

Print out hello the same number of times as the number of pixels in a picture’s width public void printHelloPictureWidthTimes() { for(int count=1; count<=_pic.getWidth(); count++){ System.out.println("Hello"); }

Print out numbers to count the width of the picture public void printPictureWidth() { for(int count=1; count<=_pic.getWidth(); count++){ System.out.println(count); } System.out.println(“The width of the picture is ” + _pic.getWidth()); The + in the last print statement is the string concatenation operator. It allows us to combine strings together (or in this case, a string and a number).

Looping through all pixels in a picture We can write similar methods for looping through the height of a picture. If we want to loop through all the pixels, then we need to combine the looping of the width and the height of a picture. Putting two loops together is called nesting of loops.

Nested loop that gets to every pixel in the picture Note that this is slightly different than what was talked about in lecture on Friday for(int column=0; column<_pic.getWidth(); column++){ for(int row=0; row<_pic.getHeight(); row++){ }

Method that prints out the color of every pixel in a picture public void printOutColorsOfPixels() { for(int column=0;column<_pic.getWidth();column++){ for(int row=0; row<_pic.getHeight(); row++) { Pixel pixel = _pic.getPixel(column, row); System.out.println(pixel.getColor()); } System.out.println("This picture is: " + _pic.getWidth()+" pixels wide "+" and " + _pic.getHeight()+" pixels tall.");

Working with Pictures We can use the basic format of the previous method any time we need to do something with the pixels of a picture. The only line that would need to change is the line after we get the pixel object. We use that line to do whatever we want to change the pixel.

Alternate loop for pictures public void sampleLoopForPictures() { for(int x = 0; x < _pic.getWidth(); x++){ for(int y = 0; y < _pic.getHeight(); y++) { Pixel pixel = _pic.getPixel(x, y); // Insert code for what you want to do // with pixels here. }