2026 02 09 BPJ 15-16
Worksheet on writing classes.
Turn in hand-written solutions.
1. Desk
The Desk class has three fields:
col, a String representing the color;price, a double representing the price in dollars;messy, a boolean which is true if the desk is messy.
When the desk is built, it is clean, so the constructor should only take in the color and price.
Methods:
public boolean cheap(): True if the price is less than 20.00.public void cleanup(): Make the messy field be false in that desk.public static String[] available_colors(): return an array of String: “tan”, “beige”, “brown”.
2. Visibility
Here is a helper class:
class Helper {
private String a;
private int b;
/* constructor omitted. inputs: (String, int) */
public int core(String c) {
/* code: if c is "word" return length of a;
if c is "num" return b;
else return 0 */
}
private int alen() { return a.length(); }
public String seta(String aa) { a = aa; }
public static void main(String[] args) { /* omitted */ }
}
- What variables can you access from the main method of the
Explainclass (below)? - What methods/functions can you call from the main method of the
Helperclass? - Write the constructor that is described in the comment.
- What methods can you call from within the constructor? Explain.
3. Equals vs ==
public class Explain {
public static void opo (Helper x, String t) {
String s = "y" + t;
x.seta(s)
s += "l";
}
public static void rest (int n) { n += 5; }
public static void quo (String u) {
u += "est";
System.out.println(u);
}
public static void one() {
// (6)
int n = 15, m = 30;
rest(m);
// (7)
String desc = "smoki";
quo(desc);
// (8)
Helper h = new Helper("hache",5);
opo(h,"ggdrasi");
}
}
- Can a function change an int input? After section (6), what are the values of n and m?
- Can a function change a String input? After section (7), what is
the value of
desc? - After section (8), what is in Helper? List all fields.