Instructor: Craig Duckett Assignment 1 Due Lecture 5 by MIDNIGHT – NEXT – NEXT Wednesday, January 20 th one week from today I will.

Slides:



Advertisements
Similar presentations
Introduction to Programming
Advertisements

Lecture 2 Calling and Defining Methods in Java. Introduction ●Calling and defining methods ●Declaring and defining a class ●Instances of a class ●The.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
Boolean Expressions Conditional Statements & Expressions CSC 1401: Introduction to Programming with Java Lecture 4 – Part 2 Wanda M. Kunkle.
COMP 14 Introduction to Programming Miguel A. Otaduy May 18, 2004.
Introduction to Computers and Programming Lecture 5 Boolean type; if statement Professor: Evan Korth New York University.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Introduction to C Programming
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 30, 2005.
Robot? What’s a Robot? Introducing Karel-the-Robot.
What is RobotC?!?! Team 2425 Hydra. Overview What is RobotC What is RobotC used for What you need to program a robot How a robot program works Framework.
Lecture 10 Instructor: Craig Duckett. Assignment 2 Revision TONIGHT DUE TONIGHT Wednesday, August 5 th Assignment 3 NEXT DUE NEXT Monday, August 10 th.
Lecture 6a Instructor: Craig Duckett. Upcoming Assignments & Mid-Term Assignment 1 Revision Assignment 1 Revision is due NEXT Wednesday, July 29 th, by.
Chapter 5 Conditionally Executing Instructions
Instructor: Craig Duckett Assignment 1 Due Lecture 5 by MIDNIGHT – NEXT – NEXT Tuesday, October 13 th I will double dog try to.
Created by, Author Name, School Name—State FLUENCY WITH INFORMATION TECNOLOGY Skills, Concepts, and Capabilities.
Week 2 - Monday.  What did we talk about last time?  Software development  Lab 1.
ECS 10 10/8. Outline Announcements Homework 2 questions Boolean expressions If/else statements State variables and avoiding sys.exit(…) Example: Coin.
CSC Intro. to Computing Lecture 5: Boolean Logic, Gates, & Circuits.
Week 5 - Wednesday.  What did we talk about last time?  Exam 1!  And before that?  Review!  And before that?  if and switch statements.
Institute for Personal Robots in Education (IPRE)‏ CSC 170 Computing: Science and Creativity.
Lecture 6 Instructor: Craig Duckett. Assignment 1, 2, and A1 Revision Assignment 1 I have finished correcting and have already returned the Assignment.
Week 4 - Monday.  What did we talk about last time?  Wrapper classes  if statements.
Semester Review. As we have discussed, Friday we will have in class time for you to work on a program, this program will come with instructions and you.
BIT 115: Introduction To Programming LECTURE 3 Instructor: Craig Duckett
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
Decisions in Python Bools and simple if statements.
1 Karel – Chapter 5 Conditionally Executing Instructions Note: Original slides provided by and modified for Mr. Smith’s AP Computer.
1 FUNCTIONS - I Chapter 5 Functions help us write more complex programs.
CMP-MX21: Lecture 5 Repetitions Steve Hordley. Overview 1. Repetition using the do-while construct 2. Repetition using the while construct 3. Repetition.
BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana (say Doc-tor Way-oo-see-jah-nah, Doc-tor, or Bah-bah)
1 CS161 Introduction to Computer Science Topic #9.
Decision Structures and Boolean Variables. Sequence Structures Thus far, we’ve been programming “sequence structures” Thus far, we’ve been programming.
ICT Introduction to Programming Chapter 4 – Control Structures I.
Java Programming Fifth Edition Chapter 5 Making Decisions.
Chapter 5: Making Decisions. Objectives Plan decision-making logic Make decisions with the if and if…else structures Use multiple statements in if and.
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
Programming in Karel Eric Roberts CS 106A January 6, 2016.
Mile-long hurdle race Suppose that we want to program Karel to run a one-mile long hurdle race, where vertical wall sections represent hurdles. The hurdles.
Controlling Program Flow with Decision Structures.
BIT115: Introduction to Programming
Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan.
BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana (say Doc-tor Way-oo-see-jah-nah, Doc-tor, or Bah-bah)
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
Alice and Java Unit 7 1. Day 1  Objective: Gain an introduction to Java and Eclipse  Essential skill: DM-1: Use technology to advance critical thinking.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #003 (February 14, 2015)
1 Karel J. Robot Chapter 5 Conditionally Executing Instructions.
BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana Pronounced Bah-bah Co-fee Way-ou-see-jah-nah Call him “Baba” or “Dr. Weusijana”
BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana Pronounced Bah-bah Co-fee Way-ou-see-jah-nah Call him “Baba” or “Dr. Weusijana”
Instructor: Craig Duckett Assignment 1 Due Lecture 5 by MIDNIGHT – NEXT – NEXT Monday, October 10 th I will double dog try to have.
BIT 115: Introduction To Programming L ECTURE 3 Instructor: Craig Duckett
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Prof. Jeremy.
Advanced Programing in MakeArt (Part 1)
BIT115: Introduction to Programming
Copyright © 2008 by Helene G. Kershner
Copyright © 2008 by Helene G. Kershner
BIT115: Introduction to Programming
Conditions and Ifs BIS1523 – Lecture 8.
Introduction to Primitive Data types
Unit 1 Test 1 Redo Friday at 8am here or Friday 4th BLOCK in Math Lab
SSEA Computer Science: Track A
Instructor: Craig Duckett
Thinking about programming
Introduction to Primitive Data types
Presentation transcript:

