22
BIT 115: INTRODUCTION TO PROGRAMMING Instructor: Craig Duckett [email protected] du LECTURE 4

Instructor: Craig Duckett [email protected]. Assignment 1 Due Lecture 5 by MIDNIGHT – NEXT – NEXT Tuesday, October 13 th I will double dog try to

Embed Size (px)

Citation preview

Page 1: Instructor: Craig Duckett cduckett@cascadia.edu. Assignment 1 Due Lecture 5 by MIDNIGHT – NEXT – NEXT Tuesday, October 13 th I will double dog try to

BIT 115: INTRODUCTION TO PROGRAMMING

Instructor: Craig Duckett

[email protected]

LECTURE 4

Page 2: Instructor: Craig Duckett cduckett@cascadia.edu. Assignment 1 Due Lecture 5 by MIDNIGHT – NEXT – NEXT Tuesday, October 13 th I will double dog try to

• Assignment 1 Due Lecture 5 by MIDNIGHT– NEXT Tuesday, October 13th

I will double dog try to have Assignment 1 graded and back to you by Friday, October 16th (hopefully sooner)

Today's Topics• Making Decisions in Java

– If statements– If/Else (“Either-or”) statements– Logical NOT operator– While statements

2

Announcements

Page 3: Instructor: Craig Duckett cduckett@cascadia.edu. Assignment 1 Due Lecture 5 by MIDNIGHT – NEXT – NEXT Tuesday, October 13 th I will double dog try to

3

• Assignment 1 (LECTURE 5) Tuesday, October 13 in StudentTracker by midnight

• Assignment 2 (LECTURE 8) Thursday, October 22

• Assignment 1 Revision (LECTURE 10) Tuesday, November 3

• Assignment 2 Revision (LECTURE 12) Tuesday, November 10

• Assignment 3 (LECTURE 13) Thursday, November 12

• Assignment 3 Revision (LECTURE 16) Tuesday, November 24

• Assignment 4 (LECTURE 20) Tuesday, December 8

• Assignment 4 Revision (LECTURE 21) Thursday, December 10

Assignment Dates (By Due Date)

Page 4: Instructor: Craig Duckett cduckett@cascadia.edu. Assignment 1 Due Lecture 5 by MIDNIGHT – NEXT – NEXT Tuesday, October 13 th I will double dog try to

And Now the

"Warm Up"

Quiz

4

Page 5: Instructor: Craig Duckett cduckett@cascadia.edu. Assignment 1 Due Lecture 5 by MIDNIGHT – NEXT – NEXT Tuesday, October 13 th I will double dog try to

Another Look at Extending a Class

5

Robot

MrRoboto

extends

Robot object can do• A move(); • B turnleft();• C pickThing();

MrRoboto object can do …• A• B• C

… and new methods• X turnAround();• Y move3(); • Z turnRight();

Remember: you can name the new class you’re extending anything you want (with provisions depending on whether it’s a file with only one class in it or with two or more classes in it). I named my class MrRoboto because I like the song by Styx.

Page 6: Instructor: Craig Duckett cduckett@cascadia.edu. Assignment 1 Due Lecture 5 by MIDNIGHT – NEXT – NEXT Tuesday, October 13 th I will double dog try to

Another Look at Extending a Class

6

Robot

MrRoboto

extends

Can do• A• B• C

Can do …• A• B• C

… and new methods• X• Y• Z

So, if you want a robot that can only do A, B, and C, then instantiate:

Robot lisa = new Robot(bothell, 3, 2, Direction.SOUTH);

But, if you want a robot that can do A, B, and C and X, Y, and Z, then instantiate:

MrRoboto lisa = new MrRoboto(bothell, 3, 2, Direction.SOUTH);

Page 7: Instructor: Craig Duckett cduckett@cascadia.edu. Assignment 1 Due Lecture 5 by MIDNIGHT – NEXT – NEXT Tuesday, October 13 th I will double dog try to

Another Look at Extending a Class

BIT 115: Introduction To Programming 7

Page 8: Instructor: Craig Duckett cduckett@cascadia.edu. Assignment 1 Due Lecture 5 by MIDNIGHT – NEXT – NEXT Tuesday, October 13 th I will double dog try to

BIT 115: Introduction To Programming 8

Basic Decision-Making

Page 9: Instructor: Craig Duckett cduckett@cascadia.edu. Assignment 1 Due Lecture 5 by MIDNIGHT – NEXT – NEXT Tuesday, October 13 th I will double dog try to

Chapter 4.1: Two Kinds of DecisionsIf and While Statements

