COSC 199 002 Assignments SPRING, 2007

(The 2nd and 3rd below refer to the page numbers in the 2nd and 3rd editions)

Assignment # / Description / Due date
1 / Hello world (in the Writing, compiling, and running your first Java program notes)—single-line comment at beginning / 02/12/07
2 / Hello world revised—multi-line comment at beginning / 02/12/07
3 / #2.9, p. 114 (2nd), p. 105 (3rd) / 02/19/07
4 / #1, p. 113 (2nd), p. 104 (3rd) / 02/23/07
5 / #5, p. 114 (2nd), p. 105 (3rd)
Three integer variables
Assign 62 and 99 to the first two
The variable total should be the sum of the first two variables
Use the variable names in the println statement(s)
Printout should look like:
The sum of 62 and 99 is 161 / 02/28/07
6 / #6, p. 114 (2nd), p. 105 (3rd)
Two float variables
Assign $4.6 million (as a floating point constant, with no dollar sign or commas!) to the first
Calculate the East Coast sales prediction from the first, using a floating point constant (literal) to represent 62%
Printout should look like:
With total sales of $460000.0 the East Coast division should bring in $285200.0 / 02/28/07
7 / Program question #6 from the recent test. / 03/02/07
8 / #12, p. 115 (2nd), p. 105 (3rd)
Rather than ask the user for a city name, just assign one. / 03/07/07
9 / #12, p. 115 (2nd), p. 105 (3rd)
Do the problem as stated in the book. I.e., issue a prompt (a message) asking for a favorite city; get the value of the city from a keyboard.nextLine method call. / 03/07/07
10 / #11, p. 114 (2nd), p. 105 (3rd)
Ask for all of the information with input dialog boxes. Before asking for the retail price, ask for the person’s name.
All output should be in an output dialog. Make sure to “echo” the name and retail price.
If the person’s name is John Doe, and the retail price is 100, the output dialog box should read:
The retail price entered by John Doe was $100.0.
The profit will be $40.0. / 03/09/07
11 / #5, p. 187 (2nd), p. 171 (3rd)
Ask for the mass with an input dialog box. Right after you enter the mass, an output dialog box should come up, indicating what mass was entered, and what the corresponding weight is. Sample output dialog box:
10.0 kilograms is equivalent to 98.0 Newtons
(Here, the mass entered was 10, the program calculated the 98 Newtons using the formula in the text). In this case, the weight was “in between” (10 and 1000 Newtons). Had the mass been 0.1, for example, the weight would have been 0.98—in this case, a second output dialog box should have appeared (after the first one), saying The object is too light. Had the mass been 1000.0, the weight would have been 9800.0, and a second output box should have appeared saying The object is too heavy.
Run the program at least 3 times, so that you test all possible situations (weight more than 1000 Newtons, weight less than 10 Newtons, and weight somewhere in between). / 03/14/07
12 / #2, p. 186 (2nd), p. 171 (3rd)
Input the abbreviation using the Scanner keyboard-input method. Use an if-else if-…-else structure to determine which abbreviation was entered, comparing to “NC”, “SC”, etc. The else part should be reached only if an illegal abbreviation was entered. Use the equalsIgnoreCase method in your if statements.
Make sure to test all of the possible paths—i.e., enter each abbreviation (with mixtures of upper and lower case thrown in), and enter a “bad” abbreviation. / 03/26/07
13 / #13, p. 189 (2nd), p. 171 (3rd)
Ask the user for A, B, or C, then for the number of hours used. Use a switch statement to determine which subscription package to use, making sure to allow for an incorrect package letter. You will need if statements to calculate the charges for additional hours, if any. Print the base amount charge, and, separately, the additional charge (if any) for the total. / 04/04/07
14 / #2, p. 249 (2nd), p. 234 (3rd)
Use System.in.nextInt() to read in the total number of (integer) hours totalHoursand System.in.nextDouble() to read in the (double) speed. Use while statements to make sure that the total hours cannot be less than 1, and that the speed cannot be negative (or zero!).
You will need an integer variable hour to act as the loop variable—it should range from 1 through totalHours. You will need a double variable distance, calculated inside the loop for each value of hour.
After printing the heading and dashed line, use printf to make sure that the hours and distances traveled in the detail lines are properly aligned.Use a count-controlled while loop to print out the detail lines. / 04/11/07
15 / First open assignment 14, change the class name to something else, and then Save As with the new file name. Replace the initialize-counter/test-with-while/body/increment in #14 with a for loop/body (same body). / 04/20/07
16 / #5, p. 250 (2nd), p. 235 (3rd)
Use the Scanner nextLine() method to read in both the string to be searched, and (indirectly) the character—remember that, in order to read in a single character, you must first read in a string, then use charAt(0) to get the first (and only) character. (Cf. p. 96). Note that you will need two (2) string variables: the first to hold the string that you will search; the second to for getting the single character that you will search for.
In a for loop, with the loop indexi range controlled by the length of the string (use the method length()), search for occurrences of the character by comparing each character of the string (using the method charAt(i)) to the search character. If the two are equal, increment the count.
Note that the index of the last character in the string is one less than the length of the string. This means that the test in the for statement should use <, not <=. / 04/25/07
17 / #3, p. 249 (2nd), p. 234 (3rd)
Now that you know about files, you can do this problem. Mostly, it amounts to including declarations for the outputFile object, and then changing System.out.printf (or .println) to outputFile.printf (or .println). Have the user enter the filename (distancefile.txt). / 04/30/07
18 / #8, p. 250 (2nd), p. 235 (3rd)
This is a nested-loop problem. The “outside” count-controlled loop is controlled by the number of years; the “inside” loop in each pass through the outside loop is also count-controlled (the count being 12, of course).
Instead of reading in the data from the keyboard, read it from the file rainfall.txt (also downloadable from this web site). Don’t worry about input validation. If you check the file out, you’ll notice that it begins with a count of 3, and then has 3 sets of 12 numbers (the monthly rainfall amounts). / 05/04/07
19 / #10, p. 251 (2nd), p. 236 (3rd)
We went through the “largest” logic in class. The logic for the smallest is similar.
Make up your own input data file (largestsmallestinput.txt) with a sentinel value -9999, and write the echoed data and the largest and smallest to an output file (largestsmallestoutput.txt). / 05/11/07
20 / #6, p. 250 (2nd), p. 235 (3rd)
Use the file sillytext.txt as your input file. Use end-of-file logic to loop through the file, after which you can print the count. Run the program at least three times:
  • once with a character which does appear in the file,
  • again with a character which does appear in the file,
  • and finally with one that does not.
/ 05/18/07