DCS235 Software Engineering Exercise Sheet 2 Using Java Collections

Similar documents
What is it? CMSC 433 Programming Language Technologies and Paradigms Spring Approach 1. Disadvantage of Approach 1

DCS235 Software Engineering Exercise Sheet 2: Introducing GUI Programming

Designing Robust Classes

Software Testing Prof. Meenakshi D Souza Department of Computer Science and Engineering International Institute of Information Technology, Bangalore

CS171 Midterm Exam. October 29, Name:

Comp Intermediate Programming EXAM #1 February 16, 2004 Rice University - Instructors: Cox & Nguyen

Classes, interfaces, & documentation. Review of basic building blocks

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

CS2110: Software Development Methods. Maps and Sets in Java

CS 101 Fall 2006 Midterm 1 Name: ID:

For this chapter, switch languages in DrRacket to Advanced Student Language.

CS2110: Software Development Methods. Maps and Sets in Java

You will not be tested on JUnit or the Eclipse debugger. The exam does not cover interfaces.

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

B2.52-R3: INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING THROUGH JAVA

Advanced Computer Programming

CS2 Practical 1 CS2A 22/09/2004

Decisions in Java IF Statements

CMSC 341 Lecture 7 Lists

EXAM Computer Science 1 Part 1

You must bring your ID to the exam.

CHAPTER 7 OBJECTS AND CLASSES

Outline. runtime of programs algorithm efficiency Big-O notation List interface Array lists

Summer Final Exam Review Session August 5, 2009

Introduction to the Java Basics: Control Flow Statements

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Programming Languages 3. Definition and Proof by Induction

York University Fall 2001 / Test #1 Department of Computer Science

COMP 102: Test August, 2017

EECS2030 Fall 2016 Preparation Exercise for Lab Test 2: A Birthday Book

Ticket Machine Project(s)

CT 229. Java Syntax 26/09/2006 CT229

CS 251 Intermediate Programming Java Basics

CSCI-1200 Data Structures Spring 2017 Lecture 27 Garbage Collection & Smart Pointers

Text Input and Conditionals

Lab5. Wooseok Kim

Remedial classes. G51PRG: Introduction to Programming Second semester Lecture 2. Plan of the lecture. Classes and Objects. Example: Point class

Topic 10: The Java Collections Framework (and Iterators)

Extensibility Patterns: Extension Access

Pointers, Arrays and Parameters

CS2 Practical 2 CS2Ah

Week 4 EECS 183 MAXIM ALEKSA. maximal.io

Programming Problems 22nd Annual Computer Science Programming Contest

CS 101 Spring 2006 Final Exam Name: ID:

ASSIGNMENT 4. COMP-202, Fall 2013, All Sections. Due: December 6 th,2013(23:59)

18-Dec-09. Exercise #7, Joongle. Background. Sequential search. Consider the following text: Search for the word near

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

CS 101 Fall 2005 Midterm 2 Name: ID:

Object Oriented Design: Identifying Objects

COS 126 Written Exam 2 (Spring 2015)

CMSC 202H. Classes and Objects: Reusing Classes with Composition

CHAPTER 7 OBJECTS AND CLASSES

CS 61A, Fall, 2002, Midterm #2, L. Rowe. 1. (10 points, 1 point each part) Consider the following five box-and-arrow diagrams.

Computer Science 330 Assignment

What is an Iterator? An iterator is an abstract data type that allows us to iterate through the elements of a collection one by one

CS 455 Midterm Exam 1 Fall 2015 [Bono] Thursday, Oct. 1, 2015

Common Misunderstandings from Exam 1 Material

CS 3114 Data Structures and Algorithms READ THIS NOW!

This exam is worth 30 points, or 18.75% of your total course grade. The exam contains

CMPSCI 187: Programming With Data Structures. Lecture #23: Linked Lists David Mix Barrington 31 October 2012

COMP 250 Midterm #2 March 11 th 2013

In this lab, you will learn more about selection statements. You will get familiar to

Assignment 2. Application Development

Initializers: Array initializers can be used with class base types as well. The elements of the initializer can be expressions (not just constants).

Objects and Classes. 1 Creating Classes and Objects. CSCI-UA 101 Objects and Classes

Implementation. Learn how to implement the List interface Understand the efficiency trade-offs between the ArrayList and LinkedList implementations

CS 270 Algorithms. Oliver Kullmann. Binary search. Lists. Background: Pointers. Trees. Implementing rooted trees. Tutorial

What is the Java Collections Framework?

Object-Oriented Programming (OOP) Basics. CSCI 161 Introduction to Programming I

