CSci 2101 Lab 1. Java variables, data types.
Due Tuesday, May 24th at 11:59pm (by e-mail)
25 points
The lab may be done in pairs or individually.
Setup
- Log in to one of the lab machines. If you don't remember your
password, I will
help you reset it.
- Start a terminal window (Applications -> System Tools -> terminal)
- If you just got a new account, reset your password to something
you can remember:
type
passwd
on the command line in a terminal window. You
will be asked to type your current password and then
the new password twice. Note that as you are typing your password,
nothing shows up on the screen (as a security measure).
- Create a subfolder in your home directory for this course files
(for instance, csci2101) and a subfolder for
the lab (lab1). Open a new file using jEdit and save
FirstJava.java (under the same name) into
the new folder. Open the file with jEdit.
- In the terminal window type
cd
followed by the path
to the file.
- Compile your file:
javac FirstJava.java
. If there are
no errors,
run it: java FirstJava
.
Lab questions
When you are answering these questions, feel free to delete some of
the program code
or comment out unnecessary print statements. All your newly added code
should be in main.
- Write a comment at the top of the file with your name(s) and the
lab number. Always do this for all work that you do in this
class.
- Add a statement to multiply two integer (i.e.
int
) variables,
print out the result.
- Add a statement to divide two integers, store the result in a
variable, and print that variable. What happens when the two numbers
cannot be evenly divided? Write a comment in your program to explain
the results.
- Print out the value of n. Then write
n = n + 3;
Print out its value again. Briefly explain (in comments) what happens.
- What does
n++;
do? Briefly explain (in comments).
- Based on the four printouts with a a comment "implicit type conversions" explain
what happens when division works on a mix of integer and double numbers.
- Change the comparison
x < 5
to x <= 5
, verify that it works as expected.
- What happens if you compare characters 'a' and 'b' using <?
- Make two deliberate errors in the program that cause it to fail compilation.
What changes did you make? What were the error messages? Were they
understandable? Comment out the errors.
- Come up with two "what if" questions (for instance: what if I
assign a double to an int? What if I declare two variables with the
same name?). Write down your questions, your tests, and your
answers.
How to submit
Send the FirstJava.java file with all your comments by email to me:
elenam at morris.umn.edu. The subject of the message must be 2101
Lab 1. Make
sure to CC your group partner if you worked with another person.
CSci 2101 course web site.