|
|
Homework #3Due: Wednesday, January 28, by 5:00 p.m. No late asssignments will be accepted.You may turn in this written assignment in lecture that day, or turn it in at the front desk of the CSE main office in the Allan Center before that office closes at 5:00 p.m. Some of these questions ask you to analyze code and describe what it does. Obviously you could type the code into DrJava and just transcribe the answer. But don't even think of doing that until you've tried to solve the problem on paper first, or you won't gain the benefit of doing the exercise. 1. Consider the following Java class definition, which would be stored
in a file named
public class Mover { private int count; private double strength; public Mover(boolean superSize, double power) { if (superSize) { count = 10; } else { count = 1; } strength = count * power; } public double getStrength() { return strength; } public boolean isAbleToMove(double load) { return (strength >= load); } } 2. Assume that the source file in problem 1 has been compiled to produce
Mover b = new Mover(true,2.0);
3. Write down the shortest possible class definition that will compile
and produce a class file. (The keyword 4. Refer to the Java library documentation for the
5. Consider the following code. public class ChatNode { private String name; private String host; private boolean paid; public ChatNode(String chatName,String chatHost) { name = chatName; host = chatHost; paid = false; } public String getName() { return name; } public boolean getPaid() { return paid; } public void setPaid(boolean paidStatus) { paid = paidStatus; } public String toString() { if (paid) { return name + ":" + host; } else { return name + ":" + host + " (lite)"; } } }
6. Assume that the source file in problem 5 has been compiled to produce
ChatNode c = new ChatNode("dwj","ivisit.net");
7. For each of the following statements, assume that the variable
8. Imagine that we have run the following lines of code inside a method: int x = 4; double y = 6.0; boolean z = true; Once again, for each of the following statements, assume that the variable
9. Consider the following snippets of code which have been cut out of some method. For each snippet, say what the value of the variable "answer" is at the end.
a. int answer = 0; int x = 3; int y = 1; while (y <= x) { answer = answer + 5; y = y+1; } b. int answer = 0; int x = 0; int y = 3; if (x == y) { answer = 15; } else if (x == answer) { answer = y; } else { answer = x; }
|