H212 Introduction to Software Systems Honors

Size: px
Start display at page:

Download "H212 Introduction to Software Systems Honors"

Transcription

1 Introduction to Software Systems Honors Lecture #15: October 21, /34

2 Generic classes and types Generic programming is the creation of programming constructs that can be usedwith many different types. For example, the ArrayList class uses the technique of generic programming. As a result, you can form array lists that collect elements of different types, such as ArrayList<String>, ArrayList<BankAccount> When declaring a generic class, you specify a type variable for each type parameter. Here is how the standard Java library declares the ArrayList class, using the type variable E for the element type: public class ArrayList<E> public ArrayList()... public void add(e element) Here, E is a type variable, not a Java reserved word. 2/34

3 Generic classes and types In order to use a generic class, you need to instantiate the type parameter, that is, supply an actual type. You can supply any class or interface type, for example ArrayList<BankAccount> ArrayList<Measurable> However, you cannot substitute any of the eight primitive types for a type parameter. It would be an error to declare: ArrayList<double> Use the corresponding wrapper class instead, such as ArrayList<Double> 3/34

4 Implementing generic types Simple generic class that stores pairs of objects, each of which can have an arbitrary type. This class can be useful when you implement a method that computes two values at the same time. A method cannot simultaneously return a String and an Integer, but it can return a single object of type Pair<String, Integer>. public class Pair<T, S> private T first; private S second; public Pair(T firstelement, S secondelement) first = firstelement; second = secondelement; public T getfirst() return first; public S getsecond() return second; 4/34

5 Interfaces A Java interface is a collection of abstract methods and constants. An abstract method is a method header without a method body. An abstract method can be declared using the modifier abstract, but because all methods in an interface are abstract, usually it is left off. An interface is used to establish a set of methods that a class will implement. None of the methods in an interface are given a definition (body). An interface cannot be instantiated. Methods in an interface have public visibility by default. A class formally implements an interface by: stating so in the class header providing implementations for every abstract method in the interface If a class declares that it implements an interface, it must define all methods in the interface. 5/34

6 Interfaces In addition to (or instead of) abstract methods, an interface can contain constants. When a class implements an interface, it gains access to all its constants. A class that implements an interface can implement other methods as well. A class can implement multiple interfaces. The interfaces are listed in the implements clause. The class must implement all methods in all interfaces listed in the header. 6/34

7 The Java API contains many helpful interfaces. The Comparable interface The Comparable interface contains one abstract method called compareto, which is used to compare two objects. The String class implements Comparable, giving us the ability to put strings in lexicographic order. Any class can implement Comparable to provide a mechanism for comparing objects of that type The value returned from compareto should be: negative is obj1 is less that obj2, 0 if they are equal, and positive if obj1 is greater than obj2. It's up to the programmer to determine what makes one object less than another. 7/34

8 Sorting an array of objects A sorting algorithm repeatedly compares elements and rearranges them if they are out of order. The rules for doing the comparison are different for each class, and the sorting algorithm should just call a method supplied by the class. If a class wants to enable sorting for its objects, it should implement the Comparable interface. We want to compare strings against strings, employees against employees, and so on. For that reason, the Comparable interface has a type parameter. public interface Comparable<T> int compareto(t other); For example, the String class implements Comparable<String> so that its compareto method has the signature int compareto(string other). 8/34

9 Comparing When calling x.compareto(y), the compareto method returns an integer value to indicate whether x or y should come first. A positive return value (not necessarily 1) indicates that x should come after y. A negative integer (not necessarily -1) is returned when x should come before y. If x and y are considered equal, the returned value is 0. Note that the return value can be any integer. That flexibility is useful because it allows you to return a difference of non-negative integers. public class Employee implements Comparable<Employee>... public int compareto(employee other) return getid() - other.getid(); // Ok if IDs always >= 0 9/34

10 Implementing Comparable You can implement the Comparable interface for your own classes as well. For example, to sort a collection of coins, the Coin class would need to implement this interface and declare a compareto method: public class Coin implements Comparable... public int compareto(object otherobject) Coin other = (Coin) otherobject; if (value < other.value) return -1; if (value == other.value) return 0; return 1;... 10/34

11 Example Suppose we have player objects with pairs (name, points) and we want to sort them according to the number of points. import java.util.*; public class Player implements Comparable<Player> private int points; private String name; Player(String name, int points) this.name = name; this.points = points; public int compareto(player other) if (this.points < other.points) return -1; // ascending else if (this.points > other.points) return 1; else // return 0; return this.name.compareto(other.name); 11/34

12 public String tostring() return // this.getclass().getname() + "(" + this.name + ":" + this.points; // + ")"; public static void main(string[] args) Player a = new Player("Tommy", 19); Player b = new Player("Troy", 19); Player c = new Player("Trevor", 19); Player d = new Player("Shea", 21); Player e = new Player("Lane", 24); System.out.println( c ); ArrayList<Player> players = new ArrayList<Player>(); players.add(c); players.add(b); Trevor:19 players.add(a); [Trevor:19, Troy:19, Tommy:19, Shea:21, Lane:24, Jack:14] [Jack:14, Tommy:19, Trevor:19, Troy:19, Shea:21, Lane:24] players.add(d); players.add(e); players.add(new Player("Jack", 14)); System.out.println( players ); Collections.sort( players ); System.out.println( players ); 12/34

13 Collections The Java collections framework provides implementations of common data structures. To make it easy to write code that is independent of the choice of data structures, the collections framework provides a number of common interfaces. A collection is an object that serves as a repository for other objects. A collection provides services for adding, removing, and otherwise managing the elements it contains. Sometimes the elements in a collection are ordered, sometimes they are not. Collections should be abstractions, that is, they should hide unneeded details. We want to separate the interface of the structure from its underlying implementation. This helps manage complexity and makes it possible to change the implementation without changing the interface. 13/34

