OOP Week 3 1 Object Oriented Programming in Java Monday, Week 3 Interface PinBallTarget OOP Concepts Last Week’s Assignment Arrays Collection Class --

Slides:



Advertisements
Similar presentations
11 Copyright © 2005, Oracle. All rights reserved. Using Arrays and Collections.
Advertisements

Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
AbstractClassesInterfacesPolymorphism1 Abstract Classes, Interfaces, Polymorphism Barb Ericson Georgia Tech April 2010.
C8: Understanding Inheritance. Intuitive description Intuitive: FLORISTS are SHOPKEEPERS, inheriting various shopkeeper behaviors Tension in OOP languages:
CSE 143 Lecture 22: Advanced List Implementation (ADTs; interfaces; abstract classes; inner classes; generics; iterators)
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
COMS S1007 Object-Oriented Programming and Design in Java August 7, 2007.
29-Jun-15 Java Concurrency. Definitions Parallel processes—two or more Threads are running simultaneously, on different cores (processors), in the same.
Building Java Programs Inner classes, generics, abstract classes reading: 9.6, 15.4,
1 Threads Chapter 4 Reading: 4.1,4.4, Process Characteristics l Unit of resource ownership - process is allocated: n a virtual address space to.
OOP Week 4 1 Object Oriented Programming in Java Monday, Week 4 Last week’s asst. –rating…. This week’s asst. OOP Concepts Inheritance & Substitutability.
Threading in Java – a Tutorial QMUL IEEE SB. Why Threading When we need to run two tasks concurrently So multiple parts (>=2) of a program can run simultaneously.
50.003: Elements of Software Construction Week 5 Basics of Threads.
OOP Languages: Java vs C++
Concurrency Recitation – 2/24 Nisarg Raval Slides by Prof. Landon Cox.
3.1 Documentation & Java Language Elements Purpose of documentation Assist the programmer with developing the program Assist other programers who.
Programming Languages and Paradigms Object-Oriented Programming.
Advanced Java Session 3 New York University School of Continuing and Professional Studies.
Session 12 Introduction to PinBallGame (Chaper 7).
Object Oriented Programming Lecture 8: Introduction to laboratorial exercise – part II, Introduction to GUI frames in Netbeans, Introduction to threads.
1 Concurrent Languages – Part 1 COMP 640 Programming Languages.
GENERIC COLLECTIONS. Type-Wrapper Classes  Each primitive type has a corresponding type- wrapper class (in package java.lang).  These classes are called.
CS 11 java track: lecture 4 This week: arrays interfaces listener classes inner classes GUI callbacks.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
Session 21 Chapter 10: Mechanisms for Software Reuse.
Recommendation: Play the game and attempt to answer the questions yourself without looking at the answers. You’ll learn much less if you just look at the.
Data Design and Implementation. Definitions of Java TYPES Atomic or primitive type A data type whose elements are single, non-decomposable data items.
1 Web Based Programming Section 8 James King 12 August 2003.
1/20/03A2-1 CS494 Interfaces and Collection in Java.
Chapter 7: Pinball Game Construction Kit. Vectors Example of a “collection class” Must “import java.util.Vector” More flexible than arrays: will grow.
Session 16 Pinball Game Construction Kit:. Pinball Version 1 Replaced the fire button with a mouse event. Multiple balls can be in the air at once. –Uses.
CSE 143 Lecture 24 Advanced collection classes (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, , slides.
Session 18 Chapter 8: Understanding Inheritance. Recall Exercise 2 From Tuesday It’s very annoying to move a target from the pallet and drop it in the.
Chapter 14 Abstract Classes and Interfaces. Abstract Classes An abstract class extracts common features and functionality of a family of objects An abstract.
Java Thread and Memory Model
Threads Doing Several Things at Once. Threads n What are Threads? n Two Ways to Obtain a New Thread n The Lifecycle of a Thread n Four Kinds of Thread.
Concurrency Control 1 Fall 2014 CS7020: Game Design and Development.
Session 13 Pinball Game Construction Kit (Version 3):
13-1 Chapter 13 Concurrency Topics Introduction Introduction to Subprogram-Level Concurrency Semaphores Monitors Message Passing Java Threads C# Threads.
Recitation 5 Enums and The Java Collections classes/interfaces 1.
Thread basics. A computer process Every time a program is executed a process is created It is managed via a data structure that keeps all things memory.
Week 2 - Friday.  What did we talk about last time?  Computing Big Oh.
Data Design and Implementation. Definitions Atomic or primitive type A data type whose elements are single, non-decomposable data items Composite type.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Written by: Dr. JJ Shepherd
Interfaces, Classes, Collections School of Engineering and Computer Science, Victoria University of Wellington COMP T2 Lecture 3 Marcus Frean.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
OOP Basics Classes & Methods (c) IDMS/SQL News
Concurrency (Threads) Threads allow you to do tasks in parallel. In an unthreaded program, you code is executed procedurally from start to finish. In a.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
1 Threads in Java Jingdi Wang. 2 Introduction A thread is a single sequence of execution within a program Multithreading involves multiple threads of.
Concurrency in Java MD. ANISUR RAHMAN. slide 2 Concurrency  Multiprogramming  Single processor runs several programs at the same time  Each program.
RealTimeSystems Lab Jong-Koo, Lim
1 Introduction to Threads Race Conditions. 2 Process Address Space Revisited Code Data OS Stack (a)Process with Single Thread (b) Process with Two Threads.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
A brief intro to: Parallelism, Threads, and Concurrency
EECE 310: Software Engineering
ADT’s, Collections/Generics and Iterators
Programming Language Concepts (CIS 635)
Abstract Class As per dictionary, abstraction is the quality of dealing with ideas rather than events. For example, when you consider the case of ,
Object Oriented Programming in java
Threads Chapter 4.
Java Concurrency 17-Jan-19.
Java Concurrency.
Java Concurrency.
Expanding the PinBallGame
Java Concurrency 29-May-19.
CSE 332: Concurrency and Locks
Presentation transcript:

OOP Week 3 1 Object Oriented Programming in Java Monday, Week 3 Interface PinBallTarget OOP Concepts Last Week’s Assignment Arrays Collection Class -- vector Threads Exception Handling Reading Budd, Ch 7, 18; AGH Ch. 3 Asst (due Thursday) Ch. 7: Exercises 2,3,4 (5 optional), (8 everybody try!) 8,9 (for intermediate and advanced (for advanced) and any other pinball enhancement of your invention!

OOP Week 3 2 Arrays private Ball [ ] ballArray; private static final int BallArraySize = 10; ballArray = new Ball [ BallArraySize ]; for (int i = 0; i < BallArraySize; i++) { ballArray[i] = new Ball (10,15,5); ballArray[i].setColor (ballColor); ballArray[i].setMotion (3.0+i, 6.0-i); }

OOP Week 3 3 Arrays Creating an array does not create the objects that are stored in the array. Two ways to create Java arrays –1. Button buttons[ ] = new Button[10]; –2. int lookup_table[ ] = {1, 2, 4, 8, 16}; Menu m = createMenu(“File”, new String[ ] {“Open…”, “Save”, “Quit”}); The size of an array is not part of its type String[ ] strings; strings = new String[10]; strings = new String[20]; Arrays behave “like” objects, e.g., x = ballArray.length

OOP Week 3 4 Collection Classes (set, list, vector,...) Collections (aka containers) are holders that letyou store and organize objects in useful ways for efficient access. java.util contains the general collection framework. java.lang.Comparable public int size() public boolean isEmpty() public boolean contains (Object elem) public boolean remove (Object elem) Iteration public boolean hasNext () public Object next() public void remove() public void add (Object elem) public void set (Object elem) Ordering (using Comparable and Comparator) public int compareTo (Object other) public int compare (Object o1, Object o2)

OOP Week 3 5 Collection Classes The legacy collection types: Vector, Stack, etc. Vector. All methods that access the contents of a Vector are synchronized. Vector is analogous to ArrayList, and so inherits from List. How are arrays and vectors alike, and how different? public final void addElement (Object elem) public final void insetElementAt(Object elem, int index) public final void removeElementAt(int index) public final void removeAllElements ( )

OOP Week 3 6 Last week’s homework…. If not finished, do that before going on… if not happy with it - you will have a chance to redo (& resubmit) BEFORE the midterm speaking of the midterm…. Ok to put off til wed of week 6? Short answer (15 min) -- week 5? Robert Murphy????

OOP Week 3 7 This Week’s Lab & Homework Norman’s two kinds of cognition -- –experiential and what??? Advice –make yourself a map of the program space! –Think how this might be different programming for you…. use the API ref on help page or the java API docs –have a plan before you jump in! become better acquainted with the tool –be aware of differences in browsers… PDF doc -- Integrated Development Environment

OOP Week 3 8 Threads Threads are independent parts of a program that run simultaneously. Except on machines with more than one processor, only one thread is actually running at a time. Threaded programs are managed by a process that deciddes which thread runs at any given time. You can’t use them to sew on buttons…

OOP Week 3 9 Threads in Java Java is one of only a few programming languages that support multiple threads. The implementation of threads in Java differs among platforms. The differences can cause surprises if you aren’t aware of the threaded nature of most Java programs.

OOP Week 3 10 Concurrent Programming Multiple threads create many problems related to the order in which events occur. For example, if two threads access the same data field of an object, the results can depend on the timing of the access. Access to shared data must be synchronized in order to avoid unpredictable, incorrect results.

OOP Week 3 11 Concurrency Example Consider the following two code fragments that are executing concurrently, unsynchronized, and accessing the variables a,b,c, from the same object. Assume b=3 and c=2, initially: Thread 1 a = b + 10; c = a + 5; Thread 2 b = c - 4; What is the final value of a? of b? of c?

OOP Week 3 12 Synchronization Java provides synchronization so that situations such as the example don’t occur. Code that accesses shared data is kinown as a critical section. Critical sections are synchronized by obtaining exclusive access to the data they modify.

OOP Week 3 13 Synchronization Example With synchronization, only two orders of execution are possible in our example. –S1, s2, s3 –s3, s1, s2 The third possible ordering of the statements is presented by the synchronization. The possible orders produce different values, but each is a predicatable result that doesn’t depend on a so-called “race condition”.

OOP Week 3 14 Control of Threads Java provides methods that allow: –Suspension of a thread –Termination of a thread –Creation of new threads –Synchronization of threads

OOP Week 3 15 Creation of Threads There are two ways to define classes that can run as separate threads: –extending the class Thread –implementing the Runnable interface. Which you choose depends on the situation –Implementing Runnable makes sense if you want to inherit from a class that is not a subclass of Thread.

OOP Week 3 16 Exception Handling

OOP Week 3 17 Interfaces -- PinBallTarget 3 domain classes -- Peg, Wall, Hole and Spring share the same behavior (type), but have no structure in common. The behavior may be implemented differently. A variable can be declared to hold a value of PinBallTarget type (and thus a value of either Peg, Wall or Spring)