Computer Science 210
Data Structures

Fall 2016, Siena College

Lab 1: Java Review
Due: 11:59 PM, Tuesday, September 13, 2016

For a variety of reasons, students sometimes arrive in this course without the level of Java programming expertise needed to jump right in to the new, more advanced programming. The first few labs will include review 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.

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 (hopefully) straightforward tasks. However, you should feel free to ask your instructor, the tutors, and your classmates for help as you work through them.

This lab focuses on local variables, conditionals, loops, console I/O, output formatting, and random number generation. If you think you need to use arrays, multiple classes, or even define multiple methods, you're probably making it harder than necessary on yourself.

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 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 that prints sequences of numbers starting at a lower bound, continuing to an upper bound by some increment. Details below. (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 print the numbers starting at 1, up to and including 5, counting by 1:


For "3", "7", "1", starting at 3, up to 7, by 1, your program should output:


For "-3", "10", "4", counting from -3 to 10 by 4, your program should output:


For "49", "23", "-12", counting from 49 to 23 by -12, your program should output:


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.

One of the things that will be very important this semester is to develop your programs incrementally. That is, you should implement one or two small features, then make sure your program compiles and works properly for those features, before continuing. For example, here, you might first set up a class with a main method that just prompts for the first number. When that works, create your Scanner and use it to read in the first number. When that works, read in the other two numbers. Then do error checking on those numbers, verifying that they make sense. Then write the loop that counts up, then write the loop that counts down. And compile and test the program after each of these steps. This kind of approach not only helps you find problems with your program as soon as you make them, so you know where to look to fix them, it also makes the entire process less daunting, since you are worrying about one small bit of functionality at a time.

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:

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,

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.

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 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. (15 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

Again, it is absolutely essential to approach this problem one small step at a time. Come up with a sequence of features you can add, each of which is likely just a few lines of code long, that once all implemented (and tested) will result in a fully-functional program.


Before 11:59 PM, Tuesday, September 13, 2016, submit your lab for grading. To complete the submission, upload a copy of your lab (a .7z or .zip file containing your project directory) to Blackboard under "Lab1".


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

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