Computer Science 252
Problem Solving with Java
Fall 2015, The College of Saint Rose
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:
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 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:
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
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:
> Feature | Value | Score |
ConsoleIO correctness | 5 | |
Seq correctness | 15 | |
DiscountCards correctness | 15 | |
Total | 35 | |