Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

CSDS 293

Software Craftsmanship

2021 Fall Semester

Programming Assignment 2


Reading

In addition to the following topics, the quiz syllabus includes any material covered in the lectures:

● Chapter 14 in Code Complete

● Items 28, 50, 60, and 62 in Effective Java

● Java’s BigInteger and BigDecimal documentation (introduction only)

● Section 19.1 in Code Complete (excluding “Forming Boolean Expression Positively”, “Guidelines for Comparison to 0”, “In C-derived languages …”, “In C++, consider ...”)

● Section 15.1 (“Plain if-then Statements” only) in Code Complete

● Sections 17.1, 18.1 (excluding “Two Issues …”), and 19.4 in Code Complete (“Convert a nested if …”, “Factor deeply nested code …”, and “Use a more object-oriented approach” only)

● The object factory example on canvas


Programming

In this and the next few programming assignments, you will design a Geographic Information System (GIS) that maintains information on landmarks and geographic points of interest.


Overview

A Geographic Information System is a framework for storing geographic data, answering queries about objects located in an area, and presenting them visually as a map. In Programming Assignments 2 through 5, you will develop the GIS. Programming Assignment 2 gets you started with the representation of the underlying data structures. The first objective of this series of assignments is to store information about points of interest along with their longitude (horizontal coordinate) and latitude (vertical coordinate). The main feature of the GIS is that it will count the number of points within a user-defined geographical area.


Package

You should organize your project in a package. The package name is up to you: it could range from the simple (‘gis’) to the detailed (‘edu.cwru.<cwruid>.gis).


Coordinate

A coordinate represents a pair (x, y) of horizontal and vertical values denoting the location of a landmark. Define a

public record Coordinate(BigDecimal x, BigDecimal y)

implements Comparable<Coordinate>

with the following two validation methods:

● public final Coordinate validate() that throws a NullPointerException if either x or y are null, and otherwise returns this Coordinate.

● public static final Coordinate validate(Coordinate coordinate) that throws a NullPointerException if either the coordinate or its x or y values are null, and otherwise returns the argument.

The public static final Coordinate ORIGIN is at the (0, 0) location. A Coordinate (x1, y1) is less than (x2, y2) if x1 < x2 or x1 = x2 and y1 < y2. The public String toSimpleString() method returns a textual representation that is as informative as that from toString but less needlessly verbose.


Error Handling

Exceptions and Assertions

Error checking will be a topic of a future lecture. For the time being, the following guidelines should be implemented:

● If a method is visible outside the package (e.g., public methods in public classes), failed pre-conditions should cause the code to throw an appropriate exception. An exception can carry supplemental information that is passed as the constructor’s argument(s).

● If a method is invisible from outside the package (e.g., private, or package-private methods), failed pre-conditions should be verified by an assert. An assert can specify supplemental information as the expression following the colon.


Supplemental Information

In both cases, the error handling should be supplemented by additional information. In Java, the supplemental information can take the form of one or more of the following:

● Nothing, if the source of the error is clear from other information already available (such as a stack trace).

● A String that outlines the reasons for the error. Strings should only be used if their use conforms to Item 62 in the reading assignment.

● A Throwable that describes the cause of the error.


Null Arguments

A common occurrence of failed pre-conditions are arguments that are null even though null parameters are not expected or meaningful. Unless stated otherwise, you should assume through this series of assignments that null parameters constitute a failed pre-condition that necessitates error handling.

The Java Language Features module on canvas contains pointers on assertions, exceptions, and null parameter checking.


Interest Points

An interest point is a landmark whose information is recorded in the GIS. Create a

public record InterestPoint<M>(

Coordinate coordinate,

M marker)

A marker denotes the general characteristics of a point of interest. With reference to the figure, markers could be RESIDENCE, CLASSROOM, or LAB. The InterestPoint has a

public final InterestPoint validate()

that validates the coordinate and throws a NullPointerException if the marker is null. It also has a

public static final InterestPoint

validate(InterestPoint interestPoint)

like the corresponding method in the coordinate. The InterestPoint delegates the x() and y() methods to the Coordinate, and is has a

public boolean hasMarker(M marker)

that returns whether the coordinate has a marker that equals the argument. The toString returns a compact and informative string representation of the interest point.


2D Maps

A 2D map is used to maintain information about multiple landmarks in a geographical area. To make it easier to extract data about points of interest in an area, the 2D map is organized as a SortedMap of SortedMaps. The top level SortedMap organizes information by the horizontal coordinates and each of its entry contains information about the landmarks that have the same x value. Once the 2D map is accessed by x coordinate, a second level map associates each y coordinate to a Collection containing information about the corresponding location. Here is an example.

