EECS1710. Announcements. Labtests have been returned Term Test #01 marking is in progress. Next Labtest: Thu Oct 23/Fri Oct 24

Similar documents
EECS1710. Announcements. epost updated, please check online Next Labtest: Thu Oct 23/Fri Oct 24. Next Term Test: Tue Oct 28.

EECS1710. Checklist from last lecture (Sept 9, 2014) " get an EECS account (if you don t have it already) " read sections

What we are reinforcing with the exercises this class

Checklist (for Today) What we are reinforcing with the exercises this class. recognizing the various different arithmetic operators (in situ)

EECS168 Exam 3 Review

A Foundation for Programming

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

Introduction to Programming Using Java (98-388)

CSE1720. General Info Continuation of Chapter 9 Read Chapter 10 for next week. Second level Third level Fourth level Fifth level

CS 251 Intermediate Programming Methods and Classes

ECE 122. Engineering Problem Solving with Java

CSE Lecture 24 Review and Recap. High-Level Overview of the Course!! L1-7: I. Programming Basics!

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 2. COMP1406/1006 (the Java course) Fall M. Jason Hinek Carleton University

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

Announcement. Agenda 7/31/2008. Polymorphism, Dynamic Binding and Interface. The class will continue on Tuesday, 12 th August

CS 251 Intermediate Programming Methods and More

All classes in a package can be imported by using only one import statement. If the postcondition of a method is not met, blame its implementer

Lecture #6-7 Methods

Anatomy of a Class Encapsulation Anatomy of a Method

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

Fall 2017 CISC124 9/16/2017

Introduction Basic elements of Java

CS 335 Java Programming Controls. Fall 2007

CSC207 Week 2. Larry Zhang

Zheng-Liang Lu Java Programming 45 / 79

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

Lab5. Wooseok Kim

COP 3330 Final Exam Review

Full file at

Lesson:9 Working with Array and String

CS1004: Intro to CS in Java, Spring 2005

Encapsulation. You can take one of two views of an object: internal - the structure of its data, the algorithms used by its methods

Pace University. Fundamental Concepts of CS121 1

Midterms Save the Dates!

Objectives for this class meeting. 1. Conduct review of core concepts concerning contracts and pre/post conditions

CHAPTER 7 OBJECTS AND CLASSES

Chapter 13 Abstract Classes and Interfaces

CHAPTER 7 OBJECTS AND CLASSES

Computational Expression

last time in cs recitations. computer commands. today s topics.

An Introduction To Writing Your Own Classes CSC 123 Fall 2018 Howard Rosenthal

Using APIs. Chapter 3. Outline Fields Overall Layout. Java By Abstraction Chapter 3. Field Summary static double PI

Lecture 7: Classes and Objects CS2301

Lecture 1: Overview of Java

Introduction to Visual Basic and Visual C++ Introduction to Java. JDK Editions. Overview. Lesson 13. Overview

Lecture 5: Methods CS2301

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

CS 112 Programming 2. Lecture 10. Abstract Classes & Interfaces (1) Chapter 13 Abstract Classes and Interfaces

Programming Language Concepts: Lecture 2

CSC207 Week 3. Larry Zhang

Introduction to Java

COE318 Lecture Notes Week 3 (Week of Sept 17, 2012)

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

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

Tue Nov 11 & Thu Nov 13 : complete Ch 5 Thu Nov 13/Fri Nov 14 : lab exercises Ch 5. Tue Nov 18 : TERM TEST #3 (Ch.4,5)

COE318 Lecture Notes Week 8 (Oct 24, 2011)

CS 335 Lecture 02 Java Programming

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

CS171:Introduction to Computer Science II

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

CS-202 Introduction to Object Oriented Programming

Introduction to Programming (Java) 4/12

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

H212 Introduction to Software Systems Honors

CSCI 355 LAB #2 Spring 2004

12/22/11. Java How to Program, 9/e. public must be stored in a file that has the same name as the class and ends with the.java file-name extension.

CS110: PROGRAMMING LANGUAGE I

Announcements/Follow-ups

Table of Contents Date(s) Title/Topic Page #s. Chapter 4: Writing Classes 4.1 Objects Revisited

