CSIS 10B Lab 2 Bags and Stacks

Similar documents
Stacks. Chapter 5. Copyright 2012 by Pearson Education, Inc. All rights reserved

Bags. Chapter 1. Copyright 2012 by Pearson Education, Inc. All rights reserved

CMPSCI 187: Programming With Data Structures. Lecture 12: Implementing Stacks With Linked Lists 5 October 2011

CSCI 200 Lab 4 Evaluating infix arithmetic expressions

Stacks. Chapter 5. Copyright 2012 by Pearson Education, Inc. All rights reserved

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

CMPSCI 187: Programming With Data Structures. Lecture #11: Implementing Stacks With Arrays David Mix Barrington 28 September 2012

Stack and Its Implementation

CS/ENGRD 2110 SPRING Lecture 7: Interfaces and Abstract Classes

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

Data Abstraction and Specification of ADTs

CS121/IS223. Object Reference Variables. Dr Olly Gotel

Problem 1: Building and testing your own linked indexed list

CS/ENGRD 2110 SPRING 2018

CSCI 200 Lab 1 Implementing and Testing Simple ADTs in Java

Introduction to Computer Science II (ITI 1121) Midterm Examination

Collections Chapter 12. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

STACKS. A stack is defined in terms of its behavior. The common operations associated with a stack are as follows:

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014

CS 211 Programming Practicum Spring 2017

COMP 250 Winter stacks Feb. 2, 2016

CMSC131. Inheritance. Object. When we talked about Object, I mentioned that all Java classes are "built" on top of that.

Programming, Data Structures and Algorithms Prof. Hema A Murthy Department of Computer Science and Engineering Indian Institute of Technology, Madras

ITI Introduction to Computing II

Project 1: Implementation of the Stack ADT and Its Application

CSE 143 SAMPLE MIDTERM

CSCD 326 Data Structures I Stacks

CSC 1052 Algorithms & Data Structures II: Stacks

Linked Bag and List Implementations

CS 211 Programming Practicum Fall 2018

Computer Science E-119 Fall Problem Set 3. Due before lecture on Wednesday, October 31

CMPS 390 Data Structures

Inheritance, Polymorphism, and Interfaces

CMPSCI 187 / Spring 2015 Postfix Expression Evaluator

CS/ENGRD 2110 FALL Lecture 7: Interfaces and Abstract Classes

Array based Bag and List Implementations

CMPSCI 187 / Spring 2015 Hanoi

CSCI 204 Introduction to Computer Science II. Lab 6: Stack ADT

Lesson 10B Class Design. By John B. Owen All rights reserved 2011, revised 2014

Stacks Fall 2018 Margaret Reid-Miller

Stack ADT. ! push(x) puts the element x on top of the stack! pop removes the topmost element from the stack.

Programming Exercise 14: Inheritance and Polymorphism

Project #1 rev 2 Computer Science 2334 Fall 2013 This project is individual work. Each student must complete this assignment independently.

Software Design and Analysis for Engineers

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

COMP250: Stacks. Jérôme Waldispühl School of Computer Science McGill University. Based on slides from (Goodrich & Tamassia, 2004)

Postfix Notation is a notation in which the operator follows its operands in the expression (e.g ).

Stacks and Queues

CSE Data Structures and Algorithms... In Java! Stacks. CSE2100 DS & Algorithms 1

Object-Oriented Design Lecture 3 CSU 370 Fall 2007 (Pucella) Friday, Sep 14, 2007

CS 151. Linked Lists, Recursively Implemented. Wednesday, October 3, 12

CS 211 Programming Practicum Spring 2018

1. Stack Implementation Using 1D Array

University of Waterloo Department of Electrical and Computer Engineering ECE 250 Algorithms and Data Structures

Stacks. Access to other items in the stack is not allowed A LIFO (Last In First Out) data structure

Introduction to Computing II (ITI 1121) Final Examination

