Expressions, Statements, and Control Structures

Similar documents
Variables, Types, and Expressions

Object-Oriented Programming

Expressions and Control Statements

Objects and Graphics

Programming Lecture 2. Programming by example (Chapter 2) Hello world Patterns Classes, objects Graphical programming

What is Java? professional software engineering.

Expressions and Control Statements

Programming Lecture 2. Programming by example (Chapter 2) Hello world Patterns Classes, objects Graphical programming

CS 106A, Lecture 11 Graphics

Drawing Geometrical Objects. Graphic courtesy of Eric Roberts

Solutions for Section #2

CS 106A, Lecture 5 Booleans and Control Flow

Solutions for Section #2

Introduction to Java

Programming Lecture 4

Friday Four Square! Today at 4:15PM, Outside Gates

Simple Java YEAH Hours. Brahm Capoor and Vrinda Vasavada

Programming Lecture 4

Introduction to Java

Programming Lecture 4

Control Statements. if for while

Quick Reference. There are several already named colors which you can use, but you must import the appropriate Java library.

Programming Lecture 4

Getting Familiar with ACM_JTF

Introduction to Java

CS 106A, Lecture 23 Interactors and GCanvas

Assignment 2: Welcome to Java!

Variables Chris Piech CS106A, Stanford University. Piech, CS106A, Stanford University

CS 106A, Lecture 25 Life After CS 106A, Part 1