Aggregation. Introduction to Computer Science I. Overview (1): Overview (2): CSE 1020 Summer Bill Kapralos. Bill Kapralos.

The next three class meetings will focus on Chapter 6 concepts. There will be a labtest on Chapter 5 concepts on Thurs Nov 20/Fri Nov 21.

Chapter 4 Defining Classes I

CS 251 Intermediate Programming Java Basics

CS321 Languages and Compiler Design I. Winter 2012 Lecture 2

Chapter 2: Data and Expressions

Python Boot Camp. Day 3

1 Shyam sir JAVA Notes

Lab5. Wooseok Kim

Inheritance and Interfaces

Introduction Course Information Object Oriented Concepts Operators & Control Statements Assignment 3. CS Java. Introduction to Java

CS 110 Practice Final Exam originally from Winter, Instructions: closed books, closed notes, open minds, 3 hour time limit.


F1 A Java program. Ch 1 in PPIJ. Introduction to the course. The computer and its workings The algorithm concept

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

Inf1-OOP. Textbooks. Who and What. Organizational Issues. Why Java? Course Overview. Hello, World! in Java. Ewan Klein, Perdita Stevens

Object interactions Lecture 6

Introduction to Classes and Objects. David Greenstein Monta Vista High School

CSC207H: Software Design. Java + OOP. CSC207 Winter 2018

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

The Sun s Java Certification and its Possible Role in the Joint Teaching Material

Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types.

CS 11 java track: lecture 1

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

CSE P 501 Compilers. Java Implementation JVMs, JITs &c Hal Perkins Winter /11/ Hal Perkins & UW CSE V-1

Programming for Mobile Computing

The next three class meetings will focus on Chapter 6 concepts. There will be a labtest on Chapter 5 concepts on Thurs Nov 28/Fri Nov 29.

11/19/2014. Objects. Chapter 4: Writing Classes. Classes. Writing Classes. Java Software Solutions for AP* Computer Science A 2nd Edition

Two s Complement Review. Two s Complement Review. Agenda. Agenda 6/21/2011

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

Transcription:

EECS1710 Click to edit Master Week text 05, styles Lecture 10 Second level Third level Fourth level Fifth level Fall 2014! Thursday, Oct 09, 2014 1 Announcements Labtests have been returned Term Test #01 marking is in progress Next Labtest: Thu Oct 23/Fri Oct 24 Next Term Test: Tue Oct 28 2 1

Checklist from prev lecture (Oct 02, 2014) " Ch 3 " read section 3.1 Anatomy of an API " review Ch 3 KC s 1-6 " do Ch 3 RQ s 1-13 " do Ch 3 Ex s 3.1-3.11 3 Plan for today 3.1 General properties of APIs 3.1 Passing by value 3.1 Overloading 3.1 Binding with the Most Specific 4 2

Walkthrough: Looking at an API 5 Basic Info about Java APIs The tool called javadoc generates the html for the APIs it scrapes the comments from the source code The tool javadoc has evolved over successive versions html format has changed (from what is described in the textbook) The cascading style sheets (CSS) has changed as well The basic format is as follows: Class Section Summary Details 6 We will review each of these in turn 3

Class Section f"lly qualified name of class allows client to impor6 this class what does this class extend and/or implement advanced topic, we will ret"r; to this later Description of what ser=ices this class provides, in a general way 7 The Summary Section field(s) a listing of the class aabibutes that are public and thus can be accessed by clients of this class one field is static the other field is non-static constb"ctor(s) a listing of constb"ctors for instantiating this class notice the default constb"ctor is not listed the constb"ctor for this class requires one parameter utilitd classes do not have any constb"ctor defined method(s) a listing of the methods that are public and thus can be accessed by clients of this class one method is static the other methods are non-static one method is overloaded 8 4

The Detail Section the summard section includes only the first sentence of the documentation for each class member and each constb"ctor the detail section will have f"r6her inforeation, including the contract for each method 9 API & UML Class Diagram: Two different sides of the same coin chapter03::teddybear defaultfurcolor : Color heightincentimeters : int TeddyBear(Color) compareto(object) : int getgenericgreeting() : String wavehand() : void wavehand(boolean) : void static members are shown in underline 10 5