BIT 115: Introduction To Programming 9

• Use an if statement to perform an action once or not at all.• Use a while statement to perform an action zero or more times.• Use an if-else statement to perform either one action or another action.• Use a while statement to perform an action a specified number of times.

Up to now, a robot’s exact initial situation was known at the start of a task. When we wrote our programs, this information allowed robots to find things and avoid running into walls. However, these programs worked only in their specific initial situations. If a robot tried to execute one of these programs in a slightly different initial situation, the robot would almost certainly fail to perform the task.

To address this situation, a robot must make decisions about what to do next. Should it move or should it pick something up? In this chapter we will learn about programming language statements that test the program’s current state and choose the next statement to execute based on what they find.

One form of this capability is the if statement: If something is true, then execute a group of statements. If it is not true, then skip the group of statements.

Another form of this capability is the while statement: while something is true, execute a group of statements.

Page 10: Instructor: Craig Duckett cduckett@cascadia.edu. Assignment 1 Due Lecture 5 by MIDNIGHT – NEXT – NEXT Tuesday, October 13 th I will double dog try to

Two Kinds of Decisions

BIT 115: Introduction To Programming 10

So far, our programs have been composed of a sequence of statements executed in order.

The if and while statements are different. As the program is running, they can ask a question. Based on the answer, they choose the next statement or group of statements to execute.

In a robot program, the question asked might be, “Is the robot’s front blocked by a wall?” or “Is there something on this intersection the robot can pick up?”

All of these questions have “yes” or “no” answers. In fact, if and while statements can only ask yes and no questions. Java uses the keyword true for “yes” and false for “no.” These keywords represent Boolean values, just like the numbers 0 and 23 represent integer values.

George Boole (2 November 1815 – 8 December 1864) was an English-born mathematician, philosopher and logician. His work was in the fields of differential equations and algebraic logic, and he is now best known as the author of The Laws of Thought. As the inventor of the prototype of what is now called Boolean logic, which became the basis of the modern digital computer, Boole is regarded in hindsight as a founder of the field of computer science.

Page 11: Instructor: Craig Duckett cduckett@cascadia.edu. Assignment 1 Due Lecture 5 by MIDNIGHT – NEXT – NEXT Tuesday, October 13 th I will double dog try to

If and While

BIT 115: Introduction To Programming 11

When the simplest form of an if statement asks a question and the answer is true, it executes a group of statements once and then continues with the rest of the program. If the answer to the question is false, that group of statements is not executed.

When a while statement asks a question and the answer is true, it executes a group of statements (just like the if statement). However, instead of continuing down to the rest of the program, the while statement asks the question again. If the answer is still true, that same group of statements is executed again. This continues until the answer to the question is false.

The if statement’s question is “Should I execute this group of statements once?”