@Override public void start(stage primarystage) throws Exception { Group root = new Group(); Scene scene = new Scene(root);

YEAH Hours. January , 7-8 PM Jared Wolens

SUBCLASSES IN JAVA - II

Programming via Java Subclasses

Memory Chris Piech CS106A, Stanford University. Piech, CS106A, Stanford University

CS106A Review Session

CS 106A, Lecture 27 Final Exam Review 1

Assignment #2: Simple Java Programs Due: 1:15pm on Friday, April 19th

Nested Loops Chris Piech CS106A, Stanford University. Piech, CS106A, Stanford University

Classes. How can we manage large programs?

CS 106A, Lecture 27 Final Exam Review 1

CSC System Development with Java Introduction to Java Applets Budditha Hettige

Tutorium für Studentinnen im 1. Semester

Assignment 6 YEAH Hours. Ben Barnett and Avery Wang

(0,0) (600, 400) CS109. PictureBox and Timer Controls

Breakout YEAH hours. Brahm Capoor & Jared Wolens

File: GFace.java /* * File: GFace.java * This class implements a face as a GCompound. */

CS 106A, Lecture 14 Events and Instance Variables

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

Simple Arrays. Eric Roberts CS 106A February 15, 2017

In this lecture we will briefly examine a few new controls, introduce the concept of scope, random numbers, and drawing simple graphics.

Practice Midterm Examination

CS 106A, Lecture 7 Parameters and Return

Interactive Graphics. Eric Roberts Handout #23 CS 106A January 25, 2016 Interactive Graphics. Computer Graphics and the Utah Teapot.

Assignment #4 Hangman Due: 10:30am on Friday, Febuary 17th This assignment may be done in pairs (which is optional, not required)

CS112 Lecture: Working with Numbers

NPSL-2D Language Reference Manual Glenn Barney October 18, 2007

Appendix F: Java Graphics

Starting Out with Java: From Control Structures Through Objects Sixth Edition

CS 106A, Lecture 14 Events and Instance Variables

Practice Final Examination #1

Practice Final Examination #1

Revisiting acm.graphics

Data Structure Design II Chris Piech CS106A, Stanford University. Piech, CS106A, Stanford University

Control Structures and Methods

Interactive Graphics

CS 106A, Lecture 24 Interactors and NameSurfer

TOPIC 5 INTRODUCTION TO PICTURES

Here is a list of a few of the components located in the AWT and Swing packages:

Solutions for Section #4

Assignment #2: Simple Java Programs Due: 11AM PST on Monday, April 23 rd

TOPIC 5 INTRODUCTION TO PICTURES

CMSC 113: Computer Science I Review Questions for Exam #1

Geometric Primitives. Chapter 5

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

JAVA OPERATORS GENERAL

CS 101 Computer Programming

Appendix F: Java Graphics

Section Handout #7: Interactors and Classes

Java TM Applets. Rex Jaeschke

SSEA Computer Science: CS106A. Dr. Cynthia Lee Lecturer in Computer Science Stanford

CS 106A Midterm Review. Rishi Bedi, adapted from slides by Kate Rydberg and Nick Troccoli Summer 2017

JavaScript Programs Once upon a time...

YEAH 2: Simple Java! Avery Wang Jared Bitz 7/6/2018

CS 106A, Lecture 16 Arrays

TWO-DIMENSIONAL FIGURES

Assignment #2: Intro to Java Due: 11AM PST on Wednesday, July 12

Practice Midterm 1. Problem Points Score TOTAL 50

CS313D: ADVANCED PROGRAMMING LANGUAGE

AP CS Unit 12: Drawing and Mouse Events

Graphics Applets. By Mr. Dave Clausen

Adapted from slides by Brahm Capoor. Breakout YEAH hours. Michael (Chung Troute)

Assignment #3 Breakout!

Repetition. We might start out with the following program that draws only the left most object.

Programming Lecture 3

Slide 1 CS 170 Java Programming 1

Midterms Save the Dates!

Chapter 3 Syntax, Errors, and Debugging. Fundamentals of Java

Solutions to Midterm Exam

CSE115 Lab 4 Fall 2016

Introduction to Programming with Java

Transcription:

Expressions, Statements, and Control Structures

Announcements Assignment 2 out, due next Wednesday, February 1. Explore the Java concepts we've covered and will be covering. Unleash your creative potential!

YEAH Hours Your Early Assignment Help Hours. Review session going over major points of the assignment. Tonight at 7:00PM in Braun Auditorium. Should be available on SCPD tomorrow.

Highlights from Emails

CS is not lame, Too many essays are lame, Prove I'm not just fuzz. I play Temple Run, And like to watch the sky and, Waste time with haikus.

Sending Messages To call a method on an object stored in a variable, use the syntax For example: object.method(parameters) label.setfont("comic Sans-32"); label.setcolor(color.orange);

Operations on the GObject Class The following operations apply to all GObjects: object.setcolor(color) Sets the color of the object to the specified color constant. object.setlocation(x, y) Changes the location of the object to the point (x, y). object.move(dx, dy) Moves the object on the screen by adding dx and dy to its current coordinates. Standard color names defined in the java.awt package: Color.BLACK Color.DARK_GRAY Color.GRAY Color.LIGHT_GRAY Color.WHITE Color.RED Color.YELLOW Color.GREEN Color.CYAN Color.BLUE Color.MAGENTA Color.ORANGE Color.PINK Graphic courtesy of Eric Roberts

Drawing Geometrical Objects Graphic courtesy of Eric Roberts

Drawing Geometrical Objects Constructors new GRect( x, y, width, height) Creates a rectangle whose upper left corner is at (x, y) of the specified size Graphics Program +x (x, y) +y Graphic courtesy of Eric Roberts (x + width, y + height)

Drawing Geometrical Objects Constructors new GRect( x, y, width, height) Creates a rectangle whose upper left corner is at (x, y) of the specified size new GOval( x, y, width, height) Creates an oval that fits inside the rectangle with the same dimensions. Graphics Program +x (x, y) +y Graphic courtesy of Eric Roberts (x + width, y + height)

Drawing Geometrical Objects Constructors new GRect( x, y, width, height) Creates a rectangle whose upper left corner is at (x, y) of the specified size new GOval( x, y, width, height) Creates an oval that fits inside the rectangle with the same dimensions. new GLine( x 0, y 0, x 1, y 1 ) Creates a line extending from (x 0, y 0 ) to (x 1, y 1 ). Graphics Program (x 0, y 0 ) +x +y Graphic courtesy of Eric Roberts (x 1, y 1 )

Drawing Geometrical Objects Constructors new GRect( x, y, width, height) Creates a rectangle whose upper left corner is at (x, y) of the specified size new GOval( x, y, width, height) Creates an oval that fits inside the rectangle with the same dimensions. new GLine( x 0, y 0, x 1, y 1 ) Creates a line extending from (x 0, y 0 ) to (x 1, y 1 ). Methods shared by the GRect and GOval classes object.setfilled( fill) If fill is true, fills in the interior of the object; if false, shows only the outline. object.setfillcolor( color) Sets the color used to fill the interior, which can be different from the border. Graphic courtesy of Eric Roberts

The Collage Model

The Collage Model

Constants Not all variables actually vary. A constant is a name for a value that never changes. Syntax (defined outside of any method): private static final type name = value; By convention, constants are named in UPPER_CASE_WITH_UNDERSCORES to differentiate them from variables.

Magic Numbers A magic number is a number written in a piece of code whose meaning cannot easily be deduced from context. double weight = 9.8 * (m - 14); Constants make it easier to read code: double weight = GRAVITY * (m - TARE_MASS); Avoid magic numbers in your code by using constants.

Expressions

class Add2Integers extends ConsoleProgram { public void run() { println("this program adds two numbers."); int n1 = readint("enter n1: "); int n2 = readint("enter n2: "); int total = n1 + n2; println("the total is " + total + "."); } } n1 n2 total 17 25 42

class Add2Integers extends ConsoleProgram { public void run() { println("this program adds two numbers."); int n1 = readint("enter n1: "); int n2 = readint("enter n2: "); int total = n1 + n2; println("the total is " + total + "."); } } n1 n2 total 17 25 42

Expressions Variables and other values can be used in expressions. Some familiar mathematical operators: + (addition) (subtraction) * (multiplication) / (division)

Fun with Division

Size of the Graphics Window Methods provided by GraphicsProgram class getwidth() Returns the width of the graphics window. getheight() Returns the height of the graphics window. Note: receiver of these calls is the GraphicsProgram itself, so we don t need to specify a separate object as receiver. Based on slides by Eric Roberts

Centering an Object getwidth() / 2.0; getwidth(); Graphics Program W / 2.0 W x = (getwidth() / 2.0) (W / 2.0); x = (getwidth() - W) / 2.0;

The Remainder Operator The special operator % computes the remainder of one value divided by another. For example: 15 % 3 = 0 14 % 8 = 6 21 % 2 = 1 14 % 17 = 14

Operator Precedence Java's mathematical operators have the following precedence: () (highest) * / % + - (lowest) Operators of equal precedence are evaluated left-to-right.

A Useful Shorthand Commonly, programs contain code like this: x = x + 1; y = y * 137; z = z / 14; w = w 3; The statement variable = variable op value; can be rewritten as variable op= value;

A Useful Shorthand Commonly, programs contain code like this: x = x + 1; y = y * 137; z = z / 14; w = w 3; The statement variable = variable op value; can be rewritten as variable op= value;

A Useful Shorthand Commonly, programs contain code like this: x += 1; y *= 137; z /= 14; w -= 3; The statement variable = variable op value; can be rewritten as variable op= value;

Another Useful Shorthand In the special case of writing variable = variable + 1; we can instead write variable ++; In the special case of writing variable = variable - 1; we can instead write variable --;

Boolean Expressions A boolean expression is a test for a condition (it is either true or false). Value comparisons: == equals (note: not single =)!= not equals (cannot say <>) > greater than < less than >= greater than or equal to <= less than or equal to Based on slides by Mehran Sahami

Logical Operators We can apply logical operators to boolean values to produce new values. Logical NOT:!p!p is true if p is false;!p is false if p is true. Logical AND: p && q p && q is true when both p and q are true. Logical OR: p q p q is true when p is true, q is true, or both p and q are true. Order of precedence given above.

Short-Circuit Evaluation Cute observations: true p is always true. false && p is always false. The logical operators short-circuit: if the answer is known from the left operand, the right side is not computed. Example: The code boolean b = (x == 0) ((y / x) < 20) will never divide by zero. Based on slides by Mehran Sahami

Control Statements Revisited

Control Structures in Karel for if while

Control Structures in Karel for if while

This is called the initialization statement and is performed before the loop starts. This is called the step or increment and is performed at the end of each loop iteration. for (int i = 0; i < 3; i++) { } This is called the loop condition or termination condition. The loop will check whether this statement is true before each execution.

Nyan nyan nyan nyan, nyan nyan nyan nyan nyan, nyan, nyan nyan nyan

for (int i = 0; i < 4; i++) { println(""); } Console Program

for (int i = 0; i < 4; i++) { println(""); } int i 0 Console Program

for (int i = 0; i < 4; i++) { println(""); } int i 0 Console Program

for (int i = 0; i < 4; i++) { println(""); } int i 0 Console Program

for (int i = 0; i < 4; i++) { println(""); } int i 0 Console Program

for (int i = 0; i < 4; i++) { println(""); } int i 0 Console Program

for (int i = 0; i < 4; i++) { println(""); } int i 1 Console Program

for (int i = 0; i < 4; i++) { println(""); } int i 1 Console Program

for (int i = 0; i < 4; i++) { println(""); } int i 1 Console Program

for (int i = 0; i < 4; i++) { println(""); } int i 1 Console Program

for (int i = 0; i < 4; i++) { println(""); } int i 1 Console Program

for (int i = 0; i < 4; i++) { println(""); } int i 2 Console Program

for (int i = 0; i < 4; i++) { println(""); } int i 2 Console Program

for (int i = 0; i < 4; i++) { println(""); } int i 2 Console Program

for (int i = 0; i < 4; i++) { println(""); } int i 2 Console Program

for (int i = 0; i < 4; i++) { println(""); } int i 2 Console Program

for (int i = 0; i < 4; i++) { println(""); } int i 3 Console Program

for (int i = 0; i < 4; i++) { println(""); } int i 3 Console Program

for (int i = 0; i < 4; i++) { println(""); } int i 3 Console Program

for (int i = 0; i < 4; i++) { println(""); } int i 3 Console Program

for (int i = 0; i < 4; i++) { println(""); } int i 3 Console Program

for (int i = 0; i < 4; i++) { println(""); } int i 4 Console Program

for (int i = 0; i < 4; i++) { println(""); } int i 4 Console Program

for (int i = 0; i < 4; i++) { println(""); } int i 4 Console Program