14 Dynamic Structures A static data structure has a fixed size. This meaning is different from the meaning of the static modifier. Arrays are static; once you define the number of elements it can hold, the size doesn t change. A dynamic data structure grows and shrinks at execution time as required by its contents. A dynamic data structure is implemented using object references as links. Recall that an object reference is a variable that stores the address of an object. A reference also can be called a pointer. References often are depicted graphically: student John Smith /34

15 Object references can be used to create links between objects. References as Links Suppose a class contains a reference to another object of the same class: class Node int info; Node next; References can be used to create a variety of linked structures, such as a linked list: 15/34

16 Intermediate nodes The objects being stored should not be concerned with the details of the data structure in which they may be stored. For example, the Student class should not have to store a link to the next Student object in the list. Instead, use a separate node class with two parts: a reference to an independent object a link to the next node in the list The internal representation becomes a linked list of nodes. Example: Magazine collection Magazine objects managed by the MagazineList class private inner class MagazineNode 16/34

17 //******************************************************************** // Magazine.java // Represents a single magazine. //******************************************************************** public class Magazine private String title; // // Sets up the new magazine with its title. // public Magazine(String newtitle) title = newtitle; // // Returns this magazine as a string. // public String tostring() return title; 17/34

18 //******************************************************************* // MagazineList.java // // Represents a collection of magazines. //******************************************************************* public class MagazineList private MagazineNode list; // // Sets up an initially empty list of magazines. // public MagazineList() list = null; 18/34

19 // // Creates a new MagazineNode object and adds it to the end of // the linked list. // public void add(magazine mag) MagazineNode node = new MagazineNode(mag); MagazineNode current; if (list == null) list = node; else current = list; while (current.next!= null) current = current.next; current.next = node; 19/34

20 // // Returns this list of magazines as a string. // public String tostring() String result = ""; MagazineNode current = list; while (current!= null) result += current.magazine + "\n"; current = current.next; return result; 20/34

21 //***************************************************************** // An inner class that represents a node in the magazine list. // The public variables are accessed by the MagazineList class. //***************************************************************** private class MagazineNode public Magazine magazine; public MagazineNode next; // // Sets up the node // public MagazineNode(Magazine mag) magazine = mag; next = null; 21/34

22 //******************************************************************* // MagazineRack.java // Driver to exercise the MagazineList collection. //******************************************************************* public class MagazineRack Output Time // Woodworking Today // Creates a MagazineList object, adds several magazines to the Communications of the ACM // list, then prints it. House and Garden // IEEE Computer public static void main(string[] args) MagazineList rack = new MagazineList(); rack.add(new Magazine("Time")); rack.add(new Magazine("Woodworking Today")); rack.add(new Magazine("Communications of the ACM")); rack.add(new Magazine("House and Garden")); rack.add(new Magazine("IEEE Computer")); System.out.println(rack); 22/34

23 A node can be inserted into a linked list with a few reference changes: Inserting a node Code that inserts newnode after the node pointed to by current. newnode.next = current.next; current.next = newnode; 23/34

24 Deleting a node A node can be removed from a linked list by changing the next pointer of the preceding node: 24/34

25 Other dynamic representations Doubly linked list: Linked list with a header node: 25/34

26 Queues A queue is a list that adds items only to the rear of the list and removes them only from the front It is a FIFO data structure: First-In, First-Out Analogy: a line of people at a bank teller s window Operations for a queue: enqueue dequeue empty - add an item to the rear of the queue - (or serve) remove an item from the front of the queue - returns true if the queue is empty 26/34

27 Stacks A stack ADT is also linear, like a list or a queue. Items are added and removed from only one end of a stack. It is therefore LIFO: Last-In, First-Out. Stack operations: push pop peek empty - add an item to the top of the stack - remove an item from the top of the stack - (or top) retrieves the top item without removing it - returns true if the stack is empty 27/34

28 GUI: mouse events Java divides event that are generated when using a mouse into two categories: mouse events mouse motion events When you click the mouse button over a Java GUI component, three events are generated: one when the mouse button is pushed down (mouse pressed) and two when it is let up (mouse released and mouse clicked). A mouse click is defined as pressing and releasing the mouse button in the same location. If you press the mouse button down, move the mouse, and then release the mouse button, a mouse clicked event is not generated. 28/34

29 GUI: mouse events Java divides event that are generated when using a mouse into two categories: mouse events mouse motion events mouse pressed mouse released mouse clicked mouse entered mouse exited the mouse button is pressed down the mouse button is released the mouse button is pressed down and released without moving the mouse in between the mouse pointer is moved onto (over) a component the mouse pointer is moved off of a component mouse moved mouse dragged the mouse is moved the mouse is moved while the mouse button is pressed down 29/34

30 GUI: mouse events Listeners for mouse events are created using the MouseListener and MouseMotionListener interfaces. A MouseEvent object is passed to the appropriate method when a mouse event occurs. For a given program, we may only care about one or two mouse events. To satisfy the implementation of a listener interface, empty methods must be provided for unused events. Example: The Dots program responds to one mouse event. It draws a green dot at the location of the mouse pointer whenever the mouse button is pressed. 30/34

31 //******************************************************************** // DotsPanel.java // // Represents the primary panel for the Dots program. //******************************************************************** import java.util.arraylist; import javax.swing.jpanel; import java.awt.*; import java.awt.event.*; public class DotsPanel extends JPanel private final int SIZE = 6; // radius of each dot private ArrayList<Point> pointlist; 31/34

