Lecture 23 - CS 159 Lab 14
Prof. Alvin Chao
Due this week:
POGIL Abstract Class LoudToy extended by subclasses
public abstract class LoudToy {
private int volume;
public LoudToy(int volume) {
this.volume = volume;
}
public int getVolume() {
return volume;
}
public void setVolume(int volume) {
this.volume = volume;
makeNoise();
}
public abstract void makeNoise();
}
public class ToySheep extends LoudToy {
public ToySheep() {
super(3);
}
public void makeNoise() {
System.out.println("Baaa");
}
}
public class ToyRobot extends LoudToy {
private int chargeLevel;
public ToyRobot() {
super(10);
chargeLevel = 5;
}
public void recharge() {
chargeLevel = 10;
}
public void makeNoise() {
System.out.println("Beep Beep!");
}
Pogil Model 3
public interface Rechargeable {
int MAX_CHARGE = 10;
int getCharge();
void recharge();
}
public class CellPhone implements Rechargeable {
private int chargeLevel;
private int volume;
public CellPhone(int chargeLevel, int volume) {
this.chargeLevel = chargeLevel;
this.volume = volume;
}
public int getCharge() {
return chargeLevel;
}
public void recharge() {
chargeLevel = MAX_CHARGE;
}
public int getVolume() {
return volume;
}
public void setVolume(int volume) {
this.volume = volume;
}
public void makeCall() {
System.out.println("Ring... Hello?");
}
}
UML from Pogil
UML reminders:
Interfaces and Abstract Classes
abstract
Table
-, Y, N Object Type�
Variable�type
| Tossable | Ball | Rock | Baseball | Football |
Tossable | - | | | | |
Ball | | - | | | |
Rock | | | | | |
Baseball | | | | | |
Football | | | | | |
PI Question
Write classes on paper
Write the source code for the UML diagram.
• In Rock.java, the toss method should print"Tossing a Rock!"
• In Baseball.java, the toss method should print"Tossing a Baseball!", and the bounce
method should print"Bouncing a Baseball!"
• In Football.java, the toss method should print"Tossing a Football!", and the bounce
method should print"Bouncing a Football!"
Code then validate Main.java
On your computer, create a folder for today’s lab (under src/labs).
Type the code that you wrote on the previous two pages (in files named Tossable.java, Rock.java, Ball.java, Baseball.java,
and Football.java). Make a note of any compiler errors or other mistakes that you find.
Create a file named Main.java with a main() method. Copy and paste each
snippet from the previous question into main(), and run the program.
Main.java Submit answers in Canvas
Tossable tossable = obj;
Object obj = tossable;
tossable.getBrandName();
N – not compile;
X – compile but generate an exception at run-time; or
R – compile and run without generating an exception.