Java Homework Help "Defining your own instantiable classes"

Arik5405

Platinum Member
May 9, 2005
2,044
1
81
I got part A and B done, but I can't seem to figure out part C.

Part C:
The programmer who wrote the Lamp class (below) was a bit of a "dim" bulb and didn't complete the class definition. Your job is to add the missing code so that the Lab24C "tester" class will run to display the following:

There is darkness...
There is light...

Do NOT change any existing code.

Code:
public class Lab24C {
  public static void main(String[] args) {
    Lamp myLamp = new Lamp();
    System.out.println(myLamp.toString());
    myLamp.setIsOn(true);
    System.out.println(myLamp.toString());
  }
}
class Lamp {
  private boolean hasBulb;
  private boolean isOn;

  // YOUR CODE GOES HERE

  public void setHasBulb(boolean parm) {
    hasBulb = parm;
  }
  public void setIsOn(boolean parm) {
    isOn = parm;
  }
  public String toString() {
    if (hasBulb && isOn) {
      return "There is light...";
    }
    else {
      return "There is darkness...";
    }
  }
}
 

Arik5405

Platinum Member
May 9, 2005
2,044
1
81
A and B for reference:

Part A
YIKES! The Counter class of this program is MISSING its constructor methods. See if you can code them. One constructor is supposed to accept an int value representing the initial value of the counter. The other constructor must accept no arguments and set the initial counter value to 1. Within each constructor, be sure to call the "setter" method to store the counter value.

Code:
public class Lab24A {
  public static void main(String[] args) {
    Counter c1 = new Counter();
    Counter c2 = new Counter(5); 
    System.out.println(c1.getCount());
    System.out.println(c2.getCount());
  }
}
class Counter {
  private int count;

//insert code here

  public Counter() {
      setCount(1);
  }
  
  public Counter(int iCount) {
    setCount(iCount);
  }

//stop inserting code

  public void setCount(int n) {
    count = n;
  }
  public int getCount() {
    return count;
  }
  public void increment() {
    count++;
  }
}
When successful, the program should display the following output:
1
5


Part B
A duck should quack. This one will if you can define a class that meets the needs of the Lab24B "tester" class shown below...
Code:
public class Lab24B {
  public static void main(String[] args) {
    new Duck().sayHello();
  }
}

  //insert code here:

class Duck {
public void sayHello(){
System.out.println("Quack!");
}
}
 
Last edited:

bobross419

Golden Member
Oct 25, 2007
1,981
1
0
You need to give it a bulb. Probably a constructor method it looks like.

All three look like constructor problems... is this a chapter on constructors? :p
 
Last edited:

Arik5405

Platinum Member
May 9, 2005
2,044
1
81
You need to give it a bulb. Probably a constructor method it looks like.

All three look like constructor problems... is this a chapter on constructors? :p

got it! thanks. All I needed was someone else's thought for a minute.

Code:
  public Lamp()  {
      setHasBulb(true);
      setIsOn(false);
    }
 
Last edited:

beginner99

Diamond Member
Jun 2, 2009
5,320
1,768
136
got it! thanks. All I needed was someone else's thought for a minute.

Code:
  public Lamp()  {
      setHasBulb(true);
      setIsOn(false);
    }

That's probably the solution looked for but obviously there is at least 1 other as indicated by changing the Main method.

The lamp class is completely valid as is because if you don't add a constructor in the code java will always use the default constructor (=public Lamp(){})
also setIsOn(false) is not need because boolean fields are initialized as false anyway but it's sometimes called good practice to do it anyway to show that it is set to false on purpose and not because it was forgotten.
 

AyashiKaibutsu

Diamond Member
Jan 24, 2004
9,306
4
81
That's probably the solution looked for but obviously there is at least 1 other as indicated by changing the Main method.

The lamp class is completely valid as is because if you don't add a constructor in the code java will always use the default constructor (=public Lamp(){})
also setIsOn(false) is not need because boolean fields are initialized as false anyway but it's sometimes called good practice to do it anyway to show that it is set to false on purpose and not because it was forgotten.

Considering the sample code said where to place his code, I suspect there's only one acceptable answer. Yea, his professor would probably frown on him not setting default values even if java will do it for him.