Open and respond to this poll: PollEv.com/comp110. Calling Methods Mr. Roboto

Similar documents
Changing an Object s Properties

Pull Lecture Materials and Open PollEv. Poll Everywhere: pollev.com/comp110. Lecture 12. else-if and while loops. Once in a while

Introducing: main Function, Comments, Statements

Programming Karel the Robot

Assignment #1: and Karel the Robot Karel problems due: 3:15pm on Friday, October 4th due: 11:59pm on Sunday, October 6th

Searching for Roommates Using RoomeeZ Fall Spring 2019

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

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

Slide 1 CS 170 Java Programming 1

1 Getting started with Processing

Activity Guide - Will it Crash?

CSC148H Week 3. Sadia Sharmin. May 24, /20

Unit 4: Client View of a Component Methods

APCS Semester #1 Final Exam Practice Problems

FUNctions. Lecture 03 Spring 2018

CIS4930 / CIS6930: USER EXPERIENCE DESIGN INSTRUCTOR: DR. LISA ANTHONY

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 8: SEP. 29TH INSTRUCTOR: JIAYIN WANG

boolean & if-then-else

Arrays and while Loops. Lecture 15 - COMP110 - Spring 2018

CS61C Machine Structures. Lecture 3 Introduction to the C Programming Language. 1/23/2006 John Wawrzynek. www-inst.eecs.berkeley.

Tutorials. Tutorial every Friday at 11:30 AM in Toldo 204 * discuss the next lab assignment

COMP-202: Foundations of Programming. Lecture 2: Java basics and our first Java program! Jackie Cheung, Winter 2016

CSE 142 Su 04 Computer Programming 1 - Java. Objects

Real World. static methods and Console & JavaFX App Intros. Lecture 16. Go ahead and PULL Lecture Materials & Sign-in on PollEv

Lab 12: Lijnenspel revisited

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 3: SEP. 13TH INSTRUCTOR: JIAYIN WANG

COMP-202: Foundations of Programming. Lecture 2: Java basics and our first Java program! Jackie Cheung, Winter 2015

61A LECTURE 1 FUNCTIONS, VALUES. Steven Tang and Eric Tzeng June 24, 2013

Computer Science II Lecture 1 Introduction and Background

CS 201 Advanced Object-Oriented Programming Lab 6 - Sudoku, Part 2 Due: March 10/11, 11:30 PM

Algorithmic Thinking and Structured Programming (in Greenfoot) Teachers: Renske Smetsers Sjaak Smetsers

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

Software Design and Analysis for Engineers

CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall Office hours:

You can do this before your time period begins.

Final Exam. Programming Assignment 3. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings

Context-Free Grammars

Algorithmic Thinking and Structured Programming (in Greenfoot) Teachers: Renske Smetsers-Weeda Sjaak Smetsers Ana Tanase

A Document Created By Lisa Diner Table of Contents Western Quebec School Board October, 2007

Java Programming. What is a program? Programs Recipes. Programs as Recipes 8/16/12. Pancakes In one bowl. mix: 1½ cup flour

CS 151. Exceptions & Javadoc. slides available on course website. Sunday, September 9, 12

} Each object in a Java program has an identifier (name) } This includes:

Add/Drop Deadlines and Procedures. Fall 2017

Survey #2. Programming Assignment 3. Final Exam. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu.

CSC 111 Introduction to Computer Science (Section C)

Ph3 Mathematica Homework: Week 1

Lesson 10: Quiz #1 and Getting User Input (W03D2)

Lab 1: Introduction to Java

Lecture 1: Overview

Environment Diagrams. Administrivia. Agenda Step by Step Environment Diagram Stuff. The Rules. Practice it s on! What are The Rules?

COMPUTER SCIENCE IN THE NEWS (TWO YEARS AGO) CS61A Lecture 16 Mutable Data Structures TODAY REVIEW: OOP CLASS DESIGN 7/24/2012

