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

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

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

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

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

CS125 : Introduction to Computer Science. Lecture Notes #6 Compound Statements, Scope, and Advanced Conditionals

Java Programming Fundamentals - Day Instructor: Jason Yoon Website:

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

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

CS 177 Recitation. Week 1 Intro to Java

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

Container Vs. Definition Classes. Container Class

ENCAPSULATION. private, public, scope and visibility rules. packages and package level access.

Scopes Global and Block

CS-201 Introduction to Programming with Java

Programming Language Concepts Scoping. Janyl Jumadinova January 31, 2017

Programming Languages and Techniques (CIS120)

Chapter 4: Writing Classes

Classes Nov 3, Ch

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

CS 231 Data Structures and Algorithms, Fall 2016

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

Object-Oriented Programming (OOP) Basics. CSCI 161 Introduction to Programming I

C Introduction. Comparison w/ Java, Memory Model, and Pointers

Unit 5: More on Classes/Objects Notes

Lecture 3: C Programm

Chapter 5. Names, Bindings, and Scopes

CS 320: Concepts of Programming Languages

CSC 533: Organization of Programming Languages. Spring 2005

Problem 1: Building and testing your own linked indexed list

Introduction To C#.NET

Memory and C++ Pointers

CS113: Lecture 5. Topics: Pointers. Pointers and Activation Records

CS Lecture #14

CS 320: Concepts of Programming Languages

Encapsulation. Administrative Stuff. September 12, Writing Classes. Quick review of last lecture. Classes. Classes and Objects

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

Linked Lists

Introduction to Object-Oriented Programming

Separate Compilation Model

a data type is Types

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

CA31-1K DIS. Pointers. TA: You Lu

CS1150 Principles of Computer Science Methods

The return Statement

COMP 202. More on OO. CONTENTS: static revisited this reference class dependencies method parameters variable scope method overloading

NAMESPACES IN C++ You can refer the Programming with ANSI C++ by Bhushan Trivedi for Understanding Namespaces Better(Chapter 14)

Lecture 5: Methods CS2301

Intermediate Programming, Spring 2017*

Binghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program

Java Bytecode (binary file)

Pace University. Fundamental Concepts of CS121 1

W09 CPSC 219 Midterm Exam Page 1 of 7

Recursion 1. Recursion is the process of defining something in terms of itself.

CS 106 Introduction to Computer Science I

Professor Hugh C. Lauer CS-1004 Introduction to Programming for Non-Majors

G52CPP C++ Programming Lecture 9

Topic 2. Collections

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

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

9 Working with the Java Class Library

Review sheet for Final Exam (List of objectives for this course)

Lecture 5 Tao Wang 1

class objects instances Fields Constructors Methods static

Variables and numeric types

CPSC 221: Algorithms and Data Structures Lecture #1: Stacks and Queues

CS 106 Introduction to Computer Science I

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

CGS 3066: Spring 2015 JavaScript Reference

CS 106 Introduction to Computer Science I

Object Oriented Programming in C#

And Even More and More C++ Fundamentals of Computer Science

Administration. Classes. Objects Part II. Agenda. Review: Object References. Object Aliases. CS 99 Summer 2000 Michael Clarkson Lecture 7

Anatomy of a Class Encapsulation Anatomy of a Method

Loops. Variable Scope Remapping Nested Loops. Donald Judd. CS Loops 1. CS Loops 2

QUIZ Friends class Y;

CS 106 Introduction to Computer Science I

Written by John Bell for CS 342, Spring 2018

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

LAMBDA EXPRESSIONS. Summer 2018

CS 106 Introduction to Computer Science I

Friend Functions. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

TaxiBot New attributes Variables Math! TaxiBot

Ch. 10: Name Control

COMP 202. More on OO. CONTENTS: static revisited this reference class dependencies method parameters variable scope method overloading

CS 320: Concepts of Programming Languages

C++ (classes) Hwansoo Han

Example: Fibonacci Numbers

CS1150 Principles of Computer Science Methods

APCS Semester #1 Final Exam Practice Problems

Software Design and Analysis for Engineers

Chapter 5 Names, Binding, Type Checking and Scopes

Array Basics: Outline

CS 31: Intro to Systems Binary Arithmetic. Martin Gagné Swarthmore College January 24, 2016

CS201 Latest Solved MCQs

CS111: PROGRAMMING LANGUAGE II

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

COSC 2P95. Procedural Abstraction. Week 3. Brock University. Brock University (Week 3) Procedural Abstraction 1 / 26

CS 230 Programming Languages

Every language has its own scoping rules. For example, what is the scope of variable j in this Java program?

Transcription:

CS 112 Introduction to Computing II Wayne Snyder Department Boston University Today: Fields vs local variables and scope Program Structure; the keyword static Classes vs objects Creating and using objects Next time: Creating Java programs with multiple files; public vs private; Object-Oriented Design; Abstract data types; Stacks and Queues. Reading assignments arebe posted on the web site! A Java class can be thought of as a container for methods: OverloadTest.java Method Method Class Method 2 1

