CS171:Introduction to Computer Science II

Similar documents
CS171:Introduction to Computer Science II

Announcements. PS 3 is due Thursday, 10/6. Midterm Exam 1: 10/14 (Fri), 9:00am-10:53am

Opening Problem. Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.

Lecture 5: Methods CS2301

Opening Problem. Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.

CS-201 Introduction to Programming with Java

Chapter 4 Loops. Lecture notes for computer programming 1 Faculty of Engineering and Information Technology Prepared by: Iyad Albayouk

CS-201 Introduction to Programming with Java

Loops. CSE 114, Computer Science 1 Stony Brook University

JAVA Programming Concepts

Chapter 6 Methods. Dr. Hikmat Jaber

Chapter 6 Methods. Liang, Introduction to Java Programming, Tenth Edition, Global Edition. Pearson Education Limited

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

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

CS110: PROGRAMMING LANGUAGE I

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

Chapter 5 Methods. Modifier returnvaluetype methodname(list of parameters) { // method body; }

News and information! Review: Java Programs! Feedback after Lecture 2! Dead-lines for the first two lab assignment have been posted.!

Chapter 5 Methods. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

Methods. CSE 114, Computer Science 1 Stony Brook University

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

Introduction to Programming Using Java (98-388)

CS111: PROGRAMMING LANGUAGE II

CS1150 Principles of Computer Science Methods

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 );

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to define and invoke void and return java methods

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

Lecture 1 Java SE Programming

Computer Science is...

Lec 7. for loops and methods

Java Loop Control. Programming languages provide various control structures that allow for more complicated execution paths.

Chapter 5 Methods. Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

ECE 122. Engineering Problem Solving with Java

CS115 Principles of Computer Science

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

Discover how to get up and running with the Java Development Environment and with the Eclipse IDE to create Java programs.

CompSci 125 Lecture 11

Outline. Overview. Control statements. Classes and methods. history and advantage how to: program, compile and execute 8 data types 3 types of errors

Programming in the Small II: Control

Chapter 4 Loops. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

CS313D: ADVANCED PROGRAMMING LANGUAGE

Admin. CS 112 Introduction to Programming. Recap: Exceptions. Summary: for loop. Recap: CaesarFile using Loop. Summary: Flow Control Statements

CS 112 Introduction to Programming

Operational Semantics. One-Slide Summary. Lecture Outline

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

Java for Non Majors Spring 2018

CT 229 Methods Continued

Java Programming. MSc Induction Tutorials Stefan Stafrace PhD Student Department of Computing

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

The Java language has a wide variety of modifiers, including the following:

A Foundation for Programming

Please answer the following questions. Do not re-code the enclosed codes if you have already completed them.

( &% class MyClass { }

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

BM214E Object Oriented Programming Lecture 4

University of Palestine. Mid Exam Total Grade: 100

C212 Early Evaluation Exam Mon Feb Name: Please provide brief (common sense) justifications with your answers below.

Midterm Examination (MTA)

CS 101 Spring 2007 Midterm 2 Name: ID:

Java Basic Programming Constructs

ITERATION WEEK 4: EXMAPLES IN CLASS

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

204111: Computer and Programming

Selected Questions from by Nageshwara Rao

Functions. Lab 4. Introduction: A function : is a collection of statements that are grouped together to perform an operation.

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)

Lecture 05: Methods. AITI Nigeria Summer 2012 University of Lagos.

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

Birkbeck (University of London) Software and Programming 1 In-class Test Mar 2018

Bjarne Stroustrup. creator of C++

Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, Global Edition. Pearson Education Limited

CS1150 Principles of Computer Science Loops (Part II)

Repetition, Looping. While Loop

CSCI 136 Data Structures & Advanced Programming. Fall 2018 Instructors Bill Lenhart & Bill Jannen

Chapter 5 Methods. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

COMP-202. Recursion. COMP Recursion, 2011 Jörg Kienzle and others

CSCI 136 Data Structures & Advanced Programming. Lecture 3 Fall 2017 Instructors: Bill & Bill