32 // // Constructor: Sets up this panel to listen for mouse events. // public DotsPanel() pointlist = new ArrayList<Point>(); addmouselistener(new DotsListener()); setbackground(color.black); setpreferredsize(new Dimension(300, 200)); // // Draws all of the dots stored in the list. // public void paintcomponent(graphics page) super.paintcomponent(page); page.setcolor(color.green); for (Point spot : pointlist) page.filloval(spot.x-size, spot.y-size, SIZE*2, SIZE*2); page.drawstring("count: " + pointlist.size(), 5, 15); 32/34

33 //***************************************************************** // Listener for mouse events. //***************************************************************** private class DotsListener implements MouseListener // // Adds the current point to the list of points and redraws // the panel whenever the mouse button is pressed. // public void mousepressed(mouseevent event) pointlist.add(event.getpoint()); repaint(); // // Provide empty definitions for unused event methods. // public void mouseclicked(mouseevent event) public void mousereleased(mouseevent event) public void mouseentered(mouseevent event) public void mouseexited(mouseevent event) 33/34

34 //******************************************************************** // Dots.java // Demonstrates mouse events. //******************************************************************** import javax.swing.jframe; public class Dots // // Creates and displays the application frame. // public static void main(string[] args) JFrame frame = new JFrame("Dots"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.getcontentpane().add(new DotsPanel()); frame.pack(); frame.setvisible(true); 34/34

Introduction to Software Design

Introduction to Software Design CSI 1102 1 Abdulmotaleb El Saddik University of Ottawa School of Information Technology and Engineering (SITE) Multimedia Communications Research Laboratory (MCRLab) Distributed Collaborative Virtual Environments

More information

Final Exam Computer Programming 230 Dr. St. John Lehman College City University of New York Thursday, 20 May 2010

Final Exam Computer Programming 230 Dr. St. John Lehman College City University of New York Thursday, 20 May 2010 Final Exam Computer Programming 230 Dr. St. John Lehman College City University of New York Thursday, 20 May 2010 NAME (Printed) NAME (Signed) E-mail Exam Rules Show all your work. Your grade will be based

More information

Data Structures -- Introduction

Data Structures -- Introduction Data Structures -- Introduction Let s explore some advanced techniques for and managing information Chapter 16 focuses on: dynamic structures Abstract Data Types (ADTs) linked lists queues stacks 1 Static

More information

User interfaces and Swing

User interfaces and Swing User interfaces and Swing Overview, applets, drawing, action listening, layout managers. APIs: java.awt.*, javax.swing.*, classes names start with a J. Java Lectures 1 2 Applets public class Simple extends

More information

ITI Introduction to Computing II

ITI Introduction to Computing II ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science Interface Abstract data types Version of January 26, 2013 Abstract These lecture notes are meant

More information

Method Of Key Event Key Listener must implement three methods, keypressed(), keyreleased() & keytyped(). 1) keypressed() : will run whenever a key is

Method Of Key Event Key Listener must implement three methods, keypressed(), keyreleased() & keytyped(). 1) keypressed() : will run whenever a key is INDEX Event Handling. Key Event. Methods Of Key Event. Example Of Key Event. Mouse Event. Method Of Mouse Event. Mouse Motion Listener. Example of Mouse Event. Event Handling One of the key concept in

More information

Graphical User Interfaces 2

Graphical User Interfaces 2 Graphical User Interfaces 2 CSCI 136: Fundamentals CSCI 136: Fundamentals of Computer of Science Computer II Science Keith II Vertanen Keith Vertanen Copyright 2011 Extending JFrame Dialog boxes Overview

More information

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR STUDENT IDENTIFICATION NO MULTIMEDIA COLLEGE JALAN GURNEY KIRI 54100 KUALA LUMPUR FIFTH SEMESTER FINAL EXAMINATION, 2014/2015 SESSION PSD2023 ALGORITHM & DATA STRUCTURE DSEW-E-F-2/13 25 MAY 2015 9.00 AM

More information

G51PGP Programming Paradigms. Lecture 008 Inner classes, anonymous classes, Swing worker thread

G51PGP Programming Paradigms. Lecture 008 Inner classes, anonymous classes, Swing worker thread G51PGP Programming Paradigms Lecture 008 Inner classes, anonymous classes, Swing worker thread 1 Reminder subtype polymorphism public class TestAnimals public static void main(string[] args) Animal[] animals

More information

GUI DYNAMICS Lecture July 26 CS2110 Summer 2011

GUI DYNAMICS Lecture July 26 CS2110 Summer 2011 GUI DYNAMICS Lecture July 26 CS2110 Summer 2011 GUI Statics and GUI Dynamics 2 Statics: what s drawn on the screen Components buttons, labels, lists, sliders, menus,... Containers: components that contain

More information

UCLA PIC 20A Java Programming

UCLA PIC 20A Java Programming UCLA PIC 20A Java Programming Instructor: Ivo Dinov, Asst. Prof. In Statistics, Neurology and Program in Computing Teaching Assistant: Yon Seo Kim, PIC University of California, Los Angeles, Summer 2002

More information

Learning objectives: Enhancing Classes. CSI1102: Introduction to Software Design. More about References. The null Reference. The this reference

Learning objectives: Enhancing Classes. CSI1102: Introduction to Software Design. More about References. The null Reference. The this reference CSI1102: Introduction to Software Design Chapter 5: Enhancing Classes Learning objectives: Enhancing Classes Understand what the following entails Different object references and aliases Passing objects

More information

CIS 120 Programming Languages and Techniques. Final Exam, May 3, 2011

CIS 120 Programming Languages and Techniques. Final Exam, May 3, 2011 CIS 120 Programming Languages and Techniques Final Exam, May 3, 2011 Name: Pennkey: My signature below certifies that I have complied with the University of Pennsylvania s Code of Academic Integrity in

More information

The JFrame Class Frame Windows GRAPHICAL USER INTERFACES. Five steps to displaying a frame: 1) Construct an object of the JFrame class