The contents of a class can be in any order, as far as execution is concerned: usually main is last, and the other members of the class are organized for readability: put related methods next to each other. A class can also hold variables, which are called fields (go figure!), and can even hold other class definitions (called inner classes). We will focus on fields for now. Let s consider a class MyMath, which will provide some basic math functions: 2

Suppose we add a method which calculates the log 2 (..) of a double: Recall: log A (B) * log B (C) = log A (C) so if A = e and B = 2: log 2 (C) = log(c) / log(2) But it is inefficient to calculate Math.log(2.0) each time, so we add it as a field to the container: 3

Since we can put it anywhere, we put it near its only use the program: One more refinement: logoftwo is actually being used as a constant value, which should never change: to make sure we don t change it, we make it final: 4

One more refinement: logoftwo is actually being used as a constant value, which should never change: to make sure we don t change it, we make it final, so that if we accidentally try to modify it, we will get an error: Summary: a Java class is a container for methods (including main), fields, and final fields (constants). Fields can be initialized just like local variables, but final fields can not be modified after initialization: The entities declared in a class are called its members; for now we have: methods fields final fields (constants) 5

Java Program Structure: Scope of fields and methods Scope of the members of a class: Since order does not matter, the scope of a method or a field is the entire class: Scope of all members Java Program Structure: Scope of fields and methods Scope of the members of a class: Since order does not matter, the scope of a method or a field is the entire class: Scope of add, logoftwo, log2, z, and main. 6

Java Program Structure: Scope of fields and methods Scope of the members of a class: Since order does not matter, the scope of a method or a field is the entire class: Scope of x, y Scope of x Scope of add, logoftwo, log2, z, and main. Java Program Structure: Scope of fields and methods Scope of the members of a class: Since order does not matter, the scope of a method or a field is the entire class: The scope rule for members means you can call a method to initilize a field! 7

Java Program Structure: Class = container for fields and methods Summary: A Java class is an unordered container for its members (methods and fields); The scope of a member declaration is the entire class (more on this later), unlike local variables inside methods, whose scope is from the declaration to the next unmatched right curly brace; Fields can be initialized just like local variables, even using methods in the class, but final fields can not be modified after initialization. MyMath add log2 main logoftwo z Java Program Structure: Static Containers What about the keyword static? There are two different ways classes can be used to compute: The first is as a static container for its members. When an entity is static it: 1. Is created when you first run the program; 2. Has a single instance which exists during the entire run of your program; and 3. Is destroyed only when your program terminates. 8

Java Program Structure: Static Containers There is in fact a static region of your program in memory: this region contains all static members of classes; other regions of memory are dynamic and store values of local variables in method calls and entities created by new: RAM Static Region: holds your code and all static members of classes. Static Memory: Only thing that can change here is values of fields. The Heap: holds entities created by new (e.g., arrays). Free memory Dynamic Memory: for anything else that changes during execution. Run-Time Stack: holds values of local variables during method calls Java Program Structure: Static Containers So we can think of a running program as existing in two different worlds, static and dynamic: Static World Dynamic World 9

Java Program Structure: Static Containers When you run your program, a static instance of your class is created, exists for the entire run, and is destroyed only when your program terminates: Static World Dynamic World MyMath add log2 main logoftwo z Java Program Structure: Static Containers When you create a new entity using new, it exists in the Dynamic world; entities in the Static World are called Classes and entities in the Dynamic world are called Objects. Static World Dynamic World MyMath add log2 main logoftwo z 10

Java Program Structure: Programs spread over multiple files. A Java class can be simply used as a Static container for its members, and used by other programs; this is a way of creating your own libraries (such as Math); just like the Math library, you refer to the methods using the name of the class: Static World Dynamic World ClientOfMyMath main. MyMath.add(2,3) Java Program Structure: Programs spread over multiple files. A Java class can be simply used as a Static container for its members, and used by other programs; this is a way of creating your own libraries (such as Math); just like the Math library, you refer to the methods using the name of the class, as long as both files are in the same folder/directory: CS112Homework MyMath.java ClientOfMyMath.java 11

Java Program Structure: Programs spread over multiple files. Note that you call your own static container library program just like you call other static libraries in Java (String, Character, Math): you use the name of the class plus a dot. Notice also that the library program still contains a main( ) method, and can still be run like a normal program. Usually, the main method of a library is used for testing code. Java Program Structure: Creating and using Objects Creating Objects: Recall that when we declare an array in a method, we are creating a new object that lives in dynamic memory: 12

Java Program Structure: Creating and using Objects But, we can create new classes which contain members just like static classes.. Static World Dynamic World p x: 0.0 Geometry y: 0.0 main. Point p = new Point() Java Program Structure: Creating and using Objects But, we can create new classes which contain members just like static classes.. CS112Homework Geometry.java Point.java 13

Java Program Structure: Creating and using Objects But, we can create new classes which contain members just like static classes.. Static World Dynamic World p Geometry main. Point p = new Point(); p.x = 2.3; p.y = 4.5 x: 2.3 y: 4.5 14