UNIT 2 ASSIGNMENT 1 -- SHOEBOXES AND LABELS

things-shoeboxes.gif (3261 bytes)

 

PART A

1. Assume that the following declarations apply:

int i;
char c;
boolean b;
String d;
float e;
short f;

 

For each item below, give the type of the item.

  1. 42
  2. -7.343
  3. i
  4. 'c'
  5. "An expression in double-quotes"
  6. b
  7. false
  8. "false"
  9. c
  10. 'b'
  11. "b"

2. For each of the following definitions, fill in a type that would make the assignment legal.

__________ a = 3;
__________ b = true;
__________ c = 3.5;
__________ d = "true";
__________ e = "6";
__________ f = null;
__________ g = 0;
__________ h = '3';
__________ i = '\n';
__________ j = "\n";

[Footnote: There are several answers to some of these, but in each case only one "most obvious" type. It is this "most obvious" type that we are after.]

3.a. Assume that the following statements are executed, in order.

int a = 5, b = 7, c = 3, d=0;
a = b;
c = d;
a = d;

What is the value of c? Of a? Of b? Of d?

b. Assume that the following statements are executed, in order.

int a = 5, b = 7, c = 3, d=0;
a = b;
b = c;

What is the value of a? Of b? Of c?

c. Assume that the following statements are executed, in order.

char a = 'a', b = 'b', c = 'c', d='d';
a = b;
c = a;
a = d;

What is the value of a? Of b? Of c? Of d?

d. Assume that myObject is a name bound to an object (i.e., myObject is not null). After the following statements are executed in order,

Object a = myObject;
Object b = null;
Object c = a;
a = b;

which of a, b, c, or myObject is null? (The answer may be none, one, or more than one.)

e. Assume again that myObject is a name bound to an object (i.e., myObject is not null). After the following statements are executed in order,

Object d = myObject;
d = null;

is either one or both of d, myObject null?

f. Assume one more time that myObject is a name bound to an object (i.e., myObject is not null). After the following statements are executed in order,

Object e = myObject;
myObject = null;

Now which of e, myObject (or neither, or both) is null?

4. Which of the following could legitimately be used as a name in Java?

3PO
R2D2
c3po
luke
jabba_the_hut
PrincessLeia
Han Solo
obi-wan
foo
int
Double
character
string
goto
elsif
fi

5. Assume that the following declarations have been made:

int i = 3;
int j;
char c = '?';
char d = '\n';
boolean b;
String s = "A literal";
String s2;
Object o;

Complete the following table:

 

Name

shoebox or label?

Value (or null?)

i

   

j

   

c

   

d

   

b

   

s

   

s2

   

o

   

6. Assume that there is an already-defined object type called Date and that today is an already-defined Date name with a value representing today's date. Declare a new name, yesterday, and give it the value currently referred to by today. (This would be useful, e.g., if it were midnight and we might soon want to update the value referred to by today.)

PART B

  1. Run the examples in the exercises.

  2. Rewrite exercise 2.1 with your initials.

 

PART C

Write a program for each of the following:

1. Write a program which prompts the user for the first, middle and last name. Then prints them like a greeting.

    ENTER YOUR LAST NAME:  O'CONNOR

    ENTER YOUR FIRST NAME: SANDRA DAY

    HELLO, SANDRA DAY O'CONNOR

2. Write a program that initializes an integer variable n, and uses n % 1000 to remove the thousands digit from n and so on. Your program will take a number like      n = 5814 and display a message of the digits of a n are 5,8,1, and 4. The user is prompted for a number with 4 digits. Your program will display the four digits.