Homework 05: Equality, Namespaces, Packages, and the CLASSPATH

Submit this homework using Gradescope. You can type up your answers, or write legibly and scan them. Do not attempt to submit a paper copy in class, as it will not be accepted.


A. (3 points) Given the following code, what is the output? Assume a public number instance variable, and a equals() method that uses only this instance variable to check if two Bus objects values’ are semantically equal.

Bus busA = new Bus();
Bus busB = new Bus();

busA.number = 44;
busB.number = 44;

System.out.println(busA == busB);
System.out.println(busA.equals(busB));

Bus busC = busB;
busB = busA;

busA.number = 13;

System.out.println(busA == busC);
System.out.println(busA.equals(busC));

busC = busB;

System.out.println(busA == busC);
System.out.println(busA.equals(busC));

B. (1 point) Suppose you have a single directory on your CLASSPATH, /Users/student/src/. That is, the src directory (located inside student, located inside Users) is on your classpath.

Suppose the first line of a Java source file named “Banana.java” is:

package produce.fruits;

Where exactly should this file be located? Specify the full path to the file. For example, a wrong answer is “in /Users/Banana.java”. Another wrong answer is /produce/fruits/Banana.java.

C. (2 points) Suppose we add a file named “Broccoli.java” to the project above, and we want the Broccoli class to reside in the produce.vegetables package. What should the first line (the package declaration) of the Broccoli.java file be? And, what should the full path to the file be?

D. (1 point) Finally, suppose we want to reference the Broccoli class from within the “Banana.java” source. What import statement must we to add to the Banana.java file? If none is necessary, say so.