What does "pass by value" mean? In source code, methods get invoked with parameters At run-time, when the bytecode is invoked, each of these parameters get evaluated The evaluation produces a value The value gets sent to the method Within the body of the method, the value then gets utilized 11 Example I when this parameter gets evaluated, what is its value? 12 6

Example I 13 Parameter being evaluated: value Result of evaluation: a set of 8 bytes (64 bits) that represent the numerical value -345.78 in the IEEE 754 representation scheme Those 64 bits had been previously stored at some location in memory (suppose this location is address 1058) What is sent to the body of the method abs(double)? the 64 bits that represent the value -345.78 What is NOT sent to the body of the method abs(double): the memory location 1058 of variable value Example I Could the body of the method abs(double) ever cause the value of value to be changed from -345.78 to some other value? No, definitely not. The variable value is not in scope. The body of the method abs(double) was given 32 bits. The body of the method could take those 32 bits and modify them (change the value), but wouldn't have any bearing on the variable value 14 If the body of the method had the information that the memory location for variable value is 1058, then it could potentially alter the value of variable value 7

Memory Diagram Step 1: Derive the symbol table Identifier Type Block Address args String array 0 value double 4 the value for this variable will get 8 by6es, star6ing at location 4 (assuming a star6ing memord location of 0) 15 Memory Diagram Step 2: Derive the memory representation, assuming zero-offset 16 8

Step 3: Build the memory model, assume block gets loaded at some non-zero memory address 17 + = Memory Diagram When the byte is run, the assignment statement will get invoked. The IEEE 754 representation of -345.78f is as follows: 11000011 10101100 11100011 11010111 This value is stored in runtime memory, in the memory locations that have been allocated for the variable value 18 9

The memory diagram gets updated like this 19 But you will not be expected to derive the binary representation according to IEEE 754 or other numeric representations for this course So memory diagrams can get updated like this 20 10

Recap: Passing primitive values as parameters The value of a primitive value is a representation according to some scheme IEEE 754 (for float and double) two's complement (for int, long, short, byte) unsigned format (char) proprietary scheme (boolean) 21 Recap: What is the value of a non-primitive variable? 22 11

BUT Non-primitive values are a different situation The value of a non-primitive variable is a memory location What is "Passing by value" for parameters that are nonprimitive? To demonstrate this, we will use the Date class 23 About the java.util.date class We will use the Date class in the package java.util ignore the other Date class found in the package java.sql The class provides services for representing and working with points in time The representation of time is not simple: relative to which calendar? relative to which time zone? relative to what starting point? 24 12

About the java.util.date class Here's how the Date class in the package java.util works Clients of the class can create instances Each instance represents a particular point in time The instance encapsulates a long value called time the variable time is not a field; it is hidden from the client the variable time holds the number of milliseconds that have elapsed since standard base time (also called unix epoch). standard base time is represented by 0L 0L, when translated to a Gregorian calendar, corresponds to: Jan 01 00:00:00 UTC 1970 25 About the java.util.date class The default constructor Date() creates an instance that represents the time at which it was allocated, measured to the nearest millisecond since standard base time The constructor Date(long) creates an instance and initializes it to represent the specified number of milliseconds since standard base time 26 13

Demonstrating Date Can you predict this outhut? Can you predict this outhut? 27 More about the Date class The method gettime() Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object. 28 14

Demonstrating Date 29 Can you predict these outhuts? More about the Date class The number of milliseconds can be manipulated What is one day in milliseconds? 24 hours/day x 60 minutes/hour x 60 seconds/minute x 1000 msec/second 30 15

Demonstrating Date Can you predict these outhuts? 31 More about the Date class The method before(date) Tests if this date is before the specified date For example, say we have the following: timepoint1 represents Wed Oct 08 16:22:01 EDT 2014 timepoint2 represents Tue Oct 07 16:22:01 EDT 2014 timepoint2 happened before timepoint1, so timepoint2.before(timepoint1) returns true timepoint1.before(timepoint2) returns false 32 16

Demonstrating Date Can you predict these outhuts? 33 Example II 34 when this parameter gets evaluated, what is its value? 17

