Solutions to Quiz 1 (October 19, 2015)

Similar documents
Solutions to Quiz 1 (March 14, 2016)

Solutions to Quiz 1 (October 25, 2017)

Quiz 1 (October 25, 2017)

Quiz 1 (March 14, 2016)

Solutions to Quiz 1 (March 22, 2019)

CSE331 Winter 2014, Midterm Examination February 12, 2014

Quiz 2 (April 22, 2016)

CSE 331 Midterm Exam Sample Solution 2/13/12

CSE 331 Midterm Exam 2/13/12

Data abstractions: ADTs Invariants, Abstraction function. Lecture 4: OOP, autumn 2003

CSE 331 Final Exam 12/14/15 Sample Solution. Question 1. (20 points, 1 each) Warmup. For each statement, circle T if it is true and F if it is false.

Quiz 2 (November 20, 2015)

Prelim 1. CS 2110, October 1, 2015, 5:30 PM Total Question Name True Short Testing Strings Recursion

CSE wi Midterm Exam 2/8/18 Sample Solution

Refresher: Interface Specifications. ADT Documentation. Set Represented as an Array. Representation Invariant, Abstraction Function

Representation Invariant, Abstraction Function

CSE wi Midterm Exam 2/8/18. Name UW ID #

Top Down Design vs. Modularization

CSE 331 Midterm Exam Sample Solution 2/18/15

Implementing an ADT: Representation invariants

Lecture 7: Data Abstractions

Solutions to Quiz 2 (April 27, 2018)

Representation Invariants and Abstraction Functions

Lecture 5 Representation Invariants

CSE331 Spring 2015, Final Examination June 8, 2015

CSE331 Winter 2014, Midterm Examination February 12, 2014

CSE 331 Final Exam 3/16/15 Sample Solution

L6: Abstract Data Types

6.005 Elements of Software Construction Fall 2008

CSE 331 Final Exam 3/12/12

Java Fundamentals (II)

Representation invariants and abstraction functions CSE 331 UW CSE

CSE 331 Summer 2017 Final Exam. The exam is closed book and closed electronics. One page of notes is allowed.

CSE331 Autumn 2011 Midterm Examination October 28, 2011

CSE 331 Final Exam 6/5/17 Sample Solution

Solutions to Quiz 2 (December 3, 2018)

CSE 331 Summer 2016 Final Exam. Please wait to turn the page until everyone is told to begin.

CSE 331 Midterm Exam 11/9/15 Sample Solution

Exam 2 CSCI 2600 Principles of Software November 3, 2015

For this section, we will implement a class with only non-static features, that represents a rectangle

CSE 331 Winter 2016 Midterm Solution

CIS 110 Introduction To Computer Programming. February 29, 2012 Midterm

CSE 331 Final Exam 6/5/17. Name UW ID#

Assertions, pre/postconditions

Testing, Debugging, and Verification

Type Hierarchy. Comp-303 : Programming Techniques Lecture 9. Alexandre Denault Computer Science McGill University Winter 2004

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

6.170 Lecture 7 Abstract Data Types MIT EECS

Sample file. Practice Exam One COMPUTER SCIENCE A SECTION I. Directions: Determine the answer to each of the following questions or incomplete

Java: advanced object-oriented features

CSE 331 Spring 2018 Midterm

CSE 331 Software Design & Implementation

Write for your audience

Input Space Partitioning

Prelim 1 SOLUTION. CS 2110, September 29, 2016, 7:30 PM Total Question Name Loop invariants. Recursion OO Short answer

Classes Classes 2 / 36

Sec$on 2: Specifica)on, ADTs, RI WITH MATERIAL FROM MANY

Classes Classes 2 / 35

Aggregation and Composition. [notes Chapter 4]

CSE 331 Final Exam 6/7/16

double d0, d1, d2, d3; double * dp = new double[4]; double da[4];

6.096 Introduction to C++

Largest Online Community of VU Students

Equality. Michael Ernst. CSE 331 University of Washington

University of Washington CSE 331 Software Design & Implementation Spring Final exam. Monday, June 10, 2013

Prelim 1. Solution. CS 2110, 14 March 2017, 7:30 PM Total Question Name Short answer

L13: Synchronization

