CPSC 111
Introduction to Computation Introduction to Computation
October 1st, 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 October 1 st , 2009 Based on slides by Eiselt, Carter, Murphy, Pottinger Administrative Stuff Administrative Stuff If you have flu symptoms, there's a good chance that it really
Based on slides by Eiselt, Carter, Murphy, Pottinger
public class RollerCoaster2 { private int numberOfRiders; Class header p ; private int capacity; public RollerCoaster2() { numberOfRiders = 0; capacity = 2; System.out.println("Another ride is ready to go!"); } public void emptyCoaster() { numberOfRiders = 0; }
public class RollerCoaster2 { private int numberOfRiders;
p ; private int capacity; public RollerCoaster2()
Instance variables (fields)
encapsulation
p () { numberOfRiders = 0; capacity = 2; System.out.println("Another ride is ready to go!");
y y methods of this class
y p ( y g ); } public void emptyCoaster() p p y () { numberOfRiders = 0; }
public class RollerCoaster2 { private int numberOfRiders;
p ; private int capacity; public RollerCoaster2()
new object of a class Instance
p () { numberOfRiders = 0; capacity = 2; System.out.println("Another ride is ready to go!"); y p ( y g ); } public void emptyCoaster() p p y () { numberOfRiders = 0; }
public class RollerCoaster2 { private int numberOfRiders; p ; private int capacity; public RollerCoaster2() p () { numberOfRiders = 0; capacity = 2; System.out.println("Another ride is ready to go!"); y p ( y g ); } public void emptyCoaster()
p p y () { numberOfRiders = 0; }
by resetting numberOfRiders to 0
setter) because it changes (sets) one h b ’ bl (f ld ) the object’s instance variables (fields)
public void board() { if (numberOfRiders < capacity) { numberOfRiders = numberOfRiders+1; System.out.println(“Welcome on board!”); } else { System.out.println(“Sorry, the coaster’s full"); } } }
If i b f id b if
public class ThemePark2 { public static void main (String[] args) p ( g[] g ) { System.out.println("Building my theme park"); RollerCoaster2 thunderbolt = new RollerCoaster2(); RollerCoaster2 flightDeck = new RollerCoaster2(); g (); thunderbolt.board(); thunderbolt.board(); thunderbolt.board(); thunderbolt.emptyCoaster (); p y (); thunderbolt.board(); flightDeck.board(); flightDeck.board(); } }
public class ThemePark3 { public static void main (String[] args) p ( g[] g ) { System.out.println("Building my theme park"); RollerCoaster3 thunderbolt = new RollerCoaster3(); RollerCoaster3 flightDeck = new RollerCoaster3(); g thunderbolt.board(); thunderbolt.board(); thunderbolt.board(); thunderbolt.emptyCoaster(); p y thunderbolt.board(); flightDeck.board(); flightDeck.board(); System.out.println("There are " + thunderbolt.getnumberOfRiders() + “ riders on Thunderbolt."); System.out.println("There are " + flightDeck.getnumberOfRiders() + “ riders on Flight Deck."); } }
Why can we just do System.out.println("There are " + thunderbolt.numberOfRiders + y p ( “ riders on Thunderbolt."); System.out.println("There are " + flightDeck.numberOfRiders + “ riders on Flight Deck."); } }
Why can we just do System.out.println("There are " + thunderbolt.numberOfRiders + y p ( “ riders on Thunderbolt."); System.out.println("There are " + flightDeck.numberOfRiders + “ riders on Flight Deck."); } }
public class RollerCoaster3 { private int numberOfRiders; private int capacity; public RollerCoaster3() { numberOfRiders = 0; capacity = 2; System.out.println("Another ride is ready to go!"); } public ????????? { ?????? }
public class RollerCoaster3 { private int numberOfRiders; public RollerCoaster3() { numberOfRiders = 2; System out println("Another ride is ready to go!"); System.out.println( Another ride is ready to go! ); } public ????????? { return numberOfRiders; }
A method returns a value through a return statement. When a return A method returns a value through a return statement. When a return statement is executed, flow of control returns to the method that called this method. The value is returned to take the place of the call to (or invocation of) this method.
public class RollerCoaster3 { private int numberOfRiders; public RollerCoaster3() { numberOfRiders = 2; System out println("Another ride is ready to go!"); System.out.println( Another ride is ready to go! ); } public ????????? { return numberOfRiders; }
We generally try to have only one return statement in a method, though
.
We generally try to have only one return statement in a method, though it's possible to have more. We generally try to have the return statement be the last statement in the method, though it's possible for it to be elsewhere. Strive for simplicity and predictability.
public class RollerCoaster3 { private int numberOfRiders; public RollerCoaster3() { numberOfRiders = 2; System out println("Another ride is ready to go!"); System.out.println( Another ride is ready to go! ); } public ????????? { return numberOfRiders; }
f
.
Whatever we do with the return statement, if it returns a value, then the type
So if numberOfRiders is of type int then we replace that "void" we see all So if numberOfRiders is of type int, then we replace that "void" we see all the time with "int". (The "void" just means that the method doesn't return any value.).
public class RollerCoaster3 { private int numberOfRiders; public RollerCoaster3() { numberOfRiders = 2; System out println("Another ride is ready to go!"); System.out.println( Another ride is ready to go! ); } public int getNumberOfRiders() { return numberOfRiders; } .
We'd better give the method the same name that we used in ThemePark3. .
public class RollerCoaster3 { private int numberOfRiders; p ; private int capacity; public RollerCoaster3() { numberOfRiders = 0; capacity = 2; System.out.println("Another ride is ready to go!"); } public int getNumberOfRiders() { return numberOfRiders; }
public void emptyCoaster() { numberOfRiders = 0; } 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 class ThemePark3 { public static void main (String[] args) p ( g[] g ) { System.out.println("Building my theme park"); RollerCoaster3 thunderbolt = new RollerCoaster3(); RollerCoaster3 flightDeck = new RollerCoaster3(); g thunderbolt.board(); thunderbolt.board(); thunderbolt.board(); thunderbolt.emptyCoaster(); p y thunderbolt.board(); flightDeck.board(); flightDeck.board(); System.out.println("There are " + thunderbolt.getNumberOfRiders() + “ riders on Thunderbolt."); System.out.println("There are " + flightDeck.getNumberOfRiders() + “ riders on Flight Deck."); } }
public class ThemePark3 { public static void main (String[] args) p ( g[] g ) { System.out.println("Building my theme park"); RollerCoaster3 thunderbolt = new RollerCoaster3(); RollerCoaster3 flightDeck = new RollerCoaster3(); g thunderbolt.board(); thunderbolt.board(); thunderbolt.board(); thunderbolt.emptyCoaster(); p y thunderbolt.board(); flightDeck.board(); flightDeck.board(); System.out.println("There are " + thunderbolt.getnumberOfRiders() + “ riders on Thunderbolt."); System.out.println("There are " + flightDeck.getnumberOfRiders() + “ riders on Flight Deck."); } }
public class RollerCoaster3 { private int numberOfRiders; p ; private int capacity; public RollerCoaster3() { numberOfRiders = 0; capacity = 2; System.out.println("Another ride is ready to go!"); } public int getNumberOfRiders() //control shifts here { return numberOfRiders; }
public class RollerCoaster3 { private int numberOfRiders; p ; private int capacity; public RollerCoaster3() { numberOfRiders = 0; capacity = 2; System.out.println("Another ride is ready to go!"); } public int getNumberOfRiders() { return numberOfRiders; // the return statement shifts control back to ; // whatever called this method and the return // value is sent back too }
bli l Th P k3 public class ThemePark3 { public static void main (String[] args) { S t t i tl ("B ildi th k") System.out.println("Building my theme park"); RollerCoaster3 thunderbolt = new RollerCoaster3(); RollerCoaster3 flightDeck = new RollerCoaster3(); thunderbolt.board(); th d b lt b d() thunderbolt.board(); thunderbolt.board(); thunderbolt.emptyCoaster(); thunderbolt.board(); flightDeck board() flightDeck.board(); flightDeck.board(); System.out.println("There are " + 1 + “ riders on Thunderbolt."); System out println("There are " + flightDeck getnumberOfRiders() + System.out.println("There are " + flightDeck.getnumberOfRiders() + “ riders on Flight Deck."); } }
public int seatsLeft() { return (capacity – numberOfRiders); } public int addSeats() { capacity = capacity + 2; }
public class ThemePark4 { public static void main (String[] args) p ( g[] g ) { System.out.println("Building my theme park"); RollerCoaster4 thunderbolt = new RollerCoaster4(); RollerCoaster4 flightDeck = new RollerCoaster4(); g (); thunderbolt.board(); thunderbolt.board(); thunderbolt.board(); thunderbolt.addSeats(4); ( ); thunderbolt.board(); flightDeck.board(); flightDeck.board(); flightDeck.addSeats(42); g ( ) System.out.println("There are " + thunderbolt.seatsLeft() + “ seats left on The Thunderbolt."); System.out.println("There are " + thunderbolt.seatsLeft() + “ seats left on Flight Deck."); g ) } }
public void addSeats() { capacity = capacity + 2; }
type
public void addSeats(int numberOfSeats)
parameter name
{ capacity = capacity + 2; }
type type
public void addSeats(int numberOfSeats)
parameter name parameter name
{ capacity = capacity + numberOfSeats; }
public void addSeats(int numberOfSeats) { capacity = capacity + numberOfSeats; }
The names of the parameters declared in the method header are called formal parameters. The actual values passed to the method are called actual parameters. We often refer to actual parameters as arguments. (And then sometimes we're really sloppy and just use the word (And then sometimes we re really sloppy and just use the word parameter to refer to both formal and actual parameters.)
public void addSeats(int numberNewAdultSeats, int numberNewChildSeats) { numberAdultSeats = numberAdultSeats + numberNewAdultSeats; numberChildSeats = numberChildSeats + numberNewChildSeats; }
Multiple parameters are separated by commas in the parameter list, but we're not using multiple parameters in our example...yet. (And this example assumes that we've had the foresight to declare ariables called n mberAd ltSeats and n mber declare variables called numberAdultSeats and number ChildSeats somewhere. Of course.)
> java Exchange Enter amount in Canadian dollars: Enter amount in Canadian dollars: 100 100.0 Canadian dollars are worth 92.1769 U.S. dollars. > java Exchange Enter amount in Canadian dollars: 4287 4287.0 Canadian dollars are worth 3951.6237 U.S. dollars.
import java.util.Scanner; public class Exchange p g { public static void main (String[] args) { do ble e change ate 0 955721 double exchange_rate = 0.955721; double can_dollars; Scanner input = new Scanner (System.in); System.out.println("Enter amount in Canadian dollars: "); can_dollars = input.nextDouble(); System.out.println(can_dollars + " Canadian dollars are worth " + (can_dollars * exchange_rate) + " U.S. dollars."); }}
import java.util.Scanner; public class Exchange p g { public static void main (String[] args) { do ble e change ate
0 921769
double exchange_rate = 0.921769; double can_dollars; Scanner input = new Scanner (System.in); System.out.println("Enter amount in Canadian dollars: "); can_dollars = input.nextDouble(); System.out.println(can_dollars + " Canadian dollars are worth " + (can_dollars * exchange_rate) + " U.S. dollars."); }}
public class ThemePark3 { public static void main (String[] args) p ( g[] g ) { System.out.println("Building my theme park"); RollerCoaster3 thunderbolt = new RollerCoaster3(); RollerCoaster3 flightDeck = new RollerCoaster3(); g thunderbolt.board(); thunderbolt.board(); thunderbolt.board(); thunderbolt.emptyCoaster(); p y thunderbolt.board(); flightDeck.board(); flightDeck.board(); System.out.println("There are " + thunderbolt.getnumberOfRiders() + “ riders on Thunderbolt."); System.out.println("There are " + flightDeck.getnumberOfRiders() + “ riders on Flight Deck."); } }