Example II 35 Parameter being evaluated: d2 Result of evaluation: a set of 4 bytes (32 bits) that represent a location in memory where a Date object is being stored Suppose this location is address 2612 What is sent to the body of the method before(date)? the 32 bits that represent the address 2612 What is NOT sent to the body of the method abs(double): the memory location where the variable d1 is being represented (suppose the memory location for variable d1 is 800). Example II Could the body of the method before(date) ever cause the value of d1 to be changed? The body of the method before(date): was given the reference to the Date object (the 32 bits that represent the location 2612); the body of the method can potentially use that reference to change the Date object. was not given the memory location for variable d1, and so cannot change the value of the variable d1 36 18

Non-primitive variables The value of a non-primitive variable is a location. The values of the attributes of an object that lives at that location are a different thing. Be careful: do not confuse the value of a non-primitive variable with the value of an object's attribute 37 Parameters Passed by Value Key Concept #4 Parameters in Java are passed by value. This means only their values are sent to the invoked method. Other languages provide pass by reference in which the address is sent. Passing by value is safer because methods cannot change variable local to the calling program. 38 19

Recap: Primitive vs Non-Primitive Variables Variable Characteristics of its value primitive a set of using 0 s and 1 s that corresponds to a numerical or boolean value the numerical or boolean value is determined according to the relevant representation scheme (e.g., int, long, double, etc) nonprimitive a set of using 0 s and 1 s that corresponds to a memory location (address) the only type of address that is valid is the starting byte of an object in runtime memory that has the same (compatible) type as the declaration of the variable e.g., the statement Rectangle r; means that the variable r holds an address and that address must be the starting byte of a Rectangle object 39 the value of a non- primitive variable is an address What is overloading? when two or more methods in a class share the same name the compiler allows this, provided they have different parameter lists the compiler enforces that every methods must have a unique signature the compiler does not allow methods with the same signature, even if they have different how does the compiler determine which of the overloaded methods should be invoked? it performs "binding with the most specific" it needs to do this in order to create its bytecode 40 20

Binding with the Most Specific What the compiler does, given a method invocation with a list of parameter values first, we need to compile the list of parameter types from the list of parameter values 41 Here are some examples: 78, 34.0f, "Hello", d1 int, float, String, Date 1, 2 int, int 345.67, 9 double, int Binding with the Most Specific What the compiler does, Identify the relevant class (bytecode) Look within the bytecode for a method with the most compatible signature look for a matching method name 1. if the list of parameter types is an exact match to a signature, use that method 2. if the list of parameter types can be promoted into an exact match to a signature, then binding can still happen if there are multiple possible matches with promotion, then choose the option that requires the least promotion 42 21

Example Determine the method binding Blah.doMagic(4.0, 6.7); Blah.doMagic(4.0, 3f); Blah.doMagic(4, 3f); Blah.doMagic(4, 3L); Blah.doMagic(4, 3); 43 Exercise 3.3 Visit the Java API (link can be found on course website) Two of the classes are called Date. (a) How can there be two classes with the same name? (b) If such a name were referenced in a program, how would the compiler know which one to bind with? (c) Is the compareto method in java.util.date overloaded? 44 22

Exercise 3.10 Bind the invocation Math.round(2.5) (a) Is the method round overloaded? (examine API of Math class) Write a short program that proves your binding How could you prove the following: (c1) the invocation does not bind with round(float) [hint: use the compiler s type checking] 45 Exercise 3.11 Consider the fragment (a) Is the method min overloaded? (examine API of Math class) (b) With which method will the compiler bind this invocation of min? How could you prove the following: (c1) the invocation does not bind with min(long, long) (c2) the invocation does not bind with min(double, double) 46 [hint: use the compiler s type checking] 23

Checklist for next lecture (Oct 14, 2014) What you should be doing to prepare for what comes next " read section 3.2 A Development Walk-Through " review Ch 3 KC s 7-14 " do Ch 3 RQ s 13-25 " do Ch 3 Ex s 3.12-3.16 " last week s lab covered Lab Exercise L3.2 A Software Project (pp. 124-126), also listed as Ex 3.18 Have a Happy Thanksgiving Holiday 47 24