1.00/1.001 Introduction to Computers and Engineering Problem Solving Fall 2002 Name:

advertisement
1.00/1.001
Introduction to Computers and Engineering Problem Solving
Fall 2002
Name:
TA’s Name:
Tutorial:
For Graders
Question 1
Question 2
Question 3
Question 4
Question 5
Total
Problem 1 (10 Points).
Circle TRUE or FALSE for each of the following statements about static variables in Java®.
A. A static data member of a class must be declared using a primitive type.
TRUE
FALSE
B. A static data member of a class is not associated with any particular instance of the class.
TRUE
FALSE
C. A static data member of a class cannot be accessed outside the class.
TRUE
FALSE
D. A static data member of a class must be initialized in a class constructor.
TRUE
FALSE
E. A static data member of a class cannot be changed after it is initialized.
TRUE
FALSE
Problem 2 (8 Points).
Consider the following class definition:
public class Borg {
static int nextID = 0;
int borgID;
}
public Borg() {
/* assign unique id */
XXX
}
We want to give each Borg object a unique identifier. Which of the following lines could
be used in place of the XXX to assign a unique id to each Borg object?
(Circle only ONE choice)
a)
b)
c)
d)
e)
nextID
borgID
borgID
borgID
borgID
= borgID++;
+= 1;
= nextID + 1;
= ++nextID;
+= nextID;
Problem 3 (45 Points).
The following main method uses the functions print and add to produce this output:
Output:
Array a is:
1
3
5
Array b is:
7
8
3
Array c is:
8
11
8
Main Method:
public static void main (String args[ ]) {
int a[] = {1,3,5};
int b[] = {7,8,3};
System.out.println("Array a is: ");
print(a);
System.out.println("Array b is: ");
print(b);
}
int c[] = add(a, b);
System.out.println("Array c is: ");
print(c);
Provide implementations for print and add so that the main method produces the correct
output (see next page).
A. Write code for correctly printing a single array.
public static void print(int [] a) {
//WRITE CODE HERE
}
B. Write code for adding the elements of two int arrays. The result should be returned as a
new array: Assume that the arrays, a and b, have the same number of elements.
public static int[] add(int[] a, int[] b) {
//WRITE CODE HERE
}
When writing these functions remember that the output of main must match:
Array a is:
1
3
5
Array b is:
7
8
3
Array c is:
8
11
8
Problem 4 (25 Points).
Here is a class that has two instance fields: type and weight.
public class Cookie {
// type of cookie: Chocolate, Oatmeal, Raisin, …
String type;
// weight of cookie in grams: 2.9, 3.4, …
double weight;
}
// … rest of class …
Provide a constructor for this class. This constructor should take two arguments: a String
and a double. This will allow a programmer to create a new Cookie object with parameters
like “Chocolate” and 2.9
Provide another constructor for this class. This constructor should take a single String as an
argument. This will allow a programmer to create a new Cookie object with a parameter like
“Chocolate.” When no weight is provided, the Cookie should weigh 2 grams.
Problem 5 (12 Points).
public class SecretCode {
public int publicKey;
private int secretKey;
private SecretCode(int pkey, int skey) {
publicKey = pkey;
secretKey = skey;
}
public static void main(String[] args) {
SecretCode code = new SecretCode(5358979, 3238462);
UserAccount acct = new UserAccount("beaver", "timmit");
System.out.println("code's
System.out.println("code's
System.out.println("acct's
System.out.println("acct's
A
B
C
D
}
}
public key is
secret key is
username is "
password is "
"
"
+
+
+code.publicKey);
+code.secretKey);
acct.username);
acct.password);
System.exit(0);
class UserAccount {
public String username;
private String password;
}
public UserAccount(String u, String p) {
username = u;
password = p;
}
Consider the program above, which consists of two classes that are contained in the same
file. Circle TRUE or FALSE for the following statements.
8a. The line at A will compile and run.
TRUE
FALSE
8b. The line at B will compile and run.
TRUE
FALSE
A
B
C
D
8c. The line at C will compile and run.
TRUE
FALSE
8d. The line at D will compile and run.
TRUE
FALSE
Java® is a trademark or registered trademark of Sun Microsystems, Inc. in the United States and other countries.
Download