CPSC 111
Introduction to Computation Introduction to Computation
November 17rd, 2009
Based on slides by Eiselt, Carter, Murphy, Pottinger
CPSC 111 Introduction to Computation Introduction to Computation - - PowerPoint PPT Presentation
CPSC 111 Introduction to Computation Introduction to Computation November 17 rd , 2009 Based on slides by Eiselt, Carter, Murphy, Pottinger Department of Computer Science Undergraduate Events Events this week Technical Interview Skills Workshop
Based on slides by Eiselt, Carter, Murphy, Pottinger
Department of Computer Science Undergraduate Events Events this week Technical Interview Skills Workshop Workshop Leader: Sabina Nawaz CSSS Movie Night Movies: The Hangover & Star Trek Date: Mon., Nov 16 Time: 5 – 6:30 pm Location: FSC 1005, 2424 Main Mall Movies: The Hangover & Star Trek Date: Thurs., Nov 19 Time: 6 ‐ 10 pm Location: DMP 301 CS Distinguished Lecture Series Speaker: Glenn Entis Date: Thurs Nov 19 Cool Things: Entrance FREE! One FREE popcorn and pop for every attendee! ‘ Date: Thurs., Nov 19 Time: 3:30 ‐ 4:50 pm Location: DMP 110 Cranium and Board Games Night – ‘Byte to Eat’ Fundraising Date: Fri., Nov 20 Time: 5 – 6 pm Masters of Digital Media Program Info Session Date: Thurs., Nov 19 Time: 5 – 6 pm Time: 5 6 pm Location: X‐Wing Undergraduate Student Lounge Time: 5 6 pm Location: X‐Wing Undergraduate Student Lounge.
Big reading in Big Java 3rd edition: Chapter 1 1 through 1 8 (introduction) Chapter 1.1 through 1.8 (introduction) Chapter 2.1 through 2.10 (using objects) Chapter 3.1 through 3.8 (implementing classes) Chapter 4 (data types) Chapter 4 (data types) Chapter 5.1 through 5.4 (decisions) Chapter 6.1 through 6.5 (iteration) Chapter 7.1, 7.5, 7.6, 7.7 (arrays) Chapter 14.1, 14.3 (searching and sorting) Chapter 8.1 through 8.9 (designing classes) Chapter 9.1, 9.2, 9.3 (interfaces and polymorphism) Chapter 10 (inheritance) Chapter 2.11, 2.12 (simple graphics) Chapter 9.5, 9.6, 9.7, 9.8, 10.9, 10.10 (event handling d hi l i t f ) and graphical user interfaces)
Big reading in Big Java 2nd edition: Ch t 1 1 th h 1 8 (i t d ti ) Chapter 1.1 through 1.8 (introduction) Chapter 2.1 through 2.10 (using objects) Chapter 3.1 through 3.8 (implementing classes) Chapter 4 (data types) Chapter 4 (data types) Chapter 6.1 through 6.4 (decisions) Chapter 7.1 through 7.5 (iteration) Chapter 8.1, 8.5, 8.6, 8.7 (arrays) p , , , ( y ) Chapter 19.1, 19.3 (searching and sorting) Chapter 9.1 through 9.9 (designing classes) Chapter11.1, 11.2, 11.3 (interfaces and polymorphism) Chapter 13 (inheritance) Chapter 5.1, 5.2 (simple graphics) Chapter 11.5, 12.1, 12.2, 12.3 (event handling d h l f ) and graphical user interfaces)
O fi t ti f ll t li it d i i l t d f ti lit Our very first generation of roller coasters were limited in simulated functionality: folks could board roller coasters, but I had no way to let riders get off. That is, the simulated roller coasters had board() methods, but no empty() methods.
public class RollerCoaster{ private int numberOfRiders; private int capacity; public RollerCoaster(int aCapacity) { numberOfRiders = 0; capacity = aCapacity; System.out.println("Another ride is ready to go!"); } //we also had another constructor with no parameters // but we’ll ignore it for this discussion public void board() { if (numberOfRiders < capacity) { numberOfRiders = numberOfRiders+1; System.out.println("Welcome on board!"); } else{ else{ System.out.println("Sorry, the coaster’s full"); } }}
An evolutionary step beyond these primitive roller coasters is the class of roller An evolutionary step beyond these primitive roller coasters is the class of roller coasters that let people get off. These new roller coasters were exactly the same as their predecessors in all other respects.
public class RollerCoaster2{ public class RollerCoaster2{ private int numberOfRiders; private int capacity; public RollerCoaster2(aCapacity) { numberOfRiders = 0; capacity = aCapacity; System.out.println("Another ride is ready to go!"); } public void board() { if (numberOfRiders < capacity){ numberOfRiders = numberOfRiders+1; System.out.println("Welcome on board!"); } else{ System.out.println("Sorry, the coaster’s full"); } } public void empty() { numberOfRiders = 0; }}
has a empty() method has a empty() method.
public class ShinyRollerCoaster extends RollerCoaster { }
What does that do? We just created a new class called ShinyRollerCoaster that inherited most non‐static methods and non‐static variables from the RollerCoaster class. To enhance ShinyRollerCoaster beyond this, we just add the additional variables and methods that we want but that aren't included in the RollerCoaster class. Inheritance is the process by which a new class is derived from an existing
i programming.
The subclass (or child class) inherits all methods except constructor methods from the superclass (or parent class). To use the constructor methods from the superclass, we need to use the To use the constructor methods from the superclass, we need to use the reserved word super.
public class ShinyRollerCoaster extends RollerCoaster { public ShinyRollerCoaster(int aCapacity) { super(aCapacity);}}
The reserved word super, in the context of a constructor method for a subclass, tells Java to use the appropriate constructor method of the subclass, tells Java to use the appropriate constructor method of the superclass.
public class ShinyThemePark { public static void main( String[] args ) { System.out.println("Building my theme park"); ShinyRollerCoaster thunderbolt = new ShinyRollerCoaster(2); RollerCoaster flightDeck = new RollerCoaster(2); thunderbolt.board(); thunderbolt.board(); //thunderbolt.empty();// not defined yet thunderbolt.board(); flightDeck.board(); flightDeck.board(); } }
Building my theme park A 2 person ride is ready to go A 2 person ride is ready to go Welcome on board! Welcome on board! Sorry, the coaster’s full Welcome on board! Welcome on board!
empty() method
public class ShinyRollerCoaster extends RollerCoaster { public ShinyRollerCoaster(int aCapacity) p y ( p y) { super(aCapacity); } public void empty () { numberOfRiders = 0; }} }}
empty() method
public class ShinyRollerCoaster extends RollerCoaster { public ShinyRollerCoaster(int aCapacity) p y ( p y) { super(aCapacity); } public void empty () { numberOfRiders = 0; }} }}
Error: ShinyRollerCoaster. java:11: numberOfRiders has private access in RollerCoaster
empty() method
public class ShinyRollerCoaster extends RollerCoaster { public ShinyRollerCoaster(int aCapacity) p y ( p y) { super(aCapacity); } public void empty () { numberOfRiders = 0; }} }}
The subclass will inherit all the non‐static variables of the superclass, but the access control on private variables is so tight that a subclass won't be able to directly access an inherited private variable
public class RollerCoaster{ private int numberOfRiders; private int capacity; private int capacity; public RollerCoaster(int aCapacity) { numberOfRiders = 0; capacity = aCapacity; S t t i tl ("A th id i d t !") System.out.println("Another ride is ready to go!"); }
public class RollerCoaster{ protected int numberOfRiders; protected int capacity; protected int capacity; public RollerCoaster( int aCapacity) { numberOfRiders = 0; capacity = aCapacity; S t t i tl ("A th id i d t !") System.out.println("Another ride is ready to go!"); }
public class RollerCoaster{ protected int numberOfRiders; protected int capacity; protected int capacity; public RollerCoaster( int aCapacity) { numberOfRiders = 0; capacity = aCapacity; S t t i tl ("A th id i d t !") System.out.println("Another ride is ready to go!"); }
public interface Ride { public void board(); public int seatsLeft(); public int getNumRiders(); public void empty(); }
public interface Ride { public abstract void board(); public abstract int seatsLeft(); public abstract int getNumRiders(); public abstract void empty(); p p y(); }
public interface Ride { void board(); int seatsLeft(); int getNumRiders(); void empty(); p y(); }
from Absolute Java by Walter Savitch.
public class RollerCoaster2008 implements Ride { private int numberOfRiders; private int numberOfRiders; private int capacity; public RollerCoaster2008(int aCapacity){ numberOfRiders = 0; capacity = aCapacity; System.out.println("Another roller coaster is ready to go!"); } public int getNumRiders(){ return numberOfRiders; } public void empty(){ numberOfRiders = 0; } …
… public void board(){ if (numberOfRiders < capacity) { numberOfRiders = numberOfRiders+1; System.out.println("Welcome on board the roller coaster!"); } else { System.out.println("Sorry, the coaster’s full"); } } bli i t t L ft(){ public int seatsLeft(){ return capacity ‐ numberOfRiders; } }
public class WaterRide2008 implements Ride { private int numberOfRiders; private int numberOfRiders; private int capacity; public WaterRide2008( int aCapacity){ numberOfRiders = 0; capacity = aCapacity; System.out.println("Another water ride is ready to go!"); } public int getNumRiders(){ return numberOfRiders; } public void empty(){ numberOfRiders = 0; } …
… public void board(){ if (numberOfRiders < capacity) { numberOfRiders = numberOfRiders+1; System.out.println("Welcome on board the water ride!"); } else { System.out.println("Sorry, the water ride is full"); } } bli i t t L ft(){ public int seatsLeft(){ return capacity ‐ numberOfRiders; } }
Ride
i l t i l t implements implements
RollerCoaster 2008 WaterRide 2008 KiddieRide 2008 ThrillRide 2008
extends
EvenCooler RollerCoaster
*assuming you come from the planet Nerdtron
from Ja a 5 Ill minated b Anderson and Franceschi
public class ThemePark2008 { public static void main(String [] args) public static void main(String [] args) { Ride foo1 = new RollerCoaster2008(42); Ride foo2 = new WaterRide2008(42); f b d() foo1.board(); foo2.board(); } } Another roller coaster is ready to go! Another water ride is ready to go! W l b d th ll t ! Welcome on board the roller coaster! Welcome on board the water ride!
public class ThemePark2008 { public static void main(String [] args) public static void main(String [] args) { Ride foo1 = new RollerCoaster2008(42); Ride foo2 = new WaterRide2008(42); f b d() foo1.board(); foo2.board(); } }
if we want to get on the ride foo1 and foo1 is a RollerCoaster2008
if we want to get on the ride foo1 and foo1 is a RollerCoaster2008 then print “welcome on board the roller coaster" else if we want to get on the ride foo1 and foo1 is a WaterRide2008 then print “welcome on board the water ride" else if we want to get on the ride foo1 and foo1 is a ThrillRide2008 then print " welcome on board the BOO! Thrill Ride" else if we want to get on the ride foo1 and foo1 is a KiddieRide2008…