Survey #2. Variable Scope. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings. Scope Static.

Similar documents
Survey #2. Teen Talk Barbie TM Reloaded. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu Partially Filled Arrays ArrayLists

Survey #2. Assignment #3. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings. Static Interface Types.

Survey #2. Programming Assignment 3. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings

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

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

Midterms Save the Dates!

Midterms Save the Dates!

Midterms Save the Dates!

Last Week: Organizing Data. Last Week: Parallel Arrays. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu.

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

Using Classes and Objects. Lecture 7. Midterms Save the Dates! Extra Credit Survey

Midterms Save the Dates!

Midterms Save the Dates!

Midterms Save the Dates!

Midterms Save the Dates!

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

University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner

Midterms Save the Dates!

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #2 Examination 12:30 noon, Thursday, March 15, 2012

! This week: Chapter 6 all ( ) ! Formal parameter: in declaration of class. ! Actual parameter: passed in when method is called

University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner

Survey #3. Final Exam. Today is totally optional! University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings.

CS 121 Intro to Programming:Java - Lecture 2. Professor Robert Moll (+ TAs) CS BLDG

CS 161: Object Oriented Problem Solving

PeerWise Study. Midterms Save the Dates! University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Constants Using Classes and Objects

CS 121 Intro to Programming:Java - Lecture 2. Professor Robert Moll (+ TAs) CS BLDG

University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner

Midterms Save the Dates!

University of Massachusetts Amherst, Electrical and Computer Engineering

University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner

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

BCIS 3630 Dr. GUYNES SPRING 2018 TUESDAY SECTION [JAN version] GRADER COURSE WEBSITE

! Midterm 2: Thu Mar 16, 6:30pm (TODAY!) ! Woodward 2. ! hour-long exam, reserve 6:30-8 time slot. ! no labs/tutorials this week. ! Bunny.

CS 455 Midterm Exam 1 Fall 2016 [Bono] Thursday, Sept. 29, 2016

Agenda CS121/IS223. Reminder. Object Declaration, Creation, Assignment. What is Going On? Variables in Java

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #1 Examination 12:30 noon, Tuesday, February 14, 2012

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

Midterms Save the Dates!

Express Yourself. Writing Your Own Classes

CS121/IS223. Object Reference Variables. Dr Olly Gotel

CS 151. Exceptions & Javadoc. slides available on course website. Sunday, September 9, 12

1. The Normal Distribution, continued

CS 241 Data Organization. August 21, 2018

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

} Evaluate the following expressions: 1. int x = 5 / 2 + 2; 2. int x = / 2; 3. int x = 5 / ; 4. double x = 5 / 2.

Recommended Group Brainstorm (NO computers during this time)

CS 116 Week 8 Page 1

CS 251 Intermediate Programming Coding Standards

Objects and Classes Lecture 2

Advanced Object Oriented Programming EECS2030Z

Project #1 rev 2 Computer Science 2334 Fall 2013 This project is individual work. Each student must complete this assignment independently.

Note: This is a miniassignment and the grading is automated. If you do not submit it correctly, you will receive at most half credit.

CPSC 121: Models of Computation

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

User Defined Classes. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

Note: This is a miniassignment and the grading is automated. If you do not submit it correctly, you will receive at most half credit.

CS 161: Object Oriented Problem Solving

Outline. Intro. Week 1, Fri Jan 4. What is CG used for? What is Computer Graphics? University of British Columbia CPSC 314 Computer Graphics Jan 2013

Intro. Week 1, Fri Jan 4

CS221: Algorithms and Data Structures. Asymptotic Analysis. Alan J. Hu (Borrowing slides from Steve Wolfman)

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

CS 351 Design of Large Programs Coding Standards

Lesson 5C MyClass Methods. By John B. Owen All rights reserved 2011, revised 2014

Computer Science II Lecture 1 Introduction and Background

Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

CS 455 Final Exam Fall 2012 [Bono] Dec. 17, 2012

Project Compiler. CS031 TA Help Session November 28, 2011

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

CS61BL. Lecture 1: Welcome to CS61BL! Intro to Java and OOP Testing Error-handling

CS1004: Intro to CS in Java, Spring 2005

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

Prelim 1. CS 2110, 13 March 2018, 5:30 PM Total Question Name Short answer

CISC 181 Lab 2 (100 pts) Due: March 4 at midnight (This is a two-week lab)

