CPSC 111 Introduction to Computation Introduction to Computation - - PowerPoint PPT Presentation

cpsc 111
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

CPSC 111

Introduction to Computation Introduction to Computation

October 1st, 2009

Based on slides by Eiselt, Carter, Murphy, Pottinger

slide-2
SLIDE 2

Administrative Stuff Administrative Stuff

If you have flu symptoms, there's a good chance that it really is the flu If it is the flu there's a really good chance really is the flu. If it is the flu, there s a really good chance that it's the H1N1 or "swine flu" virus. Do not go to class, or lab, or tutorial, or office hours. Stay home until you are well (at least 24 hours after your fever subsides without the help of medication). p ) Do not go to your doctor just to get a note to bring to us. We don't want you to get a note We want you to point your We don t want you to get a note. We want you to point your web browser to this URL:

https://www cs ubc ca/local/facilities/h1n1 jsp https://www.cs.ubc.ca/local/facilities/h1n1.jsp

slide-3
SLIDE 3

Administrative Stuff

Here's the same URL again: htt // b /l l/f iliti /h1 1 j https://www.cs.ubc.ca/local/facilities/h1n1.jsp Here, you can inform us that you are staying home because y y y g

  • f the flu with the click of a button. Instructors in all your CS

classes will be automatically informed that you are sick. Clicking the button is your substitute for medical Clicking the button is your substitute for medical documentation (i.e., it's your doctor's note). It applies only if you suspect you have the flu, not to other illnesses When you have determined that you are well enough to return, you return to the URL and click the button that let's us know you're back. Again, your CS instructors will be automatically notified.

slide-4
SLIDE 4

Administrative Stuff Administrative Stuff

Between the time you declare yourself sick and the time you declare yourself well you are excused from exams declare yourself well, you are excused from exams, quizzes, homework, and labs in all your CS courses. You'll have to make them up later. You are not expected to attend class, and you will not be allowed to write exams or quizzes in any CS courses during that time. If you are sick but feel well enough to try to keep up, follow along with your assigned readings, published lecture slides

  • r notes and homework assignments as best you can

You

  • r notes, and homework assignments as best you can. You

can also do your labs at home at show them to your lab TA when you are well. When you return, you and I can discuss how to catch up.

slide-5
SLIDE 5

Administrative Stuff Administrative Stuff

Your first exam is next week!

  • Wed Oct 7, 6:30 -7:30

L ti

  • Location:
  • Go to the course WebCT Vista page
  • Select link “Important: Midterm 1 rooms”
  • Find your name in the list that appears

Find your name in the list that appears

  • The label next to it indicates the room you

ill ha e to go to will have to go to

slide-6
SLIDE 6

Administrative Stuff Administrative Stuff

Homework assignment due tomorrow!

TIP:

  • Ch 3.9 (5.5 in second edition) explains some of the

concepts you need to do part 2 of the assignment.

  • It also introduces the Graphics2D package and some

methods associated with it methods associated with it.

  • You can safely ignore them, as long as you use only

the methods listed in the assignment instructions in your solution your solution

slide-7
SLIDE 7

Administrative Stuff Administrative Stuff

Big reading in Big Java (2nd or 3rd edition): Chapter 1.1 through 1.8 Chapter 2 1 through 2 10 Chapter 2.1 through 2.10 Chapter 4.1, 4.7 before your next lab Chapter 3.1 through 3.8 p g Chapter 4 this week (includes material already covered in Ch. 2)

slide-8
SLIDE 8

Review of previous Review of previous

1 C ti l d bj t

  • 1. Creating our own classes and objects
  • 2. Understanding encapsulation and how it is

g p enforced via private variables

slide-9
SLIDE 9

Classes for a Roller Coasters Simulator Classes for a Roller Coasters Simulator

slide-10
SLIDE 10

There is even a Game about Roller Coasters:

ll Roller Coaster Tycoon

slide-11
SLIDE 11

Roller Coaster class so far

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; }

slide-12
SLIDE 12

Roller Coaster class so far

public class RollerCoaster2 { private int numberOfRiders;

  • Instance variables (fields)

p ; private int capacity; public RollerCoaster2()

Instance variables (fields)

  • Declared as private to preserve

encapsulation

  • They can only be accessed via

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; }

slide-13
SLIDE 13

Roller Coaster class so far

public class RollerCoaster2 { private int numberOfRiders;

  • Constructor method

p ; private int capacity; public RollerCoaster2()

  • The method that actually creates a

new object of a class Instance

  • Must have the same name as the class

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; }

slide-14
SLIDE 14

Roller Coaster class so far includes this..

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()

  • Simulates people getting off a coaster

p p y () { numberOfRiders = 0; }

by resetting numberOfRiders to 0

  • It is called a mutator method (or a

setter) because it changes (sets) one h b ’ bl (f ld ) the object’s instance variables (fields)

slide-15
SLIDE 15

followed by this ...followed by this

