CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 9: OCT. 4TH INSTRUCTOR: JIAYIN WANG

Similar documents
CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 8: SEP. 29TH INSTRUCTOR: JIAYIN WANG

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 10: OCT. 6TH INSTRUCTOR: JIAYIN WANG

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 14: OCT. 25TH INSTRUCTOR: JIAYIN WANG

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 3: SEP. 13TH INSTRUCTOR: JIAYIN WANG

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

QUIZ Lesson 4. Exercise 4: Write an if statement that assigns the value of x to the variable y if x is in between 1 and 20, otherwise y is unchanged.

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

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 2: SEP. 8TH INSTRUCTOR: JIAYIN WANG

Information Science 1

M e t h o d s a n d P a r a m e t e r s

CS1150 Principles of Computer Science Methods

CS1150 Principles of Computer Science Methods

More on Arrays CS 16: Solving Problems with Computers I Lecture #13

CS11 Java. Fall Lecture 1

Visual C# Instructor s Manual Table of Contents

Computer Science II Lecture 1 Introduction and Background

QUIZ: What value is stored in a after this

Review Chapter 6 in Bravaco. Short Answers 1. This type of method does not return a value. a. null b. void c. empty d. anonymous

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 19: NOV. 15TH INSTRUCTOR: JIAYIN WANG

Lesson 7. Reading and Writing a.k.a. Input and Output

Today s Agenda. Quick Review

Java Bytecode (binary file)

1 Lexical Considerations

4 Programming Fundamentals. Introduction to Programming 1 1

Fundamentals of Programming Session 4

Chapter 1. Section 1.4 Subprograms or functions. CS 50 - Hathairat Rattanasook

Exam Duration: 2hrs and 30min Software Design

Lecture 5: Methods CS2301

The for Loop. Lesson 11

QUIZ How do we implement run-time constants and. compile-time constants inside classes?

Chapter 4 Defining Classes I

Full file at

Methods. Contents Anatomy of a Method How to design a Method Static methods Additional Reading. Anatomy of a Method

COMP-202 Unit 2: Java Basics. CONTENTS: Using Expressions and Variables Types Strings Methods

Computer Programming & Problem Solving ( CPPS ) Turbo C Programming For The PC (Revised Edition ) By Robert Lafore

CS313D: ADVANCED PROGRAMMING LANGUAGE

Lesson 05 Methods. MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

Fundamentals of Programming Session 25

Lexical Considerations

EECS168 Exam 3 Review

LAB 11: METHODS. CPCS The Lab Note Lab 11 Page 1. Statement Purpose:

Pace University. Fundamental Concepts of CS121 1

Introduction to Programming Using Java (98-388)

Key Differences Between Python and Java

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

CS 231 Data Structures and Algorithms, Fall 2016

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

COMP 111 PROGRAMMING I MODULARITY USING FUNCTIONS

CIS 110: Introduction to Computer Programming

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

CSCE 206: Structured Programming in C++

CSE 142. Lecture 1 Course Introduction; Basic Java. Portions Copyright 2008 by Pearson Education

CS 152: Data Structures with Java Hello World with the IntelliJ IDE

CS 170 Java Programming 1. Week 5: Procedures and Functions

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

Lexical Considerations

Creating a Program in JCreator. JCreator is then used to create our program. But the first step is to create a new file.

Building Java Programs Chapter 2

Your first C++ program

Programming in C. main. Level 2. Level 2 Level 2. Level 3 Level 3

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

CS 106 Introduction to Computer Science I

COMP Summer 2015 (A01) Jim (James) Young jimyoung.ca

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

Programming Languages and Techniques (CIS120)

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

Information Science 1

Lecture 3: C Programm

Data Structure and Programming Languages

CS110D: PROGRAMMING LANGUAGE I


IT 374 C# and Applications/ IT695 C# Data Structures

Final Exam. Programming Assignment 3. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings

Programming Logic and Design Seventh Edition Chapter 2 Elements of High-Quality Programs

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

QUIZ. What is wrong with this code that uses default arguments?

Cmpt 135 Assignment 2: Solutions and Marking Rubric Feb 22 nd 2016 Due: Mar 4th 11:59pm

COMP-202: Foundations of Programming. Lecture 2: Variables, and Data Types Sandeep Manjanna, Summer 2015

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

Building Java Programs Chapter 2. bug. Primitive Data and Definite Loops. Copyright (c) Pearson All rights reserved. Software Flaw.

Building Java Programs Chapter 2

2 nd Week Lecture Notes

6.096 Introduction to C++

IPCoreL. Phillip Duane Douglas, Jr. 11/3/2010

QUIZ on Ch.5. Why is it sometimes not a good idea to place the private part of the interface in a header file?

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

Technical Questions. Q 1) What are the key features in C programming language?

