CSci 2101 Lab 1. Java variables, data types.

Due Monday, January 20 by 11:59pm (by e-mail)

20 points

Setup

Please work in pairs on this lab.

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.

  1. Write a comment at the top of the file with your name and the lab number. Always do this for all work that you do in this class.
  2. Add a statement to multiply two integer variables (i.e. two variables of type int), print out the result.
  3. Add a statement to divide two ints, 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.
  4. Print out the value of n. Then write
    n = n + 3;
    
    Print out its value again. Briefly explain (in comments) what happens.
  5. Change the comparison x < 5 to x <= 5, verify that it works as expected.
  6. What happens if you compare characters 'a' and 'b' using <?
  7. The maximum positive value that one can store in an int variable is 2147483647. What happens if you attempt to store a larger number?
  8. 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.
  9. 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.
  10. 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.
  11. 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.
  12. 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.

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.

The views and opinions expressed in this page are strictly those of the page author. The contents of this page have not been reviewed or approved by the University of Minnesota.