TOPIC 10 THE IF STATEMENT. Making Decisions. Making Decisions. If Statement Syntax. If Statement

Similar documents
TOPIC 10 THE IF STATEMENT

Changing a picture. Modifying Pictures. Looping (iteration) Changing all the pixels in a picture TOPIC 6 MODIFYING PICTURES USING LOOPS

TOPIC 6 MODIFYING PICTURES USING LOOPS

TOPIC 8 MORE ON WHILE LOOPS

Introduction to Computer Science with Java UCSD CSE 8AL Lab 6 2/11/09

TOPIC 5 INTRODUCTION TO PICTURES

The University of Western Ontario Department of Computer Science Computer Science 1026a Midterm Exam 2 hours

CSE 8A Lecture 20. LAST WEEK! You made it! (Almost ) PSA 9 due tonight (yayy!! Last one) Bring graded Exam #4 to Wed 3/13/13 lecture for review

Learning Goals LG1: Implement the peer instruction process by A) clicking in on an answer B) discussing with your team C) coming to consensus and

TOPIC 9 MODIFYING PIXELS IN A MATRIX: COPYING, CROPPING

CS 100 Python commands, computing concepts, and algorithmic approaches for final Fall 2015

TOPIC 5 INTRODUCTION TO PICTURES

CSE 8A Lecture 12. Reading for next class: : Sounds! Today s topics: Logical Operators:! && Start PSA 6: Chromakey!

Topic 4 - Introduction to Metering on a DSLR

A1: Introduction to digital pictures and color Questions: 1. How many bits does it take to represent the values from 0 to 255?

The first program: Little Crab

Control Structures in Java if-else and switch

Objects in Memory. Objects in Memory. Trace. Trace. Outline TOPIC 13 OBJECTS IN MEMORY, PASSING PARAMETERS

TOPIC 12 CREATING CLASSES PART 1

Conditional Statements

Sample Final Exam CSE8A Winter 2009: This exam not indicative of actual exam length

Part 1 Change Color Effects, like Black & White

In Java, data type boolean is used to represent Boolean data. Each boolean constant or variable can contain one of two values: true or false.

Creating objects TOPIC 3 INTRODUCTION TO PROGRAMMING. Making things to program with.

CSI201Proj4 READ assg. TODAY! 3 Inputs. 1 Output

Outline for Today CSE 142. CSE142 Wi03 G-1. withdraw Method for BankAccount. Class Invariants

APCS Semester #1 Final Exam Practice Problems

Range Objects and the ActiveCell

Key Points. COSC 123 Computer Creativity. Java Decisions and Loops. Making Decisions Performing Comparisons. Making Decisions

if (x == 0); System.out.println( x=0 ); if (x = 0) System.out.println( x=0 );

Lecture 8 CSE11 Fall 2013 While Loops, Switch Statement, Simplifying Conditionals

Outline for Today CSE 142. Programming a Teller Machine. CSE142 Wi03 I-1. ATM Algorithm for Dispensing Money

