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

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

CS 201 Advanced Object-Oriented Programming Lab 4 - Asteroids, Part 2 Due: February 24/25, 11:30 PM

10 Java Collections; Equality, JUnit

Write for your audience

Documentation Requirements Computer Science 2334 Spring 2016

Program Correctness and Efficiency. Chapter 2

Exceptions and Libraries

Introduction to Programming System Design CSCI 455x (4 Units)

CS520 Setting Up the Programming Environment for Windows Suresh Kalathur. For Windows users, download the Java8 SDK as shown below.

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

Jtest Tutorial. Tutorial

ICOM 4015 Advanced Programming Laboratory. Chapter 1 Introduction to Eclipse, Java and JUnit

CSE 331 Software Design & Implementation

Introduction to Programming Using Java (98-388)

Why Design by Contract! CS 619 Introduction to OO Design and Development. Design by Contract. Fall 2012

Spring, 2014 CIT 590. Programming Languages and Techniques Homework 7

Prelim 1. CS 2110, September 29, 2016, 7:30 PM Total Question Name Loop invariants

Tips from the experts: How to waste a lot of time on this assignment

CompuScholar, Inc. 9th - 12th grades

TeenCoder : Java Programming (ISBN )

Tips from the experts: How to waste a lot of time on this assignment

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

Lab Exercise 6: Abstract Classes and Interfaces CS 2334

Javadoc. Computer Science and Engineering College of Engineering The Ohio State University. Lecture 7

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

Assertions, pre/postconditions

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Index. Index. More information. block statements 66 y 107 Boolean 107 break 55, 68 built-in types 107

11 Using JUnit with jgrasp

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

Review. these are the instance variables. these are parameters to the methods

Chapter 4 Defining Classes I

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Com S 227 Spring 2018 Assignment points Due Date: Thursday, September 27, 11:59 pm (midnight) "Late" deadline: Friday, September 28, 11:59 pm

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

CS 201 Advanced Object-Oriented Programming Lab 5 - Sudoku, Part 1 Due: March 3/4, 11:30 PM

CSE wi Midterm Exam 2/8/18 Sample Solution

PREPARING FOR THE FINAL EXAM

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

CSE 331 Final Exam 3/16/15 Sample Solution


Family Map Server Specification

EXCEPTION HANDLING. Summer 2018

Packaging Your Program into a Distributable JAR File

CS/ENGRD 2110 FALL Lecture 3: Fields, getters and setters, constructors, testing

Address book. Presenting the Java assignment about an Address Book v

02/03/15. Compile, execute, debugging THE ECLIPSE PLATFORM. Blanks'distribu.on' Ques+ons'with'no'answer' 10" 9" 8" No."of"students"vs."no.

CMPSCI 187 / Spring 2015 Hanoi

Outline. iterator review iterator implementation the Java foreach statement testing

CMPSCI 187 / Spring 2015 Implementing Sets Using Linked Lists

Tips from the experts: How to waste a lot of time on this assignment

Assertions & Design-by-Contract using JML Erik Poll University of Nijmegen

10 Generating Javadocs; HashMap: Overriding equals

Our second exam is Thursday, November 10. Note that it will not be possible to get all the homework submissions graded before the exam.

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

LAT-TD-xxxxx-xx May 21, 2002

Programming Project 5: NYPD Motor Vehicle Collisions Analysis

CS 170 Java Programming 1. Week 13: Classes, Testing, Debugging

Project 1: Remote Method Invocation CSE 291 Spring 2016

EE 422C HW 6 Multithreaded Programming

INF 111 / CSE 121. Homework 3: Code Reading

Homework #10 due Monday, April 16, 10:00 PM

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

Programming II (CS300)

Introduction to Eclipse

Exceptions and assertions

ACORN.COM CS 1110 SPRING 2012: ASSIGNMENT A1

Initial Coding Guidelines

CS3901 Intermediate Programming and Data Structures Winter 2008 (Q2 AY 08) Assignment #5. Due: noon Tuesday, 3 March 2008

Object Oriented Programming

Homework 09. Collecting Beepers

JAVA CONCEPTS Early Objects

Getting Started with Eclipse/Java

Programming II (CS300)