Tail Calls. CMSC 330: Organization of Programming Languages. Tail Recursion. Tail Recursion (cont d) Names and Binding. Tail Recursion (cont d)

Topic 6: Exceptions. Exceptions are a Java mechanism for dealing with errors & unusual situations

Announcements. Final exam. Course evaluations. No classes next week. Saturday Dec 15 10:20 am -- 12:20 pm Room: TBA. Wednesday November 28th

Object-Oriented Design Lecture 11 CS 3500 Spring 2010 (Pucella) Tuesday, Feb 16, 2010

CS112 Lecture: Defining Classes. 1. To describe the process of defining an instantiable class

EXAMINATIONS 2005 END-YEAR. COMP 103 Introduction to Data Structures and Algorithms

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

CMPSCI 187 / Spring 2015 Sorting Kata

What can we do with Coin dispenser?

CE204 Data Structures and Algorithms Part 2

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

Stacks. Revised based on textbook author s notes.

Inheritance. Transitivity

Data Structure (CS301)

CSE143 Exam with answers Problem numbering may differ from the test as given. Midterm #2 February 16, 2001

COMP 250. Lecture 8. stack. Sept. 25, 2017

Computer Science CS221 Test 2 Name. 1. Give a definition of the following terms, and include a brief example. a) Big Oh

Data Structures And Algorithms

CmpSci 187: Programming with Data Structures Spring 2015

Basic Data Structures

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

Due: 9 February 2017 at 1159pm (2359, Pacific Standard Time)

COMP 103 Introduction to Data Structures and Algorithms

Assignment 5. Introduction

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

Problem with Scanning an Infix Expression

Introduction to Computer Science II (ITI 1121) Midterm Examination

Computer Science E-119 Fall Problem Set 1. Due before lecture on Wednesday, September 26

Slide Set 1. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

! Mon, May 5, 2:00PM to 4:30PM. ! Closed book, closed notes, clean desk. ! Comprehensive (covers entire course) ! 30% of your final grade

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

Computer Science 2 Lecture 4 Inheritance: Trinidad Fruit Stand 02/15/2014 Revision : 1.7

Algorithms & Datastructures Laboratory Exercise Sheet 1

+ Abstract Data Types

Basic Data Structures 1 / 24

ADT Stack. Inserting and deleting elements occurs at the top of Stack S. top. bottom. Stack S

CPSC 221: Algorithms and Data Structures Lecture #1: Stacks and Queues

CS350: Data Structures Stacks

Inf1-OP. Inf1-OP Exam Review. Timothy Hospedales, adapting earlier version by Perdita Stevens and Ewan Klein. March 20, School of Informatics

09/02/2013 TYPE CHECKING AND CASTING. Lecture 5 CS2110 Spring 2013

1.00/ Introduction to Computers and Engineering Problem Solving. Final / December 13, 2004

CSC 273 Data Structures

Transcription:

CSIS 10B Lab 2 Bags and Stacks Part A Bags and Inheritance In this part of the lab we will be exploring the use of the Bag ADT to manage quantities of data of a certain generic type (listed as T in the UML diagram below). We will also get to work with two classes that are related through inheritance, and two classes that are related through composition (adapter classes) The Bag class was discussed in lecture. Here is the UML diagram for all the methods you can use in the Bag class: You will want to keep this diagram handy as you work on these exercises. Demo 1 Defining a Bag object and why generic type specifiers are good We are given some code that compiles but gives us a warning: Lab2.java uses unchecked or unsafe operations. This warning is telling us something about how we are using the Bag object b. The Bag class is designed to take a specifier for the type of information your Bag object will contain. If you don t specify a type, Java still compiles the program, but it then lets you add any class of object you like, from String to Double to Person objects! Try the code and see how it runs.

