1 of 10

Lecture 23 - CS 159 Lab 14

Prof. Alvin Chao

Due this week:

  • Wed 11pm Lab 13 Sorted Aisle
  • Fri 11pm Lab 14 Canvas
  • **Sun 11pm HW8(long)
  • Next week: Wed Exam 3 in class on paper

2 of 10

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!");

}

3 of 10

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?");

}

}

4 of 10

UML from Pogil

UML reminders:

  • <<?>> used for abstract and interface
  • Italics = abstract methods
  • Dashed lines = implements
  • solid lines = relationship is or extends

5 of 10

Interfaces and Abstract Classes

abstract

6 of 10

Table

-, Y, N Object Type�

Variable�type

Tossable

Ball

Rock

Baseball

Football

Tossable

-

Ball

-

Rock

Baseball

Football

7 of 10

PI Question

8 of 10

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!"

9 of 10

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.

10 of 10

Main.java Submit answers in Canvas

  1. Ball ball = new Football("Spalding");�B) Ball ball = new Football("Spalding");
  2. Baseball baseball = (Baseball) ball;
  3. Object obj = new Baseball("Spalding");
  4. Object obj = new Baseball("Spalding");

Tossable tossable = obj;

  • Tossable tossable = new Baseball("Spalding");

Object obj = tossable;

  • Tossable tossable = new Baseball("Spalding");

tossable.getBrandName();

N – not compile;

X – compile but generate an exception at run-time; or

R – compile and run without generating an exception.