Computer Science 523
Advanced Programming
Summer 2014, The College of Saint Rose
Lab 1: Java Basics
Due: 4:30 PM, Tuesday, June 3, 2014
This first lab is intended primarily as review for those of you who
are Java programmers, and a chance to adapt your programming knowledge
to Java for those who aren't.
You are to work individually on this lab.
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.
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: Gaddis Checkpoint Exercise 2.22, p. 62 (2 points)
LA Question 2: Gaddis Checkpoint Exercise 3.12, p. 129 (2 points)
LA Question 3: Gaddis Checkpoint Exercise 4.9, p. 206 (2 points)
Practice Programs
Practice Program: Your first task is to compute and display the final
cost, including sales tax, for the purchase of a single article of
clothing. In New York State, clothing is taxable only if it costs
$110.00 or more.
Your input is the price of a single article of clothing (which you
would specify in dollars and cents form like "25.99"). If the cost
is under $110.00, the output would be "No tax on that!". But if it
is $110.00 or more, you will need to compute the sales tax and output
a message such as "With sales tax of $16.00, that will cost you
$216.00." (assuming a purchase of $200.00 and a tax rate of 8%).
Write a Java program in a class called ClothingTax that solves
the above problem. (10 points)
Notes and requirements:
- You may assume a sales tax rate of 8% and the tax threshold
of $110.00, but each of these should be specified only in a named
constant at the top of your main method.
- You must use only JOptionPane dialogs for input and
output - no Scanner or System.out.print[ln] allowed for
this.
- To convert the String that will be returned to your
program by the JOptionPane.showInputDialog into a
double, you should use the Double.parseDouble method,
which works just like the Integer.parseInt we saw in the
class example, except that it expects to be given a String
that can be converted to a double and returns a double.
- Your program should repeatedly prompt for a purchase amount
until a positive number is entered. You need not worry about the
error that occurs if someone enters a non-numeric value.
- Be sure you have exactly 2 digits after the decimal point in
your output.
Practice Program: Your next task is to create a rudimentary password
quality checker. It's so rudimentary that all it does it judges the
quality of the password on the length of the password string.
Your input is a candidate password string. Your output is a statement
about the quality of the password. Passwords whose length is less
than 5 characters is reported as "Poor", 5-8 characters as "Fair",
9-12 characters as "Good", and 13 or more as "Great".
Write a Java program in a class called PasswordChecker that
solves the above problem. You may use either terminal I/O with a
Scanner and System.out.println calls or dialog boxes with
JOptionPanes. (8 points)
Programming Assignment
You are to write a program GradeSummary.java that will be
responsible for reading in the names and grades for a group of
students, then reporting some statistics about those grades. The
statistics to be gathered are the number of scores entered, the
highest score and the name(s) of the student(s) who earned that score,
the lowest score and the name(s) of the student(s) who earned that
score, and the average score for the class.
So if the names and scores are as follows:
Luke 76
Hermione 99
Wally 34
Linus 99
Penny 25
then your program would print output:
There were 5 scores, averaging 66.6.
The lowest score was 25 by Penny.
The highest score was 99 by Hermione and Linus.
- The program will need loops in two places: (i) to be able to
read in data repeatedly, and (ii) to ensure that any erroneous
input is detected and re-read until a valid input is obtained.
- The program should read in names (single-word names are
sufficient here) and scores until the name is entered as
"done", at which time we stop reading names and scores and
report the final statistics.
- All scores should be in a valid range 0 to 100. If a score is
entered outside of that range, your program should issue an
appropriate message and re-read only the score (not the name, we
already know that!) until a valid score is entered.
- The program will need to keep track of a running total of the
number of scores entered and the total number of points (to be used
later to compute the average).
- The program will need to keep track of the highest (and lowest)
score seen so far and the name(s) of the students who earned that
highest (and lowest) score.
- Correct initialization of the variables you will use to keep
track of high and low scores is essential to get this right. Note
that the first score read in should become both the high and low
score after the first time through the main loop. Think about how
you can initialize those variables so that they will be set to the
first score on the first loop iteration without treating that
iteration as a special case (that is, the body of your loop does
exactly the same thing on the first iteration as it will do on all
subsequent iterations).
- Note that if there is a tie (among any number of students) for
the highest and/or lowest score, all students who tied the high or
low should have their name included in the printouts at the end.
Hint: think string concatenation.
- If no valid names and scores are entered, a special message
"You did not enter any scores!" should be printed rather than
potentially erroneous stats.
- Prompt for and read in both the name (which you may still assume
consists of a single word) and score (which you may assume is an
integer) in one step. That is, issue one prompt, then have two
calls to your Scanner: one to read the name, the next to read
the score. If the name is read as done, then do not attempt to
read any score given on that line (nor report any error if no such
score is present on the input).
- Note that all comparisons of String values should use the
equals method, and any comparisons of numeric values should use
==
and !=
.
- Define named constants to represent the highest possible score
and the special name that is used to stop the loop. Those
definitions should be the only places in the program where I see the
int literal 100 or the String literal "done".
- Remember to take the proper precautions when computing your
average (which should be a double) from the scores (which are
all ints). When you print your average, use a
DecimalFormat object to make sure it prints with exactly one
digit after the decimal point.
The program is worth a total of 45 points, as broken down in the table
at the end of this document.
Question 1: Gaddis defines a term for a special value that is
used to terminate a loop, as the special name "done" does
here. What is that term? (1 point)
Question 2: By using the special name "done" in this manner,
we have placed a restriction on the valid names our students may
have. What name would cause a problem for our program? (1 point)
Question 3: Describe in a few sentences your approach to the
"correct initialization" described in the bullet item above. Why
did you choose the initial values you did and how do they ensure
that the first score entered will become the new high and low score
without treating that as a special case. (2 points)
Submitting
Before 4:30 PM, Tuesday, June 3, 2014, submit your lab
for grading. There are four 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". (iii)
Demonstrate the execution of your programs for your instructor.
(iv) 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 75 points, which are distributed as follows:
>
Feature | Value | Score |
Lecture assignment questions | 6 | |
ClothingTax correctness | 10 | |
PasswordChecker correctness | 8 | |
GradeSummary input loop | 7 | |
GradeSummary error checking | 4 | |
GradeSummary reports total number of scores | 4 | |
GradeSummary reports average score | 5 | |
GradeSummary reports high/low scores | 5 | |
GradeSummary report names of all high/low scorers | 6 | |
GradeSummary output formatting | 2 | |
GradeSummary uses named constants | 2 | |
GradeSummary comments | 5 | |
GradeSummary naming conventions | 3 | |
GradeSummary formatting | 2 | |
Using all correct file names | 2 | |
Lab questions | 4 | |
Total | 75 | |
|