COMP 250. Lecture 7. Sorting a List: bubble sort selection sort insertion sort. Sept. 22, 2017

Notes from the Boards Set # 5 Page

6.170 Laboratory in Software Engineering Java Style Guide. Overview. Descriptive names. Consistent indentation and spacing. Page 1 of 5.

Context-Free Grammars

Lines of Symmetry. Grade 3. Amy Hahn. Education 334: MW 8 9:20 a.m.

Staff Directory & Online Classroom: A Picture Book

AP Computer Science A Summer Assignment 2017

Claremont McKenna College Computer Science

Turn in a printout of your code exercises stapled to your answers to the written exercises at 2:10 PM on Thursday, January 13th.

Survey #2. Variable Scope. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings. Scope Static.

Intro. Scheme Basics. scm> 5 5. scm>

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 9: OCT. 4TH INSTRUCTOR: JIAYIN WANG

Tutorial 2 Today we will have a quick introduction to ArgoUML and how to use it, as well as look at some examples of class diagrams.

Lecture 3: Recursion; Structural Induction

Array Basics: Outline. Creating and Accessing Arrays. Creating and Accessing Arrays. Arrays (Savitch, Chapter 7)

Final Exam. Programming Assignment 3. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings

CS61A Lecture 16 Mutable Data Structures. Jom Magrotker UC Berkeley EECS July 16, 2012

We have written lots of code so far It has all been inside of the main() method What about a big program? The main() method is going to get really

Introduction to Programming

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

COMP 250 Winter 2011 Reading: Java background January 5, 2011

Announcements. 1. Forms to return today after class:

Term Definition Introduced in: This option, located within the View tab, provides a variety of options to choose when sorting and grouping Arrangement

Chapter 6: The C Preprocessor

Assignment #1 Simple C++

Making Apps With JavaFX COMP110 - Lecture 23

CMSC201 Computer Science I for Majors

Programs as Models. Procedural Paradigm. Class Methods. CS256 Computer Science I Kevin Sahr, PhD. Lecture 11: Objects

4J SCHOOL DISTRICT - VOICE MAIL SYSTEM ACTIVATING YOUR VOICE MAILBOX STEP 1

Programming Languages and Techniques (CIS120)

6.096 Introduction to C++

GET TO KNOW YOUR HOME PHONE

Homework: More Abstraction, Trees, and Lists

contain a geometry package, and so on). All Java classes should belong to a package, and you specify that package by typing:

Lecture 3: Maps and Iterators

BCIS 3630 Dr. GUYNES SPRING 2018 TUESDAY SECTION [JAN version] GRADER COURSE WEBSITE

CS50 Supersection (for those less comfortable)

CSE : Python Programming

Review: Using Imported Code. What About the DrawingGizmo? Review: Classes and Object Instances. DrawingGizmo pencil; pencil = new DrawingGizmo();

Here are a couple of warnings to my students who may be here to get a copy of what happened on a day that you missed.

METHODS. CS302 Introduction to Programming University of Wisconsin Madison Lecture 15. By Matthew Bernstein

HTML/CSS Lesson Plans

Boolean Expressions. Is Equal and Is Not Equal

Lecture 7. Instructor: Craig Duckett OUTPUT

CS1150 Principles of Computer Science Methods

2. Click on the link at the bottom of the Marywood home page.

Boolean Expressions. Is Equal and Is Not Equal

ECE573 Introduction to Compilers & Translators

Transcription:

Open and respond to this poll: PollEv.com/comp110 Lecture 1 Calling Methods Mr. Roboto Fall 2016 Special thanks to Dr. Andy van Dam, my grad school advisor, and Brown s CS15 course for this fantastic method of gently introducing OOP in Java. I ve yet to find a better way.

Southeast

Announcements Go ahead and respond to the first PollEv Question Open comp110.com/pollev Pro-tip: always keep a browser open on PollEv Waitlist Update The official waitlists on ConnectCarolina have all gotten in as soon as someone drops you can add yourself via ConnectCarolina. Sign-up for text alerts via coursicle.com/notify Section Swaps I will have an update from my student services coordinator tomorrow

