Computer Science 252
Problem Solving with Java

Fall 2015, The College of Saint Rose

Lab 1: Java Review, Part 1
Due: 11:59 PM, Thursday, September 10, 2015

Students often arrive in this course with a level of Java programming expertise that is not quite good enough to be able to jump right in to the new, more advanced programming. This is the first of a series of lab exercises designed to refresh and sharpen your prerequisite Java programming skills. There are several programs to write, but all are things you should be comfortable doing, maybe with a little refreshing of your memory, having completed the prerequisites successfully. As these will overlap with assignments and lab exercises on our new material, they are spread out over the first few weeks of the semester.

You are to work individually on these tasks. Searching for similar programs on the Internet (or anywhere) is not permitted, and would be counterproductive to your ability to learn how to approach these straightforward tasks. However, you should feel free to ask your instructor, our graduate assistant, or the ASC tutors for help as you work through them.

This first review lab focuses on local variables, conditionals, loops, console I/O, output formatting, and random number generation.

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.

Practice Program: Console I/O

You saw in class on our first day how to run Java applications in BlueJ. The programs for this lab are all to be Java applications.

As you complete the first program, you'll want to remember the following:

Practice Program: Write a program ConsoleIO.java that reads at least one String from a keyboard Scanner and uses that String as part of its output. This could be as simple as a program that asks for your name, then prints a message that includes your name, or you can get a bit more creative. (5 points)

Practice Program: Sequence Generation

Practice Program: Write a Java program named Seq.java that clones some of the functionality of the Unix seq command, which prints sequences of numbers starting at a lower bound, continuing to an upper bound by some increment. Details below. This practice program counts for 15 points.

The program should prompt for and read in 3 numbers from a keyboard Scanner, the first is a starting value, the second is an ending value, and the last is an increment.

For example, if the three numbers are "1", "5", and "1", your program should output

1
2
3
4
5

For "3", "7", "1", your program should output

3
4
5
6
7

For "-3", "10", "4", your program should output

-3
1
5
9

For "49", "23", "-12", your program should output

49
37
25

Note that the ending value might not always be printed if the sequence generated does not include that value.

If your program is presented with an invalid set of numbers (e.g., a negative increment when the ending number is larger than the starting number) your program should print an appropriate error message and exit. Your program should result in correct output for all cases of parameters that make sense, and print errors in cases where they don't.

Once you have read in and checked the validity of the numbers, you just need a couple of loops to do the printouts.

This program will be graded entirely on the number of example input tests that it handles correctly. These will include parameters that don't make sense (e.g., counting from 1 to 10 by -1), and of course, a variety of valid input parameters.

Practice Program: Random Numbers

Our programs will need to use random numbers from time to time this semester. Java's Random class is the mechanism to use.

Java API Documentation: java.util.Random

Once you have constructed and remembered your Random with a statement such as:

  Random r = new Random();

you are likely to make use of two of its methods most heavily:

nextInt
Called without any parameters, nextInt returns any of the 4.3 billion possible int values that Java knows how to store. That's probably not useful. More likely, you'll want to pass it a parameter, which should be a positive int value. This parameter determines the number of possible values are possible for the return value. So for example,
    r.nextInt(10)

will return one of 10 possible values. We have no choice of the range of those values - in this case it will be one of 0, 1, 2, 3, 4, 5, 6, 7, 8, or 9.

If we want int values in some range other than 0 through an upper bound, we can do some math on the output. For example, if I want an integer in the range 1 through 100, the following will do it:

    r.nextInt(100) + 1

The method call will return a value from 0 to 99, and I then "shift it up" by 1 to get into the range I want.

nextDouble
This method takes no parameters, and always returns a double value in the range from 0.0 to 1.0. If we want a result in a different range, we can multiply or divide our result to "stretch" or "shrink" the range, and add or subtract to shift the range so it doesn't start at 0.0. For example, the following would generate a number in the range 40.0 through 50.0:
    r.nextDouble() * 10.0 + 40.0;

One other item you'll need to remember for the practice program in this section is how to format a floating-point number as a String with a specific number of digis after the decimal point. There are a few ways to do this and you're welcome to use any, but the most common are to use the System.out.printf method or the DecimalFormat class:

Java API Documentation: java.text.DecimalFormat

Practice Program: Write a program DiscountCards.java that implements a random discount card generation for a retail chain. The idea each customer is issued a randomly-generated set of discount cards. Both the number of cards and the values of each card are chosen randomly for each customer. Our program is just a simulation, and the numbers of cards and their ranges of values will be entered from the keyboard. Details below. (20 points)

More specifics about the DiscountCards program:

A sample run of the reference solution follows. Of course, since random numbers are involved, your program would produce different output for the same inputs.

Up to how many cards to generate for this customer? (0 to quit) 4
Minimum discount? 20
Maximum discount? 45.5
Card 1 for 40.4%
Up to how many cards to generate for this customer? (0 to quit) 9
Minimum discount? 1
Maximum discount? 55
Card 1 for 49.7%
Card 2 for 19.9%
Card 3 for 3.5%
Up to how many cards to generate for this customer? (0 to quit) 7
Minimum discount? 87
Must be in range 1-50.  Minimum discount? 0.5
Must be in range 1-50.  Minimum discount? 3
Maximum discount? 2
Must be in range 3.0-60.  Maximum discount? 50
Card 1 for 31.5%
Card 2 for 36.6%
Card 3 for 15.3%
Card 4 for 34.7%
Up to how many cards to generate for this customer? (0 to quit) 0

Submitting

Before 11:59 PM, Thursday, September 10, 2015, submit your lab for grading. There are three things you need to do to complete the submission: (i) Upload a copy of your lab (a .7z or .zip file containing your project directory) using Submission Box under assignment "Lab1". (ii) Demonstrate the execution of your programs for your instructor. (iii) Hand a printout of the Java files that make up the programming assignment (not practice programs) to your instructor. (2 business day grace period for demos and printouts).

Grading

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

> FeatureValueScore
ConsoleIO correctness 5
Seq correctness 15
DiscountCards correctness 15
Total 35