Chapter 5. Repetition. Contents. Introduction. Three Types of Program Control. Two Types of Repetition. Three Syntax Structures for Looping in C++

! labs this week. ! midterms returned. ! work through what you got wrong on midterm. ! can earn back up to 5 out of 70 points

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.

CSE 143: Computer Programming II Summer 2015 HW6: 20 Questions (due Thursday, August 13, :30pm)

CMSC162 Intro to Algorithmic Design II Blaheta. Lab March 2019

CS 161: Object Oriented Problem Solving

INTERFACES IN JAVA. Prof. Chris Jermaine

CSSE 220. Intro to Java Graphics. Check out IntroToJavaGraphics and BiggestFan projects from SVN

CS 231 Data Structures and Algorithms, Fall 2016

Java Basics Lecture: January 26, 2012 (On-line Lecture 1)

TA office hours are over after this week, but Dan and Maja will still be around for the next month

Lab 6 Vectors and functions

CS/ENGRD 2110 Object-Oriented Programming and Data Structures Spring 2012 Thorsten Joachims

CS 106A, Lecture 27 Final Exam Review 1

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

APCS Semester #1 Final Exam Practice Problems

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

Software and Programming 1

CS 161: Object Oriented Problem Solving

University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner

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

COMP-202: Foundations of Programming. Lecture 26: Review; Wrap-Up Jackie Cheung, Winter 2016

MSO Lecture 1. Wouter Swierstra (adapted by HP) September 11, 2017

B. Subject-specific skills B1. Problem solving skills: Supply the student with the ability to solve different problems related to the topics

Project 1 Computer Science 2334 Spring 2016 This project is individual work. Each student must complete this assignment independently.

CSE 143: Computer Programming II Spring 2015 HW7: 20 Questions (due Thursday, May 28, :30pm)

A function is a named piece of code that performs a specific task. Sometimes functions are called methods, procedures, or subroutines (like in LC-3).

Transcription:

University of British Columbia CPSC 111, Intro to Computation Alan J. Hu Scope Static Readings This Week: Ch 8.3-8.8 and into Ch 9.1-9.3 (Ch 9.3-9.8 and Ch 11.1-11.3 in old 2 nd ed) (Reminder: Readings are absolutely vital for learning this stuff!) Lecture 27 Reusing some slides by Margaret Dulat and Joe Luk Labs and Tutorials This Week: Lab #8 A catch-up lab Survey #2 Dr. Ben Yu s second survey for you all is up on WebCT now. Completely optional But, there s a bit of extra credit for doing all three surveys by their respective deadlines! (I think the deadline for this one is March 22, but check on WebCT to be sure.) Learning Goals By the end of class today you will be able to Figure out exactly which declarations (variables, methods) are visible from which parts of a program. Explain what static means in Java programs and why you might want to use static methods and fields. Scope tells you which declarations you can see from which points in the program. The scope of a variable is the places in the program where the variable can be accessed. Or you can think of scope as the places where you can see some variable. (Think scope meaning extent, as in the scope of my legal authority or the scope of the inquest. Not scope as in telescope or scoping out the beach. 1