This is considered unsafe because it prevents the compiler from checking your code for incorrect use of your Bag object. In general we want our Bags to contain objects of only one type. If you want to allow anything to be added, and prevent the warning, you could declare a Bag<Object>, but this is not recommended. Now fix the program by specifying b is a Bag<String> both in front of the variable declaration b and after the new operator. Now when you compile notice the error message alerting you of a type mismatch error. Now modify turn all the items added into String and run the program again. It should run without error. Problem 1 Working with the Bag class Now work with your partner to complete problem 1, trying out all the methods asked for in each part, and printing the result. In most cases you can type System.out.println( "some message " + mybag.methodtocheck() ); where methodtocheck() is the method you are trying to verify, such as contains(). Problem 2 Using a bag to represent a Club of Persons a) Now you can use the generic specifier <Person> to create a Bag of Person objects called CS_club, and add three persons to the Bag. b) In order to be able to see if the club contains a certain Person, such as Joe Frank, you would use the statement CS_club.contains(new Person("Joe Frank")); First, try printing out whether the club contains Joe Frank. You should get the value false. Which is not correct! To understand why, it helps to realize that the contains method needs to traverse the bag s internal array to see if the argument (such as Person("Joe Frank")) EQUALS one of the objects stored in the array, and return true if a match is found. But If we don t properly define an equals method for the Person class, the contains method won t work for a Bag of Person objects. c) To get the correct result for b), please define an equals method for the Person class using the pattern we often used in CSIS10A, as shown for the Item class in the lecture slides. Try running b) again and verify that the CS_club s contains method now shows Joe Frank as being in the club. d) Wait, it STILL doesn t work? OK, now implement the equals method so it takes a parameter of type Object, as shown for the Item class in the slide after the CSIS10A version. NOW you should finally get the proper behavior when you check to see if the CS_club contains Joe Frank!