Lecture 18 Tao Wang 1

Object Oriented Design

Chapters 1 & 2 Programming and Programs

Chapter 6 SINGLE-DIMENSIONAL ARRAYS

CT 229 Fundamentals of Java Syntax

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

1. Describe History of C++? 2. What is Dev. C++? 3. Why Use Dev. C++ instead of C++ DOS IDE?

Introduction to Programming

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

Chapter 2, Part I Introduction to C Programming

Computer Programming: C++

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

Transcription:

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 9: OCT. 4TH INSTRUCTOR: JIAYIN WANG 1

Notice Assignments Reading Assignment: Chapter 3: Introduction to Parameters and Objects The Class 9 Exercise for today is posted here. The homework 3 and posted it here They are due this coming Sunday, at 11:59 PM. Mid- term Exam Date: the class on October 27th It covers the knowledge from Class 1 to Class 14 (October 20) To prepare the mid- term exam: class notes & quizzes & homework & exercises The exam will not be like the quizzes. You will be expected to read and write short segments of code A Mid- term review will be in the class on October 25 th hold by TA 2

Review Scope The scope of a an identifier is the part of the program where it can be used Scope applies to Variables Constant Methods The scope of a static method is the entire class in which it appears This means that any method can be called by any other method in the class 3

Scope A code block is section of code that is surrounded... by a matched pair of curly braces, { } We can use the idea of a code block to describe the scope of a variable The scope of a variable starts with the line where it is declared... and continues to the end of the code block in which it is declared We can use the same variable name inside two different methods... because each method has a different scope A variable declared inside a method is called local variables 4

The Scope of for Loop Variable The general scope rule for variables does not work for loop variables The scope of a loop variable is The part of the code between the for loop curly braces And the part of the code between the parentheses that starts the loop 5

Pseudocode When you write more complex algorithms, it s hard to write the entire algorithm immediately One way to deal with this is to use pseudocode When we write pseudocode, we write steps to solve a problem in simple English Then we break down these steps into simpler steps We keep doing this until we have an English description of the steps that can then be translated into Java code 6

Reusing Existing Code The Java code of DrawDownwardTriangle is like this When we run this code we get 7

Reusing Existing Code Sometimes the easiest way to write a program... is to modify some existing code Let's say you want to write a program to draw the following We might be able to use DrawDownwardTriangle.java instead If you look carefully, you will see that the 1st line of DrawDownwardTriangle... is the same as the 5th line of the figure we want... And so on 8

Reusing Existing Code So all we have to do to get the figure we want is change the direction of counting in the outer for loop of DrawDownwardTriangle In DrawDownwardTriangle.java the outer loop counted from 1 to 5 For this code, we want the outer loop to count from 5 to 1 Here is how we do it 9

Class Constants For the code in DrawCone.java for ( int i = 1; i <= (11 - (2 * line_number)); i++) Someone reading the code for the first time might ask "Why 11?" Programmers call values like this magic numbers They make the program work but it is not clear how Magic numbers make for bad code Whenever we can, we should replace such numbers with an expression... and those expressions should use constants A constant is like a variable except its value cannot be changed 10

Class Constants When creating a constant, you must use the keyword final... using the following format final DATA_TYPE CONSTANT_NAME = VALUE; like this final inthours_in_a_day = 24; By convention, constant names are spelled using ALL_CAPITALS The best place to put a constant is inside the class but outside all the methods. Such constants are called class constants. That way they are available to all methods in the class. The format is like this: public static final DATA_TYPE CONSTANT_NAME = VALUE; 11

New Materials Outline Parameters The Mechanics of Parameters Limitation of Parameters Multiple Parameters Overloading Methods 12

Parameters Let's look at the DrawCone2 class again When writing a program, we break a big problem down into smaller tasks If we turn each of the inter for loop into methods, the program would be cleaner 13

Parameters Java allows us to do that using parameters We use parameters to pass a value to a method which the method can use to do its work A parameter is a special local variable of a method that receives its value from the method call When you write the method header of a method that has parameters... you must specify the number and type of parameters the method uses The parameter declaration appears in the method header... after the name of the method and inside the parentheses 14

Parameters We need to rewrite the method header for writespaces as follows public static void writespaces (intnumber) Then we need to modify the method so it uses this parameter When we call this new method, we must put a value inside the parentheses of a method call The value can be any valid expression That means an argument can be a Literal Variable Constant Calculation using operators 15

Parameters Let's rewrite the code DrawCone3: Argument is the value given to the formal parameters inside the parentheses of the method call A formal parameter is the local variable declared in the method header The scope of the parameters is the method in which it is defined We can use the same variable name in both methods A parameter gets its value from the argument in the method call 16

