COMP 110-003 Fall 2008

Lab 2

25 points

Assigned: Friday, September 5
Due: Friday, September 12 by 2:00pm

Description

In-class exercise 1

Download Lab2InClass1.java and try to compile it in jGRASP.

  1. You will get a compiler error. What is the error? Why did you get this error?

  2. Use type casting to fix the code. Compile the code again. Run the code and input the value 20. What is the output of the program?

  3. Run the code again, and this time input the value 2000. What is the output of the program? Why? Hint: look at Display 2.2 on p. 53 (4th edition) or Figure 2.1 on p. 48 (5th edition).


In-class exercise 2

Download Lab2InClass2.java and compile it in jGRASP.

  1. Run the program, and enter 3 as the input. Why does the output say 0.00%?

  2. Use type casting to fix the code.


Part 1 (7 points)

Characters are stored as numbers in a computer. Each character has a unique Unicode character code. Appendix 3 (4th edition) and Appendix 7 (5th edition) have a subset of the Unicode character set. Download UnicodeFun.java (don't forget to add the appropriate header!). Compile the program and run it. You should see:

Hello world!

Note that the " world!" part of the output is constructed using integers that are type cast as characters. Change the program so that it instead says:

Hello yourfullname!

where yourfullname is, of course, your full name. For example, for me, the output would be:

Hello Luv Kohli!

Everything that comes after "Hello" must be constructed using integers that are type cast as characters. Yes, I realize that some people's names are longer than other names, but it will be so exciting to see your name printed using type casting that it won't matter! :-)


Part 2 (15 points)

Download the skeleton program StringFun.java. Compile and run the program in jGRASP. (Don't forget to add the appropriate header!)

For this part of the lab you will modify the code StringFun.java to prompt the user for a string (of characters) and output information about the string as well as manipulate the input string. You will be referencing pages 79-84 (4th edition) or 75-80 (5th edition) of your textbook.


Additional Questions (3 points)


Grading


How to hand in the assignment