Back to 111 Main Page

Mobius strip

Assignment 03

Task

Get a little more practice with data types, operators, expressions, variables, assignment, and understanding errors.

Textbook: 1.5 (errors); 2.4 - 2.5

Steps

Below are 10 short sections of code. Each one includes one System.out.println statement. For each section, if the code will run successfully, tell me the following things:

  • What is printed to the screen?
  • What is the data type of the argument (the thing in the parentheses) of the println method call? (If this argument is an expression, tell me what the data type is of the resulting single value.)

If the code contains an error that will prevent it from compiling or causes a crash at runtime, instead tell me the following things:

  • Does the error occur at compile time or runtime?
  • What is the cause of the error?
  • What's one way the error be fixed or prevented?

Some of these code snippets are examples of bad code (that you should not emulate, but must still be able to read), and their purpose is often not obvious. So don't worry about logical errors (such as int math, apparently incorrect output, etc) for now. For this assignment, it only counts as an error if the program doesn't compile or doesn't complete (crashes); if it runs, tell me the value and data type of what is printed.

Here are two example code sections and the appropriate answers:

int answer = 3;
System.out.println("Answer: " + answer);

Prints: "Answer: 3"
Data type: String


int average = 3;
int count = 0;
System.out.println(average / count);

Error: runtime
Cause: cannot divide by 0 (count contains the value 0)
Fix: initialize count to 1 rather than 0.

You should first try to answer these questions by just examining the code. Then, to check your answers, paste each section into a main method, compile it, and run the resulting program.

Questions

  1. System.out.println("A baker's dozen = " + 12 + 1);
    
  2. String out = "";
    out += "One, ";
    out = "Two, " +
           "Three, ";
    out += "Four";
    System.out.println(out);
    
  3. int num = 15;
    System.out.println(num / 6);
    
  4. int num = 15;
    System.out.println(num / 6.0);
    
  5. int num = 15.0;
    System.out.println(num / 6);
    
  6. double num = 15;
    System.out.println(num / 6);
    
  7. int num = 10;
    int post = num++;
    int pre = ++num;
    System.out.println("Post: " + post + "   Pre: " + pre + "   Num: " + num);
    
  8. String trip = "\tf\n\ta\n\tl\n\tl";
    System.out.println("Don't" + trip);
    
  9. byte b = 0;
    int s = 5 / b--;
    s -= 5 / (-1 * b);
    System.out.println((short) s);
    
  10. long a, b = 3, c;
    a = c = b * 2 - 3 * 4;
    c *= ++c;
    System.out.println(a);
    

What to submit

Save your answers in a plain text file named UsernameA03.txt. Submit this file to Tamarin.

Grading [4 points]

4 - Questions Answered
0.4 points per question (minus penalty for improper submission).

FAQs

Data type? You mean like "expression" or "variable"?
No. Variables and expressions are not themselves data types. They evaluate to or contain a single value, and it is this value that has a data type associated with it.

Data types are either one of the 8 primitive data types or else a class name. For this assignment, valid data types include: byte, short, int, long, float, double, char, boolean, and String. These are all the data types we've learned about at this point. Remember that primitive data types are all lower case.


~ztomasze Index : TA Details: ICS111: A03
http://www2.hawaii.edu/~ztomasze
Last Edited: 27 Jan 2009
©2008 by Z. Tomaszewski.