The 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 information

Lists using LinkedList

Lists using LinkedList Lists using LinkedList 1 LinkedList Apart from arrays and array lists, Java provides another class for handling lists, namely LinkedList. An instance of LinkedList is called a linked list. The constructors

More information

CSI Introduction to Software Design. Prof. Dr.-Ing. Abdulmotaleb El Saddik University of Ottawa (SITE 5-037) (613) x 6277

CSI Introduction to Software Design. Prof. Dr.-Ing. Abdulmotaleb El Saddik University of Ottawa (SITE 5-037) (613) x 6277 CSI 1102 Introduction to Software Design Prof. Dr.-Ing. Abdulmotaleb El Saddik University of Ottawa (SITE 5-037) (613) 562-5800 x 6277 elsaddik @ site.uottawa.ca abed @ mcrlab.uottawa.ca http://www.site.uottawa.ca/~elsaddik/

More information

CS24 Week 4 Lecture 2

CS24 Week 4 Lecture 2 CS24 Week 4 Lecture 2 Kyle Dewey Overview Linked Lists Stacks Queues Linked Lists Linked Lists Idea: have each chunk (called a node) keep track of both a list element and another chunk Need to keep track

More information

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Events and Listeners CSC 1051 Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: www.csc.villanova.edu/~map/1051/ Some slides

More information

STACKS AND QUEUES. Problem Solving with Computers-II

STACKS AND QUEUES. Problem Solving with Computers-II STACKS AND QUEUES Problem Solving with Computers-II 2 Stacks container class available in the C++ STL Container class that uses the Last In First Out (LIFO) principle Methods i. push() ii. iii. iv. pop()

More information

GUI (Graphic User Interface) Programming. Part 2 (Chapter 8) Chapter Goals. Events, Event Sources, and Event Listeners. Listeners

GUI (Graphic User Interface) Programming. Part 2 (Chapter 8) Chapter Goals. Events, Event Sources, and Event Listeners. Listeners GUI (Graphic User Interface) Programming Part 2 (Chapter 8) Chapter Goals To understand the Java event model To install action and mouse event listeners To accept input from buttons, text fields, and the

More information

Java Foundations John Lewis Peter DePasquale Joe Chase Third Edition

Java Foundations John Lewis Peter DePasquale Joe Chase Third Edition Java Foundations John Lewis Peter DePasquale Joe Chase Third Edition Pearson Education Limited Edinburgh Gate Harlow Essex CM20 2JE England and Associated Companies throughout the world Visit us on the

More information

Queues Fall 2018 Margaret Reid-Miller

Queues Fall 2018 Margaret Reid-Miller Queues 15-121 Fall 2018 Margaret Reid-Miller Today Exam 2 is next Tuesday, October 30 Writing methods various classes that implement Lists. Methods using Lists and Big-O w/ ArrayList or LinkedLists Prove

More information

Outline. Topic 9: Swing. GUIs Up to now: line-by-line programs: computer displays text user types text AWT. A. Basics

Outline. Topic 9: Swing. GUIs Up to now: line-by-line programs: computer displays text user types text AWT. A. Basics Topic 9: Swing Outline Swing = Java's GUI library Swing is a BIG library Goal: cover basics give you concepts & tools for learning more Assignment 7: Expand moving shapes from Assignment 4 into game. "Programming

More information

CS/ENGRD 2110 SPRING 2018

CS/ENGRD 2110 SPRING 2018 CS/ENGRD 2110 SPRING 2018 Lecture 7: Interfaces and http://courses.cs.cornell.edu/cs2110 1 2 St Valentine s Day! It's Valentines Day, and so fine! Good wishes to you I consign.* But since you're my students,

More information

Chapter 5: Enhancing Classes

Chapter 5: Enhancing Classes Chapter 5: Enhancing Classes Presentation slides for Java Software Solutions for AP* Computer Science 3rd Edition by John Lewis, William Loftus, and Cara Cocking Java Software Solutions is published by

More information

CSIS 10A Practice Final Exam Solutions