Problem 3 Using inheritance to derive a Student class from the Person class We can derive a Student class from Person because a Student IS A Person. Fill in the parts of the class indicated with *** comments. Then test the Student class by activating the driver found in the main method at the bottom of the Student class file. Add a tostring method that invokes tostring from the super class and appends to it the ID number. Add an equals method for the Student class, using the equals method for the super class and also verifying the ID fields are the same. Verify your methods work by running the code already given in Problem 3. Then try to add a couple Student objects to your club of Persons. Does it work? It should! Inheritence lets you add a Student to a Club of Persons since a Student IS A Person. (however if the club was defined to only work with Student, a Person could not join the club because a Person is NOT (always) a Student Problem 4 Making a Club adapter class (with a Bag inside to hold data) Perhaps it seems clumsy to create a Bag<Person> CS_club. After all, a club is NOT a Bag!! Suppose we wanted to make a Club class using a Bag to hold all of the members of the club. Such a class would be considered an Adapter class (it uses Composition) and would start out like the following: public class Club { private Bag<Person> members; // the set of club members... Here is a UML diagram of how the Club class might look:

The constructor would simply assign members = new Bag<Person>() ; The other methods would essentially Pass the buck to the members Bag. In other words, public boolean isfull() { return members.isfull(); } Create a new Club class now as an adapter for Bag and define a few of the methods above. Then modify your solution to problem 2 to use your Club class instead of a Bag<Person>. Note that you won t say Club<Person> since members is hard coded to be a Bag<Person>, no generic type specifier is needed. Is this beneficial? It does bring more logic to the code, and we could also add attributes to Club that are unique to clubs. For example, we could add a field that holds the contact information for the Club, or the name of the Secretary, or the location where the club meetings take place. All these fields would then be managed by additional Club methods, but each club would still use a Bag to store the list of members. Problem 5 Static Method to Find the Intersection of Two Bags (EXTRA CREDIT) The intersection of two collections is a new collection of the entries that occur in both collections. That is, it contains the overlapping entries. Define a static (client) method that takes two Bag objects and returns a third bag containing the items in common to both bags. Be sure to verify that when your method returns, that baga and bagb have their original items (it s ok if their order is different). Use the following header already provided inside the Lab2 class file: public static <T> Bag<T> intersection( Bag<T> baga, Bag<T> bagb)

Part B Stacks and Interfaces We will now turn to the Stack, a real workhorse data structure in computing. As discussed in lecture, the UML for StackInterface is StackInterface<T> +push(newentry: T): void +pop(): T +peek(): T +isempty(): boolean +clear(): void +tostring(): String You will probably need to refer to this during the lab. Problem 1 Using StackInterface to define Stack variables We ll begin this part of the lab in the Lab2B project folder, and in the Lab2B program file. Create a StackInterface variable and assign it either a new ArrayStack or new LinkedStack. Create another of the other variety. Notice how a StackInterface variable can refer to either an ArrayStack or a LinkedStack. This is because either class is guaranteed to have all the methods needed for a Stack, because they both said public class XXXXStack implements StackInterface and the compiler forced them to have the exact same methods as the interface specified. Problem 2 Examine and remove items Write code to use peek to print the top item of the Stack without removing it, then write a while loop that removes all the items as long as the stack is not empty. Problems 2-4 see Lecture Slides or Video Problem 5 Postfix Processor Step 1. Run the postfix solution and watch it evaluate postfix expressions like: 53+ or 67*6-3/ Step 2. Run the program at problem 5 Then, uncomment out problem 5 in Lab2B. This code sets up all the variables you will need to process a postfix expression (as discussed in lecture) and compute its numeric result using a Stack of numbers. Since inputting data is a bit tricky, we're reading a complete postfix expression as a single String (MAKE SURE YOU DO NOT ENTER SPACES in your postfix input) such as 34+5* which means (3+4)*5 or 60.

Each character is either a digit or an operator (+,-,*,/). We then use a traversal (for) loop so we can process the expression one character at a time using mathexpression.charat(k) to retrieve the k th symbol in the expression. As given, the program currently reads an expression and prints it to the screen one letter at a time Step 3. Process digit tokens To convert each digit token to a number we can use methods in the Character class. First, google java Character class and read through the javadocs for Character. Look for a method that tells you if a character is a digit. static boolean isdigit(char ch) Determines if the specified character is a digit. also look for a method that will convert a character digit to a numeric value static int getnumericvalue(char ch) Returns the int value that the specified Unicode character represents. Now, in the for loop, after the line that prints symbol, add an if statement that tests if symbol is a digit, and if so, write another statement that converts symbol to an integer (use getnumericvalue above), and store it in the double variable value. Finally, push value onto the stack. This marks the end of the if block. Also, add a statement that prints out the current stack contents at the bottom of the while block. Then run your program and it should show the numeric tokens being pushed onto the stack. Step 4. Process operators Now we will add an else block to our previous if statement. This is to handle when the token is not a digit, i.e. it is either +,-,*,/ This else block should: a. pop an item from the stack, and store in op2 (the first item off is the last item in the operation) b. do it again and store in op1 c. Then construct four more if statements inside this else block so that if symbol is '+' you will push onto numstack the sum of op1+op2, d. and so on for the other operations. Finally, after the whole traversal loop finishes processing the expression, pop off the last item from the stack (it will be the final answer) and print it out. Try it out on a few expressions.

When finished, call the instructor over to get signed off. EXTRA POINTS (3 points) STACK SORT Consider the following algorithm to sort the entries in a stack S1. First create two empty stacks, S2 and S3. At any given time, stack S2 will hold the entries in sorted order, with the smallest at the top of the stack. Move the top entry of S1 to S2. Pop and consider the top entry t of S1. Pop entries of stack S2 and push them onto stack S3 until you reach the correct place to put t. Then push t onto S2. Next move all the entries from S3 to S2. a. Write a Java implementation of this algorithm. b. Consider the following revision of this algorithm. After moving the top entry of S1 to S2, compare the new top entry t of S1 with the top entry of S2 and the top entry of S3. Then either move entries from S2 to S3 or from S3 to S2 until you locate the correct position for t. Push t onto S2. Continue until S1 is empty. Finally, move any entries remaining in S3 to S2. Implement this revised algorithm.