Faculty of Engineering Computer Engineering Department Islamic University of Gaza C++ Programming Language Lab # 6 Functions

Anatomy of a Method. HW3 is due Today. September 15, Midterm 1. Quick review of last lecture. Encapsulation. Encapsulation

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

CSCE 206: Structured Programming in C++

CS313D: ADVANCED PROGRAMMING LANGUAGE. Lecture 3: C# language basics II

CS 231 Data Structures and Algorithms, Fall 2016

1 class Lecture4 { 2 3 "Loops" / References 8 [1] Ch. 5 in YDL 9 / Zheng-Liang Lu Java Programming 125 / 207

CS 211: Methods, Memory, Equality

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

FORM 2 (Please put your name and form # on the scantron!!!!)

Lecture 3. Lecture

Java Methods. Lecture 8 COP 3252 Summer May 23, 2017

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

CS111: PROGRAMMING LANGUAGE II

EECS168 Exam 3 Review

PROGRAMMING FUNDAMENTALS

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

Controls Structure for Repetition

2/3/2018 CS313D: ADVANCED PROGRAMMING LANGUAGE. Lecture 3: C# language basics II. Lecture Contents. C# basics. Methods Arrays. Dr. Amal Khalifa, Spr17

Chapter 5 Methods. Lecture notes for computer programming 1 Faculty of Engineering and Information Technology Prepared by: Iyad Albayouk

CS 1316 Exam 1 Summer 2009

Computer Programming, I. Laboratory Manual. Experiment #7. Methods

Object-Oriented Programming

Transcription:

CS171:Introduction to Computer Science II Department of Mathematics and Computer Science Li Xiong 9/7/2012 1

Announcement Introductory/Eclipse Lab, Friday, Sep 7, 2-3pm (today) Hw1 to be assigned Monday, Sep 10

Pretest Postmortem Java Review Roadmap Types, variables, assignments, expressions Control flow statements Methods Arrays OO and Inheritance 3

Declaration Assignments Conditionals Loops Break and continue Statements

Simple if Statements if (booleanexpression) { statement(s); if (!passedtest ) { System.out.println( I ll write good code! ); Boolean Expression false! passedtest false true true Statement(s) System.out.println( I ll write good code! ); (A) (B)

The if...else Statement if (booleanexpression) { statement(s)-for-the-true-case; else { statement(s)-for-the-false-case; 6

World Without Loops is Painful System.out.println( I will write good code! ); System.out.println( I will write good code! ); System.out.println( I will write good code! ); System.out.println( I will write good code! ); System.out.println( I will write good code! ); System.out.println( I will write good code! ); System.out.println( I will write good code! );

A Better Approach: Loops int count=0; while (count < 100){ System.out.println( I will write good code! ); count++;

whileloop Flow Chart while (loop-continuation-condition){ // loop-body; Statement(s); intcount = 0; while (count < 100) { System.out.println( I ll write good code!"); count++; count = 0; Loop Continuation Condition? false (count < 100)? false true Statement(s) (loop body) true System.out.println("I ll write good code!"); count++; (A) (B)

do-whileloop Statement(s) (loop body) true do { // Loop body; Statement(s); while (loop-continuation-condition); Loop Continuation Condition? false 10

forloops for (initial-action; loop-condition; action-after-each-iteration) { // loop body; Statement(s); for(int count=0; count < 100; count++ ){ System.out.println( I will write good code! ); 11

forloops for (initial-action; loopcontinuation-condition; action-after-each-iteration) { // loop body; Statement(s); for (inti= 0; i< 100; i++) { System.out.println( I ll write good code!"); Initial-Action i = 0 Loop Continuation Condition? false (i < 100)? false true Statement(s) (loop body) true System.out.println( "I ll write good code"); Action-After-Each-Iteration i++ (A) (B) 12

Which loop to use? Use the one that is most intuitive and comfortable for you. A for loop may be used if the number of repetitions is known, as, for example, when you need to print a message 100 times. A while loop may be used if the number of repetitions is not known, as in the case of reading the numbers until the input is 0. A do-while loop can be used to replace a while loop if the loop body has to be executed before testing the continuation condition. 13

Question What is the output of the following code fragment?. int sum = 1; for (int i = 0; i <= 5; sum = sum + i++); System.out.print(sum); 15

Bonus question What is the value of x after the following statements? intx = 0, j = 0; boolean done = false; while(!done) { for (inti= 0; i<5; i++) { j = j + i; if (j > 12) { x = j; done = true; 16

Roadmap Lab session Pretest Postmortem Java Review Types, variables, assignments, expressions Control flow statements Methods Arrays OO and Inheritance 17

Levels of Abstraction: Software Design Old times: computer programs manipulated primitive types such as numbers and characters Methods: Encapsulate routine computations to black boxes Object-oriented programming: Encapsulate data fields and methods to black boxes

Example Computing sum intsum = 0; for (inti = 1; i <= 5; i++ ) { sum += i; System.out.println( sum is: + sum);

Example -Print the sums of 1 10, 25 35, 40 50 public static void main(string[] args) { intsum = 0; for (int i = 1; i <= 10; i++) { sum += i; System.out.println("The sum of 1-10 is: " + sum); sum = 0; for (int i = 25; i <= 30; i++) { sum += i; System.out.println("The sum of 25-30 is: " + sum); sum = 0; for (int i = 40; i <= 50; i++) { sum += i; System.out.println("The sum of 40-50 is: " + sum);

Defining a Method sum public static intsum(intstart, intend) { intsum = 0; for (inti = start; i <= end; i++) { sum += i; return sum; // return is required

Using a method Sum public static void main(string[] args) { System.out.println("sum(1, 10) is: " + sum(1, 10) ); System.out.println("sum(25, 30) is: " + sum(25, 30) ); System.out.println("sum(40, 50) is: " + sum(40, 50) );

Defining and Using Methods Define a method give a definition of what the method is to do modifier returntype methodname(list of parameters) { collection of statements; Call or invoke a method use a method methodname(list of parameters) 23

Passing Parameters When calling a method, the arguments must match the parameters in order, number, and compatible type public static void nprintln(string message, int n) { for (int i = 0; i < n; i++) System.out.println(message); public static void main(string[] args) { nprintln( Hello!, 3); nprintln( So that s how the methods work, 10); When invoking a method, the value of the argument is passed to the parameter. The variable itself is not affected. This is referred to as 24 pass-by-value.

Mechanics of the Method-Calling Process 1. 2. 3. 4. 5. Evaluate the argument expressions Copy argument value into the corresponding parameter, (allocated in a newly assigned region of memory called a stack frame) Execute body, using the new stack frame for local variables. On a return statement, compute the return value andsubstitutesthatvalueinplaceofthecall. Discard the stack frame for the method and returns to the caller, continuing where it left off.

Sum Example: Call Stack public static void main(string[] args) { // 1. evaluate arguments System.out.println("sum(1, 10) is: " + sum(1, 10) ); // 1+2+...+10 System.out.println("sum(25, 30) is: " + sum(25, 30) ); //25+26+...+30 System.out.println("sum(40, 50) is: " + sum(40, 50) ); //40+41+...+50 public static intsum(intstart, intend) { // 2. copy args, new SF intsum = 0; // 3. execute the body for (inti= start; i<= end; i++) { sum += i; return sum; //4 and 5. return the value, and discard stack frame

Overloading methods Method overloading: multiple methods can have the same name but different parameter lists Compiler determines which method is used based on the method signature (method name and parameters) Early binding

Overloading Methods public static int max(int num1, int num2) { if (num1 > num2) return num1; else return num2; public static double max(double num1, double num2) { if (num1 > num2) return num1; else return num2; max(1, 3); max(1.0, 3.0); max(1.0, 3); 28

Overloading Methods public static int max(int num1, int num2) { if (num1 > num2) return num1; else return num2; public static double max(double num1, double num2) { if (num1 > num2) return num1; else return num2; max(1, 3); max(1.0, 3.0); max(1.0, 3); 29

Roadmap Lab session Pretest Postmortem Java Review Types, variables, assignments, expressions Control flow statements Methods Arrays OO and Inheritance