public void board() { if (numberOfRiders < capacity) { numberOfRiders = numberOfRiders+1; System.out.println(“Welcome on board!”); } else { System.out.println(“Sorry, the coaster’s full"); } } }

  • Simulates a new rider trying to board the coaster
  • Checks if there are still seats available

If i b f id b if

  • If yes, increases number of riders by one, if not expresses regret
slide-16
SLIDE 16

Modified main method Modified main method

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(); } }

? f What now? We run it and see what happens, of course.

slide-17
SLIDE 17

Today’s plan Today s plan

  • 1. How to make a method return values via the

return statement

  • 2. Describe how to how to create a method that

expects values passed as parameters expects values passed as parameters

  • 3. Wake-up call
slide-18
SLIDE 18

Let’s make another method Let s make another method

emptyCoaster() is a mutator method (aka a setter) b it h th l i t i bl because it changes the value on an instance variable Now let’s make an accessor method (aka a getter), that is a method that allows us to retrieve the value of an instance variable. In this case, we want to provide a way to retrieve the number of riders on a roller coaster from outside the l F i t ’d lik t b bl t dd thi t

  • class. For instance, we’d like to be able to add this to
  • ur theme park...
slide-19
SLIDE 19

ThemePark3

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."); } }

slide-20
SLIDE 20

Why?

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."); } }

slide-21
SLIDE 21

Why?

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."); } }

Because numberOfRiders is private and cannot be p directly accessed from outside the RollerCoaster class Encapsulation!

slide-22
SLIDE 22

What will the new method look like? What will the new method look like?

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 ????????? { ?????? }

slide-23
SLIDE 23

The return statement

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.

slide-24
SLIDE 24

The return statement

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.

slide-25
SLIDE 25

The return statement

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

  • f that value has to be declared in the method header.

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.).

slide-26
SLIDE 26

The return statement

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. .

slide-27
SLIDE 27

And now it looks like this And now it looks like this...

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; }

slide-28
SLIDE 28

...followed by this

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

Let’s try it out

slide-29
SLIDE 29

What return does

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."); } }

slide-30
SLIDE 30

What return does

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."); } }

slide-31
SLIDE 31

execution shifts to the called method

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; }

slide-32
SLIDE 32

execution shifts to the called method

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 }

slide-33
SLIDE 33

What return does

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."); } }

Th l t d b th ll d th d ff ti l l The value returned by the called method effectively replaces the call to the method itself while the statement is executed.

slide-34
SLIDE 34

Let’s practice creating some more th d methods

Everyone hates queues

Have you ever waited in line for a roller coaster for a really long time? What can we do to help a potential rider avoid long lines?

slide-35
SLIDE 35

A idi l Avoiding long queues

What about:

A way to let us know how many seats were left on the ride before we tried to get on? A way to add two more seats to the ride to reduce line time?

slide-36
SLIDE 36

seatsLeft and addSeats seatsLeft and addSeats

slide-37
SLIDE 37

seatsLeft and addSeats seatsLeft and addSeats

public int seatsLeft() { return (capacity – numberOfRiders); } public int addSeats() { capacity = capacity + 2; }

slide-38
SLIDE 38

Okay, but adding two seats does not h l h h l help much with long queues

How could we add a varying number of seats? How could we add a varying number of seats? h i h ld d That is, how could we do:

slide-39
SLIDE 39

That is That is…

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 ) } }

slide-40
SLIDE 40

Today’s plan Today s plan

  • 1. How to make a method return values via the

return statement 2 Describe how to create a method that expects

  • 2. Describe how to create a method that expects

values passed as parameters 3 W k ll

  • 3. Wake-up call
slide-41
SLIDE 41

Parameters Parameters

public void addSeats() { capacity = capacity + 2; }

We can pass values to a method through its parameter list. A method's parameter list is contained within the parentheses following the method name. following the method name. Right now, the addSeats() method doesn't accept any parameters parameters.

slide-42
SLIDE 42

Parameters

type

Parameters

public void addSeats(int numberOfSeats)

parameter name

{ capacity = capacity + 2; }

To pass a value to a method, we have to put

  • the data type of the value and

the name by which the method will refer to the value

  • the name by which the method will refer to the value

in the parameter list.

slide-43
SLIDE 43

Parameters

type type

Parameters

public void addSeats(int numberOfSeats)

parameter name parameter name

{ capacity = capacity + numberOfSeats; }

To pass a value to a method, we have to put

  • the data type of the value and

the name by which the method will refer to the value

  • the name by which the method will refer to the value

in the parameter list. Then we replace the constant 2 with the parameter name, which we can now use just like a variable name throughout the method. the method.

slide-44
SLIDE 44

Parameters Parameters

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.)

slide-45
SLIDE 45

Parameters 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.)

slide-46
SLIDE 46

Wake-up call Wake up call

Write a Java program that allows the user to enter a number representing Canadian dollars and prints the equivalent representing Canadian dollars and prints the equivalent value in U.S. dollars. As of Monday, the exchange rate was 1.00 Canadian dollars is worth 0.921769 U.S. dollars. Here are some examples:

> 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.

slide-47
SLIDE 47

Wake-up call

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."); }}

slide-48
SLIDE 48

Wake-up call

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."); }}

slide-49
SLIDE 49

ThemePark3

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."); } }

slide-50
SLIDE 50

ThemePark3

How do we make ThemePark3 more fluent, i.e. capable

  • f saying

y g “There is 1 rider on Thunderbolt” vs “There are 4 riders on Thunderbolt”