Instructor: Craig Duckett

Similar documents
Today. Homework 1 / Test 1 Making decisions in Java If statements If/Else ( Either-or ) statements Logical NOT operator

BIT 115: Introduction To Programming LECTURE 3. Instructor: Craig Duckett

Lecture 6. Instructor: Craig Duckett

Lecture 12. Instructor: Craig Duckett

Asking For Help. BIT 115: Introduction To Programming 1

Lecture 10. Instructor: Craig Duckett

Lecture 8 " INPUT " Instructor: Craig Duckett

Lecture 7. Instructor: Craig Duckett OUTPUT

ASSIGNMENT 4 Classes and Objects

Lecture 13. Instructor: Craig Duckett. Boolean Expressions, Logical Operators, and Return Values

Lecture 11. Instructor: Craig Duckett. Instance Variables

CS1 Lecture 3 Jan. 18, 2019

Combo Lecture Lecture 14/15. Instructor: Craig Duckett

CS1 Lecture 3 Jan. 22, 2018

Robots. Byron Weber Becker. chapter 6

Lecture 17. Instructor: Craig Duckett. Passing & Returning Arrays

ASSIGNMENT 3 Classes, Objects and the Robot World

Lecture 8. A series on variable types This episode starring Parameter Variables

Lecture 2 Announcements

SSEA Computer Science: Track A. Dr. Cynthia Lee Lecturer in Computer Science Stanford

Variables and Classes. In which we over-ride the super class, work with multiple classes, and move towards non-robot based programming

ASSIGNMENT 4 Records and Objects

Physics 2660: Fundamentals of Scientific Computing. Lecture 5 Instructor: Prof. Chris Neu

CSE 113 A. Announcements - Lab

Programming Lecture 3

Introduction to Java

COMS 1003 Fall Introduction to Computer Programming in C. Bits, Boolean Logic & Discrete Math. September 13 th

cis20.1 design and implementation of software applications I fall 2007 lecture # I.2 topics: introduction to java, part 1

Programming in Karel. Eric Roberts and Jerry Cain CS 106J April 5, 2017

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

CS112 Lecture: Defining Instantiable Classes

Outline. Announcements. Homework 2. Boolean expressions 10/12/2007. Announcements Homework 2 questions. Boolean expression

Topic 4 Expressions and variables

Formal Methods of Software Design, Eric Hehner, segment 1 page 1 out of 5

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

C++ Data Types. 1 Simple C++ Data Types 2. 3 Numeric Types Integers (whole numbers) Decimal Numbers... 5

Math-2. Lesson 3-1. Equations of Lines

2SKILL. Variables Lesson 6. Remembering numbers (and other stuff)...

We know have to navigate between Karel s World view, Karel s Program view and Karel s Execution (or Run) view.

Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types.

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #44. Multidimensional Array and pointers

CS112 Lecture: Introduction to Karel J. Robot

COMP 202 Java in one week

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Computer Programming, I. Laboratory Manual. Experiment #3. Selections

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

Fundamentals of Programming CS-110. Lecture 2

CMSC 201 Computer Science I for Majors

Variables and Data Representation

Math 2280: Introduction to Differential Equations- Syllabus

ENGR 101 Engineering Design Workshop

Computational Expression

MICROPROCESSORS A (17.383) Fall Lecture Outline

COMP combinational logic 1 Jan. 18, 2016

CONTENTS: What Is Programming? How a Computer Works Programming Languages Java Basics. COMP-202 Unit 1: Introduction

StudyHub+ 1. StudyHub: AP Java. Semester One Final Review

Basic Data Types and Operators CS 8: Introduction to Computer Science, Winter 2019 Lecture #2

Genome Sciences 373: Genome Informatics. Quiz section #1 March 29, 2018

SSEA Computer Science: CS106A. Dr. Cynthia Lee Lecturer in Computer Science Stanford

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004

CS 106A, Lecture 5 Booleans and Control Flow

CSE 142 Su 04 Computer Programming 1 - Java. Objects

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. A Guide to this Instructor s Manual:

Skill 1: Multiplying Polynomials

Decisions, Decisions. Testing, testing C H A P T E R 7

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Coding Workshop. Learning to Program with an Arduino. Lecture Notes. Programming Introduction Values Assignment Arithmetic.

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #06 Loops: Operators

CS112 Lecture: Working with Numbers

Robots. Byron Weber Becker. chapter 6

Visual C# Instructor s Manual Table of Contents

Announcements. 1. Forms to return today after class:

Assignment #1: /Survey and Karel the Robot Karel problems due: 1:30pm on Friday, October 7th

1 Making Decisions in Arduino Programs

CS 360: Programming Languages Lecture 10: Introduction to Haskell

Activity Boolean Algebra

Mehran Sahami Handout #7 CS 106A September 24, 2014

printf( Please enter another number: ); scanf( %d, &num2);

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Introduction to C++ General Rules, Conventions and Styles CS 16: Solving Problems with Computers I Lecture #2

CSC-140 Assignment 6

Computational Expression

C++ Reference NYU Digital Electronics Lab Fall 2016

Most of the class will focus on if/else statements and the logical statements ("conditionals") that are used to build them. Then I'll go over a few