Instructor: Craig Duckett

Assignment 1 Due Lecture 5 by MIDNIGHT – NEXT – NEXT Wednesday, January 20 th one week from today I will double dog try to have Assignment 1 graded and back to you by Friday, January 22 nd NO CLASSES NEXT MONDAY AND TUESDAY Monday, January 18 th : Martin Luther King, Jr Day Tuesday, January 19 th : Non-Instructional Day 2 Announcements

3 Assignment 1 (LECTURE 5) Wednesday, January 20 in StudentTracker by midnight Assignment 2 (LECTURE 8) Monday, February 1 Assignment 1 Revision (LECTURE 10) Monday, February 8 Assignment 2 Revision (LECTURE 12) Wednesday, February 17 Assignment 3 (LECTURE 13) Monday, February 22 Assignment 3 Revision (LECTURE 16) Wednesday, March 2 Assignment 4 (LECTURE 19) Monday, March 14  NO REVISION Extra Credit 01 (LECTURE 20) Wednesday, March 16 Assignment Dates (By Due Date)

Today's Topics Some More Methods to Use Decisions Making Decisions in Java – If statements – While statements – If/Else (“Either-or”) statements – Logical NOT operator 4

And Now the "Warm Up" Quiz Quiz 5

REFRESHER Extending a Class 6

Another Look at Extending a Class 7 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.

Another Look at Extending a Class 8 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);

Another Look at Extending a Class BIT 115: Introduction To Programming9

Some More Methods (Robot Class) 10

Some More Methods (Robot Class) 11 frontIsClear() – Checks to see if the front is clear countThingsInBackpack() – Checks number of things in backpack canPickThing() – Checks to see if a thing is on intersection getDirection() – Checks the direction the robot is facing getStreet() – Checks the street number robot is on getAvenue() – Checks the avenue number robot is one How these can be used in a program will make more sense after I’ve finished discussing the if and while statements in the next portion of the lecture Up until now we’ve been making use of just a few Robot class methods, like move(), turnLeft(), putThing(), and pickThing(), as well as some new methods we might have created like turnaround(), move3(), or turnRight(). Here are some additional Robot class methods that we are going to start introducing into our programs : Go to Becker Library: Select becker.robots from top left column, then Robot from bottom left column, then scroll over to the Method Summary section in the center right windowhttp://

12 Basic Decision-Making

Chapter 4.1: Two Kinds of Decisions If and While Statements BIT 115: Introduction To Programming13 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.

Two Kinds of Decisions BIT 115: Introduction To Programming14 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.

IfWhile If and While BIT 115: Introduction To Programming15 W hen 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. W hen 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 }.

Example: If Statement 16 ASKS THE QUESTION: Should this statement or group of statements be executed once or not at all? Not At AllOnce MsRobotoIf.java

Example: While Statement 17 ASKS THE QUESTION: Should this statement or group of statements be executed again? YesNo MsRobotWhile.java

BIT 115: Introduction To Programming18

Built-In Queries, Predicates BIT 115: Introduction To Programming19 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

Logical Negation Operator BIT 115: Introduction To Programming20 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”

Testing Integer Queries BIT 115: Introduction To Programming21 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

Two-Sided Queries BIT 115: Introduction To Programming22 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).

The If-Else Statement BIT 115: Introduction To Programming23 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

Brief Intro: Parameters & Variables 24 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!

Brief Intro: Parameters & Variables 25 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 number Inside 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.

4 NOTE: 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 26