CSIS 10A Practice Final Exam Solutions CSIS 10A Practice Final Exam Solutions 1) (5 points) What would be the output when the following code block executes? int a=3, b=8, c=2; if (a < b && b < c) b = b + 2; if ( b > 5 a < 3) a = a 1; if ( c!=

More information

Interfaces & Polymorphism part 2: Collections, Comparators, and More fun with Java graphics

Interfaces & Polymorphism part 2: Collections, Comparators, and More fun with Java graphics Interfaces & Polymorphism part 2: Collections, Comparators, and More fun with Java graphics 1 Collections (from the Java tutorial)* A collection (sometimes called a container) is simply an object that

More information

H212 Introduction to Software Systems Honors

H212 Introduction to Software Systems Honors Introduction to Software Systems Honors Lecture #19: November 4, 2015 1/14 Third Exam The third, Checkpoint Exam, will be on: Wednesday, November 11, 2:30 to 3:45 pm You will have 3 questions, out of 9,

More information

References. Chapter 5: Enhancing Classes. Enhancing Classes. The null Reference. Java Software Solutions for AP* Computer Science A 2nd Edition

References. Chapter 5: Enhancing Classes. Enhancing Classes. The null Reference. Java Software Solutions for AP* Computer Science A 2nd Edition Chapter 5: Enhancing Classes Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis, William Loftus, and Cara Cocking Java Software Solutions is published

More information

&KDSWHU(QKDQFLQJ&ODVVHV

&KDSWHU(QKDQFLQJ&ODVVHV &KDSWHU(QKDQFLQJ&ODVVHV Presentation slides for Java Software Solutions Foundations of Program Design Second Edition by John Lewis and William Loftus Java Software Solutions is published by Addison-Wesley

More information

Graphical User Interfaces 2

Graphical User Interfaces 2 Graphical User Interfaces 2 CSCI 136: Fundamentals of Computer Science II Keith Vertanen Copyright 2014 2011 Extending JFrame Dialog boxes Overview Ge

More information

Dartmouth College Computer Science 10, Winter 2012 Solution to Midterm Exam

Dartmouth College Computer Science 10, Winter 2012 Solution to Midterm Exam Dartmouth College Computer Science 10, Winter 2012 Solution to Midterm Exam Thursday, February 9, 2012 Professor Drysdale Print your name: If you need more space to answer a question than we give you,

More information

Overview. ITI Introduction to Computing II. Interface 1. Problem 1. Problem 1: Array sorting. Problem 1: Array sorting. Problem 1: Array sorting

Overview. ITI Introduction to Computing II. Interface 1. Problem 1. Problem 1: Array sorting. Problem 1: Array sorting. Problem 1: Array sorting Overview ITI 1121. Introduction to Computing II Rafael Falcon and Marcel Turcotte (with contributions from R. Holte) Electrical Engineering and Computer Science University of Ottawa Interface Abstract

More information

1 P age DS & OOPS / UNIT II

1 P age DS & OOPS / UNIT II UNIT II Stacks: Definition operations - applications of stack. Queues: Definition - operations Priority queues - De que Applications of queue. Linked List: Singly Linked List, Doubly Linked List, Circular

More information

CMPT 126: Lecture 11 Collections: Abstract Data Types and Dynamic Representation (Chapt. 12)

CMPT 126: Lecture 11 Collections: Abstract Data Types and Dynamic Representation (Chapt. 12) CMPT 126: Lecture 11 Collections: Abstract Data Types and Dynamic Representation (Chapt. 12) Tamara Smyth, tamaras@cs.sfu.ca School of Computing Science, Simon Fraser University October 30, 2007 1 Collections

More information

CSCI 136 Data Structures & Advanced Programming. Lecture 12 Fall 2018 Profs Bill & Jon

CSCI 136 Data Structures & Advanced Programming. Lecture 12 Fall 2018 Profs Bill & Jon CSCI 136 Data Structures & Advanced Programming Lecture 12 Fall 2018 Profs Bill & Jon Last Time Assertions SLL Improvements Tail pointers Circularly Linked Lists Doubly Linked Lists Practice with recursion

More information

Generics. IRS W-9 Form

Generics. IRS W-9 Form Generics IRS W-9 Form Generics Generic class and methods. BNF notation Syntax Non-parametrized class: < class declaration > ::= "class" < identifier > ["extends" < type >] ["implements" < type list >]

More information

CSIS 10A PRACTICE FINAL EXAM Name Closed Book Closed Computer 3 Sheets of Notes Allowed

CSIS 10A PRACTICE FINAL EXAM Name Closed Book Closed Computer 3 Sheets of Notes Allowed CSIS 10A PRACTICE FINAL EXAM Name Closed Book Closed Computer 3 Sheets of Notes Allowed MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question. 1) What would

More information

1. Stack Implementation Using 1D Array

1. Stack Implementation Using 1D Array Lecture 5 Stacks 1 Lecture Content 1. Stack Implementation Using 1D Array 2. Stack Implementation Using Singly Linked List 3. Applications of Stack 3.1 Infix and Postfix Arithmetic Expressions 3.2 Evaluate

More information

Computer Science 62. Bruce/Mawhorter Fall 16. Midterm Examination. October 5, Question Points Score TOTAL 52 SOLUTIONS. Your name (Please print)

Computer Science 62. Bruce/Mawhorter Fall 16. Midterm Examination. October 5, Question Points Score TOTAL 52 SOLUTIONS. Your name (Please print) Computer Science 62 Bruce/Mawhorter Fall 16 Midterm Examination October 5, 2016 Question Points Score 1 15 2 10 3 10 4 8 5 9 TOTAL 52 SOLUTIONS Your name (Please print) 1. Suppose you are given a singly-linked

More information

Graphical User Interfaces 2

Graphical User Interfaces 2 Graphical User Interfaces 2 CSCI 136: Fundamentals of Computer Science II Keith Vertanen Copyright 2011 Extending JFrame Dialog boxes Ge?ng user input Overview Displaying message or error Listening for

More information

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question. CSIS 10A PRACTICE FINAL EXAM SOLUTIONS Closed Book Closed Computer 3 Sheets of Notes Allowed MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question. 1) What

More information

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques (CIS120) Lecture 35 April 15, 2013 Swing III: OO Design, Mouse InteracGon Announcements HW10: Game Project is out, due Tuesday, April 23 rd at midnight If you want

More information

COMP 250. Lecture 29. interfaces. Nov. 18, 2016

COMP 250. Lecture 29. interfaces. Nov. 18, 2016 COMP 250 Lecture 29 interfaces Nov. 18, 2016 1 ADT (abstract data type) ADT s specify a set of operations, and allow us to ignore implementation details. Examples: list stack queue binary search tree priority

More information

CS 120 Fall 2008 Practice Final Exam v1.0m. Name: Model Solution. True/False Section, 20 points: 10 true/false, 2 points each

CS 120 Fall 2008 Practice Final Exam v1.0m. Name: Model Solution. True/False Section, 20 points: 10 true/false, 2 points each CS 120 Fall 2008 Practice Final Exam v1.0m Name: Model Solution True/False Section, 20 points: 10 true/false, 2 points each Multiple Choice Section, 32 points: 8 multiple choice, 4 points each Code Tracing

More information

CMSC 341. Nilanjan Banerjee

CMSC 341. Nilanjan Banerjee CMSC 341 Nilanjan Banerjee http://www.csee.umbc.edu/~nilanb/teaching/341/ Announcements Just when you thought Shawn was going to teach this course! On a serious note: register on Piazza I like my classes

More information

Linked List. April 2, 2007 Programming and Data Structure 1

Linked List. April 2, 2007 Programming and Data Structure 1 Linked List April 2, 2007 Programming and Data Structure 1 Introduction head A linked list is a data structure which can change during execution. Successive elements are connected by pointers. Last element

More information

Computer Science 210: Data Structures. Intro to Java Graphics

Computer Science 210: Data Structures. Intro to Java Graphics Computer Science 210: Data Structures Intro to Java Graphics Summary Today GUIs in Java using Swing in-class: a Scribbler program READING: browse Java online Docs, Swing tutorials GUIs in Java Java comes

More information

Recursion is a fundamental programming technique that can provide an elegant solution certain kinds of problems. COMP Week 8 & 9 1

Recursion is a fundamental programming technique that can provide an elegant solution certain kinds of problems. COMP Week 8 & 9 1 Recursion is a fundamental programming technique that can provide an elegant solution certain kinds of problems COMP 202 - Week 8 & 9 1 A recursive definition is one which uses the word or concept being

More information

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written. QUEEN'S UNIVERSITY SCHOOL OF COMPUTING HAND IN Answers Are Recorded on Question Paper CISC124, WINTER TERM, 2012 FINAL EXAMINATION 9am to 12pm, 26 APRIL 2012 Instructor: Alan McLeod If the instructor is

More information

CSSE 220. Event Based Programming. Check out EventBasedProgramming from SVN

CSSE 220. Event Based Programming. Check out EventBasedProgramming from SVN CSSE 220 Event Based Programming Check out EventBasedProgramming from SVN Interfaces are contracts Interfaces - Review Any class that implements an interface MUST provide an implementation for all methods

More information

class BankFilter implements Filter { public boolean accept(object x) { BankAccount ba = (BankAccount) x; return ba.getbalance() > 1000; } }

class BankFilter implements Filter { public boolean accept(object x) { BankAccount ba = (BankAccount) x; return ba.getbalance() > 1000; } } 9.12) public interface Filter boolean accept(object x); Describes any class whose objects can measure other objects. public interface Measurer double measure(object anobject); This program tests the use

More information

COSC 123 Computer Creativity. Java Lists and Arrays. Dr. Ramon Lawrence University of British Columbia Okanagan

COSC 123 Computer Creativity. Java Lists and Arrays. Dr. Ramon Lawrence University of British Columbia Okanagan COSC 123 Computer Creativity Java Lists and Arrays Dr. Ramon Lawrence University of British Columbia Okanagan ramon.lawrence@ubc.ca Objectives 1) Create and use arrays of base types and objects. 2) Create