The world is broken up into packages. import lets you see public stuff from other packages. Or you can give full name, e.g., java.util.scanner Other than that, can t see inside other packages import java.util.random; Random foo = new Random(); Packages contain classes Within a package, everyone can see all classes and non-private stuff within classes TalkingDoll a = new TalkingDoll(); System.out.println(a.getName()); System.out.println(a.name); Packages contain classes Within a package, everyone can see all classes and non-private stuff within classes TalkingDoll a = new TalkingDoll(); System.out.println(a.getName()); System.out.println(a.name); The variable name is private in the TalkingDoll class! Classes contain methods and fields All methods can see other methods and fields in same class public void setlabscore(int labnum, double score) { labscores[labnum-1] = score; computetotallabscore(); Methods contain statements, executed one-by-one You can see declarations that happened already Parameters are like declarations that happen at start of method. Curly braces define a compound statement, which limits scope int a = 0; System.out.println(args[0]); int b = 2*a; int a = 3; // Not OK! b = c+a; // Not OK! -- can t see c int c; Compound statement limits scope You can see out, but not in. int a = 0; int b = 0; { // start of a block int a = 2; // Not OK! a already declared int c = 2; System.out.println(c); // prints 2 System.out.println(a); // prints 0 System.out.println(c); // Not OK! int c = 2; // OK 2

Compound statement limits scope for loop is special: variables declared in header are treated as if declared inside loop body int a = 0; int b = 0; for (int c=0; c < 3; c++) { // start of a block int a; System.out.println(c); // OK System.out.println(c); // Not OK! : Shadowing Local variable declarations wins over instance variable declarations. private static int a = 0; int b = 0; { // start of a block int a = 2; // OK! Shadows other a int c = a; System.out.println(c); // prints 2 System.out.println(a); // prints 0 System.out.println(c); // Not OK! int c = 2; // OK -- Summary The world is broken up into packages. import lets you see public stuff from other packages. Or you can give full name, e.g., java.util.scanner Packages contain classes Within a package, everyone can see all classes and non-private stuff within classes Classes contain methods and fields All methods can see other methods and fields in same class Methods contain statements, executed one-by-one You can see declarations that happened already Parameters are like declarations that happen at start of method. Curly braces define a compound statement, which limits scope Compound statement limits scope You can see out, but not in. for loop is special: variables declared in header are visible only inside the loop Questions The Meaning of static From our very first Java program, we had: public static void main (String[] args) But what does static mean? Review: Static Methods A method that is tagged as static: Can be called on the class, rather than on an object of the class (e.g., Math.sin(1.0)) Does not have an implicit parameter this Cannot directly access non-static methods or fields (because there is no this!) 3

The Meaning of static But what does static mean? In general, static means something that belongs to the class as a whole, and not to a specific object: Static methods have no implicit parameter this Static fields belong to the whole class, shared by all objects. The Meaning of static But what does static mean? In general, static means something that belongs to the class as a whole, and not to a specific object: Static methods have no implicit parameter this Static fields belong to the whole class, shared by all objects. Huh? Java Classes When you write a class in Java, you describe what goes into each object of the class: public class UBCStudent { String name; int studentid; public String getname() { That says that each object of type UBCStudent contains a field name, and a field studentid, and the object knows how to do the getname method (applied to itself). Analogy/Intuition on static A class is a pattern/blueprint/factory that shows, by example, how to create specific types of objects: Each object has these instance fields Each object has these methods, that t do the following statements Sometimes, you want to talk about the pattern/blueprint itself, not the objects. Those are when you want to use static Real Life Analogy of static Fields A factory/blueprint for a car is like the class. The cars are the objects. The blueprint shows an engine, battery, etc. Each car gets its own engine, battery, etc. These are instance fields. But to give each car a serial number, you need a field that lives in the factory. That should be a static field. UBCStudent with Automatically Assigned Student ID Numbers Each object should get a unique student ID number assigned to it. We ll keep a variable nextid that keeps track of the next available ID number. nextid is one variable for the whole class, so it should be static! 4

Recap: Static Fields A field that is tagged as static: Is a single field of the class, rather than a separate field of each object of the class Is shared by all objects of the class Can be accessed without creating an object of the class Can be accessed from static methods Why Use static? Static methods are almost never used, with two common exceptions: Numbers aren t objects in Java, so to have a class of methods that do computation, we ll often put them in static methods (e.g., the Math class). The main method starts running before there are any objects created, so it has to be static. Why Use static? Static fields are not very common, but are needed when the class as a whole has to coordinate some information, e.g.,: Counting how many objects were created Assigning i serial numbers Having all objects share or negotiate some information in common. Recap: Variable Types Static variables declared within class associated with class, not instance, indefinite lifetime Instance variables declared within class associated with instance accessible throughout object, lifetime of object Local variables declared within method accessible within method after declared, lifetime of method (to be more precise, replace method by block ) Parameters declared in parameter list of method accessible throughout method, lifetime of method Questions Midterm Info Out of 100: raw avg=56, low=1, high=97 Will be scaled (info later)! Long exam, but not unreasonable: Everyone should be able to do it (with time). Talk to friends, learning centre, WebCT, TAs, prof. See Lecture 17 notes for tips from Beth Simon Solutions will be posted. Protocol for re-mark requests: read solutions first, carefully no re-mark requests accepted until Thursday re-mark requests must be in writing (paper attached to exam) and submitted to instructor entire exam re-marked 5

Raw Score Stats: Midterm #1 Average: 56, Low: 1, High: 97 (Out of 100) I ve made ArrayList question extra credit (3pts). Scaling Formula: (raw/97) 0.7 * 100 Raw Score Scaled Score 15 27 30 44 45 58 60 71 75 84 90 95 6