Computer Science 210
Data Structures
Fall 2016, Siena College
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:
Java API Documentation: java.util.Scanner
Don't forget to add the
import java.util.Scanner;
at the top of your program.
You create a Scanner capable of reading from the keyboard in your main method with a line such as:
Scanner keyboard = new Scanner(System.in);
This declares a local variable keyboard and initializes it with a reference to the result of the construction of a Scanner.
Once you have this Scanner, you can use its methods to read various kinds of input from the keyboard. In BlueJ, this is also done through the terminal window. For example, you might read a single word and store it in a (newly-declared) variable with code such as:
String streetName = keyboard.next();
Other common Scanner methods include nextLine() to read an entire line as a String, nextInt() to read a number and return it as an int, nextDouble() to read a number and return it as a double, along with hasNext() and related methods to return whether the Scanner has input to return.
Practice Program: Sequence Generation
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:
1 2 3 4 5
For "3", "7", "1", starting at 3, up to 7, by 1, your program should output:
3 4 5 6 7
For "-3", "10", "4", counting from -3 to 10 by 4, your program should output:
-3 1 5 9
For "49", "23", "-12", counting from 49 to 23 by -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.
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:
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.
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
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.
Submitting
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".
Grading
This assignment is worth 35 points, which are distributed as follows:
> Feature | Value | Score |
ConsoleIO correctness | 5 | |
Seq correctness | 15 | |
DiscountCards correctness | 15 | |
Total | 35 | |