T 3/1/011
|
HW due: Read to the end of Chapter 8 (pp. 94-98);
write the exercises below. Some people may be able to dash this off quickly,
but most likely you will need some help. Mr. Hansen will be available during
C, D, and E periods on Monday, 2/28. You are also encouraged to work with
classmates, although each student must produce his own homework paper.
1. As you may have surmised by now, there is essentially no difference
between an int and a char except in how they are handled in expressions. An int can be added or subtracted or multiplied or
divided, whereas a char can be
concatenated. To convert back and forth between an int and a char, we can use a special capability of Java called the
“typecast.” The purpose of a typecast is to convert a variable from one type
to another. The typecast instruction is always the name of the desired type,
in parentheses, followed by whatever you wish to convert.
For example, the following code is redundant, since it prints the char value 'X' as a capital letter X:
char testLetter='X';
System.out.println((char)testLetter);
I say “redundant” since I could just as well have said System.out.println(testLetter);
Saying
System.out.println((char)testLetter);
is rather silly, since testLetter is already a char, and the (char) typecast is a waste of time.
However, if we change the (char) typecast to an (int) typecast, and you should try this, you will have
char testLetter='X';
System.out.println((int)testLetter);
This is much more interesting! Now we get the Unicode value of the upper case
letter X, which is 88.
You can also go the other way. Predict the result of the following code stub,
and then describe in writing what happens:
int
testLetterValue=87;
System.out.println((char)testLetterValue);
2. OK, now that you understand a little bit about converting between an int and a char by using typecasts, run the following stub in your main method:
String alphabet="
ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int i=0;
char scratchChar;
while(i<=26) {
scratchChar=alphabet.charAt(i);
System.out.println((char)scratchChar+"
"+(int)scratchChar);
i++;
}
What is this program doing? Write a sentence or two.
3. In #2, why is there only a blank and the number 32 on the first line of
output?
4. Write a char method called
encryptDecrypt13 that satisfies the following requirements:
Requirement A. The method must return a char and must accept as input a char called arg.
Requirement B. If the input argument is a blank space, and you
can easily check this by saying
if
((int)arg==32),
then return a blank
space.
Requirement C. If the input argument is one of the capital letters
A through M, which you would check by comparing against integer values 65
through 77, then add 13 to the integer value and return the result as a char.
Hint: If you want to do this in a single line, try something like
this:
if ((int)arg>=65
&& (int)arg<=77) {return (char) ((int)arg+13);}
Requirement D. If the input argument is one of the capital
letters N through Z, which you would check by comparing against integer
values 78 through 90, then subtract
13 from the integer value and return the result as a char.
Requirement E. If the input argument is anything other than those
listed in Requirements B through D, then return the asterisk (*) character,
as a char, in order to
indicate an encryption/decryption error.
Use the following code in your main method to invoke encryptDecrypt13:
String mrHansenTest="VCY VF
IREL PUNYYRATVAT, OHG VG PNA OR SHA NYFB";
int i=0;
while
(i<=mrHansenTest.length()-1) {
System.out.print(encryptDecrypt13(mrHansenTest.charAt(i)));
i++;
}
System.out.println(".");
System.out.println("End of
Mr. Hansen's test string.");
Make a printout of your code (both the main method and the encryptDecrypt13 method).
5. Below your program listing, either copy and paste your output, or write
out your output in pencil.
6. Optional bonus: Find a way of improving the readability of your code by
using fewer typecasts in the comparisons. Explain why although your method
works for the char passed to the encryptDecrypt13 method, it would not work for String parameters.
7. Optional super bonus: Write a secret message that would be readable by
anyone else who has a version of encryptDecrypt13 satisfying Requirements A through E.
|
|
W 3/2/011
|
HW due: Write your test review questions. These can
be simple (e.g., vocabulary terms) or hard (e.g., complicated code stub
problems). Try to write things that you personally find challenging. If the
review questions are not challenging enough, additional ones will be posed
for you to answer in class.
In class: Review for test.
|
|
Th 3/3/011
|
Test (100
points, pencil required). You are
responsible for all vocabulary through Chapter 8, as well as all terminology
discussed in class (much of which is not in the textbook), but the only code
stubs you will be expected to produce are those using techniques described in
Chapters 1 through 7, plus typecasts. Grading of code stubs will be somewhat
lenient, since you will be writing your code in pencil instead of on the
computer. For example, an occasional forgotten semicolon will not count against
you, but if you forget all of your
semicolons, that would require some points to be deducted. Squiggly braces
are more important, but you will not penalized for the first squiggly brace
error. Indentation, however, must be logical.
Methods other than main may be entered
above or below main and in any
order you wish, as long as main is either the first or the last method. You need to
know how to insert code stubs in the proper place. However, the following
“wrapper” will be provided for you in order to reduce the amount of material
you have to memorize:
public class TestMarch3 {
public static void main(String[]
args) {
}
}
Simple hex conversions may be tested, but floating-point conversions
(requiring negative powers of 2 or 16) will not be on this test. Here are two
sample problems to show the level of difficulty that is possible:
1. Convert 0xCAB to decimal (base 10).
Solution: C=12, A=10, and B=11.
Place values are 162 = 256, 161 = 16, and 160
= 1. Answer = 12(256) + 10(16) + 11(1) = 3243.
2. Convert the base-10 integer 492 to hex.
Solution: 492 equals 1 group of
256, with a remainder of 236; 236 equals 14 groups of 16, with a remainder of
12. Therefore, 492 = 0x1EC.
|
|
F 3/4/011
|
HW due: Write the program that you did not do on yesterday’s
test. Print out your program listing as well as the output. If you did #6
without using a loop, you must also re-do #6.
3. Square numbers are numbers (1,
4, 9, 16, etc.) based on patterns of dots arranged in squares. It is a
mathematical fact that the nth
square number equals the (n −
1)st square number plus 2n −
1. Use that fact to write a recursive
method called sqNum, which takes an integer argument, n, and returns the nth
square number. Now, clearly, sqNum(n)
could be much more easily computed as n
* n, but that would not be a
recursive method.
Other requirements for sqNum are as follows: (a) sqNum should return an error
value, −9999, as well as a printed error message, if n is less than 0, and (b) sqNum should
return 0 and print a warning message if n
is greater than 25.
Your main method has the following requirements: (a) it must
begin by printing a line that reads, “Table of square numbers and
double-check values,” and (b) it must then print the values for n, sqNum(n), and n2
in three columns, for n between −4
and 29, inclusive.
[Note: Please use the Unicode tab
character, "\t", to create neat columns. Also note that the range
of values for n has been enlarged
from what it was on the in-class version of the test, so that you can make
sure that your error traps are working correctly.]
6. Write a program that prints the base 10 values for 0xC8 through 0xD8, one
per line. [Before you start, you may wish to figure out what the base 10
values for 0xC8 and 0xD8 are, and then code those into your loop condition.
Or, you can simply use the constants 0xC8 and 0xD8 exactly as they are. The
choice is yours. However, you must
use a loop! It is usually considered poor programming practice to hard-code
things that should be in a loop.]
7. Write a program that includes a method called scrunchStr such that (a)
scrunchStr accepts a String argument and prints a shorter string in which
only the 1st, 3rd, 5th, . . . characters are displayed, all together on the
same line, (b) if the argument passed to scrunchStr is null, scrunchStr
should print only the word "null", and (c) your main method causes the scrunchStr method to be invoked
on the null string and the following two phrases:
HappyCal is
fun.
IPL is fun.
The correct output would therefore be as follows:
null
Hpya sfn
ILi u.
|
|
M 3/7/011
|
No class.
|
|
T 3/8/011
|
No additional HW due. JD, please write up all three
program stubs from the 3/4 calendar entry.
|
|
W 3/9/011
|
HW due: Write Exercise 8.4abdf. If you need help, please
call a classmate or leave voice mail at 703-599-6624, since e-mail has been
down for several days and is still down.
|
|
Th 3/10/011
|
HW due: Correct yesterday’s assignment so that it
meets all of the stated requirements and
includes a few well-worded comments; read Chapter 9 (pp. 103-114).
|
|
F 3/11/011
|
HW due: Skim (do not read thoroughly) the Wikipedia article on
Mersenne primes. The 25th Mersenne prime, namely was discovered in
1978. For convenience, we will refer to this gigantic integer as M21701 from now on. It is a
6533-digit monster (in base 10) that would be unmanageable by any computer,
then or now, using primitive integer types.
However, with multiple-precision arithmetic, we can add, subtract, multiply,
or divide numbers this size and even larger! Answer the questions below.
1. In Java, the int type provides 4 bytes of storage. If we elect to treat
the sign bit as if it were the same as any other bit (what C calls unsigned), we would therefore have how many bits of storage
that we could use in a single variable of type int? Hint: A byte equals 8 bits. This
question is meant to be easy. Do not over-think it.
2. Notational reminder: A subscript “2” denotes base 2, and the prefix “0x”
denotes hexadecimal (hex, base 16). For now, do not worry about how the
software would have to be written to deal with large numbers. We will deal
with those questions later. Instead, observe the following patterns and fill
in the blanks:
22 − 1 = 3 = 112 = 0x00000003
23 − 1 = 7 = 1112 = 0x00000007
24 − 1 = 15 = 11112 = 0x________________
25 − 1 = 31 = 111112 = 0x0000001F
28 − 1 = 255 = 111111112 = 0x000000FF
216 − 1 = 65535 = 11111111111111112 = 0x0000FFFF
224 − 1 = ________________ = 1111111111111111111111112
= 0x________________
232 − 1 = 4294967295 = 111111111111111111111111111111112
= 0xFFFFFFFF
221701 − 1 = M21701
= ________________________2
(For the last blank, you are not expected to write out all the bits! Simply
describe what would go in the blank.)
3. Prove that if we were to join 700 int
variables together in some fashion, we could easily represent integers as
large as M21701. Hint: Figure out how many bits are
required to represent M21701,
and then show that 700 int variables would have more than that number of bits.
4. How many hex digits would be required to represent M21701? Hint:
One hex digit equals 4 bits.
|
|
M 3/14/011
|
Pi Day (no class).
|
|
T 3/15/011
|
Beware the Ides of March! No additional written HW
is due. However, there will be a
40-point Vocabulary Quiz
today, which is cumulative through Chapter 9. A few terms from in-class
discussions may also be quizzed. Examples include kludge, rainbow table,
requirements creep, stack overflow, LIFO, FIFO, overflow, and underflow.
Note: Under school policy, you may
be required to take up to 2 tests on any day, but you can obtain an alternate
testing date if your teachers have scheduled 3 or more tests for the same
day. However, quizzes are not covered by this policy. Nevertheless, since
some IPL students (namely Chase and Matthew) already have 2 tests scheduled
for today, anyone who wishes to take the vocabulary quiz after chapel on
Monday, March 14, may do so. If you wish to do that, please send an e-mail to
Mr. Hansen so that an advance copy will be waiting for you. Otherwise, please
plan on taking the quiz during class on March 15.
The quiz will last about 20 minutes and should not be a “killer quiz.” On the
first vocabulary quiz, on Feb. 17, most people did well, and the average
score was 93% (A).
|
|
W 3/16/011
|
HW due: Read pp. 119-121 (through §10.3); write the
exercise at the bottom of p. 121 with the additional requirement that the array
to be copied is an array of 10 integers whose values are the numbers 35
through 44. The copy that results should then be printed, one line per entry.
|
|
Th 3/17/011
|
HW due: Correct yesterday’s assignment so that it
resembles the code stub below (not exactly, of course; you should add a few
comments and enhancements as you deem appropriate). Make sure that it runs
correctly. Then, read pp. 122-127 (to the end of Chapter 10) and modify
yesterday’s assignment (below) so that the second loop uses the length
attribute of array a, instead of the hard-coded upper limit of 9 or 10, when
copying the elements of a to b.
public class ArrayTester {
public static void main(String[]
args) {
int[] a=new int[10];
int[] b=new int[10];
int i;
for (i=0;i<=9;i++)
{
a[i]=35+i;
}
for
(i=0;i<=9;i++) {
b[i]=a[i];
System.out.println(b[i]);
}
}
}
In class: We will plan on having a guest speaker today: Mr. Joe Morris of
MITRE Corporation. Mr. Morris is a St. Albans graduate, class of 1962, and he
has more than twice as much experience in the computer field as Mr. Hansen!
|
|
F 3/18/011
|
Last day of third quarter. No additional HW is due
today, but J.D. still needs to see me regarding the assignment due Thursday,
3/17.
Deadline for all work, including optional extra-credit crossword puzzles (www.mathcross.net) is 3:00 p.m. today.
|
|
|
Spring break.
|
|