More information

Lecture 5: Java Graphics

Lecture 5: Java Graphics Lecture 5: Java Graphics CS 62 Spring 2019 William Devanny & Alexandra Papoutsaki 1 New Unit Overview Graphical User Interfaces (GUI) Components, e.g., JButton, JTextField, JSlider, JChooser, Containers,

More information

Final Examination Semester 2 / Year 2010

Final Examination Semester 2 / Year 2010 Southern College Kolej Selatan 南方学院 Final Examination Semester 2 / Year 2010 COURSE : JAVA PROGRAMMING COURSE CODE : PROG1114 TIME : 2 1/2 HOURS DEPARTMENT : COMPUTER SCIENCE LECTURER : LIM PEI GEOK Student

More information

Cosc 241 Programming and Problem Solving Lecture 9 (26/3/18) Collections and ADTs

Cosc 241 Programming and Problem Solving Lecture 9 (26/3/18) Collections and ADTs 1 Cosc 241 Programming and Problem Solving Lecture 9 (26/3/18) Collections and ADTs Michael Albert michael.albert@cs.otago.ac.nz Keywords: abstract data type, collection, generic class type, stack 2 Collections

More information

cs Java: lecture #6

cs 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 information

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

COMP 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 - Week 7 1 Static member variables So far: Member variables

More information

GUI in Java TalentHome Solutions

GUI in Java TalentHome Solutions GUI in Java TalentHome Solutions AWT Stands for Abstract Window Toolkit API to develop GUI in java Has some predefined components Platform Dependent Heavy weight To use AWT, import java.awt.* Calculator

More information

Dartmouth College Computer Science 10, Fall 2015 Midterm Exam

Dartmouth College Computer Science 10, Fall 2015 Midterm Exam Dartmouth College Computer Science 10, Fall 2015 Midterm Exam 6.00-9.00pm, Monday, October 19, 2015 105 Dartmouth Hall Professor Prasad Jayanti Print your name: Print your section leader name: If you need

More information

DATA STRUCTURE UNIT I

DATA STRUCTURE UNIT I DATA STRUCTURE UNIT I 1. What is Data Structure? A data structure is a mathematical or logical way of organizing data in the memory that consider not only the items stored but also the relationship to

More information

LIFO : Last In First Out

LIFO : Last In First Out Introduction Stack is an ordered list in which all insertions and deletions are made at one end, called the top. Stack is a data structure that is particularly useful in applications involving reversing.

More information

[ DATA STRUCTURES] to Data Structures

[ DATA STRUCTURES] to Data Structures [ DATA STRUCTURES] Chapter - 01 : Introduction to Data Structures INTRODUCTION TO DATA STRUCTURES A Data type refers to a named group of data which share similar properties or characteristics and which

More information

CS171 Midterm Exam. October 29, Name:

CS171 Midterm Exam. October 29, Name: CS171 Midterm Exam October 29, 2012 Name: You are to honor the Emory Honor Code. This is a closed-book and closed-notes exam. You have 50 minutes to complete this exam. Read each problem carefully, and

More information

Chapter. We've been using predefined classes. Now we will learn to write our own classes to define objects