CMSC 433 Section 0101 Fall 2012 Midterm Exam #1

COMP 102: Test August, 2017

Model Solutions. COMP 102: Test. 18 August, 2016

Part 2: The Material PART 2

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

University of Massachusetts Amherst, Electrical and Computer Engineering

Notes from a Short Introductory Lecture on Scala (Based on Programming in Scala, 2nd Ed.)

FILE ORGANIZATION. GETTING STARTED PAGE 02 Prerequisites What You Will Learn

CS 201 Advanced Object-Oriented Programming Lab 6 - Sudoku, Part 2 Due: March 10/11, 11:30 PM

CSC324 Principles of Programming Languages

Fall 2013 Program/Homework Assignment #2 (100 points) -(Corrected Version)

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

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger

7 Abstracting over the Data Type; Java Documentation; Direct Access Data Structures

Praktische Softwaretechnologie

COMP 250 Winter 2011 Reading: Java background January 5, 2011

INF2220: algorithms and data structures Series 4

Name: Checked: Objectives: Practice creating classes and methods, and using them in your programs.

CS 177 Week 15 Recitation Slides. Review

CMSC 330: Organization of Programming Languages. Operational Semantics

Another class in the Form arsenal of Items is ChoiceGroup.

About this exam review

CS103 Handout 42 Spring 2017 May 31, 2017 Practice Final Exam 1

Course Content. Objectives of Lecture 18 Black box testing and planned debugging. Outline of Lecture 18

Lab 1: Introduction to C Programming. (Creating a program using the Microsoft developer Studio, Compiling and Linking)

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

CS1020: DATA STRUCTURES AND ALGORITHMS I

INFO1x05 Tutorial 09

Digital Citizenship. Student Packet Grades K-2 UNIT

CS 100J Prelim 2 Have a good break!!! 15 March 2007

CS 237 Meeting 19 10/24/12

Transcription:

DCS235 Software Engineering Exercise Sheet 2 Using Java Collections Prerequisites October 2004 You should understand Java interfaces. If you need to revise this, see Jia section 4.4.7 and 5.3. You should be able to use the Java library documentation. Read about Java s Collections Classes. Section 8.2 of Jia s book is a good description; there is also a section in Sun s Java tutorial see: http://java.sun.com/docs/books/tutorial/collections/index.html. It is not necessary to appreciate all the details to start this exercise. Aims The aim of this exercise is to gain some practical experience using The Java collections framework. In this exercise, you will design and implement a simple program using classes from the collection framework. Section 1 describes the problem scenario and the requirements. The other sections contain some hints for the design and implementation. The task is to write a program to meet these requirements. You are advised to use TogetherEC. This exercise can be done individually or in a small group. DCS 235 Lab Exercise 2 (version 1.2, 12 Oct 04) Page 1 of 5

1.Problem Scenario In a large family, there are many uncles (and aunts) and many nieces (and nephews). Each uncle buys a present for each niece on her birthday every year. Unfortunately, this can cause all sorts of problems: Last year Amy received a charming book ( The Wonder of Computers ) from Uncle Albert. Unfortunately, Uncle Bill gave it to her too. When Beatrice opened her present from Uncle Charlie, she found it was the same thing he had given to Claire. How could he? she shouted rudely, I m not at all like Claire!. Uncle David is getting forgetful. Little Emily was so upset not to get a present. 1.1 Program Requirements The family decide they need a computer program to manage the giving of presents. The requirements are: 1. Uncles and nieces can be added to the system. The date of each niece s birthday is recorded. 2. A list of uncles can be generated in alphabetical order by name. 3. A list of nieces can be generated in order of birthday. 4. The system holds a list of the presents selected by each uncle for the next birthday of one of his nieces. Each present is described in a few words. 5. An uncle can enter the present he intends to give to one of his nieces. The program ensures that: i. each niece receives something different from each uncle ii. each uncles gives something different to each niece. 6. A list of the presents given by one of the uncles can be generated, showing the niece who is to receive it. The nieces for whom no present has been chosen should also be listed. 7. A list of presents to be received by one of the nieces can be generated, showing the uncle giving it. The uncles who have no present for the niece should also be listed. 8. The list of presents for a niece can be deleted (that s done when her birthday is past). 1.2 Your Task Write a program to meet the requirements. To make a start, write classes to provide the behaviour, without a user interface. The public classes you should provide are: Family, Niece and Uncle. The public methods of these classes create an interface (often called an API Application Program Interface) to which a user interface could be attached. The API is described below; note that your program will also contain other classes and methods. Class Family Class representing a family. Constructor Summary Family() Create an empty family with no uncles, nieces or presents. DCS 235 Lab Exercise 2 (version 1.2, 12 Oct 04) Page 2 of 5