Office Hours Office Hours begin tomorrow in Sitterson 008 Starting the semester: Monday through Friday: 11am - 9pm Sunday: 11am - 4pm Come by for help with anything COMP110 related: Eclipse Setup Lecture Setup (what we re doing today) Problem Set 0 - Gotta Catch em All (released tomorrow)

Today Method Calling Class Definitions Method Signatures Problem Set 0 Walkthrough

Meet bb8 bb8 is a robot who lives in a 2D grid world bb8 knows how to do two things: move forward any number of tiles turn right by 90 degrees We will learn how to communicate with bb8 using Java

bb8 s World 0 1 2 3 This is bb8 s world 0 1 bb8 starts at square (0, 0) It wants to get to the square (0, 1) The black lines are walls bb8 cannot pass through

Giving Instructions Goal: move bb8 to the golden nugget by giving it a series of instructions. bb8 only knows instructions move forward n tiles and turn right What instructions should we give? Can you think of a less efficient solution? 0 1 0 1 2 3 (Check in on Poll Everywhere when you re done!)

Giving Instructions 1. Move forward 3 tiles. 0 1 2 3 2. Turn right. 3. Move forward 1 tile. 4. Turn right. 0 1 5. Move forward 3 tiles.

Giving Instructions We have to give bb8 these instructions in a language it understands. That s where Java comes in! In Java, we give instructions to an object by sending it messages. 0 1 0 1 2 3

Calling Methods : Sending Messages in Java bb8 can only handle messages that it knows how to respond to These responses are called methods! method is short for method for responding to a message Objects cooperate by sending each other messages. object sending the message is the caller object receiving the message is the receiver

Calling Methods : Sending Messages in Java bb8 already has one method for move forward n tiles and another method for turn right When we send a message to bb8 to move forward 3 tiles or turn right in Java, we are calling a method on bb8. The khaller. Yo BB8, turn right! The method call. (Message passed from caller to receiver.) The receiver. (Responds to the message with a method.)

Turning bb8 right bb8 s turn right method is called turnright To call the turnright method on bb8: To call methods of bb8 in Java, you need to address it by name Every command to bb8 takes the form: bb8.<method name( )>; You substitute for anything in < >! Semi-colon ; ends a Java statement What are those parenthesis at the end of the method call for?

Moving bb8 forward When telling bb8 to move forward, need to tell it how many tiles to move bb8 s move forward method is named moveforward To call this method in Java: bb8.moveforward(<number of tiles>); This means that if we want to move bb8 forward 2 tiles, say: bb8.moveforward(2);

Guiding bb8 in Java Move forward 3 tiles. Turn right. Move forward 1 tile. Turn right. Move forward 3 tiles. bb8.moveforward(3); bb8.moveforward(1); bb8.moveforward(3); Pseudocode 0 0 1 2 3 Java 1