Abstraction Functions Lecture 9 Fall 2005

public static boolean isoutside(int min, int max, int value)

Module 10A Lecture - 20 What is a function? Why use functions Example: power (base, n)

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

Lecture 12: Abstraction Functions

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

Exam Review. CSE 331 Section 10 12/6/12. Slides by Kellen Donohue with material from Mike Ernst

1: Introduction to Object (1)

Readability [Skrien 4.0] Programs must be written for people to read, and only incidentally for machines to execute.

CS 315 Software Design Homework 3 Preconditions, Postconditions, Invariants Due: Sept. 29, 11:30 PM

Chapter 11: Collections and Maps

CS 215 Software Design Homework 3 Due: February 28, 11:30 PM

CSE 142 Su 04 Computer Programming 1 - Java. Objects

Object-Oriented Design Lecture 5 CSU 370 Fall 2008 (Pucella) Friday, Sep 26, 2008

The Dynamic Typing Interlude

CSE 331 Software Design & Implementation

public static void negate2(list<integer> t)

CSE331 Winter 2014, Final Examination March 17, 2014 Please do not turn the page until 8:30. Rules:

Object Oriented Programming in Java. Jaanus Pöial, PhD Tallinn, Estonia

Creating an Immutable Class. Based on slides by Prof. Burton Ma

PIC 20A The Basics of Java

In Java there are three types of data values:

CS 3 Introduction to Software Engineering. 3: Exceptions

Contract Programming For C++0x

A Practical Approach to Programming With Assertions

Abstraction and Specification

Advanced Object Oriented Programming EECS2030Z

CSE 331 Final Exam 6/7/16 Sample Solution

Mutually Enhancing Test Generation and Specification Inference

Outline. Computer Science 331. Information Hiding. What This Lecture is About. Data Structures, Abstract Data Types, and Their Implementations

Assertions. Assertions - Example

CSE 331 Final Exam 12/9/13

C a; C b; C e; int c;

Transcription:

MIT 6.005: Software Construction Prof. Rob Miller & Max Goldman revised Monday 19 th October, 2015, 13:57 Solutions to Quiz 1 (October 19, 2015) Problem 1 (Multiple Choice) (14 points). Circle all correct answers for the following questions. (a) Which of the following will fail to compile due to static checking? Treat each part as an independent piece of code. A. String s = null; System.out.println(s.toLowerCase()); B. int[] arr = new int[] { 1, 2, 3 ; arr[1] = 1; C. int[] arr = new int[] { 1, 2, 3 ; assert arr.length == 2; D. int[] arr = new int[] { 1, 2, 3 ; arr[3] = "4"; A will compile successfully, but have a runtime error of NullPointerException because null is a hole in the type system, passing the compiler s static checks. B will compile successfully, and reassign the second entry in the array, so that the array becomes 1, 1, 3. C will compile successfully, and the assertion will be checked at runtime. D does not compile, because a String cannot be assigned to an int. (b) When implementing the Object contract: A. equals() must be reflexive and symmetric, but not necessarily transitive B. only mutable types should override equals() and hashcode() C. two objects with the same hashcode() must be equals() D. two objects that are equals() must have the same hashcode() E. two objects that are not equals() must have different hashcode() values

2 Solutions to Quiz 1 (October 19, 2015) A is not correct, equals() must also be transitive. B is not correct, immutable types should definitely override equals() and hashcode(). C is not correct, two objects can share the same hashcode but not be equal. D is correct. The essence of the Object contract. E is not correct, the contract doesn t specify that objects that are not equals() must have different hashcode() values, only that objects that are equals() must have the same hashcode(). (c) You are given the following data type: * Represents the orders being processed in a factory. * A factory must always have at least one order in it. * Factories are mutable. class Factory { public List<Order> orders; // the orders in this factory are sorted from oldest to newest, // with no duplicates. * @return a list containing the orders of this factory, * with no duplicates. public List<Order> getorders() { return orders; //... other code Consider the following clients of the Factory data type: @return newest order in the factory public Order client1(factory factory) { Order newest = factory.getorders().get(factory.orders.size()-1); return newest; @return any order in the factory public Order client2(factory factory) { Order anyorder = factory.getorders().get(0); return anyorder; Circle all of the true statements below. A. client1 is correct for the code and comments as shown B. client2 is correct for the code and comments as shown C. client1 depends on Factory s representation

Solutions to Quiz 1 (October 19, 2015) 3 D. client2 depends on Factory s representation E. Changing orders to private would fix the rep exposure of Factory A is correct, since the orders list contains at least one order, and the last one is the newest one. B is correct, since the orders list contains at least one order, and client2 can return any of them. C is correct, because client1 refers to the orders field directly. Factory has an exposed rep. D is not correct because client2 only depends on the getorders() method, not any of Factory s representation. E is not correct because the getorders() method still would return a reference to the List<Order> which could then be modified. Problem 2 (Specs) (18 points). Imagine you are given the following interface with a single method: public interface Mode { * Finds one of the most frequent integers in an array. * @param values array in which at least one value occurs more than once. * @return a number that appears most often in values public int getmode(int[] values); Along with a class that implements it: public class MyMode implements Mode { * TODO @Override public int getmode(int[] values){... Write a spec for MyMode.getMode() in which the precondition and postcondition are both different from Mode.getMode(), while ensuring that MyMode is a well-defined spec that legally implements Mode. (a) Precondition: @param

4 Solutions to Quiz 1 (October 19, 2015) (b) Postcondition: @returns In order for the implementation to be legal, the new precondition must be weaker, and the new postcondition must be stronger. For example: * Finds the most repeated integer out of an array of values. * @param values an array of integer values of length > 0 * @return the smallest mode of the array Note that the empty array must either be excluded by the precondition or specified by the postcondition. Problem 3 (Testing) (18 points). For this spec: * @param n a nonnegative integer * @returns the number of digits in a base-10 representation of n public int countdigits(int n); Write a black box testing strategy for countdigits with exactly one good partition for n and exactly one good partition for the return value result. Each partition should be a list of well-formed mathematical expressions containing only numbers, variable or constant names, and equality or inequality operators (<, >, =,, ). (a) One partition for n: n=0, n=1, 1<n<10, n=10, 10<n<MAXINT, n=maxint This partition uses four boundary values. It is a partition because it covers the entire legal space for n. Note that the specification requires that n be nonnegative, so we don t have to (and cannot) test n<0.

Solutions to Quiz 1 (October 19, 2015) 5 (b) One partition for result: result=1, result>1 It isn t necessary to use MAXINT as a boundary value here, because we can never plausibly reach it. Problem 4 (ADTs) (20 points). Louis Reasoner has written an ADT for keeping track of relationships among strings. Unfortunately, he hasn t taken 6.005 and doesn t understand the concepts that make ADTs powerful. 1 2 * Represents a list of collections, where a collection is a set of strings 3 * that are related for some reason, such as: 4 * - synonyms in English, e.g. {"tool", "instrument", "utensil" 5 * - synonyms in different languages, e.g. {"tool", "outil", "instrumento" 6 * Each collection is considered fixed, so it never changes once created. 7 * But when new collections are discovered, they may be added to this list. 8 9 public class StringCollection { 10 public final List<Set<String>> collections; 11 12 Make an empty StringCollection 13 public StringCollection() { 14 this.collections = new ArrayList<Set<String>>(); 15 16 17 Make StringCollection from an existing StringCollection 18 * @param oldcollection 19 public StringCollection(StringCollection oldcollection) { 20 this.collections = oldcollection.collections; 21

6 Solutions to Quiz 1 (October 19, 2015) 22 23 Add a new collection of strings 24 * @param newcollection set of strings that are related to each other 25 public void addcollection(set<string> newcollection) { 26 this.collections.add(newcollection); 27 28 29 Get all collections known to this StringCollections object 30 * @return the collections in this object 31 public List<Set<String>> fetchall() { 32 return this.collections; 33 34 35 Get all known collections that share a particular word 36 * @param filterword String to look for 37 * @param result list that receives the collections found 38 * Adds all collections that contain filterword to the result list. 39 public void filter(string filterword, List<Set<String>> result) { 40 for (Set<String> collection : this.collections) { 41 if (collection.contains(filterword)) 42 result.add(collection); 43 44 45 (a) Classify each of the methods in StringCollection using the four types of ADT operations. StringCollection() StringCollection(StringCollection oldcollection) void addcollection(set<string> newcollection) List<Set<String>> fetchall() void filter(string filterword, List<Set<String>> result) creator producer mutator observer observer Unfortunately, this ADT is littered with representation exposure issues. Lend your knowledge to Louis and clean up his code! Which lines are responsible for representation exposure? Write: the line number a one-sentence reason that the line causes rep exposure a one-sentence fix to it that still satisfies the spec.

Solutions to Quiz 1 (October 19, 2015) 7 There are more boxes below than you need. (b) Line #: (10) makes the collections rep field publicly-accessible. Make it private (c) Line #: (20) Shares a mutable list instance between the two StringCollections. Make a deep copy of the collections list, or use a rep-exposure free version of fetchall(), or make a new list containing the sets wrapped in unmodifiable wrappers. (d) Line #: (26) Sets are mutable. Keeping a reference to a passed in set means the client can modify the list which explicitly goes against the spec of the class. Make a defensive copy of the set. An unmodifiable wrapper around the set is not a good fix, because it still shares the underlying set with the client. (e) Line #: (32) Returns a reference to collections out of the ADT, which can then be modified by adding new collections or changing existing collections, which is against the spec. The answer is to deep copy the list we want to return or allocate a new list with unmodifiable sets around the underlying sets. (f) Line #: (42) The rep exposure here is directly adding a set to the result list. If we simply copy the set here, we don t risk the client being able to interfere with the rep of this ADT. (g) Line #: Problem 5 (Scopes) (18 points). Suppose we have the following classes. 1 public class WordList { 2 private List<String> wordlist; 3 public Frequency frequency; 4 public static int maxsize; 5 // other code... 6 7 8 public class Frequency {

8 Solutions to Quiz 1 (October 19, 2015) 9 public static int max; 10 // other code... 11 12 public Map<Integer, Set<String>> invertfrequencies(map<string, Integer> frequencies) { 13 Set<String> words; 14 Integer i; 15 Map<Integer, Set<String>> reversemap = new HashMap<Integer, Set<String>>(); 16 17 for (String s: frequencies.keyset()) { 18 i = frequencies.get(s); 19 if (!reversemap.containskey(i)) { 20 words = new Hashset<String>(); 21 words.add(s); 22 reversemap.put(i, words); 23 24 else { 25 reversemap.get(i).add(s); 26 27 28 return reversemap; 29 30 (a) Which of these pairs of variables have the same scope of access? (Select all that apply) A. maxsize, max B. max, words C. wordlist, frequency D. frequencies, reversemap E. s, i A is correct because maxsize and max are both global variables, accessible from anywhere in the program. B is incorrect because max is a global variable, but words is a local variable inside invertfrequencies. C is incorrect because wordlist is a private instance variable accessible only to code inside WordList, but frequency is a public instance variable accessible from any code with a reference to a WordList object. D is correct because frequencies is a parameter whose scope is the body of invertfrequencies, and reversemap is a local variable with the same scope. E is incorrect because the scope of i is the outermost curly-brace block of invertfrequencies, while the scope of s is just the body of the for loop. (b) Two variables can be moved to minimize their scopes, without affecting any other code. Write down the variable name and the line number that its variable declaration should be moved to. Variable: Variable: Declaration: Declaration:

Solutions to Quiz 1 (October 19, 2015) 9 i: 18, words: 20 Problem 6 (AF/RI) (12 points). Consider this ADT: * Represents one of the suits in a standard 52 card deck - clubs, hearts, * spades, or diamonds. public class CardSuit { private int suit; private static final int CLUBS = 0; private static final int DIAMONDS = 1; private static final int HEARTS = 2; private static final int SPADES = 3; public CardSuit(int suit) { this.suit = suit; @Override public String tostring() { switch (suit) { case CLUBS: return "clubs"; case DIAMONDS: return "diamonds"; case HEARTS: return "hearts"; case SPADES: return "spades"; default: assert false; // shouldn t get here (a) What is the domain of the abstraction function? int (b) What is the range of the abstraction function?

10 Solutions to Quiz 1 (October 19, 2015) The four suits in a deck - clubs, hearts, diamonds, and spades (c) What is the rep invariant? 0 <= suit <= 3