The Java Language Rules And Tools 3

Midterms Save the Dates!

Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

CS112 Lecture: Extending Classes and Defining Methods

In Java, data type boolean is used to represent Boolean data. Each boolean constant or variable can contain one of two values: true or false.

More loops Ch

These are notes for the third lecture; if statements and loops.

Exam 1 Prep. Dr. Demetrios Glinos University of Central Florida. COP3330 Object Oriented Programming

PRG PROGRAMMING ESSENTIALS. Lecture 2 Program flow, Conditionals, Loops

Variables and Functions. ROBOTC Software

Chapter 1 Getting Started

Programming Karel the Robot

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

Discussion 1H Notes (Week 2, 4/8) TA: Brian Choi Section Webpage:

Let's Look Back. We talked about how to create a form in HTML. Forms are one way to interact with users

Transcription:

Instructor: Craig Duckett cduckett@cascadia.edu

Announcements Assignment 1 Due Lecture 5 by MIDNIGHT I will double dog dare try to have Assignment 1 graded and back to you by Wednesday, January 24 th hopefully sooner! You betcha! 2

Assignment Dates (By Due Date) Assignment 1 (LECTURE 5) Section 1: Monday, January 22 nd The Fickle Finger of Fate Assignment 2 (LECTURE 8) Section 1: Wednesday, January 31 st Assignment 1 Revision (LECTURE 10) Section 1: Wednesday, February 7 th Assignment 2 Revision (LECTURE 12) Section 1: Wednesday, February 14 th Assignment 3 (LECTURE 13) Section 1: Wednesday, February 21 st Assignment 3 Revision (LECTURE 16) Section 1: Monday, March 5 th Assignment 4 (LECTURE 18) NO REVISION AVAILABLE! Section 1: Monday, March 12 th 3

Today's Topics: Mish-Mash Some More Methods to Use Making Decisions in Java If statements While statements If/Else ( Either-or ) statements Comparison Operators Logical NOT operator! Intro to Variables and Parameters 4

And Now the "Warm Up" Quiz 5

REFRESHER Extending a Class 6

Another Look at Extending a Class Robot Robot object can do A move(); B turnleft(); C pickthing(); extends MrRoboto 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. 7

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

Another Look at Extending a Class 9

Some More Methods (Robot Class) 10

Some More Methods (Robot Class) 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: 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 Go to Becker Library: http://www.learningwithrobots.com/doc/ 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 window 11

Basic Decision-Making Lewis Carroll (Charles Dodgson) 1832-1898 12

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

Two Kinds of Decisions 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 falsefor 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. 14

If and While 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 these statements at least once? if (test statement) { // list of statements } The while statement s question is Should I execute these statements again? while (test statement) { // list of statements } 15

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

Example: While Statement While: Loop as long as true ASKS THE QUESTION: Should this statement or group of statements be executed again? Yes MsRobotWhile.java No 17

BIT 115: Introduction To Programming 18

Built-In Queries, Predicates The Robot class has several built-in queries that answer questions like 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. BIT 115: Introduction To Programming 19

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 Logical Negation Operator 20

Testing Integer Queries The if and while statements always ask True or Falsequestions. 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 BIT 115: Introduction To Programming 21

Using Comparison Operators: A Few Snippets of Code while(bob.countthingsinbackpack() > 0) // What does this do? { bob.puthing(); bob.move(); } if(bob.getdirection() == Direction.WEST) // What does this do? { bob.turnleft(); bob.turnleft(); } while(bob.getdirection()!= Direction.NORTH) // What does this do? { bob.turnleft(); } 22

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

The If-Else Statement 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 24

A Brief Introduction: Variables and Parameters 25

Brief Intro: Variables & Parameters PLEASE NOTE: We will be going over Variables & Parameters again in more detail in several upcoming Lectures. This is just a teaser or "taste" of things to come 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). In other words, a variable can start out one number, like a 0, and be changed by the program into another number, like a 1 or a 2 or a 3, etcetera. 26

Brief Intro: Variables & Parameters PLEASE NOTE: We will be going over Variables & Parameters again in more detail in several upcoming Lectures. This is just a teaser or "taste" of things to come When we set up a variable it is called declaring the variable. This declaration consists of three parts: The type of data the variable will hold The name of the variable The initial value the variable starts with In programming, the type of data is called data type and can be a whole number (integer or int for short), a number that can contain a decimal point (float), a true or false value (boolean), or even a single character (char). The name of the variable can be anything you want, but by convention it must start with an alphabetical character (never a number) although could also start with an underscore _ or a dollar sign $. It can contain numbers, but again, never at the beginning. In programming, when you give a variable a starting value or initial value this is called initialization. Before you can use a variable, it must first be initialized. 27

Brief Intro: Variables & Parameters 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 (function) when calling it in main. Jo.moveExactly(12); 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. 28

Brief Intro: Variables & Parameters 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. 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 29

Brief Intro: Variables & Parameters Passing a value from the method call in main to the method itself. public void dropthingandmove(int num) { while(this.getavenue() < num) { this.putthing(); this.move(); } } Jo.dropThingAndMove(10); But, how does this work with Java? Let's take a look at some example code! movetest.java movetest2.java - NumTest.java 30

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 31