Lec 7. for loops and methods

Similar documents
Lec 3. Compilers, Debugging, Hello World, and Variables

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

PROGRAMMING FUNDAMENTALS

CSE 201 JAVA PROGRAMMING I. Copyright 2016 by Smart Coding School

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Lecture 2. COMP1406/1006 (the Java course) Fall M. Jason Hinek Carleton University

1.00 Introduction to Computers and Engineering Problem Solving. Quiz 1 March 7, 2003

Introduction to Programming Using Java (98-388)

CS171:Introduction to Computer Science II

Lecture 14 CSE11 Fall 2013 For loops, Do While, Break, Continue

Computer Programming, I. Laboratory Manual. Final Exam Solution

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

Java Basic Programming Constructs

Interpreted vs Compiled. Java Compile. Classes, Objects, and Methods. Hello World 10/6/2016. Python Interpreted. Java Compiled

Exam 1 Prep. Dr. Demetrios Glinos University of Central Florida. COP3330 Object Oriented Programming

(A) 99 (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

Classes and Objects 3/28/2017. How can multiple methods within a Java class read and write the same variable?

Fundamentals of Programming Data Types & Methods

INDEX. A SIMPLE JAVA PROGRAM Class Declaration The Main Line. The Line Contains Three Keywords The Output Line

A Foundation for Programming

Variables and Java vs C++

Review. Primitive Data Types & Variables. String Mathematical operators: + - * / % Comparison: < > <= >= == int, long float, double boolean char

Computational Expression

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

(A) 99 ** (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

Introduction to Java

Outline. Parts 1 to 3 introduce and sketch out the ideas of OOP. Part 5 deals with these ideas in closer detail.

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

Zheng-Liang Lu Java Programming 45 / 79

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. Chapter 1 Introduction to Computers, Programs, and Java

Array. Prepared By - Rifat Shahriyar

Expressions & Flow Control

Lecture Set 4: More About Methods and More About Operators

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

public class Foo { private int var; public int Method1() { // var accessible anywhere here } public int MethodN() {

Chapter 5 Methods. public class FirstMethod { public static void main(string[] args) { double x= -2.0, y; for (int i = 1; i <= 5; i++ ) { y = f( x );

Pace University. Fundamental Concepts of CS121 1

CHAPTER 7 OBJECTS AND CLASSES

CS171:Introduction to Computer Science II

I pledge by honor that I will not discuss this exam with anyone until my instructor reviews the exam in the class.

1 class Lecture2 { 2 3 "Elementray Programming" / References 8 [1] Ch. 2 in YDL 9 [2] Ch. 2 and 3 in Sharan 10 [3] Ch.

Object-Oriented Programming

CS 177 Recitation. Week 8 Methods

Array. Array Declaration:

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

Primitive Data, Variables, and Expressions; Simple Conditional Execution

McGill University School of Computer Science COMP-202A Introduction to Computing 1

COMP-202: Foundations of Programming. Lecture 8: for Loops, Nested Loops and Arrays Jackie Cheung, Winter 2016

CHAPTER 7 OBJECTS AND CLASSES

Type Conversion. and. Statements

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

SPRING 13 CS 0007 FINAL EXAM V2 (Roberts) Your Name: A pt each. B pt each. C pt each. D or 2 pts each

Java Identifiers, Data Types & Variables

Lecture Set 4: More About Methods and More About Operators

Outline. Object Oriented Programming. Course goals. Staff. Course resources. Assignments. Course organization Introduction Java overview Autumn 2003

( &% class MyClass { }

Introduction to Software Development (ISD) Week 3

Chapter 8 Objects and Classes Dr. Essam Halim Date: Page 1

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

Condi(onals and Loops

CMSC131. Introduction to your Introduction to Java. Why Java?

Functions. x y z. f (x, y, z) Take in input arguments (zero or more) Perform some computation - May have side-effects (such as drawing)

Introduction to Programming (Java) 4/12

Full file at

CONDITIONAL EXECUTION

Introduction. Lecture 1 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz

Object Class. EX: LightSwitch Class. Basic Class Concepts: Parts. CS257 Computer Science II Kevin Sahr, PhD. Lecture 5: Writing Object Classes

COMP-202: Foundations of Programming. Lecture 4: Methods Jackie Cheung, Winter 2016

Introduction to Java

Java Bytecode (binary file)

COMP-202: Foundations of Programming. Lecture 4: Flow Control Loops Sandeep Manjanna, Summer 2015

Day 3. COMP 1006/1406A Summer M. Jason Hinek Carleton University

Day 2. COMP 1006/1406A Summer M. Jason Hinek Carleton University

Following is the general form of a typical decision making structure found in most of the programming languages:

Chapter 1: Introduction to Computers, Programs, and Java

Exercises Software Development I. 05 Conversions and Promotions; Lifetime, Scope, Shadowing. November 5th, 2014

1.00 Lecture 8. Using An Existing Class, cont.

Computer Components. Software{ User Programs. Operating System. Hardware

4. If the following Java statements are executed, what will be displayed?

Chapter 5 Control Statements: Part 2 Section 5.2 Essentials of Counter-Controlled Repetition

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

CS 335 Lecture 02 Java Programming

Object-Oriented Programming. Topic 2: Fundamental Programming Structures in Java

Object Oriented Programming and Design in Java. Session 2 Instructor: Bert Huang

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

Selected Questions from by Nageshwara Rao

Computer Components. Software{ User Programs. Operating System. Hardware

Gaddis: Starting Out with Java: From Control Structures through Objects, 6/e

York University Department of Electrical Engineering and Computer Science. Regular Expressions

COMP-202: Foundations of Programming. Lecture 5: Arrays, Reference Type, and Methods Sandeep Manjanna, Summer 2015

CS Week 2. Jim Williams, PhD

Last Time. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings

COSC 111: Computer Programming I. Dr. Bowen Hui University of Bri>sh Columbia Okanagan

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

Java Tutorial. Saarland University. Ashkan Taslimi. Tutorial 3 September 6, 2011

3.1 Class Declaration

Starting Out with Java: From Control Structures through Data Structures 3e (Gaddis and Muganda) Chapter 2 Java Fundamentals

Transcription:

Lec 7 for loops and methods

Announcements Quiz 1 on Friday Review today. 5:00. CENTR 212

Assume there s a method drawrandomfruit() How would you create this:

While loops final int DIMENSION = 9; int row = 0; int col = 0; while (row < DIMENSION) { while (col < DIMENSION) { drawrandomfruit(); col++; nextline(); col=0; row++;

While loops final int DIMENSION = 9; int row = 0; int col = 0; while (row < DIMENSION) { while (col < DIMENSION) { drawrandomfruit(); col++; nextline(); col=0; Update variable row++; Initialize variables Update variable Condition

for loops final int DIMENSION = 9; for (int row = 0; row < DIMENSION; row++) { for (int col = 0; col < DIMENSION; col++) { drawrandomfruit(); nextline(); for (start; condition; update) { code; Start: int row = 0; Condition: row < DIMENSION; Update: row++

while vs. for loops When to use one vs. other? Choose the best loop for each situation: 1) Loop through all pixels in a picture and set each pixel s red value equal to its green value 2) Loop until a user enters a stop condition, e.g. quit 3) Loop until two variables match each other, e.g. x ==y, where both x and y are changed in the loop body

Summary for Use when you know number of iterations to loop while Use when unsure how many times to loop

Methods

Methods

GTA 5 Menu switch (selection) { case SINGLE_PLAYER: // 3 million lines of code; break; case MULTI_PLAYER: // 5 million lines of code; // much of it duplicated from above; break; case YOU_GET_THE_POINT:

GTA 5 Menu switch (selection) { case SINGLE_PLAYER: singleplayer(); break; case MULTI_PLAYER: multiplayer(); break; case YOU_GET_THE_POINT:

methods Already have experience calling methods println(), next(), drawc() Already have experience defining a method public static void main(string[] args) { private static void draws(int x, int y) {

Methods in math f(x) = x + 3; Function name: f Input: x Output: x+3

Method definition public static void main(string[] args) { public static we ll cover later in the class, for now assume it s required void what does the method return. Can be a primitive type, String, or nothing (void) main the name of the method. main is special (String[] args) the parameters. What gets passed into the method

What gets printed public class Test { public static void newmethod(int i) { System.out.println( Value: + i); return; public static void main(string[] args) { int x = 10; newmethod(5); newmethod(x-1); A) Nothing B) Compiler error C) Value: 5 Value: 9 D) Value: 5 Value: 10 E) None of the above

Calling methods & Scope Variables can only be seen inside the method that defines them Global variables exist When you call method, parameter types must match public class Test { public static int everyone = 10; public static void newmethod(int i, String s, boolean b) { boolean test2 =!b; int alpha = 1; return; public static void main(string[] args) { boolean test1 = true; char c = A ; newmethod(3, adam, test);

Calling methods & Scope Variables can only be seen inside the method that defines them Global variables exist When you call method, parameter types must match public class Test { public static int everyone = 10; public static void newmethod(int i, String s, boolean b) { boolean test2 =!b; int alpha = 1; return; public static void main(string[] args) { boolean test1 = true; char c = A ; newmethod(3, adam, test);

return Can only return nothing or 1thing. And type must match the method definition Can have multiple return statements, but only 1 will be executed public static float calculatearea(float radius) { float result = 3.14 * radius * radius; return result; public static void main(string[] args) { float area = calculatearea(2);

parameters Like a variable definition Memory is allocated and argument s value assigned Parameter can only be used within the scope of the method Even if no parameters needed, must have () Only pass-by-value

Simple example public class Test { public static void newmethod(int i) { i++; return; public static void main(string[] args) { int i = 10; newmethod(i); System.out.println(i); A) 10 B) 11 C) Compiler Error D) Runtime error E) I m lost

Harder example public class Test { public static int confuse = 0; public static void newmethod(int i) { i++; confuse++; return; public static void main(string[] args) { confuse++; newmethod(confuse); System.out.println(confuse); A) 0 B) 1 C) 2 D) 3 E) Compiler error