Putting Code Fragments in a Real Program Let s write this code for real class RobotMover { First, need to put it inside a real Java program Grayed-out code is the context in which bb8 executes these instructions It is part of the stencil code written for you by the COMP110 staff, which also includes bb8 s ability to moveforward and turnright void moverobot(robot bb8) { bb8.moveforward(3); bb8.moveforward(1); bb8.moveforward(3);

Installing Lecture Materials Go to comp110.com Navigate to the Topics page Look for: Getting Started > Setting Up Lecture Materials If you don t have Eclipse installed today, pair up with a neighbor who does today and get yours setup ASAP!

Let s Give bb8 Some Commands Open Lecture > src > comp110.lecture01 > RobotRunner.java Press the Green Play Button When you start working on a new project OR you change projects you are working on, always run the project s Runner Java file before coding. The Run Project Button

Let s Make bb8 Do a Flip Open RobotMover.java Let s make bb8 do a flip. class RobotMover { void moverobot(robot bb8) {

Playing with bb8 Now I ll turn it over to you to guide bb8 to the gold triangle! Feel free to work together and help your neighbor. class RobotMover { void moverobot(robot bb8) { bb8.moveforward(1); Check-in on Poll Everywhere when you re done!

Calling Methods: Major s Method calls in Java have parenthesis after method s name Extra pieces of information needed by a method receiver are called parameters; the values the caller provides are called arguments More on this terminology next week! If the method receiver needs any information, include it between parentheses. e.g., bb8.moveforward(2); If no extra information is needed, just leave the parentheses empty e.g.,

So what s all this other code? Before, we talked about objects receiving messages via methods. Introducing a new concept classes! class RobotMover { void moverobot(robot bb8) { bb8.moveforward(3); bb8.moveforward(1); bb8.moveforward(3);

What is a class? A class is a blueprint for a certain type of object. An object s class defines its capabilities (methods) and properties (fields) More on properties next week! So far, we ve been working within the class RobotMover We need to tell Java about class RobotMover { void moverobot(robot bb8) { bb8.moveforward(3); bb8.moveforward(1); bb8.moveforward(3); our RobotMover

Declaring and Defining a Class (1/3) As with a dictionary entry, a term is declared - then defined. First line declares RobotMover class Breaking it down: class indicates to Java that we are about to define a new class RobotMover is the name we choose for our class Declaration class RobotMover { void moverobot(robot bb8) { bb8.moveforward(3); bb8.moveforward(1); bb8.moveforward(3);

Declaring and Defining a Class (2/3) Class definition (aka body ) defines properties and capabilities of class It is contained within the curly braces that follow the declaration. A class s capabilities ( what it knows how to do ) are defined by its methods. Our RobotMover class only knows this very specific moverobot method. class RobotMover { void moverobot(robot bb8) { bb8.moveforward(3); bb8.moveforward(1); bb8.moveforward(3); Definition

Declaring and Defining a Class (3/3) General form for a class: class <name> { <capabilities (methods) and properties> Each class goes in its own file, where name of file matches the name of the class: RobotMover class is in a file named RobotMover.java

The Robot Class (defined by us) Note: Normally, support code is black box that you can t examine. class Robot { /* other code elided */ void turnright() { // code that turns robot right void moveforward(int numberoftiles) { // code that moves robot forward class Robot declares a class called Robot Information about the properties and capabilities of Robots (the class definition) goes within the curly braces.

Methods of the Robot Class Remember: In our example, bb8 is a Robot! class Robot { /* other code elided */ void turnright() { // code that turns robot right void moveforward(int numberoftiles) { // code that moves robot forward The bold lines are method declarations The code inside the curly braces of a method declaration is the method s definition - are you sensing a pattern here? We ll learn a lot about method declarations and definitions next week! We send messages to an object by calling its methods. The object s class tells us the methods it has available.

Robot class Methods void turnright() void moveforward(int numberoftiles) The Robot class has 2 methods: turnright and moveforward Because bb8 s class is Robot we are able to call these methods on bb8. How would we know that moveforward needed an extra piece of information? Its method declaration tells us! int - in Java this means whole number numberoftiles - a hint as to the extra information needed On Tuesday we ll cover this in depth

Robot class Methods void turnright() void moveforward(int numberoftiles) bb8 s Method Calls bb8.moveforward(3); This is a list of things we can ask a Robot to do. This is a series of instructions we re giving to a single Robot. Intuitive take away: by knowing a class methods, we can send commands to an object of that class.

Preview of Pokemon Problemset

Trainer class Methods void choosegender(string gender) void moveforward(int numberofsteps) void moveboulder() atrainer s Method Calls atrainer.choosegender( female ); atrainer.moveforward(3); atrainer.moveboulder(); This is a list of things we can ask a Trainer to do. This is a series of instructions we re giving to a specific trainer. Intuitive take away: by knowing a class methods, we can send commands to an object of that class.