CPTR 124 Fundamentals of Programming
The purpose of this lab is to explore Java's primitive types and become familiar with their capabilities and limitations.
- Becoming acquainted with the lab
As in Lab 1, quickly read over this entire lab before you begin to work on the computer. This will give you a good overview of what we will be doing today.
- Teams
You are encouraged to work with a partner for this lab. You may elect to work alone, if you prefer.
- Preparing your working folder
Within Windows, open an Explorer window and go to your H: drive. (This is the virtual drive space provided by Information Systems.) Make a subfolder named Lab2 within your H:\cptr124 folder.
- Using the DrJava Interactions pane
We will not write any programs in this lab. Instead, we will use DrJava's Interactions environment exclusively. Start DrJava and increase the size of the Interactions pane. This will make working within the Interactions pane more convenient.
As a warm up, perform the following exercise.
- Type some Java expressions into the Interactions
pane, such as:
> int x; > x = 2; > x
- Next, right click anywhere within the Interactions pane. A popup
menu should appear with options that include:
- Cut
- Copy
- Paste
- Print Interactions...
- Print Preview...
- Execute Interactions History...
- Select the Save Interactions History... option and
name your file warmup. Be sure to
save it to your H:\cptr124\Lab2 folder. The filename is
automatically given a .hist extension.
- Reset the Interactions history via Reset Interactions.
Your Interactions history should now be clear, so any variables
you have defined and any interactions you performed are now
gone--lost forever.
Resetting the Interactions allows you to start all over again
fresh, and that is why the confirmation dialog is displayed
giving you one last chance to change your mind.
- Next, select Execute Interactions History... and
choose the interactions history you just saved. Notice that
all your interactions are replayed, and you may continue
where you left off before you reset the history in the
previous step.
- Finally for this warm up, select PrintPreview... to see how your interactions would look if you printed it out. You should not print anything now, but later you will be asked to print out your interactions history to show how you obtained your answers to various questions.
The remainder of this lab asks you questions about some of Java's primitive types.
- Type some Java expressions into the Interactions
pane, such as:
- Exploration Questions
Each of the following questions covers some aspect of Java's primitive types, expressions, variables, or arithmetic. While you can find some of the answers in the textbook, your job is to verify or even discover the right answers from experimenting with DrJava's Interactions environment.
After you work through the questions you will save the Interactions session and print out its transcript. Then you will use a pen or pencil to write on the printout indicating which portion relates to which question. You will also write the answer to the question on the printout.
Save your interactive session frequently (every ten minutes, for example) so you won't have to redo as much work if for some reason something goes wrong during the session.
For some questions you must be a little clever; what you do should make it clear that your answer is correct. For example, you cannot just type in what you believe to be the largest integer and have the interpreter echo it back; you must show that the integer that follows the largest integer clearly is not what it ought to be (which you can do by adding 1 to it). The answers to almost all the questions can be discovered (or proven, if you already know the answer) with little trouble. At least one of the questions, however, will require more in-depth thought and experimentation. Sometimes an answer to a question may result in the interpreter reporting an error; for example, an attempt to divide by 0. If you did not violate the rules of the language, though, the error message can itself be an answer to the question.
For the following questions, integer means any integral type (int, short, etc.), and floating point means either floating point type (float or double). All variables you use should be declared before their use!
- What is the largest integer (int) that Java supports?
- What is the smallest integer (int) that Java supports?
- What is the largest extended integer (long) that Java supports?
- What is the smallest extended integer (long) that Java supports?
- What is the largest 16-bit integer (short) that Java supports?
- What is the smallest 16-bit integer (short) that Java supports?
- Can chars be treated like numbers? (Can you assign a number to a char variable, for example?)
- Can numbers be treated like chars? (Can you assign a character to any numeric variable, for example?)
- Are floating point numbers precise? (Hint: They're not, but can you show it?)
- Can a float value simply be assigned to an int variable?
- Can an int value simply be assigned to a float variable?
- Can a float value simply be assigned to a double variable?
- Can a double value simply be assigned to a float variable?
- How can you force a double value to be assigned to an int variable?
- What happens when you add an extremely large floating point number to an extremely small floating point number? (Report some interesting result.)
- Which stores a greater range of values, double or float?
- What happens when you divide an integer value or variable by zero?
- What happens when you divide a floating point value or variable by zero?
- Can a floating point number be added to an integer?
- What happens when a larger int is divided by a smaller int?
- What happens when a smaller int is divided by a larger int?
- How can you force the division of two integer variables to give you an accurate floating point result.
- What happens with division when one or both of the operands are floating point values or variables?
- How do you find the remainder when dividing two integers?
- What happens when you try to print a boolean variable?
- What happens when you try to print a boolean value (true or false)?
- Can a boolean value simply be assigned to a numeric variable?
- Can a number simply be assigned to a boolean variable?
- Can a type cast be used to overcome direct boolean and number conversion restrictions?
- What happens when you attempt to reassign a constant?
- What are two other, shorter, ways to write the following statement? x = x + 1;
- Check out
I will review your lab with you before you leave. You should then print out a paper copy of your interactions activity that justifies your answers to the questions. Annotate the interactions printout with a pen or pencil, indicating the question number corresponding to a given sequence of interactions that answer that question. Write the answer to the question on the printout as well, because the answer for some questions may not be obvious from just the printout alone.
Be sure your print out has your name on it. If you worked with a partner, please submit only one printout with both names on it.
- Log out
Don't forget to log out on your lab workstation before you leave.