Lecture 4. Lecture
|
|
- Virginia Owen
- 5 years ago
- Views:
Transcription
1 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 - Håkan Jonsson 1 1. Building blocks 2. Access modifiers 3. Arrays in Java 4. InformaGon hiding 5. Example: BitArray 6. Graphics etc first steps 1. Building blocks 2. Access modifiers 3. Arrays in Java 4. InformaGon hiding 5. Example: BitArray 6. Graphics etc first steps 1
2 1. Building blocks There are three ways to parggon a Java program. A) Methods B) Classes C) Packages Good programmers strive to write parts that are as loosely- connected and independent as possible. Such parts are more easier understood. Such parts are more easily maintained. Such parts can more easily be reused. A) Methods A method is a parameterized chunk of code (usually) with a name. We can invoke (use) the code by simply calling the method using its name and suitable arguments. Don t repeat code you need more than once. Instead, make the code general/independent and put in a method that you then call (from where you needed the code). - Håkan Jonsson 5 A) Methods An example in Eclipse... 2
3 A) Methods Methods brings many advantages: Obviously no need to repeat (almost idengcal) code - Less to write Less to write means less you can mess up. Just one place to make changes, if such are needed. No risk that you forget to make a change in one of the places where the code is repeated. An independent method can easily be reused. No need to reinvent the wheel. You automagcally get future improvements of the method. Meaningful names of methods improves readability. Easier to understand and get an overview of the program. Easier for co- programmers to write correct code. B) Classes A class defines the type of a set of representagons that are something. Methods define what such a something can do. Like with methods, write independent classes. Example: The class Helicopter from the last lecture. Then, specialize the representagons where you use them (not in the class defining them). B) Classes Example: The class Helicopter in Eclipse... 3
4 B) Classes A loosely- coupled and independent class (like Helicopter) has the same advantages as an independent method. We can create any number of helicopters. We can reuse the class in other programs where we like to have helicopters. To change what a helicopter is, we only need to reprogram the class not the engre program. It is easy to understand what a helicopter is just look at the class. No need to understand a potengally enormous program. Improvements of the helicopter class make all programs that use the class beder without any need to change them() Good name on classes (like Helicopter) makes it easier for fellow programmers to read and understand your code. etc. C) Packages A package is a set of classes (and interfaces) that together define a subsystem, or subfunc8onality, of a program. The content of a package are more Gghtly coupled to each other than to content of other packages. Code in packages also have special access to each other via package access. A package is also a separate name space. Names are unique only in their own packages. In Java, each class (and interface) belongs to (exactly) one package. In Unix- based systems, each package is stored in a separate directory. Stored in my home directory down in... /l04/ex5/signal Packages are separate even if their directories lie on a path. The three packages l04 l04.ex5 l04.ex5.signal are independent of each other. - Håkan Jonsson 11 C) Packages Membership in packages is declared on the first row in each file of the package. A package can contain any number of files. A file can contain declaragons of many classes (and interfaces). All declaragons in a file belong to the same package. A file without the package declaragon belongs to the nameless, or default, package. NB The default package can lead to problems so you are advise against using it. (It can not be referred to directly.) An example of a file in the package administra8on: package administration; public class University { /*... declarations... */ class Student { /*... declarations... */ class Teacher { /*... declarations... */ } - Håkan Jonsson 12 4
5 public class Sum2 { static int counter = 0; C) Packages A (member of a) class in one file can be used in another file either in qualified form or imported. Qualified means its full name is wriden every Gme it is used. Not pracgcal in the long run. A must, however, in case of name collisions. Imported means included once for all. Very pracgcal. Imports are done with import declaragons, of whom there can be any number: You can import a single class C from package x.y with import x.y.c; or import C and everything else in package x.y with import x.y.*; In UML, a package is drawn as a large rectangle around its content and with a name tag in the upper- lej corner. packagename Y ~ zebras : Z ~ min(agent : X) : char - Håkan Jonsson 13 package l04.ex3; 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; public static void main(string[] args) { int num = 6; System.out.println("Fibonacci of " + num + " = " + fibonacci(num)); System.out.println(num + " = " + factorial(num)); } FamousMathFuncGons One stagc object during run- Gme l04.ex3 FamousMathFunctions ~ factorial(n : int) : int ~ fibonacci(n : int) : int + main(string[] args) : void Lecture 3 - Håkan Jonsson 14 package l04.ex3; 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 l04.ex3; public class { static int factorial(int n) { int i = 1, res = 1; while (i <= n) { res = res * i; i = i + 1; return res; Three stagc objects during run- Gme l04.ex3 Fibonacci Facorial ~ fibonacci(n : int) : int ~ factorial(n : int) : int FamousMathFunctions + main(string[] args) : void package l04.ex3; 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(num)); FamousMathFuncGons Lecture 3 - Håkan Jonsson 15 5
6 package l04.ex3; 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(num)); package l04.ex3; 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; } FamousMathFuncGons l04.ex3 Two stagc objects during run- Gme MyFunctions ~ factorial(n : int) : int ~ fibonacci(n : int) : int FamousMathFunctions + main(string[] args) : void MyFuncGons Lecture 3 - Håkan Jonsson 16 D) Several packages in UML functions central FamousMathFuncGons +void main(string[] args) Fibonacci +fibonacci(m:int) : int +factorial(n:int) : int - Håkan Jonsson 17 package functions; public class { public static int factorial(int n) { int i = 1, res = 1; while (i <= n) { res = res * i; i = i + 1; return res; functions central FamousMathFuncGons +void main(string[] args) Fibonacci +fibonacci(m:int) : int +factorial(n:int) : int - Håkan Jonsson 18 6
7 package functions; public class Fibonacci { public 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; } central FamousMathFuncGons +void main(string[] args) functions Fibonacci +fibonacci(m:int) : int +factorial(n:int) : int - Håkan Jonsson 19 package central; import functions.*; 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(num)); functions central FamousMathFuncGons +void main(string[] args) Fibonacci +fibonacci(m:int) : int +factorial(n:int) : int - Håkan Jonsson Building blocks 2. Access modifiers 3. Arrays in Java 4. InformaGon hiding 5. Example: BitArray 6. Graphics etc first steps 7
8 2. Access Modifiers Everything declared in a class is accessible from within that class. In addigon, the accessibility of classes, variables, and methods from other classes is controlled with access modifiers. public, protected, private, and the nameless access modifier. Note that local declaragons in a method are only accessible in the method. The visibility of a declaragon is called scope. - Håkan Jonsson Access Modifiers Public access Keyword: public Can be accessed from all other classes. (Protected access - a topic of upcoming lectures - ignore) Package access Keyword: None, rather the absence of public, protected, and private() Can be accessed from classes stored within the same package only. Private access Keyword: private Can not be accessed from other classes regardless of where they are stored. public <nothing> private - Håkan Jonsson 23 public Here, the method factorial can be used by code in all other classes in all packages. package functions; public class { public static int factorial(int n) { int i = 1, res = 1; while (i <= n) { res = res * i; i = i + 1; return res; +factorial(n:int) : int - Håkan Jonsson 24 8
9 private Here, the method factorial is hidden within the class. Only code in this class (only factorial itself right now) can access it. package functions; public class { private static int factorial(int n) { int i = 1, res = 1; while (i <= n) { res = res * i; i = i + 1; return res; factorial(n:int) : int - Håkan Jonsson 25 Package access By leaving out the access modifier, factorial becomes accessible from code in the same package only. Code in Fibonacci could, for instance, use factorial. (No modifier) package functions; public class { static int factorial(int n) { int i = 1, res = 1; while (i <= n) { res = res * i; i = i + 1; return res; ~ factorial(n:int) : int - Håkan Jonsson Building blocks 2. Access modifiers 3. Arrays in Java 4. InformaGon hiding 5. Example: BitArray 6. Graphics etc first steps 9
10 3. Arrays in Java An array is a sequence of cells that are all capable of storing one item of the same type. array of integer, array of string, array of characters etc. Can also store references [to dynamic objects]. The size of an array is the number of cells it has. If s is an array, s.length gives its size. The size can not be change ajer an array has been created. Each cell is referenced by an index. The index is in the range 0 to size Håkan Jonsson CreaGng and IniGalizing arrays Example of declaragons: String [] s; int[] ssn; s is now an array of string references, and ssn an array of integers. CreaGng the arrays: s = new String[10]; ssn = new int[120]; s now refers to an array with space for 10 references to strings indexed from 0 to 9. ssn now refers to an array with 120 integers indexed from 0 to 119. Accessing a cell: s[m] The string reference stored in the m th cell of s, where m is an integer. Trying to use an index out of bounds results in a run- Gme error. (Same for ssn.) - Håkan Jonsson Building blocks 2. Access modifiers 3. Arrays in Java 4. InformaGon hiding 5. Example: BitArray 6. Graphics etc first steps 10
11 4. InformaGon hiding When you implement a class, hide away as much implementagon detail as possible from other programmers. In pargcular, internal representagons and internal methods. The moment you give others access to your code, they will start using it and you will have a hard Gme changing it. And they will use it in the worst possible way.. Make a 100% clear difference between what you declare as externally accessible and what you keep hidden internally. Restrict access to the members of a class to an absolute minimum. This reduces misuse [by others], plain mistakes [by others], and errors [done by others] caused by you. In this course: You are free to use private as you like. However, if you use another access modifier, public for instance, you have to mo8vate why. Keep your code hidden unless there is a really good reason not to. Don t paint yourself into a corner. - Håkan Jonsson InformaGon hiding In Java, the scope of declaragons can be controlled via packages and access modifiers. Always think carefully about what other parts of a program should be able to access in the code you write. This is ojen agreed upon before the coding starts, and codified in "contracts" where programmers sgpulate what their code does and how it can be used. Exactly how code works is hidden, for instance. Much will be said about this during the second half of the course. For now we will se an example of this principle of informagon hiding. - Håkan Jonsson Building blocks 2. Access modifiers 3. Arrays in Java 4. InformaGon hiding 5. Example: BitArray 6. Graphics etc first steps 11
12 5. Example: Bit- Arrays The class BitArray provides arrays of bits that can be of arbitrary length and some operagons on them. - Håkan Jonsson 34 program Main + main(string[] args) : void ~ cointoss() : boolean ~ present(b : BitArray) : void bitarray signal BitArray - bits : Signal[] + BitArray(size : int) + BitArray(String pattern) - zeros(n : int) : String + setbit(n : int) : void + clearbit(m : int) : void + valueofbit(p : int) : int + size() : int + and(ba : BitArray) : void + convert() : BigInteger + tostring() : String Signal - on : boolean + Signal() + ison() : boolean + seton() : void + setoff() : void - Håkan Jonsson Building blocks 2. Access modifiers 3. Arrays in Java 4. InformaGon hiding 5. Example: BitArray 6. Graphics etc first steps 12
13 6. Graphics etc first steps Everything on a computer screen has to be deliberately painted. Text, geometric shapes, pictures, borders, scroll bars etc. Java provides means to make such drawings as in the form of GUI components. With strange names like JPanel, JFrame, and the like. GUI = Graphical User Interfaces. Moreover, GUI:s in Java are event- driven. They react on events like mouse- clicks, keys pressed etc. When events occur, special methods are automagcally called by the operagng system. This might seem like magic but, no, that is not the case All GUI components and their methods are available for us to reprogram them, thereby making the program react in a suitable manner. Let us start with an example to illustrate this. This has to do with lab assignment 2. - Håkan Jonsson 37 Example: A blinking signal We will proceed in three steps to explain this example. a) Observer/Observable design padern b) Timer c) The final program with the blinking signal. - Håkan Jonsson 38 a) Observer Observable The observed: Is observable. AutomaGcally gets the methods setchanged nogfyobservers addobserver Must call setchanged nogfyobservers in this order whenever it has changed. The observer: Is an observer. Must implement the method update This method is called whenever the model calls setchanged nogfyobservers Any code within scope can add an observer to an observable with the method addobserver( reference- to- an- observer ) that the observable has. - Håkan Jonsson 39 13
14 a) Example: Obs Shows how observers are nogfied as soon as the observed states that it has been changed. The funcgonality is traced via print outs. b) Timer In Java, it is fairly simple to add Gmers that acgvate code at regular intervals. The class java.swing.timer provides a Gmer suitable for GUI:s. There are other Timer class too make sure you use the right one, if you like to try Gmers on your own. To each Gmer, listeners can be added. All listerers are called when the Gmer expires. b) Example: TimerDemo 14
15 c) Signal and signal image We separate the signal from its image (view). Make the Signal class an observable. Make Display, the image class, an observer. The idea is that a Signal object does not need to know who is observing. In this case, a Display object. Leads to less dependence between the classes, which is beneficial during the programming. Also, any number of observers can be added without the need to change Signal. Then we add a Gmer and have it call a method at a given rate that flips the signal. - Håkan Jonsson 43 c) Example: DisplayedTimedSignal Signal - on : boolean ~ Signal() ~ ison() : boolean ~ seton() : void ~ setoff() : void Display - s : Signal + Display(s : Signal) + update(observable obs, Object obj) : void + paintcomponent(g : Graphics) : void Driver + run() : void() Listener - s : Signal + Listener(s : Signal)+ actionperformed(actionevent evt) : void DTS DisplayedTimedSignal + DisplayedTimedSignal(s : Signal, title : String, rate : int, x : int, y : int) Observable JPanel <<interface>> Observer Signal - on : boolean ~ Signal() ~ ison() : boolean ~ seton() : void ~ setoff() : void <<interface>> ActionListener Display - s : Signal + Display(s : Signal) + update(observable obs, Object obj) : void + paintcomponent(g : Graphics) : void Driver + run() : void() Listener - s : Signal + Listener(s : Signal)+ actionperformed(actionevent evt) : void DTS DisplayedTimedSignal + DisplayedTimedSignal(s : Signal, title : String, rate : int, x : int, y : int) Timer JFrame 15
News and information! Review: Java Programs! Feedback after Lecture 2! Dead-lines for the first two lab assignment have been posted.!
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, 107-111,
More informationLecture 5. Lecture
this GUI Example: SignalGUI Orphans D0010E Object- Oriented Programming and Design Model- View- Control Example: Counter Observer Recursive References Design PaOerns - Håkan Jonsson 1 1 About dynamic objects
More informationLecture 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 9. Lecture
Layout Components MVC Design PaCern GUI Programming Observer Design PaCern D0010E Lecture 8 - Håkan Jonsson 1 Lecture 8 - Håkan Jonsson 2 Lecture 8 - Håkan Jonsson 3 1 1. GUI programming In the beginning,
More informationGlobal Gomoku Lab 4 in D0010E
Luleå University of Technology February 20, 2012 Computer Science Håkan Jonsson Global Gomoku Lab 4 in D0010E 1 Introduction Modern forms of communication are more and more carried out over the Internet,
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 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 informationCSC 160 LAB 8-1 DIGITAL PICTURE FRAME. 1. Introduction
CSC 160 LAB 8-1 DIGITAL PICTURE FRAME PROFESSOR GODFREY MUGANDA DEPARTMENT OF COMPUTER SCIENCE 1. Introduction Download and unzip the images folder from the course website. The folder contains 28 images
More informationLab 4. D0010E Object-Oriented Programming and Design. Today s lecture. GUI programming in
Lab 4 D0010E Object-Oriented Programming and Design Lecture 9 Lab 4: You will implement a game that can be played over the Internet. The networking part has already been written. Among other things, the
More informationEquality for Abstract Data Types
Object-Oriented Design Lecture 4 CSU 370 Fall 2008 (Pucella) Tuesday, Sep 23, 2008 Equality for Abstract Data Types Every language has mechanisms for comparing values for equality, but it is often not
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 informationCOMP 250 Winter 2011 Reading: Java background January 5, 2011
Almost all of you have taken COMP 202 or equivalent, so I am assuming that you are familiar with the basic techniques and definitions of Java covered in that course. Those of you who have not taken a COMP
More informationNews! Feedback after Lecture 4! About Java and OOP!
this Example: SignalGUI News Orphans Recursive References D0010E Object-Oriented Programming and Design Lecture 5 GUI Design Patterns Applets Model-View- Control Observer Hints for Lab 2 Use a Vector to
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 informationThe JFrame Class Frame Windows GRAPHICAL USER INTERFACES. Five steps to displaying a frame: 1) Construct an object of the JFrame class
CHAPTER GRAPHICAL USER INTERFACES 10 Slides by Donald W. Smith TechNeTrain.com Final Draft 10/30/11 10.1 Frame Windows Java provides classes to create graphical applications that can run on any major graphical
More informationRecursion 1. Recursion is the process of defining something in terms of itself.
Recursion 1 Recursion is the process of defining something in terms of itself. A method that calls itself is said to be recursive. Recursion is an alternative form of program control. It is repetition
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 informationLecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University
Lecture 3 COMP1006/1406 (the Java course) Summer 2014 M. Jason Hinek Carleton University today s agenda assignments 1 (graded) & 2 3 (available now) & 4 (tomorrow) a quick look back primitive data types
More information1B1b Inheritance. Inheritance. Agenda. Subclass and Superclass. Superclass. Generalisation & Specialisation. Shapes and Squares. 1B1b Lecture Slides
1B1b Inheritance Agenda Introduction to inheritance. How Java supports inheritance. Inheritance is a key feature of object-oriented oriented programming. 1 2 Inheritance Models the kind-of or specialisation-of
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 informationGraphical User Interface (GUI)
Graphical User Interface (GUI) An example of Inheritance and Sub-Typing 1 Java GUI Portability Problem Java loves the idea that your code produces the same results on any machine The underlying hardware
More informationICOM 4015 Advanced Programming Laboratory. Chapter 1 Introduction to Eclipse, Java and JUnit
ICOM 4015 Advanced Programming Laboratory Chapter 1 Introduction to Eclipse, Java and JUnit University of Puerto Rico Electrical and Computer Engineering Department by Juan E. Surís 1 Introduction This
More informationLecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan
Lecture 08-1 Programming in C++ PART 1 By Assistant Professor Dr. Ali Kattan 1 The Conditional Operator The conditional operator is similar to the if..else statement but has a shorter format. This is useful
More informationExample: Fibonacci Numbers
Example: Fibonacci Numbers Write a program which determines F n, the (n + 1)-th Fibonacci number. The first 10 Fibonacci numbers are 0, 1, 1, 2, 3, 5, 8, 13, 21, and 34. The sequence of Fibonacci numbers
More informationLecture 04 FUNCTIONS AND ARRAYS
Lecture 04 FUNCTIONS AND ARRAYS 1 Motivations Divide hug tasks to blocks: divide programs up into sets of cooperating functions. Define new functions with function calls and parameter passing. Use functions
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 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 informationSequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;
Control Statements Control Statements All programs could be written in terms of only one of three control structures: Sequence Structure Selection Structure Repetition Structure Sequence structure The
More informationMethods. CSE 114, Computer Science 1 Stony Brook University
Methods CSE 114, Computer Science 1 Stony Brook University http://www.cs.stonybrook.edu/~cse114 1 Opening Problem Find multiple sums of integers: - from 1 to 10, - from 20 to 30, - from 35 to 45,... 2
More informationSolution register itself
Observer Pattern Context: One object (the Subject) is the source of events. Other objects (Observers) want to know when an event occurs. Or: several objects should be immediately updated when the state
More informationCOMP 202. More on OO. CONTENTS: static revisited this reference class dependencies method parameters variable scope method overloading
COMP 202 CONTENTS: static revisited this reference class dependencies method parameters variable scope method overloading More on OO COMP 202 Objects 3 1 Static member variables So far: Member variables
More informationObject Class. EX: LightSwitch Class. Basic Class Concepts: Parts. CS257 Computer Science II Kevin Sahr, PhD. Lecture 5: Writing Object Classes
1 CS257 Computer Science II Kevin Sahr, PhD Lecture 5: Writing Object Classes Object Class 2 objects are the basic building blocks of programs in Object Oriented Programming (OOP) languages objects consist
More informationThe high-level language has a series of primitive (builtin) types that we use to signify what s in the memory
Types and Variables We write code like: int x = 512; int y = 200; int z = x+y; The high-level language has a series of primitive (builtin) types that we use to signify what s in the memory The compiler
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 informationCS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University
CS 112 Introduction to Computing II Wayne Snyder Department Boston University Today: ; public vs private; Object-Oriented Programming; Abstract data types; Stacks as ADTs; Next time: Stacks and Queues
More informationThe NetBeans IDE is a big file --- a minimum of around 30 MB. After you have downloaded the file, simply execute the file to install the software.
Introduction to Netbeans This document is a brief introduction to writing and compiling a program using the NetBeans Integrated Development Environment (IDE). An IDE is a program that automates and makes
More informationOverview. Lab 5 Methods and Parameters
Lab 5 Methods and Parameters Overview At this point in the course, you should have a set of skills which allow you to create functionality at the level of using control structures like if statements and
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 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 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 informationReview sheet for Final Exam (List of objectives for this course)
Review sheet for Final Exam (List of objectives for this course) Please be sure to see other review sheets for this semester Please be sure to review tests from this semester Week 1 Introduction Chapter
More informationChapter Fourteen Bonus Lessons: Algorithms and Efficiency
: Algorithms and Efficiency The following lessons take a deeper look at Chapter 14 topics regarding algorithms, efficiency, and Big O measurements. They can be completed by AP students after Chapter 14.
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 informationNote: This is a miniassignment and the grading is automated. If you do not submit it correctly, you will receive at most half credit.
Com S 227 Fall 2017 Miniassignment 1 50 points Due Date: Monday, October 16, 11:59 pm (midnight) Late deadline (25% penalty): Tuesday, October 17, 11:59 pm General information This assignment is to be
More informationJava Classes - Using your classes. How the classes you write are being used
Java Classes - Using your classes How the classes you write are being used What s the use of classes? So, you have been writing a few classes by now... What for? The programs you will write will use objects
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 informationClass 16: The Swing Event Model
Introduction to Computation and Problem Solving Class 16: The Swing Event Model Prof. Steven R. Lerman and Dr. V. Judson Harward 1 The Java Event Model Up until now, we have focused on GUI's to present
More informationCISC 1600 Lecture 3.1 Introduction to Processing
CISC 1600 Lecture 3.1 Introduction to Processing Topics: Example sketches Drawing functions in Processing Colors in Processing General Processing syntax Processing is for sketching Designed to allow artists
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 informationOBJECT ORİENTATİON ENCAPSULATİON
OBJECT ORİENTATİON Software development can be seen as a modeling activity. The first step in the software development is the modeling of the problem we are trying to solve and building the conceptual
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 informationThe Java Type System (continued)
Object-Oriented Design Lecture 5 CSU 370 Fall 2007 (Pucella) Friday, Sep 21, 2007 The Java Type System (continued) The Object Class All classes subclass the Object class. (By default, this is the superclass
More informationC22a: Problem Solving using Recursion
CISC 3115 TY3 C22a: Problem Solving using Recursion Hui Chen Department of Computer & Information Science CUNY Brooklyn College 11/6/2018 CUNY Brooklyn College 1 Outline Characteristics of recursion Recursion
More informationHOW TO WRITE RECURSIVE FUNCTIONS ON YOUR OWN
HOW TO WRITE RECURSIVE FUNCTIONS ON YOUR OWN In this tutorial, you ll learn - well, you ve already read the title - how to write recursive functions on your own. If you re the person to whom recursive
More information2IS45 Programming
Course Website Assignment Goals 2IS45 Programming http://www.win.tue.nl/~wsinswan/programmeren_2is45/ Rectangles Learn to use existing Abstract Data Types based on their contract (class Rectangle in Rectangle.
More informationCSC207 Week 4. Larry Zhang
CSC207 Week 4 Larry Zhang 1 Logistics A1 Part 1, read Arnold s emails. Follow the submission schedule. Read the Q&A session in the handout. Ask questions on the discussion board. Submit on time! Don t
More informationTips from the experts: How to waste a lot of time on this assignment
Com S 227 Spring 2018 Assignment 1 100 points Due Date: Friday, September 14, 11:59 pm (midnight) Late deadline (25% penalty): Monday, September 17, 11:59 pm General information This assignment is to be
More informationGUI Event Handlers (Part I)
GUI Event Handlers (Part I) 188230 Advanced Computer Programming Asst. Prof. Dr. Kanda Runapongsa Saikaew (krunapon@kku.ac.th) Department of Computer Engineering Khon Kaen University 1 Agenda General event
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 informationChapter 3 Classes. Activity The class as a file drawer of methods. Activity Referencing static methods
Chapter 3 Classes Lesson page 3-1. Classes Activity 3-1-1 The class as a file drawer of methods Question 1. The form of a class definition is: public class {
More information1.00 Lecture 8. Using An Existing Class, cont.
.00 Lecture 8 Classes, continued Reading for next time: Big Java: sections 7.9 Using An Existing Class, cont. From last time: is a Java class used by the BusTransfer class BusTransfer uses objects: First
More informationDay 3. COMP 1006/1406A Summer M. Jason Hinek Carleton University
Day 3 COMP 1006/1406A Summer 2016 M. Jason Hinek Carleton University today s agenda assignments 1 was due before class 2 is posted (be sure to read early!) a quick look back testing test cases for arrays
More informationQUIZ. What is wrong with this code that uses default arguments?
QUIZ What is wrong with this code that uses default arguments? Solution The value of the default argument should be placed in either declaration or definition, not both! QUIZ What is wrong with this code
More informationMidterm assessment - MAKEUP Fall 2010
M257 MTA Faculty of Computer Studies Information Technology and Computing Date: /1/2011 Duration: 60 minutes 1-Version 1 M 257: Putting Java to Work Midterm assessment - MAKEUP Fall 2010 Student Name:
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 informationProgramming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur
Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur Lecture - 43 Dynamic Binding (Polymorphism): Part III Welcome to Module
More informationMethod Invocation. Zheng-Liang Lu Java Programming 189 / 226
Method Invocation Note that the input parameters are sort of variables declared within the method as placeholders. When calling the method, one needs to provide arguments, which must match the parameters
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 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 informationLecture 04 FUNCTIONS AND ARRAYS
Lecture 04 FUNCTIONS AND ARRAYS 1 Motivations Divide hug tasks to blocks: divide programs up into sets of cooperating functions. Define new functions with function calls and parameter passing. Use functions
More informationLecture (06) Java Forms
Lecture (06) Java Forms Dr. Ahmed ElShafee 1 Dr. Ahmed ElShafee, Fundamentals of Programming I, Introduction You don t have to output everything to a terminal window in Java. In this lecture, you ll be
More informationMat 2170 Week 9. Spring Mat 2170 Week 9. Objects and Classes. Week 9. Review. Random. Overloading. Craps. Clients. Packages. Randomness.
Spring 2014 Student Responsibilities Reading: Textbook, Sections 6.1 6.3 Attendance Recall: Writing Methods Decomposition: break a problem down into smaller subproblems Use methods whenever you can in
More informationCS113: Lecture 4. Topics: Functions. Function Activation Records
CS113: Lecture 4 Topics: Functions Function Activation Records 1 Why functions? Functions add no expressive power to the C language in a formal sense. Why have them? Breaking tasks into smaller ones make
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 informationCS125 : Introduction to Computer Science. Lecture Notes #38 and #39 Quicksort. c 2005, 2003, 2002, 2000 Jason Zych
CS125 : Introduction to Computer Science Lecture Notes #38 and #39 Quicksort c 2005, 2003, 2002, 2000 Jason Zych 1 Lectures 38 and 39 : Quicksort Quicksort is the best sorting algorithm known which is
More information(Refer Slide Time: 01.26)
Data Structures and Algorithms Dr. Naveen Garg Department of Computer Science and Engineering Indian Institute of Technology, Delhi Lecture # 22 Why Sorting? Today we are going to be looking at sorting.
More informationGetting Started (1.8.7) 9/2/2009
2 Getting Started For the examples in this section, Microsoft Windows and Java will be used. However, much of the information applies to other operating systems and supported languages for which you have
More information2 Getting Started. Getting Started (v1.8.6) 3/5/2007
2 Getting Started Java will be used in the examples in this section; however, the information applies to all supported languages for which you have installed a compiler (e.g., Ada, C, C++, Java) unless
More informationCSE 114 Computer Science I
CSE 114 Computer Science I Iteration Cape Breton, Nova Scotia What is Iteration? Repeating a set of instructions a specified number of times or until a specific result is achieved How do we repeat steps?
More informationCSE 331 Software Design and Implementation. Lecture 16 Callbacks and Observers
CSE 331 Software Design and Implementation Lecture 16 Callbacks and Observers Leah Perlmutter / Summer 2018 Announcements Announcements Quiz 6 due Thursday 8/2 Homework 7 due Thursday 8/2 Callbacks The
More information2. (True/False) All methods in an interface must be declared public.
Object and Classes 1. Create a class Rectangle that represents a rectangular region of the plane. A rectangle should be described using four integers: two represent the coordinates of the upper left corner
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 informationCS/ENGRD 2110 SPRING Lecture 2: Objects and classes in Java
1 CS/ENGRD 2110 SPRING 2014 Lecture 2: Objects and classes in Java http://courses.cs.cornell.edu/cs2110 Java OO (Object Orientation) 2 Python and Matlab have objects and classes. Strong-typing nature of
More informationHanley s Survival Guide for Visual Applications with NetBeans 2.0 Last Updated: 5/20/2015 TABLE OF CONTENTS
Hanley s Survival Guide for Visual Applications with NetBeans 2.0 Last Updated: 5/20/2015 TABLE OF CONTENTS Glossary of Terms 2-4 Step by Step Instructions 4-7 HWApp 8 HWFrame 9 Never trust a computer
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 informationCSE 331 Software Design and Implementation. Lecture 17 Events, Listeners, Callbacks
CSE 331 Software Design and Implementation Lecture 17 Events, Listeners, Callbacks Zach Tatlock / Winter 2016 The limits of scaling What prevents us from building huge, intricate structures that work perfectly
More informationWe will start our journey into Processing with creating static images using commands available in Processing:
Processing Notes Chapter 1: Starting Out We will start our journey into Processing with creating static images using commands available in Processing: rect( ) line ( ) ellipse() triangle() NOTE: to find
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 informationCS61C Machine Structures. Lecture 4 C Pointers and Arrays. 1/25/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/
CS61C Machine Structures Lecture 4 C Pointers and Arrays 1/25/2006 John Wawrzynek (www.cs.berkeley.edu/~johnw) www-inst.eecs.berkeley.edu/~cs61c/ CS 61C L04 C Pointers (1) Common C Error There is a difference
More informationWriting Functions. Reading: Dawson, Chapter 6. What is a function? Function declaration and parameter passing Return values Objects as parameters
What is a function? Function declaration and parameter passing Return values Objects as parameters Reading: Writing Functions Dawson, Chapter 6 Abstraction Data scoping Encapsulation Modules Recursion
More information6 The MVC model. Main concepts to be covered. Pattern structure. Using design patterns. Design pattern: Observer. Observers
Main concepts to be covered 6 The MVC model Design patterns The design pattern The architecture Using design patterns Inter-class relationships are important, and can be complex. Some relationship recur
More informationStudent Responsibilities. Mat 2170 Week 9. Notes About Using Methods. Recall: Writing Methods. Chapter Six: Objects and Classes
Student Responsibilities Mat 2170 Week 9 Objects and Classes Spring 2014 Reading: Textbook, Sections 6.1 6.3 Lab 9 Attendance 1 2 Recall: Writing Methods 3 Decomposition: break a problem down into smaller
More informationAsynchronous Programming
Asynchronous Programming Turn-in Instructions A main file, called gui.py See previous slides for how to make it main I ll run it from the command line Put in a ZIP file, along with any additional needed
More informationChapter 7 Applets. Answers
Chapter 7 Applets Answers 1. D The drawoval(x, y, width, height) method of graphics draws an empty oval within a bounding box, and accepts 4 int parameters. The x and y coordinates of the left/top point
More informationCS/ENGRD 2110 FALL Lecture 7: Interfaces and Abstract Classes
CS/ENGRD 2110 FALL 2017 Lecture 7: Interfaces and Abstract Classes http://courses.cs.cornell.edu/cs2110 1 Announcements 2 A2 is due tomorrow night (17 February) Get started on A3 a method every other day.
More informationLecture 1: Overview
15-150 Lecture 1: Overview Lecture by Stefan Muller May 21, 2018 Welcome to 15-150! Today s lecture was an overview that showed the highlights of everything you re learning this semester, which also meant
More informationAP CS Unit 11: Graphics and Events
AP CS Unit 11: Graphics and Events This packet shows how to create programs with a graphical interface in a way that is consistent with the approach used in the Elevens program. Copy the following two
More informationFinal Exam. Programming Assignment 3. 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 Interfaces vs. Inheritance Abstract Classes Inner Classes Readings This Week: No new readings. Consolidate! (Reminder: Readings
More informationGraphical User Interface (GUI)
Graphical User Interface (GUI) An example of Inheritance and Sub-Typing 1 Java GUI Portability Problem Java loves the idea that your code produces the same results on any machine The underlying hardware
More informationCS Programming Exercise:
CS Programming Exercise: An Introduction to Java and the ObjectDraw Library Objective: To demonstrate the use of objectdraw graphics primitives and Java programming tools This lab will introduce you to
More information