Computer Science 501
Data Structures and Algorithm Analysis

Fall 2015, The College of Saint Rose

Lab 1: Java Refresher
Due: 11:59 PM, Friday, September 11, 2015

This week's lab exercise is intended to bring you up-to-speed or back up-to-speed, as the case may be, with Java programming.

There are some questions to answer, practice programs to write, and one more substantial programming assignment to complete.

You may work alone or with a partner on this lab. Only one submission per group is needed.

Getting Set Up

To get your BlueJ environment set up for this week's lab assignment, start BlueJ and choose "New Project" from the "Project" menu. Navigate to your folder for this course and choose the name "Lab1" (no spaces) for the project.

Create a document where you will record your answers to the lecture assignment and lab questions. If you use plain text, call it "lab1.txt". If it's a Word document, you can call it whatever you'd like, but when you submit, be sure you convert it to a PDF document "lab1.pdf" before you submit it.

Bonus Review Opportunity

We have access to an automated Java review system called "Problets". You can earn up to 16 bonus points for completing problets. The link will be given in class. There are about a dozen Java problets available, and you will earn 2 points each for each problet completed, up to a total of 16 points. You may do more, but the bonus is limited to 16 points. To earn credit, provide the completion code produced when you finish each problet in your submission for this lab. The problets are to be done individually even if you work with a partner for other parts of the lab.

Lecture Assignment Questions

We will usually discuss these questions at the start of class on the lab due date, so no credit can be earned for late submissions of lecture assignment questions.

LA Question 1: I would like to get a better sense of everyone's backgrounds coming in. Please answer each of the following. (4 points)

LA Question 2: Bailey Problem 1.2, p. 26. (2 points)

LA Question 3: a. Show the steps needed to compute the GCD of 31415 and 14142 using Euclid's algorithm. b. How much faster is this than the more naive algorithm that checks consecutive integers starting at 14142 (the smaller of the two numbers whose GCD we are computing) until the GCD is found. (5 points)

LA Question 4: Once upon a time a farmer went to market and purchased a fox, a goose, and a bag of beans. On his way home, the farmer came to the bank of a river and rented a boat. But in crossing the river by boat, the farmer could carry only himself and a single one of his purchases - the fox, the goose, or the bag of the beans. If left together, the fox would eat the goose, or the goose would eat the beans. The farmer's challenge was to carry himself and his purchases to the far bank of the river, leaving each purchase intact. Find the shortest sequence of crossings you can (where the farmer can carry 0 or 1 of his purchases in the board on each crossing) that solve the problem. (See if you can work it out - don't just look it up!) (5 points)

LA Question 5: Write a method that takes an array of int values as its parameter and returns the number of elements in the array that contain a positive number. (5 points)

LA Question 6: Write a recursive method, sumOdd(int n) to find the sum of the first n odd numbers, for n >= 1. I.e., compute 1 + 3 + 5 + ... + (2n-1). If n <= 0, simply return 0. To receive full credit for this problem, your solution must use recursion and not use any loops. (5 points)

Practice Programs

Practice Program: Write the class described by Bailey Problem 1.8, p. 27, in a file PhoneNumber.java. In addition to fields and constructors as required, include an appropriate toString method, and write a main method that thoroughly tests your implementation. (7 points)

Practice Program: Write the class described by Bailey Problem 1.12, p. 27, in a file ComboLock.java. Again, include a main method that thoroughly tests your implementation. Notes: (i) the first parenthetical clause tells you that your class will have two constructors: one that specifies a combination and one that uses the default, and (ii) you may safely ignore the "Aside" in the question if it is confusing. Additional information below. (10 points)

The press method corresponds to someone entering the next number in the combination. One of these things can happen when someone calls press:

Practice Program: Write a program MatrixStats.java that reads in a table of data from a file and the computes and prints various statistics about that data. Details about the program are below. (15 points)

Notes and requirements for the MatrixStats.java program:

For the input file shown above, my program produces the following output:

Minimum element at [2][0] is -17
Maximum element at [1][3] is 23
Overall sum: 35, average: 2.92
Row 0:
  Minimum element at [0][2] is -2
  Maximum element at [0][0] is 9
  Row sum: 10, average: 2.50
Row 1:
  Minimum element at [1][0] is 0
  Maximum element at [1][3] is 23
  Row sum: 28, average: 7.00
Row 2:
  Minimum element at [2][0] is -17
  Maximum element at [2][1] is 10
  Row sum: -3, average: -0.75
Minimum row sum in row 2 is -3
Maximum row sum in row 1 is 28
Column 0:
  Minimum element at [2][0] is -17
  Maximum element at [0][0] is 9
  Column sum: -8, average: -2.67
Column 1:
  Minimum element at [0][1] is 1
  Maximum element at [2][1] is 10
  Column sum: 13, average: 4.33
Column 2:
  Minimum element at [0][2] is -2
  Maximum element at [1][2] is 3
  Column sum: 1, average: 0.33
Column 3:
  Minimum element at [0][3] is 2
  Maximum element at [1][3] is 23
  Column sum: 29, average: 9.67
Minimum column sum in column 0 is -8
Maximum column sum in column 3 is 29

Programming Assignment

Write the program for the lab exercise at the end of Chapter 1 of Bailey.

Submitting

Before 11:59 PM, Friday, September 11, 2015, submit your lab for grading. There are two things you need to do to complete the submission: (i) Copy your file with the answers to the lecture assignment and lab questions into your project directory. Be sure to use the correct file name. If you prepared your answers in Word, export to a PDF file and submit that. (ii) Upload a copy of your lab (a .7z or .zip file containing your project directory) using Submission Box under assignment "Lab1".

Grading

This assignment is worth 100 points, which are distributed as follows:

> FeatureValueScore
Problets bonus points up to 16
Lecture assignment questions 26
PhoneNumber.java instance variables 1
PhoneNumber.java constructors 2
PhoneNumber.java isTollFree method 1
PhoneNumber.java toString method 1
PhoneNumber.java complete main method 2
ComboLock.java instance variables 2
ComboLock.java general constructor 1
ComboLock.java default constructor 1
ComboLock.java press/reset/lock/isLocked 4
ComboLock.java complete main method 3
MatrixStats.java command line parameter 1
MatrixStats.java read from file 2
MatrixStats.java error checking 2
MatrixStats.java compute and print all stats 10
Basic Date class 5
Date constructor to generate an arbirtrary date 3
Date constructor to generate valid random dates 5
Method in Date to compute day of week 5
main method to play Conway's game 8
Date.java comments 5
Date.java naming conventions 3
Date.java formatting 2
Thought questions 5
Total 100