if (test statement) {// list of statements}

The while statement’s question is “Should I execute this group of statements again?”

while (test statement) {// list of statements}.

Page 12: Instructor: Craig Duckett cduckett@cascadia.edu. Assignment 1 Due Lecture 5 by MIDNIGHT – NEXT – NEXT Tuesday, October 13 th I will double dog try to

Example: If Statement

12

ASKS THE QUESTION: Should this statement or group of statements be executed once or not at all?

Not At AllOnceMsRobotoIf.java

Page 13: Instructor: Craig Duckett cduckett@cascadia.edu. Assignment 1 Due Lecture 5 by MIDNIGHT – NEXT – NEXT Tuesday, October 13 th I will double dog try to

Example: While Statement

13

ASKS THE QUESTION: Should this statement or group of statements be executed again?

Yes NoMsRobotWhile.java

Page 14: Instructor: Craig Duckett cduckett@cascadia.edu. Assignment 1 Due Lecture 5 by MIDNIGHT – NEXT – NEXT Tuesday, October 13 th I will double dog try to

BIT 115: Introduction To Programming 14

Page 15: Instructor: Craig Duckett cduckett@cascadia.edu. Assignment 1 Due Lecture 5 by MIDNIGHT – NEXT – NEXT Tuesday, October 13 th I will double dog try to

Built-In Queries, Predicates

BIT 115: Introduction To Programming 15

• Can I pick a Thing up from this Intersection? boolean canPickThing()• How many Things are in my backpack? int countThingsInBackpack()• What am I called (what string of characters is labeling me)? String getLabel()• What Avenue am I on? int getAvenue()• What is my speed? int getSpeed()• What Street am I on? int getStreet()

Questions with Boolean True or False answers, like canPickThing, are called predicates.

Negating a predicate gives it the opposite meaning.

The Robot class has several built-in queries that answer questions like

Page 16: Instructor: Craig Duckett cduckett@cascadia.edu. Assignment 1 Due Lecture 5 by MIDNIGHT – NEXT – NEXT Tuesday, October 13 th I will double dog try to

Logical Negation Operator

BIT 115: Introduction To Programming 16

Logical Negation Operator

The Robot class does not provide a predicate for testing if the Robot cannot pick up a Thing.

Fortunately, any Boolean expression may be negated, or given the opposite value, by using the logical negation operator “ ! ”. In English, this is usually written and pronounced as “not”.

! means “not”

Page 17: Instructor: Craig Duckett cduckett@cascadia.edu. Assignment 1 Due Lecture 5 by MIDNIGHT – NEXT – NEXT Tuesday, October 13 th I will double dog try to

Testing Integer Queries

BIT 115: Introduction To Programming 17

The if and while statements always ask True or False questions.

“Should I execute the code, true or false?”

This approach works well for queries that return a boolean value, but how can we use queries that return integers?

We do it with comparison operators

Page 18: Instructor: Craig Duckett cduckett@cascadia.edu. Assignment 1 Due Lecture 5 by MIDNIGHT – NEXT – NEXT Tuesday, October 13 th I will double dog try to

Two-Sided Queries

BIT 115: Introduction To Programming 18

The examples in the comparison operator table show an integer on only one side, but Java is more flexible than this: it can have a query on both sides of the operator, as in the following statements --

The following code tests whether the robot’s Avenue is five more than the Street. Locations where this tests true include (0,5) and (1,6).

This test determines whether karel is on the diagonal line of intersections (0,0), (1,1), (2,2), and so on. It also includes intersections with negative numbers such as (-3, -3).

Page 19: Instructor: Craig Duckett cduckett@cascadia.edu. Assignment 1 Due Lecture 5 by MIDNIGHT – NEXT – NEXT Tuesday, October 13 th I will double dog try to

The If-Else Statement

BIT 115: Introduction To Programming 19

The if statement performs an action once or not at all. Another version of the if statement, the if-else statement, choose between two groups of actions. It performs one or it performs the other based on a test.

Unlike the if statement, the if-else statement always performs an action. The question is, which action?

The general form of the if-else is as follows:

MsRobotoIfElse.java · MsRobotoWhileIfElse.java · MsRobotoWhileifNot.java

Page 20: Instructor: Craig Duckett cduckett@cascadia.edu. Assignment 1 Due Lecture 5 by MIDNIGHT – NEXT – NEXT Tuesday, October 13 th I will double dog try to

Brief Intro: Parameters & Variables

20

PLEASE NOTE: We will be going over Parameters & Variables again in more detail in several upcoming Lectures. This is just a teaser or "taste" of things to come

A parameter is a variable that you pass into a method (or function)

A variable is a piece of data like a number that can be changed programmatically (it doesn't have to be the same number over and over again, it isn't constant or set in stone).

If you think back to math class, you spend a lot of time talking about f(x).

In that case, 'f' is the function, 'x' is the variable, and the passing of 'x' inside the function's parentheses is the parameter. In other words, something passed to 'x' is going to change what the output of 'f' gives you. Being a variable, 'x' is a placeholder for any number that might be passed to the parameter.

But, how does this work with Java code? Let's take a look at some example code!

Page 21: Instructor: Craig Duckett cduckett@cascadia.edu. Assignment 1 Due Lecture 5 by MIDNIGHT – NEXT – NEXT Tuesday, October 13 th I will double dog try to

Brief Intro: Parameters & Variables

21

But, how does this work with Java? Let's take a look at some example code!

NumTest.java

If 'x' is a 2, then the output of 'f' is 14

If 'x' is a 3, then the output of 'f' is 19

If 'x' is a 4, then the output of 'f' is 26

If 'x' is a 5, then the output of 'f' is 35

If 'x' is a 6, then the output of 'f' is 46

Here is an abstract representation of what is going on.

'x' is a variable because it is a placeholder for any numberInside the function f's parentheses, 'x' represents a parameter, because the value of 'x' is going to be passed into the function, and then something is going to be done with it in some way.

Page 22: Instructor: Craig Duckett cduckett@cascadia.edu. Assignment 1 Due Lecture 5 by MIDNIGHT – NEXT – NEXT Tuesday, October 13 th I will double dog try to

4NOTE: Starting with today's lecture, I will start posting some "Solutions" for the ICEs a day or two after the current lecture on the website

22