Our second exam is Monday, April 3. Note that it will not be possible to get all the homework submissions graded before the exam.

Note: This is a miniassignment and the grading is automated. If you do not submit it correctly, you will receive at most half credit.

Java Programming Training for Experienced Programmers (5 Days)

Chapter Two Bonus Lesson: JavaDoc

Using Eclipse Europa - A Tutorial

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

METHODS. CS302 Introduction to Programming University of Wisconsin Madison Lecture 15. By Matthew Bernstein

CSE Spring 2015 Homework #1 Assigned January 22 nd, 2015, 2:00 pm Due: February 5 th, 2015, 2:00 pm

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

3 Getting Started with Objects

Pages and 68 in [JN] conventions for using exceptions in Java. Chapter 8 in [EJ] guidelines for more effective use of exceptions.

CMPSCI 187 / Spring 2015 Sorting Kata

CMPSCI 187 / Spring 2015 Hangman

Family Map Server Specification

Learning objectives. Reading carefully. Managing your time. CS2110 Fall 2017 Assignment A1. PhD Genealogy. See CMS for the due date

CS 201 Advanced Object-Oriented Programming Lab 8 - Linked Lists and Performance Analysis Due: April 7/8, 11:30 PM

Using Eclipse for Java. Using Eclipse for Java 1 / 1

Note: This is a miniassignment and the grading is automated. If you do not submit it correctly, you will receive at most half credit.

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

Pace University. Fundamental Concepts of CS121 1

6.170 Laboratory in Software Engineering Java Style Guide. Overview. Descriptive names. Consistent indentation and spacing. Page 1 of 5.

Agenda. Announcements. Extreme Java G Session 2 - Main Theme Java Tools and Software Engineering Techniques

Procedural Java. Procedures and Static Class Methods. Functions. COMP 210: Object-Oriented Programming Lecture Notes 2.

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

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

Exceptions. References. Exceptions. Exceptional Conditions. CSE 413, Autumn 2005 Programming Languages

Transcription:

CS 215 Software Design Homework 3 Due: February 28, 11:30 PM Objectives Specifying and checking class invariants Writing an abstract class Writing an immutable class Background Polynomials are a common type of mathematical function. In this assignment, we will only work with polynomials of a single variable, x, and where all terms have integer exponents and coefficients. Examples of these types of polynomials include: x^2 x^2 + x + 1 0 x^100 + x^-100 There are several ways that one might represent a polynomial. In this assignment, I provide you with a Polynomial interface and one implementation of the Polynomial interface called SparsePolynomial. The SparsePolynomial representation is well-suited for polynomials in which there are relatively few terms compared to the maximum exponent (also known as the degree) of the polynomial. For example, x^100 + x^-100 would be well-represented as a SparsePolynomial since it has just two terms even though its maximum exponent is 100. In contrast, x^2 + x + 1 has 3 terms and a maximum exponent of 2. While it could be represented as a SparsePolynomial, your assignment is to write a second implementation of Polynomial, called Dense- Polynomial, that can represent that polynomial using less memory. DensePolynomial The dense polynomial class, which you will provide, represents a polynomial with an array of coefficients. The exponent is determined by the position within the array. For example,!1

x^3 + 5x - 3 would be represented with this array: -3 5 0 1 Notice how there is no need to explicitly store the exponent, making this a compact representation for polynomials when the number of terms is similar to the maximum exponent of the polynomial as in this case. Assignment I am providing: the Polynomial interface, the SparsePolynomial class that implements Polynomial the Term class, which is used by SparsePolynomial SparsePolynomialTest, a JUnit test for SparsePolynomial You should turn in: the DensePolynomial class that implements Polynomial and uses the array representation described above DensePolynomialTest, a JUnit test for DensePolynomial an AbstractPolynomial class, which contains methods that are implemented the same way for DensePolynomial and SparsePolynomial You should not modify Polynomial or Term. The only changes you should make to Sparse- Polynomial are to move shared methods to AbstractPolynomial. Your DensePolynomial class should be written so that its objects are immutable. Dense- Polynomial should provide a constructor that takes a single coefficient and single exponent. To build polynomials with more terms, you would use the methods from the Polynomial interface. For example, to construct a DensePolynomial for 3x^2 + 2x, you would say: DensePolynomial p1 = new DensePolynomial (3, 2); // 3x^2 DensePolynomial p2 = new DensePolynomial(2, 1); // 2x DensePolynomial p3 = p1.add(p2); // 3x^2 + 2x The purpose of DensePolynomial s array representation is to minimize memory usage. Therefore, the array of coefficients should be as small as possible to represent the given polynomial. For example, x^3 + 2 should have an array of length 4: 2 0 0 1 When approaching this problem, you will need to address some specific questions in your design, among them:!2