New Materials Outline Parameters The Mechanics of Parameters Limitation of Parameters Multiple Parameters Overloading Methods 17

The Mechanics of Parameters initial Call writespaces After writespaces finishes The parameter only lives while the method runs 18

The Mechanics of Parameters When Java executes a method call, it Evaluates the expressions inside the parentheses of the method call Creates a new variable of the type specified in the method header Sets the value of this variable to the value given by the method call 19

Common Programming Error: Confusing Arguments and Formal Parameters Students sometimes make a mistake like this writespaces(int spaces1); // this doesn't work the argument of a method call should be like this: writespaces(spaces1); And a parameter is declaration inside a method header public static void writespaces(int number) The data type must be specified when the method is declared... not when a method is called 20

New Materials Outline Parameters The Mechanics of Parameters Limitation of Parameters Multiple Parameters Overloading Methods 21

Limitations of Parameters Parameters are used to get values into a method But they cannot be used to get values out of the method When you call a method, the machine sets aside memory... for each of the parameters declared in the method header... and initializes them with the values received from the method call The parameters are variables that are local to the method itself They are in a different scope from the method call Parameters only exist when the method is running... and can't be used to get information... back to the section of code that called them 22

Limitations of Parameters Consider the following example 23

Limitations of Parameters The program begins by declaring and initializing the integer variable x Then it calls doublenumber... using the value of x to initialize the parameter number... which is local to the method doublenumber The machine then executes the statements in the method It prints the original value of number, 17 Then it doubles the value of number Finally, it prints the new value of number, 34 24

Limitations of Parameters Here is the situation before the method finishes And after the method call is finished, we have The next statement in the main method prints the value of x, 17 Then a new variable, number, is declared in the main method And initialized to 42 Now the code calls doublenumber again... using the version of number in the main method as the argument 25

Limitations of Parameters Before the first statement of the method is executed we have This is an unusual situation We have two different variables named number But Java doesn't care They have different scopes and each is invisible to the other After doublenumberprints the original value, 42 It doubles value in doublenumber to 84 But this does not affect the value of number in the main method 26

Limitations of Parameters After the method call ends, we once again have So the change in the doublenumber version of number... did not affect the value of number in the main method Here is the output of the program 27

New Materials Outline Parameters The Mechanics of Parameters Limitation of Parameters Multiple Parameters Overloading Methods 28

Multiple Parameters Java allows you to define any number of parameters in a method header Here is the format for a method header public static void METHOD_NAME ( DATA_TYPE PARAMETER_NAME,... ) { STATEMENT;... } 29

Multiple Parameters Let's look at the DrawCone3 example once more In it we define two methods The only difference between them is them is the character they print We can replace these two methods with single method... if we supply the character as a second parameter 30

Multiple Parameters The new method will look like this This new parameter is of a different type, char... so we have to specify that in the message header Using this new method, we can rewrite our code as follows 31

New Materials Outline Parameters The Mechanics of Parameters Limitation of Parameters Multiple Parameters Overloading Methods 32

Overloading Methods Java allows us to have more than one method with the same name.. as long as the number and types of the parameters are different The combination of the method name and the data types of all parameters is called the method signature Allowing Java to have methods with the same name... but different signatures is called method overloading For example: 33

Class 9 Quiz 1. What is the scope of a parameter? the method in which it is defined 2. Where does a parameter get its value? from the argument in the method call 3. Where in a method header do you declare a parameter? inside the parentheses after the method name 4. Does a parameter keep its value after the method has finished? no. the parameter only lives while the method runs 5. Can a parameter be used to give a value back to the statement that calls the method? no 34

Class 9 Quiz 6. Write the method header for a method named printaverage that takes a single integer parameter called num public static void printaverage(intnum) 7. Write a statement that calls printaverage with the argument 10. printaverage(10); 8. Write the method header for a method named printmultiply that takes two integer parameters, a and b.? public static void printmultiply(int a, int b); 9. Write a statement that calls printmultiply with the arguments 4 and 5. printmultiply(4, 5); 10. What is a method signature? the name of the method together with the name and type of each of the parameters 35

Weekly Quiz1 1. What must the name of the source code file for the Java class CountToTenbe? CountToTen.java 2. If you want to run a Java program from the command line, what must the source code file contain inside the class definition? a main method 3. What is the first line of a method called? method header 4. What is a method? a group of statements that performs a specific task 36

Weekly Quiz1 5. Write the Java statement that prints "Hello world!"? System.out.println("Hello world!"); 6. In the previous statement, what does Java call "Hello world!"? a string literal 7. Write the escape sequence for the Tab character? \t 8. Can you use a Java keyword as an identifier? no 37

Weekly Quiz1 9. Is 1st_try a valid Java identifier? no 10. Write a one line comment that contains the text "This is a one line comment" // This is a one line comment 38