News and information! Review: Java Programs! Feedback after Lecture 2! Dead-lines for the first two lab assignment have been posted.!
|
|
- Bryce Miller
- 6 years ago
- Views:
Transcription
1 True object-oriented programming: Dynamic Objects Reference Variables D0010E Object-Oriented Programming and Design Lecture 3 Static Object-Oriented Programming UML" knows-about Eckel: 30-31, 41-46, , Riley: 5.1, 5.2 Lecture 3 - Håkan Jonsson 1 News and information Dead-lines for the first two lab assignment have been posted , lab , lab 2 About workloads: Lab 1 middle, lab 2 middle, lab 3 easy, lab 4 hard, and lab 5 hard and large. In labs 2, 4 and 5 you will be working in groups. Lecture 3 - Håkan Jonsson 2 Feedback after Lecture 2 Are loops and recursion really equally powerful? Yes But it s non-trivial to prove. <= : Any recursive Java program is transformed into an iterative program by the compiler. The instruction set of computers lack recursive parts, so instead the computer simulates recursion using iteration and a stack. => : Later on in the course, I will show how any of Java s loop can be transformed into (a semantically identical) recursion. So, for any iterative program there is a recursive program that computes the same result. In practice, there are some semantic differences. For example: Infinite recursion might exhaust the stack, and cause termination after a finite number if steps, while the corresponding infinite loop does not terminate the program. But note that neither computes anything sensible in this case. Review: Java Programs A Java program consists of source code. The source code contains at least one class. Classes are written by programmers, have names that start with an upper-case letter, and, contain declarations of attributes like variables and methods. Java is object-oriented, not class-oriented. What does this mean? Lecture 3 - Håkan Jonsson 3 Lecture 3 - Håkan Jonsson 4
2 Classes versus Objects Classes exist (are created) when a programmer writes a Java program. The classes describe objects that will/can be created once the program is executed. In practice, everything that happens in a program takes place in methods of objects. The thread of execution shifts between methods when they call each other and when methods end. The program starts when the operating system calls the method main(). A class defines a type. Classes versus Objects We differ between two stages in time: Compile-time: The time during which a program is being written and then compiled. Run-time: The time during which a program is executed. There exists no objects during compiletime. There are only source code being written. Objects are created and exists during runtime only. Lecture 3 - Håkan Jonsson 5 Lecture 3 - Håkan Jonsson 6 Two kinds of objects Classes as templates (Old style of programming.) Static objects. One per class. Contains everything in the class declared with the modifier static. Is automatically created when the program starts. Exists until the program terminates. Attribute x in the static object described by class C is referred to by C.x (Has no type.) Today s topic Dynamic objects. Arbitrary many. Contain everything in the class not declared static. Are created during runtime using the keyword new. Exist as long as they can be reached via a reference chain all the way from the static main-method. Are referred to via reference variables ( pointers )." refvar.x Each class defines a type. So, a class can be thought of as a template for objects. Defines what objects of the type of the class contain/can do. Objects contain Methods Runnable code that can be invoked. Defines behavior. Variables Hold data, or refers to data. Defines state. (Also, some other things we will discuss later.) as declared in the class. Objects are used to represent information when the computer program executes. Lecture 3 - Håkan Jonsson 7 Lecture 3 - Håkan Jonsson 8
3 Example: The class Signal A dynamic object of type Signal represents a signal that can be turned on or off. Remember: During run-time, a program can create any number of dynamic objects. The static object of the class Signal holds statistics about all created signals. Remember: During run-time, there is exactly one (1) static object per class (e.g. Signal). public class Signal { // declarations for the static object // of the class Signal static int numberofsignals = 0; static int numberon = 0; static float percentageon() { if (numberofsignals ==0) { return 0f; } else { return (float) numberon / (float) numberofsignals; } } // declarations for dynamic objects of // type Signal (on the next slide). Lecture 3 - Håkan Jonsson 9 Lecture 3 - Håkan Jonsson 10 } public Signal() { // this is a (the) constructor numberofsignals++; } boolean on = false; // holds the state, // initially off boolean ison() { // use this method to check return on; // signal status. } void seton() { if (on) { numberon++; } on = true; } void setoff() { if (on) { numberon--; } on = false; } Lecture 3 - Håkan Jonsson 11 Writing Classes In classes, first list all static declarations and then all non-static/dynamic ones. Makes it easier to understand what the static and dynamic objects contain. public class ClassName { /* static declarations */ /* non-static declarations */ } Implementation Attributes (variables, constants) Design UML Methods (etc) Lecture 3 - Håkan Jonsson 12
4 Constructors A constructor looks much like a method but is not. Its sole purpose is to create dynamic objects:" " Example: Signal s = new Signal(); // s now refers to a signal Contains initialization code that is executed when the object is created. In Signal, it just increases the static variable numberofsignals. NB It has no return type nor is it void ; it is not a method. The name has to be identical to the name of the class (and even start with an upper-case letter). Compare: Names of methods should start with a lower-case letter. If no constructor is declared, Java adds a default constructor with no arguments. It is possible to impose restrictions on from which code in a program objects can be created, and we will do so later on. This program creates and makes use of signals, i.e. dynamic objects of type Signal. public class SignalTest { public static void main(string[] args) { Signal s1 = new Signal(); Signal s2 = new Signal(); System.out.println(s1.isOn()); s1.seton(); System.out.println(s1.isOn()); if (s2.ison()) { s1.setoff(); s2.seton(); } System.out.println(s1.isOn()); System.out.println(s2.isOn()); System.out.println("% = " + Signal.percentageOn()); Lecture 3 - Håkan Jonsson 13 Lecture 3 - Håkan Jonsson 14 } } Extra info: Overloading Constructors and methods in a class can be overloaded. X is overloaded if there are more than one declaration of X. If X is overloaded, each declaration of X has a unique list of parameter types. Otherwise, it is not possible to differ between them. The method max kan be overloaded like this:" " public static void max(int a, int b);" public static void max(int x, int y, int z);" public static void max(double u, double v);" System.out.println is an example of an overloaded method. UML The arrow means knows-about, or SignalTest knows about Signal. Sometimes a dashed arrow is also used. Lecture 3 - Håkan Jonsson 15 Lecture 3 - Håkan Jonsson 16
5 References Reference variables are used to interact with dynamic objects. A reference variable can be thought of as a road sign that points at a dynamic object. NB Be careful to distinguish between a road sign and a dynamic object pointed at by a road sign. Legal references: Example Illegal (or null) references: Exists but doesn t point at all t t t t Lecture 3 - Håkan Jonsson 17 Lecture 3 - Håkan Jonsson 18 Example s1 = new Signal(); Example Signal s1; Signal s2; Signal on == false s2 s1 s1 Lecture 3 - Håkan Jonsson 19 Lecture 3 - Håkan Jonsson 20
6 s1.seton(); Example s2 = s1; Example Signal on == true Signal on == true s2 s1 Lecture 3 - Håkan Jonsson 21 s1 Lecture 3 - Håkan Jonsson 22 s2.setoff(); Example s1 = null; Example Signal on == false Signal on == false s2 s2 s1 Lecture 3 - Håkan Jonsson 23 s1 Lecture 3 - Håkan Jonsson 24
7 Dynamic Objects Dynamic objects are the essence in objectoriented programming The static objects are very limited and neither necessary nor really used that much. They are not as flexible as dynamic objects and can unfortunately not be used in the same contexts. They are programmed much like old-fashioned imperative programming (c, for instance). However, we get them for free in Java. To understand how dynamic objects are used we need to go into how computer memory and variables function " 40" 41" 42" 43" 44" 45" 46" 47" 48" 49" 50" 51"... Memory and the Storage of Data An integral part of a computer is its memory, which can be thought of as a (long) sequence of individual cells each of which can store a number. Each cell has a unique address. There are two basic forms of access to a cell. Direct addressing The cell at the address contains the data. Indirect addressing The cell at the address contains the address of the memory cell that contains the actual data. These two basic forms of access gives rise to two very different kinds of variables. Lecture 3 - Håkan Jonsson 25 Lecture 3 - Håkan Jonsson 26 Variables Lifetime of variables In a program, a variable stands for the address of a memory cell. There are two kinds in Java: Primitive variables: The content of the memory cell contains data. Are stored in variable memory. Are used for integers, floating-point numbers, characters, and booleans. Reference variables: The content of the memory cell contains the address of the memory cell that contains the data. The data is stored in object memory. Used to keep track of dynamic objects. The class Signal The lifetime of any kind of variable spans from when it is allocated (created) until it is deallocated (destroyed). The lifetime is the period of time during which it exists. Variables that are static: Declared static so they belong to the static object. Allocated when the program starts and deallocated when the program ends. Variables that are dynamic: Attributes of a class that are non-static variables. Created when the object is created. Deallocated when the dynamic object holding the variable is deallocated. Recall that this happens when there is no longer a reference chain connecting main() and the object. Lecture 3 - Håkan Jonsson 27 Lecture 3 - Håkan Jonsson 28
8 Lifetime of variables Variables that are automatic. Local variables in, and formal parameters to, methods and constructors. Temporary variables in compound statements. for (int i = 0; ) { i is alive here only } { int a; } a exists inside the pair { and }. Automatic variables live (exist) during the execution of the construct where they are created only. Example: Dynamic Objects We show how dynamic and static objects can be created and used in the context of signals and a class Signal. The static and the dynamic objects have different functionality: A dynamic object of type Signal represents a signal that can be turned on or off. The static object of the class Signal holds statistics about all created signals. Lecture 3 - Håkan Jonsson 29 Lecture 3 - Håkan Jonsson 30 public class Signal { // declarations for the static object // of the class Signal static int numberofsignals = 0; static int numberon = 0; static float percentageon() { if (numberofsignals ==0) { return 0f; } else { return (float) numberon / (float) numberofsignals; } } // declarations for dynamic objects of // type Signal. public Signal() { numberofsignals++; } Lecture 3 - Håkan Jonsson 31 } boolean on = false; // holds the state, // initially off boolean ison() { // use this method to check return on; // signal status. } void seton() { if (on) { numberon++; } on = true; } void setoff() { if (on) { numberon--; } on = false; } Lecture 3 - Håkan Jonsson 32
9 Using Signals To illustrate the use of signals, consider the class SignalTest. s1 and s2 are reference variables of type Signal. new Signal() creates a dynamic object of type Signal. The line " Signal s1 = new Signal(); " makes s1 refer to the newly created signal. The line" s1.seton(); calls the method seton in the object referred to by s1. Lecture 3 - Håkan Jonsson 33 Assume that we halt here during a run. public class SignalTest { public static void main(string[] args) { Signal s1 = new Signal(); Signal s2 = new Signal(); System.out.println(s1.isOn()); } } s1.seton(); System.out.println(s1.isOn()); if (s2.ison()) { s1.setoff(); s2.seton(); } System.out.println(s1.isOn()); System.out.println(s2.isOn()); System.out.println("% = " + Signal.percentageOn()); Lecture 3 - Håkan Jonsson 34 A Snap-Shot at the Objects s1 s2 Signal on == false SignalTest Signal on == true Dynamic objects" numberofsignals == 2 numberon == 1 Signal Output from SignalTest false true false true % = 0.5 (Where we halted for a while.) Static objects" Lecture 3 - Håkan Jonsson 35 Lecture 3 - Håkan Jonsson 36
10 Access to objects of a class Everything in the static object is accessible by all dynamic objects, but not the other way around. The static object is referred to using the name of the class. This is known before the program is executed. (The dynamic objects are not known until run-time.) The static object is therefore often programmed to hold data and methods common to all the dynamic objects. Reference variables can only refer to dynamic objects. Access to a dynamic object requires a reference to it. Splitting code into Parts As a programmer, you have ample possibility to partition your code into separate parts in Java. Java supports this not only by allowing multiple classes but also by a built-in package feature that we will talk about on the next lecture. A package defines a separate name space. The classes of the signal example belong to the package l03.ex1. (If the package declaration is omitted, the file is assumed to belong to the nameless package This can cause severe problems so do not do this) Lecture 3 - Håkan Jonsson 37 Lecture 3 - Håkan Jonsson 38 package l03.ex2; public class FamousMathFunctions { static int factorial(int n) { int i = 1, res = 1; while (i <= n) { res = res * i; i = i + 1; } return res; } static int fibonacci(int m) { int prev, prevprev, answer; switch (m) { case 0: answer = 1; break; case 1: answer = 1; break; default: prevprev = 1; prev = 1; answer = prev; for (int i = 1; i < m; i++) { answer = prev + prevprev; prevprev = prev; prev = answer; } } return answer; } Lecture 3 - Håkan Jonsson 39 } public static void main(string[] args) { int num = 6; System.out.println("Fibonacci of " + num + " = " + fibonacci(num)); System.out.println(num + " = " + factorial(num)); } UML Lecture 3 - Håkan Jonsson 40
11 package l03.ex2; public class FamousMathFunctions { static int factorial(int n) { int i = 1, res = 1; while (i <= n) { res = res * i; i = i + 1; } return res; } static int fibonacci(int m) { int prev, prevprev, answer; switch (m) { case 0: answer = 1; break; case 1: answer = 1; break; default: public class Sum2 { static int counter = 0; prevprev = } 1; prev = 1; answer = prev; for (int i = 1; i < m; i++) { answer = prev + prevprev; prevprev = prev; prev = answer; } } return answer; } public static void main(string[] args) { int num = 6; System.out.println("Fibonacci of " + num + " = " + fibonacci(num)); System.out.println(num + " = " + factorial(num)); } } One static object during run-time FamousMathFunctions Three static objects during run-time package l03.ex2; public class Fibonacci { static int fibonacci(int m) { int prev, prevprev, answer; switch (m) { case 0: answer = 1; break; case 1: answer = 1; break; default: prevprev = 1; prev = 1; answer = prev; for (int i = 1; i < m; i++) { answer = prev + prevprev; prevprev = prev; prev = answer; } } return answer; } } Fibonacci package l03.ex2; public class Factorial { static int factorial(int n) { int i = 1, res = 1; while (i <= n) { res = res * i; i = i + 1; } return res; } } Factorial package l03.ex2; public class FamousMathFunctions { public static void main(string[] args) { int num = 6; System.out.println("Fibonacci of " + num + " = " + Fibonacci.fibonacci(num)); System.out.println(num + " = " + Factorial.factorial(num)); } } FamousMathFunctions Lecture 3 - Håkan Jonsson 41 Lecture 3 - Håkan Jonsson 42 package l03.ex2; public class MyFunctions { static int factorial(int n) { int i = 1, res = 1; while (i <= n) { res = res * i; i = i + 1; } return res; } static int fibonacci(int m) { int prev, prevprev, answer; switch (m) { case 0: answer = 1; break; case 1: answer = 1; break; default: prevprev = 1; prev = 1; answer = prev; for (int i = 1; i < m; i++) { answer = prev + prevprev; prevprev = prev; prev = answer; } } return answer; } } package l03.ex2; public class FamousMathFunctions { public static void main(string[] args) { int num = 6; System.out.println("Fibonacci of " + num + " = " + MyFunctions.fibonacci(num)); System.out.println(num + " = " + MyFunctions.factorial(num)); } } FamousMathFunctions MyFunctions Two static objects during run-time Lecture 3 - Håkan Jonsson 43
Lecture 3. Lecture
True Object-Oriented programming: Dynamic Objects Static Object-Oriented Programming Reference Variables Eckel: 30-31, 41-46, 107-111, 114-115 Riley: 5.1, 5.2 D0010E Object-Oriented Programming and Design
More informationLecture 4. Lecture
Interfaces Classes Building blocks Methods Arrays Example: BitArray Packages D0010E Object- Oriented Programming and Design InformaGon hiding Graphics and interacgon Example: A blinking signal Access Modifiers
More informationCS171:Introduction to Computer Science II
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,
More informationFORM 2 (Please put your name and form # on the scantron!!!!)
CS 161 Exam 2: FORM 2 (Please put your name and form # on the scantron!!!!) True (A)/False(B) (2 pts each): 1. Recursive algorithms tend to be less efficient than iterative algorithms. 2. A recursive function
More informationIntroduction to Programming Using Java (98-388)
Introduction to Programming Using Java (98-388) Understand Java fundamentals Describe the use of main in a Java application Signature of main, why it is static; how to consume an instance of your own class;
More informationCOE318 Lecture Notes Week 3 (Week of Sept 17, 2012)
COE318 Lecture Notes: Week 3 1 of 8 COE318 Lecture Notes Week 3 (Week of Sept 17, 2012) Announcements Quiz (5% of total mark) on Wednesday, September 26, 2012. Covers weeks 1 3. This includes both the
More informationCT 229 Methods Continued
CT 229 Methods Continued 13/10/2006 CT229 Lab Assignments Due Date for current lab assignment : Oct 20 th In Part 1 of Assignment you can declare a constant 3.14 to use as PI or you can use Math.PI Formula
More informationLecture 5: Methods CS2301
Lecture 5: Methods NADA ALZAHRANI CS2301 1 Opening Problem Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively. 2 Solution public static int sum(int i1, int i2) { int
More information3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.
3.Constructors and Destructors Develop cpp program to implement constructor and destructor. Constructors A constructor is a special member function whose task is to initialize the objects of its class.
More informationComputer Science II (20082) Week 1: Review and Inheritance
Computer Science II 4003-232-08 (20082) Week 1: Review and Inheritance Richard Zanibbi Rochester Institute of Technology Review of CS-I Syntax and Semantics of Formal (e.g. Programming) Languages Syntax
More informationCS111: PROGRAMMING LANGUAGE II
CS111: PROGRAMMING LANGUAGE II Computer Science Department Lecture 1(c): Java Basics (II) Lecture Contents Java basics (part II) Conditions Loops Methods Conditions & Branching Conditional Statements A
More informationclass objects instances Fields Constructors Methods static
Class Structure Classes A class describes a set of objects The objects are called instances of the class A class describes: Fields (instance variables)that hold the data for each object Constructors that
More informationCOMP-202: Foundations of Programming. Lecture 13: Recursion Sandeep Manjanna, Summer 2015
COMP-202: Foundations of Programming Lecture 13: Recursion Sandeep Manjanna, Summer 2015 Announcements Final exams : 26 th of June (2pm to 5pm) @ MAASS 112 Assignment 4 is posted and Due on 29 th of June
More informationLoops. CSE 114, Computer Science 1 Stony Brook University
Loops CSE 114, Computer Science 1 Stony Brook University http://www.cs.stonybrook.edu/~cse114 1 Motivation Suppose that you need to print a string (e.g., "Welcome to Java!") a user-defined times N: N?
More informationFall 2017 CISC124 9/16/2017
CISC124 Labs start this week in JEFF 155: Meet your TA. Check out the course web site, if you have not already done so. Watch lecture videos if you need to review anything we have already done. Problems
More informationPrinciple of Complier Design Prof. Y. N. Srikant Department of Computer Science and Automation Indian Institute of Science, Bangalore
Principle of Complier Design Prof. Y. N. Srikant Department of Computer Science and Automation Indian Institute of Science, Bangalore Lecture - 20 Intermediate code generation Part-4 Run-time environments
More informationPace University. Fundamental Concepts of CS121 1
Pace University Fundamental Concepts of CS121 1 Dr. Lixin Tao http://csis.pace.edu/~lixin Computer Science Department Pace University October 12, 2005 This document complements my tutorial Introduction
More informationLecture 8 Classes and Objects Part 2. MIT AITI June 15th, 2005
Lecture 8 Classes and Objects Part 2 MIT AITI June 15th, 2005 1 What is an object? A building (Strathmore university) A desk A laptop A car Data packets through the internet 2 What is an object? Objects
More informationCONTENTS: Array Usage Multi-Dimensional Arrays Reference Types. COMP-202 Unit 6: Arrays
CONTENTS: Array Usage Multi-Dimensional Arrays Reference Types COMP-202 Unit 6: Arrays Introduction (1) Suppose you want to write a program that asks the user to enter the numeric final grades of 350 COMP-202
More informationAP Computer Science Chapter 10 Implementing and Using Classes Study Guide
AP Computer Science Chapter 10 Implementing and Using Classes Study Guide 1. A class that uses a given class X is called a client of X. 2. Private features of a class can be directly accessed only within
More informationCS Week 14. Jim Williams, PhD
CS 200 - Week 14 Jim Williams, PhD This Week 1. Final Exam: Conflict Alternatives Emailed 2. Team Lab: Object Oriented Space Game 3. BP2 Milestone 3: Strategy 4. Lecture: More Classes and Additional Topics
More informationLecture 05: Methods. AITI Nigeria Summer 2012 University of Lagos.
Lecture 05: Methods AITI Nigeria Summer 2012 University of Lagos. Agenda What a method is Why we use methods How to declare a method The four parts of a method How to use (invoke) a method The purpose
More informationCOMP-202. Recursion. COMP Recursion, 2011 Jörg Kienzle and others
COMP-202 Recursion Recursion Recursive Definitions Run-time Stacks Recursive Programming Recursion vs. Iteration Indirect Recursion Lecture Outline 2 Recursive Definitions (1) A recursive definition is
More informationCS 231 Data Structures and Algorithms, Fall 2016
CS 231 Data Structures and Algorithms, Fall 2016 Dr. Bruce A. Maxwell Department of Computer Science Colby College Course Description Focuses on the common structures used to store data and the standard
More informationSchool of Computer Science CPS109 Course Notes 5 Alexander Ferworn Updated Fall 15
Table of Contents 1 INTRODUCTION... 1 2 IF... 1 2.1 BOOLEAN EXPRESSIONS... 3 2.2 BLOCKS... 3 2.3 IF-ELSE... 4 2.4 NESTING... 5 3 SWITCH (SOMETIMES KNOWN AS CASE )... 6 3.1 A BIT ABOUT BREAK... 7 4 CONDITIONAL
More informationObject-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018
Object-oriented programming 1 and data-structures CS/ENGRD 2110 SUMMER 2018 Lecture 1: Types and Control Flow http://courses.cs.cornell.edu/cs2110/2018su Lecture 1 Outline 2 Languages Overview Imperative
More informationFunctions. Lab 4. Introduction: A function : is a collection of statements that are grouped together to perform an operation.
Lab 4 Functions Introduction: A function : is a collection of statements that are grouped together to perform an operation. The following is its format: type name ( parameter1, parameter2,...) { statements
More informationCS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find
CS1622 Lecture 15 Semantic Analysis CS 1622 Lecture 15 1 Semantic Analysis How to build symbol tables How to use them to find multiply-declared and undeclared variables. How to perform type checking CS
More informationCS-201 Introduction to Programming with Java
CS-201 Introduction to Programming with Java California State University, Los Angeles Computer Science Department Lecture X: Methods II Passing Arguments Passing Arguments methods can accept outside information
More informationComputer Science II (20073) Week 1: Review and Inheritance
Computer Science II 4003-232-01 (20073) Week 1: Review and Inheritance Richard Zanibbi Rochester Institute of Technology Review of CS-I Hardware and Software Hardware Physical devices in a computer system
More informationAgenda CS121/IS223. Reminder. Object Declaration, Creation, Assignment. What is Going On? Variables in Java
CS121/IS223 Object Reference Variables Dr Olly Gotel ogotel@pace.edu http://csis.pace.edu/~ogotel Having problems? -- Come see me or call me in my office hours -- Use the CSIS programming tutors Agenda
More informationCompiler Theory. (Semantic Analysis and Run-Time Environments)
Compiler Theory (Semantic Analysis and Run-Time Environments) 005 Semantic Actions A compiler must do more than recognise whether a sentence belongs to the language of a grammar it must do something useful
More informationPROGRAMMING FUNDAMENTALS
PROGRAMMING FUNDAMENTALS Q1. Name any two Object Oriented Programming languages? Q2. Why is java called a platform independent language? Q3. Elaborate the java Compilation process. Q4. Why do we write
More informationBM214E Object Oriented Programming Lecture 4
BM214E Object Oriented Programming Lecture 4 Computer Numbers Integers (byte, short, int, long) whole numbers exact relatively limited in magnitude (~10 19 ) Floating Point (float, double) fractional often
More informationChapter 4 Defining Classes I
Chapter 4 Defining Classes I This chapter introduces the idea that students can create their own classes and therefore their own objects. Introduced is the idea of methods and instance variables as the
More informationLecture Notes on Memory Layout
Lecture Notes on Memory Layout 15-122: Principles of Imperative Computation Frank Pfenning André Platzer Lecture 11 1 Introduction In order to understand how programs work, we can consider the functions,
More information1 Shyam sir JAVA Notes
1 Shyam sir JAVA Notes 1. What is the most important feature of Java? Java is a platform independent language. 2. What do you mean by platform independence? Platform independence means that we can write
More informationAdministration. Exceptions. Leftovers. Agenda. When Things Go Wrong. Handling Errors. CS 99 Summer 2000 Michael Clarkson Lecture 11
Administration Exceptions CS 99 Summer 2000 Michael Clarkson Lecture 11 Lab 10 due tomorrow No lab tomorrow Work on final projects Remaining office hours Rick: today 2-3 Michael: Thursday 10-noon, Monday
More informationCS121/IS223. Object Reference Variables. Dr Olly Gotel
CS121/IS223 Object Reference Variables Dr Olly Gotel ogotel@pace.edu http://csis.pace.edu/~ogotel Having problems? -- Come see me or call me in my office hours -- Use the CSIS programming tutors CS121/IS223
More informationWe cover recursion in 150. Why do it again in 151?
Recursion We cover recursion in 150. Why do it again in 151? First, good solutions to problems are often recursive. Here is a quick way to sort a list of objects: split the list in half, recursively sort
More informationFaculty of Engineering Computer Engineering Department Islamic University of Gaza C++ Programming Language Lab # 6 Functions
Faculty of Engineering Computer Engineering Department Islamic University of Gaza 2013 C++ Programming Language Lab # 6 Functions C++ Programming Language Lab # 6 Functions Objective: To be familiar with
More informationObject Oriented Programming in C#
Introduction to Object Oriented Programming in C# Class and Object 1 You will be able to: Objectives 1. Write a simple class definition in C#. 2. Control access to the methods and data in a class. 3. Create
More informationWeek 6 CS 302. Jim Williams, PhD
Week 6 CS 302 Jim Williams, PhD This Week Lab: Multi-dimensional Arrays Exam 1: Thursday Lecture: Methods Review Midterm Exam 1 What is the location of the exam? 3650 Humanities 125 Ag Hall 272 Bascom
More informationNotes - Recursion. A geeky definition of recursion is as follows: Recursion see Recursion.
Notes - Recursion So far we have only learned how to solve problems iteratively using loops. We will now learn how to solve problems recursively by having a method call itself. A geeky definition of recursion
More informationpublic class Foo { private int var; public int Method1() { // var accessible anywhere here } public int MethodN() {
Scoping, Static Variables, Overloading, Packages In this lecture, we will examine in more detail the notion of scope for variables. We ve already indicated that variables only exist within the block they
More informationCOMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette
COMP 250: Java Programming I Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette Variables and types [Downey Ch 2] Variable: temporary storage location in memory.
More informationCSE 142 Su 04 Computer Programming 1 - Java. Objects
Objects Objects have state and behavior. State is maintained in instance variables which live as long as the object does. Behavior is implemented in methods, which can be called by other objects to request
More informationDesign Patterns: State, Bridge, Visitor
Design Patterns: State, Bridge, Visitor State We ve been talking about bad uses of case statements in programs. What is one example? Another way in which case statements are sometimes used is to implement
More informationIntro. Scheme Basics. scm> 5 5. scm>
Intro Let s take some time to talk about LISP. It stands for LISt Processing a way of coding using only lists! It sounds pretty radical, and it is. There are lots of cool things to know about LISP; if
More information1.00 Introduction to Computers and Engineering Problem Solving. Quiz 1 March 7, 2003
1.00 Introduction to Computers and Engineering Problem Solving Quiz 1 March 7, 2003 Name: Email Address: TA: Section: You have 90 minutes to complete this exam. For coding questions, you do not need to
More informationChapter 2. Procedural Programming
Chapter 2 Procedural Programming 2: Preview Basic concepts that are similar in both Java and C++, including: standard data types control structures I/O functions Dynamic memory management, and some basic
More informationAnnouncements. PS 3 is due Thursday, 10/6. Midterm Exam 1: 10/14 (Fri), 9:00am-10:53am
Announcements PS 3 is due Thursday, 10/6 Midterm Exam 1: 10/14 (Fri), 9:00am-10:53am Room TBD Scope: Lecture 1 to Lecture 9 (Chapters 1 to 6 of text) You may bring a sheet of paper (A4, both sides) Tutoring
More informationcs Java: lecture #6
cs3101-003 Java: lecture #6 news: homework #5 due today little quiz today it s the last class! please return any textbooks you borrowed from me today s topics: interfaces recursion data structures threads
More informationCE221 Programming in C++ Part 1 Introduction
CE221 Programming in C++ Part 1 Introduction 06/10/2017 CE221 Part 1 1 Module Schedule There are two lectures (Monday 13.00-13.50 and Tuesday 11.00-11.50) each week in the autumn term, and a 2-hour lab
More informationCS 536 Introduction to Programming Languages and Compilers Charles N. Fischer Lecture 11
CS 536 Introduction to Programming Languages and Compilers Charles N. Fischer Lecture 11 CS 536 Spring 2015 1 Handling Overloaded Declarations Two approaches are popular: 1. Create a single symbol table
More informationObject Oriented Modeling
Object Oriented Modeling Object oriented modeling is a method that models the characteristics of real or abstract objects from application domain using classes and objects. Objects Software objects are
More informationMethods and Functions
Programming with Java Module 4 Methods and Functions Theoretical Part Contents 1 Module overview 3 2 Methods 3 2.1 Methods without return value (procedures).............. 3 2.2 Functions with return value
More informationTail Calls. CMSC 330: Organization of Programming Languages. Tail Recursion. Tail Recursion (cont d) Names and Binding. Tail Recursion (cont d)
CMSC 330: Organization of Programming Languages Tail Calls A tail call is a function call that is the last thing a function does before it returns let add x y = x + y let f z = add z z (* tail call *)
More informationPrelim One Solution. CS211 Fall Name. NetID
Name NetID Prelim One Solution CS211 Fall 2005 Closed book; closed notes; no calculators. Write your name and netid above. Write your name clearly on each page of this exam. For partial credit, you must
More informationChapter 6 Introduction to Defining Classes
Introduction to Defining Classes Fundamentals of Java: AP Computer Science Essentials, 4th Edition 1 Objectives Design and implement a simple class from user requirements. Organize a program in terms of
More informationCS313D: ADVANCED PROGRAMMING LANGUAGE
CS313D: ADVANCED PROGRAMMING LANGUAGE Computer Science Department Lecture 3: C# language basics Lecture Contents 2 C# basics Conditions Loops Methods Arrays Dr. Amal Khalifa, Spr 2015 3 Conditions and
More informationAdvanced Computer Programming
Programming in the Large I: Methods (Subroutines) 188230 Advanced Computer Programming Asst. Prof. Dr. Kanda Runapongsa Saikaew (krunapon@kku.ac.th) Department of Computer Engineering Khon Kaen University
More informationShort Notes of CS201
#includes: Short Notes of CS201 The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with < and > if the file is a system
More informationI2204 ImperativeProgramming Semester: 1 Academic Year: 2018/2019 Credits: 5 Dr Antoun Yaacoub
Lebanese University Faculty of Science Computer Science BS Degree I2204 ImperativeProgramming Semester: 1 Academic Year: 2018/2019 Credits: 5 Dr Antoun Yaacoub I2204- Imperative Programming Schedule 08h00-09h40
More informationLecture Topics. Administrivia
ECE498SL Lec. Notes L8PA Lecture Topics overloading pitfalls of overloading & conversions matching an overloaded call miscellany new & delete variable declarations extensibility: philosophy vs. reality
More informationCSE450. Translation of Programming Languages. Lecture 11: Semantic Analysis: Types & Type Checking
CSE450 Translation of Programming Languages Lecture 11: Semantic Analysis: Types & Type Checking Structure Project 1 - of a Project 2 - Compiler Today! Project 3 - Source Language Lexical Analyzer Syntax
More informationReview Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013
Review Chapters 1 to 4 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 Introduction to Java Chapters 1 and 2 The Java Language Section 1.1 Data & Expressions Sections 2.1 2.5 Instructor:
More informationThis section provides some reminders and some terminology with which you might not be familiar.
Chapter 3: Functions 3.1 Introduction The previous chapter assumed that all of your Bali code would be written inside a sole main function. But, as you have learned from previous programming courses, modularizing
More informationC++ for Java Programmers
Basics all Finished! Everything we have covered so far: Lecture 5 Operators Variables Arrays Null Terminated Strings Structs Functions 1 2 45 mins of pure fun Introduction Today: Pointers Pointers Even
More informationIntroduction to Programming (Java) 4/12
Introduction to Programming (Java) 4/12 Michal Krátký Department of Computer Science Technical University of Ostrava Introduction to Programming (Java) 2008/2009 c 2006 2008 Michal Krátký Introduction
More informationPointers. A pointer value is the address of the first byte of the pointed object in the memory. A pointer does not know how many bytes it points to.
Pointers A pointer is a memory address of an object of a specified type, or it is a variable which keeps such an address. Pointer properties: P (pointer) 12316 12316 (address) Typed object A pointer value
More informationCS201 - Introduction to Programming Glossary By
CS201 - Introduction to Programming Glossary By #include : The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with
More informationCS 270 Algorithms. Oliver Kullmann. Binary search. Lists. Background: Pointers. Trees. Implementing rooted trees. Tutorial
Week 7 General remarks Arrays, lists, pointers and 1 2 3 We conclude elementary data structures by discussing and implementing arrays, lists, and trees. Background information on pointers is provided (for
More informationThe return Statement
The return Statement The return statement is the end point of the method. A callee is a method invoked by a caller. The callee returns to the caller if the callee completes all the statements (w/o a return
More informationChapter 5 Control Statements: Part 2 Section 5.2 Essentials of Counter-Controlled Repetition
Chapter 5 Control Statements: Part 2 Section 5.2 Essentials of Counter-Controlled Repetition 5.2 Q1: Counter-controlled repetition requires a. A control variable and initial value. b. A control variable
More informationQ1 Q2 Q3 Q4 Q5 Total 1 * 7 1 * 5 20 * * Final marks Marks First Question
Page 1 of 6 Template no.: A Course Name: Computer Programming1 Course ID: Exam Duration: 2 Hours Exam Time: Exam Date: Final Exam 1'st Semester Student no. in the list: Exam pages: Student's Name: Student
More informationJava Programming: from the Beginning
CSc 2310: Principle of Programming g( (Java) Spring 2013 Java Programming: from the Beginning Chapter 5 Arrays 1 Copyright 2000 W. W. Norton & Company. All rights reserved. Outline 5.1 Creating and Using
More informationUniversity of Kelaniya Sri Lanka
University of Kelaniya Sri Lanka Scope, Lifetime and Storage Class of a Variable COSC 12533/ COST 12533 SACHINTHA PITIGALA 2017 - Sachintha Pitigala < 1 What is Scope? Scope of Identifier: The scope of
More informationInheritance and Interfaces
Inheritance and Interfaces Object Orientated Programming in Java Benjamin Kenwright Outline Review What is Inheritance? Why we need Inheritance? Syntax, Formatting,.. What is an Interface? Today s Practical
More informationG Programming Languages - Fall 2012
G22.2110-003 Programming Languages - Fall 2012 Lecture 4 Thomas Wies New York University Review Last week Control Structures Selection Loops Adding Invariants Outline Subprograms Calling Sequences Parameter
More informationPrimitive vs Reference
Primitive vs Reference Primitive types store values Reference types store addresses This is the fundamental difference between the 2 Why is that important? Because a reference type stores an address, you
More informationSemantic Analysis. CSE 307 Principles of Programming Languages Stony Brook University
Semantic Analysis CSE 307 Principles of Programming Languages Stony Brook University http://www.cs.stonybrook.edu/~cse307 1 Role of Semantic Analysis Syntax vs. Semantics: syntax concerns the form of a
More informationLast Time. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings
University of British Columbia CPSC 111, Intro to Computation Alan J. Hu Writing a Simple Java Program Intro to Variables Readings Your textbook is Big Java (3rd Ed). This Week s Reading: Ch 2.1-2.5, Ch
More information1007 Imperative Programming Part II
Agenda 1007 Imperative Programming Part II We ve seen the basic ideas of sequence, iteration and selection. Now let s look at what else we need to start writing useful programs. Details now start to be
More informationCreating Classes and Objects
Creating Classes and Objects 7-2 /* For several lines */ Scope Starting point Every Java program consists of at least one class that you define. Java is case sensitive uppercase. javac Welcome1.java javadoc
More informationCOMP-202: Foundations of Programming. Lecture 4: Flow Control Loops Sandeep Manjanna, Summer 2015
COMP-202: Foundations of Programming Lecture 4: Flow Control Loops Sandeep Manjanna, Summer 2015 Announcements Check the calendar on the course webpage regularly for updates on tutorials and office hours.
More informationControls Structure for Repetition
Controls Structure for Repetition So far we have looked at the if statement, a control structure that allows us to execute different pieces of code based on certain conditions. However, the true power
More informationChapter 5. Names, Bindings, and Scopes
Chapter 5 Names, Bindings, and Scopes Chapter 5 Topics Introduction Names Variables The Concept of Binding Scope Scope and Lifetime Referencing Environments Named Constants 1-2 Introduction Imperative
More informationCS111: PROGRAMMING LANGUAGE II
1 CS111: PROGRAMMING LANGUAGE II Computer Science Department Lecture 1: Introduction Lecture Contents 2 Course info Why programming?? Why Java?? Write once, run anywhere!! Java basics Input/output Variables
More information1. Describe History of C++? 2. What is Dev. C++? 3. Why Use Dev. C++ instead of C++ DOS IDE?
1. Describe History of C++? The C++ programming language has a history going back to 1979, when Bjarne Stroustrup was doing work for his Ph.D. thesis. One of the languages Stroustrup had the opportunity
More information11. a b c d e. 12. a b c d e. 13. a b c d e. 14. a b c d e. 15. a b c d e
CS-3160 Concepts of Programming Languages Spring 2015 EXAM #1 (Chapters 1-6) Name: SCORES MC: /75 PROB #1: /15 PROB #2: /10 TOTAL: /100 Multiple Choice Responses Each multiple choice question in the separate
More informationCS201 Some Important Definitions
CS201 Some Important Definitions For Viva Preparation 1. What is a program? A program is a precise sequence of steps to solve a particular problem. 2. What is a class? We write a C++ program using data
More informationReview of the C Programming Language for Principles of Operating Systems
Review of the C Programming Language for Principles of Operating Systems Prof. James L. Frankel Harvard University Version of 7:26 PM 4-Sep-2018 Copyright 2018, 2016, 2015 James L. Frankel. All rights
More informationLinked Lists. private int num; // payload for the node private Node next; // pointer to the next node in the list }
Linked Lists Since a variable referencing an object just holds the address of the object in memory, we can link multiple objects together to form dynamic lists or other structures. In our case we will
More informationCOSC 2P95. Procedural Abstraction. Week 3. Brock University. Brock University (Week 3) Procedural Abstraction 1 / 26
COSC 2P95 Procedural Abstraction Week 3 Brock University Brock University (Week 3) Procedural Abstraction 1 / 26 Procedural Abstraction We ve already discussed how to arrange complex sets of actions (e.g.
More informationType Checking and Type Equality
Type Checking and Type Equality Type systems are the biggest point of variation across programming languages. Even languages that look similar are often greatly different when it comes to their type systems.
More informationCSE P 501 Exam 8/5/04
Name There are 7 questions worth a total of 65 points. Please budget your time so you get to all of the questions. Keep your answers brief and to the point. You may refer to the following references: Course
More informationLast Class. Introduction to arrays Array indices Initializer lists Making an array when you don't know how many values are in it
Last Class Introduction to arrays Array indices Initializer lists Making an array when you don't know how many values are in it public class February4{ public static void main(string[] args) { String[]
More informationOpening Problem. Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.
Chapter 6 Methods 1 Opening Problem Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively. 2 A Solution int sum = 0; for (int i = 1; i
More informationA First Look At Java. Didactic Module 13 Programming Languages - EEL670 1
A First Look At Java Didactic Module 13 Programming Languages - EEL670 1 Outline Thinking about objects Simple expressions and statements Class definitions About references and pointers Getting started
More information