public static void main(string[] args) { GTest mywindow = new GTest(); // Title This program creates the following window and makes it visible:

Computer Applications Info Processing

b. Suppose you enter input from the console, when you run the program. What is the output?

Day 1: Introduction to MATLAB and Colorizing Images CURIE Academy 2015: Computational Photography Sign-Off Sheet

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

COSC 123 Computer Creativity. Java Decisions and Loops. Dr. Ramon Lawrence University of British Columbia Okanagan

COMP-202 Unit 4: Programming With Iterations. CONTENTS: The while and for statements

Control Structures in Java if-else and switch

Work with Shapes. Concepts CHAPTER. Concepts, page 3-1 Procedures, page 3-5

CS 170 Java Programming 1. Week 9: Learning about Loops

Pen Tool, Fill Layers, Color Range, Levels Adjustments, Magic Wand tool, and shadowing techniques

Outline. Turtles. Creating objects. Turtles. Turtles in Java 1/27/2011 TOPIC 3 INTRODUCTION TO PROGRAMMING. Making things to program with.

Control Structures. Code can be purely arithmetic assignments. At some point we will need some kind of control or decision making process to occur

An Introduction to Processing

Warmup : Name that tune!

Administration. Conditional Statements. Agenda. Syntax. Flow of control. Lab 2 due now on floppy Lab 3 due tomorrow via FTP

Building Java Programs

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 is going to get really

Title and Modify Page Properties

Topic 0b Graphics for Science & Engineering

Krita Advanced Coloring Tutorial!

Computer Programming, I. Laboratory Manual. Final Exam Solution

MCS 2514 Fall 2012 Programming Assignment 3 Image Processing Pointers, Class & Dynamic Data Due: Nov 25, 11:59 pm.

Using The Graph Club 1.5

STUDENT LESSON A5 Designing and Using Classes

Java Review. Fundamentals of Computer Science

Topics. Chapter 5. Equality Operators

Texture Mapping. Images from 3D Creative Magazine

CS 106 Introduction to Computer Science I

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

Array Basics: Outline. Creating and Accessing Arrays. Creating and Accessing Arrays. Arrays (Savitch, Chapter 7)

CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall Office hours:

CSCI 161 Introduction to Computer Science

School of Computer Science CPS109 Course Notes 5 Alexander Ferworn Updated Fall 15

CS Introduction to Programming Midterm Exam #1 - Prof. Reed Fall 2009

Java Foundations: Unit 3. Parts of a Java Program

BSc. (Hons.) Software Engineering. Examinations for / Semester 2

Pace University. Fundamental Concepts of CS121 1

Preface A Brief History Pilot Test Results

CONTENTS: Array Usage Multi-Dimensional Arrays Reference Types. COMP-202 Unit 6: Arrays

CSC 101: Lab Manual#11 Programming Turtle Graphics in Python Lab due date: 5:00pm, day after lab session

cis20.1 design and implementation of software applications I fall 2007 lecture # I.2 topics: introduction to java, part 1

Using Flash Animation Basics

CIS 110 Introduction To Computer Programming. February 29, 2012 Midterm

The compiler is spewing error messages.

EXAM Computer Science 1 Part 1

Give one example where you might wish to use a three dimensional array

Graphics Hardware and Display Devices

STUDENT LESSON A12 Iterations

Problem Solving: Storyboards for User Interaction

CpSc 1111 Lab 6 Conditional Statements, Loops, the Math Library, and Random Numbers What s the Point?

Home. Video. Paint Balling. Quad Biking. Website Report. Archery. Contact. Target Audience. Navigation Structure

Lecture Set 4: More About Methods and More About Operators

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.

Java Programming Fundamentals - Day Instructor: Jason Yoon Website:

Introduction to C++ IT 1033: Fundamentals of Programming

Lego Controller Version1.8

COMP-202: Foundations of Programming. Lecture 26: Image Manipulation; Wrap-Up Jackie Cheung, Winter 2015

Lecture 4. CS118 Term planner. The simple if statement. Control flow: selection. The if... else statement cont... The if... else statement.

CSE8A Lecture 4 IN TERM EXAM 1 THIS FRIDAY. PSA2 published soon watch weebly web page; due next Monday

Classes, interfaces, & documentation. Review of basic building blocks

SOFTWARE DEVELOPMENT 1. Control Structures 2018W A. Ferscha (Institute of Pervasive Computing, JKU Linz)

CONTENTS: While loops Class (static) variables and constants Top Down Programming For loops Nested Loops

Course Outline. Introduction to java

Visual Programming. Lecture 3: Loops, Arrays. Mahmoud El-Gayyar

Module 2: Choice and Iteration

School of Computer Science CPS109 Course Notes 6 Alexander Ferworn Updated Fall 15. CPS109 Course Notes 6. Alexander Ferworn

Introduction to Software Development (ISD) Week 3

Transcription:

1 Outline 2 2 TOPIC 10 THE IF STATEMENT How to use conditionals Picture manipulation using conditionals: Edge detection Sepia toning Chromakey (Blue-screening) Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B. Ericson, and instructor materials prepared by B. Ericson. Making Decisions Making Decisions 3 3 4 4 Computer programs often have to make decisions Taking different actions depending on some condition Examples: If the amount you want to withdraw is less than your bank balance, you are allowed to make the withdrawal, otherwise you are not If a number is negative, you cannot take its square root More examples: If a turtle is too close to the edge of its world, it cannot move forward the specified distance If a red value for a pixel is already 255, you can t increase it Making decisions in high-level programming languages is called conditional execution and is done using the if and if- If Statement If Statement Syntax 5 5 6 6 A of s is executed only if some condition is true If the condition is false, execution falls through to the next following the if Statement or block true Next false We may want a single to be executed if the condition is true: We may want a block of s to be executed if the condition is true: Safer to always use braces, even if a single For readability, make sure you line up the braces

If - Statement If - Statement Syntax 77 Using an if and, we can do one thing if the condition is true or a different thing if the condition is false true Statement or block Next false Statement or block 8 8 If the condition is true, 1 (or block of s) is executed, otherwise 2 (or block of s) is executed 1 2 Example: if (x < y) System.out.println("y is larger"); System.out.println("x is larger"); Conditional Statement Example Conditional Statement Example 9 9 Bank withdrawal example: // amount: the amount you want to withdraw // balance: your account balance if (amount <= balance) balance = balance amount; System.out.println("Balance is " + balance); 10 To let the user know that the withdrawal was not allowed if (amount <= balance) balance = balance amount; System.out.println("New balance is " + balance); System.out.println("Sorry, you are trying to withdraw $" + amount + " and your balance is only $" + balance); What happens if amount > balance? Using Conditions in Pictures Simple Color Replacement 11 Choosing which pixels to manipulate in a picture To remove red-eye in an image To create sepia-toned images To posterize images (reduce the number of color) Do blue-screen effect etc. 12 Suppose we wanted to change all of the white in an image to a bluer white Algorithm: For each pixel in the picture If the pixel s color is white Decrease the red and green in this color

Simple Color Replacement Method 13 public void makewhitemoreblue() for (int x = 0; x < this.getwidth(); x++) for (int y = 0; y < this.getheight(); y++) Pixel pixelobj = this.getpixel(x,y); int red = pixelobj.getred(); int green = pixelobj.getgreen(); int blue = pixelobj.getblue(); if (red == 255 && green == 255 && blue == 255) pixelobj.setred(200); pixelobj.setgreen(200); 14 A better method Allow more flexibility: Replace the color when all red, green and blue are above a threshold In this case, reduce red and green, and increase blue Simple Color Replacement Method 15 public void makewhitemoreblue() for (int x = 0; x < this.getwidth(); x++) for (int y = 0; y < this.getheight(); y++) Pixel pixelobj = this.getpixel(x,y); int red = pixelobj.getred(); int green = pixelobj.getgreen(); int blue = pixelobj.getblue(); if (red >= 200 && green >= 200 && blue >= 200) pixelobj.setred(200); pixelobj.setgreen(200); pixelobj.setblue(255); 16 Multiple if s Suppose we are doing different things based on a set of ranges, for example: 0 <= x <= 5 5 < x <= 10 10 < x if (0 <= x && x <= 5) if (5 < x && x <= 10) if (10 < x) Using if for > 2 Options Sepia-Toned Pictures 17 Or we can use several if s You don t need to check if x > 5, since the first if block would have been executed if it was <= 5 if (0 <= x && x <= 5) if (x <= 10) 18 They have a yellowish tint, used to make things look old // what must x be?

Sepia-Toned Algorithm Sepia-Toned Method 19 20 First make the picture grayscale Change the darkest grays (shadows) to be even darker Make the middle grays a brown color By reducing the blue Make the lightest grays (highlights) a bit yellow By increasing red and green Or decreasing blue (less than before though) We now have more than 2 possibilities, so we need to use multiple if s public void sepiatint() // first change the current picture to grayscale this.grayscale(); // loop through the pixels for (int x = 0; x < this.getwidth(); x++) for (int y = 0; y < this.getheight(); y++) // get the current pixel and its color values Pixel pixelobj = this.getpixel(x,y); int redvalue = pixelobj.getred(); int greenvalue = pixelobj.getgreen(); int bluevalue = pixelobj.getblue(); Sepia-Toned Method (continued) Sepia-Toned Method (continued) 21 // tint the shadows darker if (redvalue < 60) redvalue = (int) (redvalue * 0.9); greenvalue = (int) (greenvalue * 0.9); bluevalue = (int) (bluevalue * 0.9); // tint the midtones a light brown by reducing the blue if (redvalue < 190) bluevalue = (int) (bluevalue * 0.8); // tint the highlights a light yellow by reducing the blue bluevalue = (int) (bluevalue * 0.9); 22 // set the colors pixelobj.setred(redvalue); pixelobj.setgreen(greenvalue); pixelobj.setblue(bluevalue); Dangling Else Problem Dangling Else Problem 23 24 Consider this example: if (x > 0) if (x < 100) System.out.println( one ); System.out.println( two ); Suppose x has the value 2. What do you think will be printed? The rule is that an goes with the closest if indentation makes no difference! Now what if we had wanted to group the with the first if? Use braces: if (x > 0) if (x < 100) System.out.println( one ); System.out.println( two );

Blue Screen Blue Screen 25 26 The weather announcers on TV are not really standing in front of a changing weather map They are in front of a solid colour background blue or green (but usually blue it s better for skintones) How do the weather map images appear behind them? If a pixel s color is blue, replace it with color of corresponding pixel from new background image Also used for special effects in TV and movies Here just a blue sheet was used Professionally, you need an evenly lit, bright, pure blue background With nothing blue in the part of the image you want to keep New Background Algorithm 27 27 28 Our method will be invoked on an image with a blue screen The blue will be replaced as a background by another image passed in as a parameter Algorithm: Loop through all the pixels If the pixel color is blue Replace the pixel color with the color from the pixel at the same location in the new background picture Method Method (continued) 29 public void bluescreen(picture newbackground) // loop through the columns for (int x=0; x<this.getwidth(); x++) // loop through the rows for (int y=0; y<this.getheight(); y++) // get the current pixel Pixel currpixel = this.getpixel(x,y); 30 /* if the color of the pixel at this location is mostly blue (blue value greater than red and green combined), then use new background color at this pixel */ if (currpixel.getred() + currpixel.getgreen() < currpixel.getblue()) Pixel newpixel = newbackground.getpixel(x,y); currpixel.setcolor(newpixel.getcolor());

31 Summary Conditionals Nested Conditionals Dangling Else Picture Algorithms that use conditionals