In this example, a query for interest points at x=1 returns the association 1→CLASSROOM, 3→{CLASSROOM, LAB}, denoting the landmarks at (1,1) and (1,3). A location such as (1,3) can contain more than one point of interest, as in the case of a building that has both classrooms and labs. Although not shown in the example, the same location can contain more than one point of interest with the same marker, such as a location with two classrooms. Similarly, a query for x=2 gives 2→OFFICE and a query for x=3 gives 1→DINING, 3→RESIDENCE, and 4→RESIDENCE. The two-layer map can be represented as:

The two-layer representation will make it easier in future assignments to find all the points in a rectangle within the mapped area.

Create a public final class BiDimensionalMap<T> that contains a

private final

SortedMap<BigDecimal, SortedMap<BigDecimal, Collection<T>>

points

initialized to an empty TreeMap. To access the information about a specific location, it has a

public final Collection<T> get(

BigDecimal x,

BigDecimal y)

and

public final Collection<T> get(Coordinate coordinate),

which are identical except for the argument type.


2D Map Updates

The BiDimensionalMap also support adding points of interest. However, addition is complicated by the fact it can take many forms. Points can be added one by one or in block, can be specified by (x, y) values or by Coordinates, and collections can be implemented in different ways. To deal with variability, create an inner class public final class Updater of BiDimensionalMap. The Updater has

● private BigDecimal x, y initially set to zero

● private Supplier<Collection<T>> collectionFactory initially set to HashSet::new that supplies the initial instance of the collection stored at the (x, y) coordinates.

● private Collection<T> values initially set to the collection created by the collectionFactory.

All private variables have a corresponding public final setter that returns this Updater. Furthermore,

● public final Updater setCoordinate(Coordinate coordinate) sets the x and y values to those of a valid coordinate and returns this Updater.

● public final Updater addValue(T value) adds a single value to the Updater’s values and returns this Updater.

The Updater has two methods to update the BidimensionalMap:

● public final Collection<T> set() that replaces the information at the (x, y) location with values. The method returns the previous collection at (x, y) if any, or null otherwise.

● public final boolean add() that adds all the values to the (x, y) location of the BiDimensionalMap. If the BiDimensionalMap contains no interest points at (x, y), a new collectionFactory is created so that the values can be copied to it. The method returns whether the interest points previously associated with location (x, y) changed because of this call.

In both cases, if there are no previous interest points at x, a new TreeMap is created so that it can hold the values. If there are no previous interest points at (x, y), a new collectionFactory is created so that it can hold the values.

The BiDimensionalMap has a public final Updater getUpdater() that returns a new Updater for this object.


General Considerations

These classes may contain as many auxiliary methods and classes as you see fit. Helper methods should be private or package-private and helper classes should be package-private. Conversely, any modification or addition to public methods or classes must be approved by the instructors at the discussion board.

        You should write JUnit tests to make sure that your primary methods work as intended. However, we will revisit testing later in the course, so extensive testing is not yet recommended. If you have never used JUnit, the JUnit module on canvas lists resources to get you started. Similarly, your code should have a reasonable number of comments, but documentation is going to be the topic of a future assignment. As a general guideline at this stage of the course, comments and tests should be like those accepted in CSDS 132. Additionally, comments should only be applied to the code sections that you feel are not self-documenting.


Canvas Resources

The module on Java Language Features contains a folder on useful Java features, such as enumerated types, regular expressions, and the optional class. A discussion board can be used to ask questions and post answers on this programming assignment.


Discussion Guidelines

The class discussion will focus on:

● High-level code organization

● The design and documentation of the implementation

● Straight-line code, conditional code

Your grade will be affected by the topics covered in this and in previous weeks. Your discussion leader may introduce material that has not been covered yet in the lecture. Although future topics will not contribute to your current grade, you are expected to follow your leader’s advice in revising the assignment.


Submission

Submit an electronic copy of your program to Canvas. In addition to your code, include a README file explaining how to compile and run the code. The code should be handed in a zip, tar.bz2, or tar.gz archive. Archives in 7z cannot be accepted. You can either bring a laptop to discussion to display your project on a projector, or present your project from the canvas submission.


Note on Academic Integrity

It is a violation of academic integrity not only to copy another group’s work but also to provide your code for other groups to copy including but not limited to posts on public github projects or social media.