Chapter. We've been using predefined classes. Now we will learn to write our own classes to define objects Writing Classes 4 Chapter 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design 2007 Pearson Addison-Wesley. All rights reserved Writing Classes We've been using predefined

More information

Lab & Assignment 1. Lecture 3: ArrayList & Standard Java Graphics. Random Number Generator. Read Lab & Assignment Before Lab Wednesday!

Lab & Assignment 1. Lecture 3: ArrayList & Standard Java Graphics. Random Number Generator. Read Lab & Assignment Before Lab Wednesday! Lab & Assignment 1 Lecture 3: ArrayList & Standard Java Graphics CS 62 Fall 2015 Kim Bruce & Michael Bannister Strip with 12 squares & 5 silver dollars placed randomly on the board. Move silver dollars

More information

.:: UNIT 4 ::. STACK AND QUEUE

.:: UNIT 4 ::. STACK AND QUEUE .:: UNIT 4 ::. STACK AND QUEUE 4.1 A stack is a data structure that supports: Push(x) Insert x to the top element in stack Pop Remove the top item from stack A stack is collection of data item arrange

More information

Chapter 6 Introduction to Defining Classes

Chapter 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 information

Lists. CSC212 Lecture 8 D. Thiebaut, Fall 2014

Lists. CSC212 Lecture 8 D. Thiebaut, Fall 2014 Lists CSC212 Lecture 8 D. Thiebaut, Fall 2014 Review List = Organization of Data in a Linear Fashion, where Order is Important Set of actions that can be carried out efficiently on the data. Typical Actions

More information

11/19/2014. Inheritance. Chapter 7: Inheritance. Inheritance. Inheritance. Java Software Solutions for AP* Computer Science A 2nd Edition

11/19/2014. Inheritance. Chapter 7: Inheritance. Inheritance. Inheritance. Java Software Solutions for AP* Computer Science A 2nd Edition Chapter 7: Inheritance Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis, William Loftus, and Cara Cocking Inheritance Inheritance allows a software developer

More information

CMSC Introduction to Algorithms Spring 2012 Lecture 7

CMSC Introduction to Algorithms Spring 2012 Lecture 7 CMSC 351 - Introduction to Algorithms Spring 2012 Lecture 7 Instructor: MohammadTaghi Hajiaghayi Scribe: Rajesh Chitnis 1 Introduction In this lecture we give an introduction to Data Structures like arrays,

More information

MIDTERM EXAMINATION Spring 2010 CS301- Data Structures

MIDTERM EXAMINATION Spring 2010 CS301- Data Structures MIDTERM EXAMINATION Spring 2010 CS301- Data Structures Question No: 1 Which one of the following statement is NOT correct. In linked list the elements are necessarily to be contiguous In linked list the

More information

Overview. Lecture 7: Inheritance and GUIs. Inheritance. Example 9/30/2008

Overview. Lecture 7: Inheritance and GUIs. Inheritance. Example 9/30/2008 Overview Lecture 7: Inheritance and GUIs Written by: Daniel Dalevi Inheritance Subclasses and superclasses Java keywords Interfaces and inheritance The JComponent class Casting The cosmic superclass Object

More information

Lecture 3: Java Graphics & Events

