UW Home     CSE Home   Announcements    Message Board    Contact Info 

 
 

Homework #3

Due: 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 Mover.java..

  1. What is the name of the class that this file defines?
  2. How many instance variables are defined in this class?
  3. How many constructors are defined for this class?
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.class and the following statement has been executed successfully in DrJava.

Mover b = new Mover(true,2.0);
  1. What value will be returned by the expression b.getStrength()?
  2. What is the type of the value returned by the getStrength() method?
  3. What value will be returned by the expression b.isAbleToMove(15.0)?

3. Write down the shortest possible class definition that will compile and produce a class file. (The keyword public is not required.)

4. Refer to the Java library documentation for the java.lang.Object class.

  1. According to the Method Summary section, how many methods are defined for this class?
  2. Of those methods, how many return a String result?

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)";
    }
  }
}
  1. How many methods (not constructors) are defined in this class?
  2. What is the type identified for parameters chatName and chatHost in the constructor?

6. Assume that the source file in problem 5 has been compiled to produce ChatNode.class and the following statement has been executed successfully in DrJava.

ChatNode c = new ChatNode("dwj","ivisit.net"); 
  1. What value will be returned by the expression c.getPaid()?
  2. What will be printed out by the statement System.out.println(c);?

7. For each of the following statements, assume that the variable status is of type boolean and state whether it is true or false after the statement executes.

  1. status = (((17*2)-34) == 0);
  2. status = ((1/17)*10 <= 170);
  3. status = 340.5 > ((2.0 * 17 *10.0)+1);

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 status is of type boolean and state whether it is true or false after the statement executes, given the values above for x, y, and z.

  1. status = (x > 3) || z;
  2. status = (x > 3) && z;
  3. status = (x > y) || (!z);
  4. status = ((x > 6) || (y < 6.5)) && (x > 4);
  5. status = !((x > y) && (x < y));

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;
    }