Method Summary boolean addniece(java.lang.string name, int day, int month) Add a new niece. If there is already a niece of this name, false is returned and nothing is added. boolean adduncle(java.lang.string name) Add a new uncle. If there is already an uncle of the name, false is returned and nothing is added. Niece findniece(java.lang.string name) Lookup a niece by name; return null if not found. Uncle finduncle(java.lang.string name) Lookup an uncle by name; return null if not found. void listnieces() List (to the console) the nieces recorded. void listuncles() List (to the console) the uncles recorded. Class Uncle Class representing an uncle. Note that the constructor is not public use Family.addUncle(). Method Summary boolean addpresent(niece recipient, java.lang.string description) Adds a new present, given by this uncle. Return true if the present is allowed. void listpresents() Lists (to the console) the presents given by this uncle, showing the recipient. Nieces with no present from this uncle are also listed. Class Niece Class representing a niece. Note that the constructor is not public use Family.addNiece(). Method Summary int clearpresents() Delete all the presents chosen for this niece. Return the number removed. void listpresents() Lists (to the console) the presents to be received by this niece, showing the giver. Uncles with no present for this niece are also listed. DCS 235 Lab Exercise 2 (version 1.2, 12 Oct 04) Page 3 of 5

2.Specification In this section two method of understanding the problem in more detail are suggested. 2.1 Sets and Function We can describe the problem using simple sets and functions. There is a set People. The uncles and nieces are people: Uncles People Nieces People There is a set Presents. We assume that each present has a description; we write the description of present p as p D. It is possible for two different presents p 1, p 2 to have the same description (p D 1 = p D 2 ), implying that presents p 1, p 2 must not both be given to one of the nieces. The presents recipient and giver of each present is described by the functions r and g: r Presents Nieces g Presents Uncles For example, if Bill gives Amy p 1 = book, David gives Amy p 2 = toy and Bill gives Emily p 3 = sweets then and r p 1 = Amy r p 2 = Amy r p 3 = Emily g p 1 = Bill g p 2 = David g p 3 = Bill The presents given by uncle u, paired with their recipients, are: {(p, r p) g p = u} The constraint that all the presents received by a niece are different is expressed as: p 1, p 2 Presents p 1 p 2 r p 1 = r p 2 p D 1 p D 2 Describe the other constraint in the same style. 2.2 Class Diagram Draw a class diagram to describe the problem. As well as the public classes in the API, consider classes Person and Present. Show the attributes of each class. DCS 235 Lab Exercise 2 (version 1.2, 12 Oct 04) Page 4 of 5

3.Design and Implementation Hints For this exercise, you should use the classes from the Java collections framework (in package java.util) to hold the data 1. 3.1 Design 1 A possible design would be to hold the data as a list of presents, with each present having a reference to the giver and recipient. This design is simple. What is the disadvantage of this design (remember, the family is very large)? Use a class diagram and/or simple mathematics to specify the classes, attributes and operations in this design. 3.2 Design 2 An alternative design is to hold a map from each uncle to the set of presents given and from each niece to the set of presents received. With this data structure, adding a present means inserting it into each map. Generating the lists of presents is also easy, and is much more efficient than in the first design. However, deleting the presents for one of the nieces is more complex. 3.3 Implementation Testing To test your program, write a main method which creates a family and adds nieces, uncles and presents. Constraints It is suggested that you first implement the program without the constraints. Then consider how they are checked in each design. Dates The date (day and month only) of the birthday for each niece is recorded. The Java Date class is rather complex; it is suggested that you implement your own simple DayMonth class. 4.Additional Exercises 4.1 User Interface Add a simple command line user interface. 4.2 More Sorting The requirements given above can be implemented using a single natural order for uncles and nieces. However, presents have no order, so the lists of presents appear in an arbitrary order. It would be better to show the presents given by an uncle in alphabetic order of the name of the niece getting the present, and similarly the list of presents received should in order of the uncle s name. [Hint: you need to implement two classes implementing Comparator for Present.] 4.3 Performance Measurements Measure the performance when very large number of entities are present. Compare different designs and different implementations of List and Map. 4.4 Deleting Uncles Occasionally an uncle dies. Implement a method to delete an uncle. [WARNING: Be careful not to create a space leak by leaving a present referring to the uncle, so that the uncle is not garbage collected,] 1 A real system would use a database, but that s another course! DCS 235 Lab Exercise 2 (version 1.2, 12 Oct 04) Page 5 of 5