Lecture 3: Java Graphics & Events Lecture 3: Java Graphics & Events CS 62 Fall 2017 Kim Bruce & Alexandra Papoutsaki Text Input Scanner class Constructor: myscanner = new Scanner(System.in); can use file instead of System.in new Scanner(new

More information

CMP Points Total Midterm Spring Version (16 Points) Multiple Choice:

CMP Points Total Midterm Spring Version (16 Points) Multiple Choice: CMP-338 106 Points Total Midterm Spring 2017 Version 1 Instructions Write your name and version number on the top of the yellow paper. Answer all questions on the yellow paper. One question per page. Use

More information

CSE 2123: Collections: Priority Queues. Jeremy Morris

CSE 2123: Collections: Priority Queues. Jeremy Morris CSE 2123: Collections: Priority Queues Jeremy Morris 1 Collections Priority Queue Recall: A queue is a specific type of collection Keeps elements in a particular order We ve seen two examples FIFO queues

More information

COMP 213 Advanced Object-oriented Programming Lecture 8 The Queue ADT (cont.)

COMP 213 Advanced Object-oriented Programming Lecture 8 The Queue ADT (cont.) COMP 213 Advanced Object-oriented Programming Lecture 8 The Queue ADT (cont.) Recall: The Queue ADT A data structure in which elements enter at one end and are removed from the opposite end is called a

More information

Test 1: CPS 100. Owen Astrachan. October 1, 2004

Test 1: CPS 100. Owen Astrachan. October 1, 2004 Test 1: CPS 100 Owen Astrachan October 1, 2004 Name: Login: Honor code acknowledgment (signature) Problem 1 value 20 pts. grade Problem 2 30 pts. Problem 3 20 pts. Problem 4 20 pts. TOTAL: 90 pts. This

More information

Lecture 6. COMP1006/1406 (the OOP course) Summer M. Jason Hinek Carleton University

Lecture 6. COMP1006/1406 (the OOP course) Summer M. Jason Hinek Carleton University Lecture 6 COMP1006/1406 (the OOP course) Summer 2014 M. Jason Hinek Carleton University today s agenda assignments A1,A2,A3 are all marked A4 marking just started A5 is due Friday, A6 is due Monday a quick

More information

12 Abstract Data Types

12 Abstract Data Types 12 Abstract Data Types 12.1 Foundations of Computer Science Cengage Learning Objectives After studying this chapter, the student should be able to: Define the concept of an abstract data type (ADT). Define

More information

Stacks Fall 2018 Margaret Reid-Miller

Stacks Fall 2018 Margaret Reid-Miller Stacks 15-121 Fall 2018 Margaret Reid-Miller Today Exam 2 is next Tuesday, October 30 Today: Quiz 5 solutions Recursive add from last week (see SinglyLinkedListR.java) Stacks ADT (Queues on Thursday) ArrayStack

More information

AP CS Unit 12: Drawing and Mouse Events

AP CS Unit 12: Drawing and Mouse Events AP CS Unit 12: Drawing and Mouse Events A JPanel object can be used as a container for other objects. It can also be used as an object that we can draw on. The first example demonstrates how to do that.

More information

2IS45 Programming

2IS45 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 information

Stacks. stacks of dishes or trays in a cafeteria. Last In First Out discipline (LIFO)

Stacks. stacks of dishes or trays in a cafeteria. Last In First Out discipline (LIFO) Outline stacks stack ADT method signatures array stack implementation linked stack implementation stack applications infix, prefix, and postfix expressions 1 Stacks stacks of dishes or trays in a cafeteria

More information

CSCI 136 Written Exam #0 Fundamentals of Computer Science II Spring 2015

CSCI 136 Written Exam #0 Fundamentals of Computer Science II Spring 2015 CSCI 136 Written Exam #0 Fundamentals of Computer Science II Spring 2015 Name: This exam consists of 6 problems on the following 7 pages. You may use your single-sided handwritten 8 ½ x 11 note sheet during

More information

Chapter 1 GUI Applications

Chapter 1 GUI Applications Chapter 1 GUI Applications 1. GUI Applications So far we've seen GUI programs only in the context of Applets. But we can have GUI applications too. A GUI application will not have any of the security limitations

More information

ITI Introduction to Computing II

ITI Introduction to Computing II ITI 1121. Introduction to Computing II Queues ArrayQueue Marcel Turcotte School of Electrical Engineering and Computer Science Version of March 10, 2014 Abstract These lecture notes are meant to be looked

More information

CMSC 132: Object-Oriented Programming II. Interface

CMSC 132: Object-Oriented Programming II. Interface CMSC 132: Object-Oriented Programming II Interface 1 Java Interfaces An interface defines a protocol of behavior that can be implemented by any class anywhere in the class hierarchy. Interfaces are Java's

More information

CSE 143 SAMPLE MIDTERM

CSE 143 SAMPLE MIDTERM CSE 143 SAMPLE MIDTERM 1. (5 points) In some methods, you wrote code to check if a certain precondition was held. If the precondition did not hold, then you threw an exception. This leads to robust code

More information

CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013

CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013 CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013 Name: This exam consists of 6 problems on the following 6 pages. You may use your two-sided hand-written 8 ½ x 11 note sheet during the exam.

More information

Advanced Java Concepts Unit 3: Stacks and Queues

Advanced Java Concepts Unit 3: Stacks and Queues Advanced Java Concepts Unit 3: Stacks and Queues Stacks are linear collections in which access is completely restricted to just one end, called the top. Stacks adhere to a last-in, first-out protocol (LIFO).

More information

CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013

CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013 CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013 Name: This exam consists of 6 problems on the following 6 pages. You may use your two-sided hand-written 8 ½ x 11 note sheet during the exam.

More information

Control Flow: Overview CSE3461. An Example of Sequential Control. Control Flow: Revisited. Control Flow Paradigms: Reacting to the User

Control Flow: Overview CSE3461. An Example of Sequential Control. Control Flow: Revisited. Control Flow Paradigms: Reacting to the User CSE3461 Control Flow Paradigms: Reacting to the User Control Flow: Overview Definition of control flow: The sequence of execution of instructions in a program. Control flow is determined at run time by

More information

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University Day 4 COMP1006/1406 Summer 2016 M. Jason Hinek Carleton University today s agenda assignments questions about assignment 2 a quick look back constructors signatures and overloading encapsulation / information

More information

G51PRG: Introduction to Programming Second semester

G51PRG: Introduction to Programming Second semester G51PRG: Introduction to Programming Second semester Lecture 7 Natasha Alechina School of Computer Science & IT nza@cs.nott.ac.uk Previous lecture es interfaces collections hierarchy in Java Lecture 7:

More information

Standard ADTs. Lecture 19 CS2110 Summer 2009

Standard ADTs. Lecture 19 CS2110 Summer 2009 Standard ADTs Lecture 19 CS2110 Summer 2009 Past Java Collections Framework How to use a few interfaces and implementations of abstract data types: Collection List Set Iterator Comparable Comparator 2

More information

CS2012 Programming Techniques II

CS2012 Programming Techniques II 27 January 14 Lecture 6 (continuing from 5) CS2012 Programming Techniques II Vasileios Koutavas 1 27 January 14 Lecture 6 (continuing from 5) 2 Previous Lecture Amortized running time cost of algorithms

More information

ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java ECE 122 Engineering Problem Solving with Java Lecture 21 Interfaces and Abstract Classes Overview Problem: Can we make inheritance flexible? Abstract methods Define methods that will be filled in by children

More information

Comparing Objects 3/14/16

Comparing Objects 3/14/16 Comparing Objects 3/14/16 Agenda HW Notes Practice Tests Returned Searching Data One of the most important and useful things that computers do is search through huge amounts of data. Search Engines Patterns

More information

EXAMINATIONS 2011 Trimester 2, MID-TERM TEST. COMP103 Introduction to Data Structures and Algorithms SOLUTIONS

EXAMINATIONS 2011 Trimester 2, MID-TERM TEST. COMP103 Introduction to Data Structures and Algorithms SOLUTIONS T E W H A R E W Ā N A N G A O T E Ū P O K O O T E I K A A M Ā U I VUW V I C T O R I A UNIVERSITY OF WELLINGTON Student ID:....................... EXAMINATIONS 2011 Trimester 2, MID-TERM TEST COMP103 Introduction

More information