How should you represent the constant 0? What is the class invariant for this representation? What should you do if add is called and the parameter passed in is a SparsePolynomial? As part of your implementation, you should do the following: For each method, you should create a Javadoc-style comment that describes preconditions and postconditions. With your instance variables, you should have a comment describing the invariants related to those variables. You should implement a wellformed method to check the class invariants. Write this early and use it to help you debug your code. For each public method that creates or modifies a DensePolynomial, you should use a Java assert statement to check your class invariants (by calling the wellformed method) before returning. Remember to run your program with assertion checking enabled (that is, use the -ea VM argument). tostring should work the same for all Polynomials. It should display a polynomial in a standard mathematical way. Terms should be sorted by exponent, terms with a zero coefficient should not be printed, there should be at most one term printed per exponent. For example, the following is in canonical form: 2x^3 + 3x + 1 while these are not: 2x^3 + 1 + 3x 2x^3 + 0x^2 + 3x + 1 x^3 + x^3 + 3x + 1 equals for Polynomials should return true if the polynomials represent the same mathematical expression, even if one is implemented as a SparsePolynomial and one as a DensePolynomial. You should write your implementation assuming any polynomial parameters that are passed in can be any subtype of Polynomial. You should write a JUnit test for DensePolynomial. Please let me know if you are unfamiliar with JUnit. If you find methods that can be implemented the same way for both SparsePolynomial and DensePolynomial, you should define an AbstractPolynomial class that implements Polynomial and is a superclass of SparsePolynomial and DensePolynomial and place the common implementations there. Java Background The remainder of this handout provides details on topics you may already be familiar with. Skip ahead to the grading rubric at the end if you are already familiar with these topics. Using javadoc Using assert statements Importing code into Eclipse Javadoc Comments The descriptions of classes and methods that you see when you link to API documentation for standard Java libraries (http://docs.oracle.com/javase/8/docs/api/) is generated by a tool called javadoc. Javadoc generates this html documentation based on the contents of!3

specially-formatted comments within your Java programs. For each method, javadoc allows you to provide general information about what the method does, a description of each parameter to the method, a description of the return result, and a description of each exception thrown. A javadoc comment is distinguished from other comments by enclosing the comments inside /** */ You place the javadoc comment for a method immediately before the declaration of the method. Within the comment, javadoc looks for specific tags to identify parameter comments, return value comments, and exception comments: @param <parameter-name> Parameter description, one for each parameter @return Return value description, omit if void Description of when thrown, one for each possible @exception <exception-name> exception Unfortunately, javadoc does not have explicit tags for preconditions or postconditions. Preconditions that pertain to a particular parameter should be included in the parameter description. Preconditions that depend on other things should be described in the general comments for the method. Postconditions that pertain to the return value should be described in the return value description. Postconditions that pertain to when exceptions are thrown should be provided with the exception descriptions. Other postconditions should be described in the general comments. An example should help clarify this. Here is what the javadoc comment for the Polynomial add method is: /** * Returns a polynomial by adding the parameter to this. Neither this * nor the parameter are modified. * * @param q the polynomial to add to this. q should not be null. * @return this + q * @exception NullPointerException if q is null */ public Polynomial add (Polynomial q); Given comments like these for the entire class, javadoc creates a nicely-formatted Web page for us. This is the basics, but it is all you need to know for 95% of what you will do with javadoc. For more help with javadoc, please see Oracle s documentation on Javadoc (http://docs.oracle.com/javase/8/docs/technotes/guides/javadoc/index.html). Below are instructions on how to run javadoc from within Eclipse. Please refer to Oracle s documentation if you wish to run javadoc from the command line instead.!4

CS 215 Homework 3 Java Assert Statements Java has support for assertion checking. Java does not distinguish between preconditions, postconditions, and invariants. Instead it provides one statement, an assert statement, and a runtime flag that either enables assertion checking or turns it off. Thus, for each precondition that you want to check, you would add an assert statement at the beginning of the method. For each invariant, you would need to add an assertion for that invariant at the end of each public method that modifies the state of the object. Assertion checking can be a powerful assistant while developing code, but generally should be disabled when you are convinced the code works correctly and you want to let others use it. Let's assume that we have an implementation of wellformed for dense polynomials. Using the assert statement, we could write the DensePolynomial add method as: } public Polynomial add (Polynomial q) { // Precondition assert q!= null : parameter is null ; // Do the work creating a new polynomial to // hold the sum of this and q and place that // in result. // Invariant assert result.wellformed() : "add result is not well formed"; return result; The assert statement optionally contains an expression following a colon, as shown above. This expression is printed out if the assertion fails. This can be helpful for debugging. If you leave it out and your code fails, you will get a stack trace that you can use to find the line of code where the error occurred. It can be very helpful to use this feature to display the value of a variable or expression that is in error when an assertion fails. If assertion checking is enabled and an assertion is violated, Java will produce a stack trace identifying the location of the assertion failure. Liberal use of assertion checking is an excellent way to find errors close to where the problematic code is. By default, assertions are disabled at runtime to improve performance of the executing code. To enable assertion checking, you must pass the "-ea" argument to the Java virtual machine. See below for instructions on turning assertion checking on and off from within Eclipse. For more details on assertion checking in Java, please see Oracle s documentation on assertion checking (http://docs.oracle.com/javase/7/docs/technotes/guides/language/assert.html). Eclipse To do this assignment in Eclipse, create a project called Polynomial. Now, download Polynomial.java, SparsePolynomial.java and SparsePolynomialTest.java from Moodle.!5

To import those files into your project, open the File menu and select Import... Then select File system and click the Next button. Select the directory where your file downloaded to using the Browse button. Then select the directory again (but don't click on the box) in the left panel Then click on the boxes next to the Java files in the right panel. Set the folder to import into to be the src folder in your Polynomial project. Then click Finish. Now, we will set a preference so you get reasonable javadoc stub comments when you implement your new class. Open the Window menu and select Preferences. Select Java, then Code Style, and finally Code Templates from the list of preferences. Now select Comments and then Overriding methods in the right panel. Click the Edit button. Change the comment to be a javadoc comment, that is, it should begin with /** rather than /*. Erase the part that says (non-javadoc). Add a line to the comment. Click the Insert variable button and select tags. Now create your new class, DensePolynomial. Open the File menu, select New and then Class. Be sure the Source Folder is the src folder in your polynomial project, enter Dense- Polynomial in the name field. Click the Add button next to the interfaces box, and enter Polynomial. Under stubs, select main and inherited abstract methods. Then click the Finish button. The top right panel should now show an outline for your new class. There should be stub implementations of all the methods you need to define to implement the Polynomial interface. Running javadoc from within Eclipse Eclipse assists with creating javadoc comments. In particular, after entering the signature of a method, you can move the cursor to the line immediately preceding the method. Then type /** followed by the enter key. Eclipse will create a stub for your javadoc comment that includes the appropriate @param, @return, and @exception tags. To generate the javadoc html pages within Eclipse, select your project in the Package Explorer. Open the File menu and select Export. Select javadoc and click the Finish button. All the defaults are fine. This will create a doc directory within your project. If you open the doc folder, it will reveal lots of files. Find index.html and double-click on that. That should start a browser within Eclipse showing you the generated Web pages. Cool! Turning assertion checking on and off with Eclipse To have Eclipse run your code with the "-ea" argument, do the following. Run your program once using Run As Application. Quit your program. Open the Run menu and select Run Configurations... Select your run configuration, which should have a familiar looking name, from the list on the left. Click on the Arguments tab. In the VM Arguments area, enter -ea. Grading Javadoc comments Assertion checking Class invariants / wellformed method tostring 5 points!6

equals Immutability AbstractPolynomial Simplicity of design Style Correctness 5 points 20 points Turning in your work To turn in your work, create a zip file that contains your source code. Submit that on Moodle.!7