CSci 2101 Lab 1. Java variables, data types, conditionals.
Due Monday, January 30 by 11:59pm (by e-mail)
25 points
Setup
Please work in pairs on this lab.
- Log in to one of the lab machines. If you just got an account, I
will tell you what your password is.
- Start a terminal window (go to Activities and type "terminal"; once it is found, you might want to right-click and add it to the start menu)
- 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).
- Go to Activities and click on the file cabinet icon to get to your file system. 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, copy the program text into it from
FirstJava.java, and save it under the same name
(don't forget the .java extension) in
the folder that you just created.
- In the terminal window type
cd
followed by the path
to the file. Use Tab
key for auto-complete.
- 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. Don't forget to recompile your program after every
change.
- Write a comment at the top of the file with your name, the name of your group partner, and the
lab number. Always do this for all work that you do in this
class.
- Add a statement to multiply two integer variables (i.e. two
variables of type
int
),
print out the result.
- Add a statement to divide two
int
variables
(say, n and m), store the result in another
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.
- What happens if you compare characters 'a' and 'b' using
< ?
- The maximum positive value that one can store in an int variable
is 2147483647. What happens if you attempt to store a larger
number?
- Declare a new variable of any primitive type (give it any name you
want that's not in
the program already), initialize it, then print its value. Add a
statement to change its value, print it out again. Feel free to use
types
long
, short
, etc if you prefer.
- An operation
%
computes the remainder (also known as the
residue) of integer division;
it is defined on ints. Add a few statements to the program to check how it
works. Does it correspond to the mathematical definition of a
remainder on integer numbers? Write down your
observations.
-
Declare an integer number k, then write an if/else statement that will print one of the 4 things:
- k is divisible by 2, but not by 3
- k is divisible by 3, but not by 2
- k is divisible by both 2 and 3
- k is not divisible by 2 or 3
Test your program with different values of k, write down (in comments) which values you have tried.
- At the end of the program add the statements that call the
method
charAt
on the string name
:
System.out.println(name.charAt(1));
System.out.println(name.charAt(2));
Explain what gets printed. Change the numbers passed
to charAt
if you would like to study the method more.
Add another statement to print the very
first character of the string.
After you have studied the method experimentally, read the description of the method
in the Java String API (description of all string methods). Continue consulting the API for the rest of the lab questions on strings.
-
The method
length()
(with no parameters) returns the
length of the string. Call it on the string name
to print out the
string's length.
-
Using the methods from the previous two questions to print the last character of a string. Make sure that your approach works even when you change the string to a longer or a shorter one.
-
Use the method
substring()
to print the portion of the string from the second character to the end (i.e. all the string except the first character). Make sure it works even if you change the string (i.e. you don't just access the character at a fixed position, but the position adjusts as the length of the string changes). Consult the API for the description of the method.
-
Use the method
substring()
to print the portion of the string from the first character until the second-to-last (i.e. all the string except the last character). Make sure it works even if you change the string. Consult the API for the description of the method.
- 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, then recompile the program to make sure that it compiles.
- Make a deliberate error in your program that doesn't prevent your program from compiling, but makes it break at running time
- Come up with two "what if" questions (for instance: what if I
assign a double to a float? What if I declare two variables with the
same name?). Write down your questions, your tests, and your
answers.
- Use any string method not mentioned above and explain (in comments) what it does.
How to submit
Send the FirstJava.java file with all your comments by email to me:
elenam at morris.umn.edu, cc your group partner. The subject of the message must be 2101
Lab 1.
CSci 2101 course web site.