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

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

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

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

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

Assignment 4: Dodo gets smarter

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

Assignment 3: Optimizing solutions

Assignment 8: Sokoban

Assignment 1: Meet Dodo

Assignment 6: Dodo keeps getting smarter

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

Assignment 1: Meet Dodo

Assignment 7: Dodo s Race

CSE115 Introduction to Computer Science I Coding Exercise #7 Retrospective Fall 2017

Java Review. Fundamentals of Computer Science

Computer Programming

Getting to know Greenfoot

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

COMP 202 Recursion. CONTENTS: Recursion. COMP Recursion 1

APCS Semester #1 Final Exam Practice Problems

CS 170 Java Programming 1. Week 13: Classes, Testing, Debugging

Pointers, Arrays and Parameters

Lesson 2.4 Arraylist

Loops and Files. Chapter 04 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz

Computer Programming

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

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

CSCI 1100L: Topics in Computing Lab Lab 11: Programming with Scratch

Answer Key Lesson 5: Area Problems

Lecture Transcript While and Do While Statements in C++

Ticket Machine Project(s)

Lesson 12: OOP #2, Accessor Methods (W03D4)

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

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

Methods. Methods. Mysteries Revealed

CPSC 217 Assignment 3

Recommended Group Brainstorm (NO computers during this time)

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

Subtracting with Multi-Digit Numbers Adaptable for 2 nd, 3 rd, 4 th, and 5 th grades*

Exam 1. CSI 201: Computer Science 1 Fall 2018 Professors: Shaun Ramsey

AP Computer Science Homework Set 1 Fundamentals

CS 170 Java Programming 1. Week 10: Loops and Arrays

Express Yourself. Writing Your Own Classes

Another interface: Comparable

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

School of Computer Science CPS109 Course Notes 6 Alexander Ferworn Updated Fall 15. CPS109 Course Notes 6. Alexander Ferworn

COMP-202: Foundations of Programming. Lecture 26: Review; Wrap-Up Jackie Cheung, Winter 2016

Lesson 1. Introduction to Programming OBJECTIVES

Methods and Data (Savitch, Chapter 5)

CS 170 Java Programming 1. Week 5: Procedures and Functions

Unit 4: Client View of a Component Methods

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Table of Contents Date(s) Title/Topic Page #s. Chapter 4: Writing Classes 4.1 Objects Revisited

Creating objects TOPIC 3 INTRODUCTION TO PROGRAMMING. Making things to program with.

Mobile App:IT. Methods & Classes

Inheritance and Interfaces

Outline. Turtles. Creating objects. Turtles. Turtles in Java 1/27/2011 TOPIC 3 INTRODUCTION TO PROGRAMMING. Making things to program with.

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

School of Computer Science CPS109 Course Notes 5 Alexander Ferworn Updated Fall 15

CSC 121 Spring 2017 Howard Rosenthal

While Loops A while loop executes a statement as long as a condition is true while condition: statement(s) Statement may be simple or compound Typical

Activity 6: Loops. Content Learning Objectives. Process Skill Goals

Introduction to the Java Basics: Control Flow Statements

LECTURE 5 Control Structures Part 2

3 ADT Implementation in Java

Claremont McKenna College Computer Science

Definite Loops. Computer Science S-111 Harvard University David G. Sullivan, Ph.D. Using a Variable for Counting

CS112 Lecture: Defining Instantiable Classes

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

University of Hull Department of Computer Science. Wrestling with Python Week 04 Using Lists

CSCI-1200 Data Structures Spring 2018 Lecture 14 Associative Containers (Maps), Part 1 (and Problem Solving Too)

If you don t, it will return the same thing as == But this may not be what you want... Several different kinds of equality to consider:

Programming in C++ PART 2

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

STUDENT OUTLINE. Lesson 8: Structured Programming, Control Structures, if-else Statements, Pseudocode

Lesson 13: Exploring Factored Form

ALICE: An introduction to progamming

There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions.

QUIZ Lesson 4. Exercise 4: Write an if statement that assigns the value of x to the variable y if x is in between 1 and 20, otherwise y is unchanged.

CS 170 Java Programming 1. Week 12: Creating Your Own Types

CS1 Lecture 5 Jan. 25, 2019

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

AP Computer Science in Java Course Syllabus

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

Activity 6: Loops. Content Learning Objectives. Process Skill Goals

CONTENTS: While loops Class (static) variables and constants Top Down Programming For loops Nested Loops

CMSC 341 Lecture 7 Lists

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

Curriculum Map Grade(s): Subject: AP Computer Science

4. Java language basics: Function. Minhaeng Lee

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

Welcome to... CS113: Introduction to C

Creating Java Programs with Greenfoot

CMSC 201 Computer Science I for Majors

Data Types primitive, arrays, objects Java overview Primitive data types in Java

Chapter 4 Defining Classes I

CS1004: Intro to CS in Java, Spring 2005

Le L c e t c ur u e e 3 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Control Statements

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

Transcription:

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

Today s Lesson plan (3) 10 min Looking back What did we learn last week? Blocks of: Theory Exercises Course exercises and discuss problems / homework 5 min Wrapping up Homework Next week: quiz

Retrospective Parameters, signatures, method calls, results Mutator / accessor methods Getter / Setter methods Flowcharts

Retrospective: sequence

Retrospective: sequence

Accessor methods (questions) flowchart code CAN MOVE True Start NOT border Ahead Initial situation: Dodo is standing in the world False public boolean canmove() { if (! borderahead () ) { return true; } else { return true return false } return false; } End Final situation: equal to initial situation 10

Mutator methods (behavior) flowchart code SEARCH FOR FOOD Start Initial situation: searching for food public void act() { move move(); eatfood True foundfood? False if ( foundfood() ) { eatfood(); gohome(); } gohome } Einde Final situation: moved. Still searching for food or heading home 11

Challenge & problem You must perform two aspects well: 1) Create a problem-solving algorithm (a disciplined and creative process) We use a systematic approach 2) Formulate that algorithm in terms of a programming language (a disciplined and very precise process) We use Java Always check that your algorithm is correct by running/testing the implementation!

Computational thinking Working in a structured manner: Breaking problems down into subproblems Design, solve and test solutions to subproblems Combining these (sub)solutions to solve problem Analyzing the quality of a solution Reflecting on the solution chosen and proces Generalizing and re-use of existing solutions

Anatomy of a method (1) Signature: first line of a method declaration (up to { ) public void jump( int distance ) { signature body }

Anatomy of a method (2) Name of this method public void jump( int distance ) { }

Anatomy of a method (3) What type of result (value) is returned? void = nothing returned int = returns an integer (0, 1, 2,...) etc. a method can return anything public void jump( int distance ) { }

Anatomy of a method (4) Parameters for passing info to this method (here one parameter) Parameter type: the kind of information passed Parameter name public void jump( int distance ) { }

Anatomy of a method (5) Return a boolean (true or false) public boolean canjump( int distance ) { } << body >>

Getter and setter methods A class has it s own: Methods Data Getter vs. setter methods No other object may touch/change this (safe idea!!!) Want info: ask the object with a get method Want to change data: ask the object with a set method

Object-Oriented class design Data: double moneyinwallet Student Methods: double countmoneyinwallet ( )

Class has data and methods Data: int nrofeggslaid int nreggstohatch MyDodo Methods: int getnrofeggslaid ( ) void setnrofeggstohatch ( int nreggstohatch )

Getter vs. Setter methods int getnrofeggslaid ( ) Question: Dodo, please tell me how many eggs you have laid Effect: Dodo returns the number of eggs laid (int) void setnrofeggstohatch (int nreggstohatch) Statement: Dodo, this is the number of eggs you have to hatch Effect: Dodo changes her data so that she remembers (or stores) this new amount.

Today s Lesson Goals Checking and assigning values Algorithms & flowcharts: Sequences Selection (if-then-else) Repetition (while) Structured code modification & debugging Quality of a solution

Any questions so far?

Counting floors

Counting Starts at. NL 3e verdieping 1e verdieping 0: Begane Grond USA 4 th floor 2 nd floor First floor

Counting starts at Tradition Starts counting at USA 1 NL 0 US tradition: skip 13 th floor

Counting starts at Tradition Starts counting at USA 1 NL 0 Tradition Starts counting at Maths 1 Comp. Science 0 US tradition: skip 13 th floor

Start counting at 0!!! 2 1 0

Unplugged: Swap puzzle What it s about: Coming up with an algorithm Looking / planning ahead Efficiency Testing

Swap Puzzle level 1 Square 0 Square 1 Square 2

Swap Puzzle Pieces start on different (non-white) color A piece can move to an empty adjacent square Can jump over an adjacent piece of another color onto an empty square Method to use: getsthepiecefrom Step 1: Square 1 GETS THE PIECE FROM Square 0 Goal: Solve the puzzle in the least amount of steps

Swap Puzzle level 1 Square 0 Square 1 Square 2

Swap Puzzle level 2 A piece can move to an empty adjacent square Can jump over an adjacent piece of another color onto an empty square Goal: Solve the puzzle in the least amount of steps Write down the steps In 5 minutes: compare and share algorithms Square 0 Square 1 Square 2 Square 3 Square 4 Method to use: getsthepiecefrom Move 1: Square 2 GETS THE PIECE FROM Square 1

Swap Puzzle level 2 Challenge: Most efficient algorithm - What to count / how to compare efficiency? - How do you know that your algorithm works? Square 0 Square 1 Square 2 Square 3 Square 4 Tip: SQUARE 2 GETS PIECE FROM SQUARE 1 Can be simplified to (Java code): square2 = square1;

Swap Puzzle level2 Square 0 Square 1 Square 2 Square 3 Square 4 Can be solved in 8 moves: Move1: square2 = square1; // sq2 gets piece from sq1 Move2: square1 = square3; // sq1 gets piece from sq3 Move3: square3 = square4; // sq3 gets piece from sq4 Move4: square4 = square2; // sq4 gets piece from sq2 Move5: square2 = square0; // sq2 gets piece from sq0 Move6: square0 = square1; // sq0 gets piece from sq1 Move7: square1 = square3; // sq1 gets piece from sq3 Move8: square3 = square2; // sq3 gets piece from sq2

Swap Puzzle level 3 Can you come up with the most efficient algorithm? Answer will be revieled next week!

Swap puzzle: what its about Describing your steps => algorithm!! Specific series of actions to get the job done Write down algorithm => then you ll still have solution next week Importance of testing: before: step through your answer (like processor) after: don t assume it works, check it! Efficiency Think of a solution, then check for smarter solution Looking ahead vs. trail and error Look ahead and consider all possible moves Necessary for efficient result with complex problem

Swap-puzzle and assigning values Assigning values using = square 1 gets the value of square 2 Set square1 to (value of) square 2 In Java code: square1 = square2; Check value using == Does square 1 have the value of? In Java, to check if square1 is / has redpiece: if ( square1 == redpiece ) { }

Checking values == means EQUALS TO recall: = means gets value or becomes! Means NOT && Means AND Means OR

Java building blocks (for specifying behaviour) Control structures: constructions to compose programs, Who are you? I m Mimi Sequence Selection (Choice) Repetition 47

Specifying behavior Control structures: constructions to compose programs like: Sequence: Selection: Repetition: stepa; stepb; if ( check() ) then stepsthen else stepselse while ( check() ) stepswhile

Sequence flowchart code public methodname( ) { step1(); step2(); step3(); }

Selection (choice, if..then..else) public methodname( ) { flowchart code } if( check () ) { step1a(); }else{ step1b(); }

Repetition (iteration, loop) WHILE While true, then repeat so, the TRUE-part is repeated flowchart code public methodname( ) { while ( check () ) { dosomething (); } }

Turn facing North- using if Assume you may use the methods: How to turn so that facing North? 1. Algorithm (in words) 2. Flowchart 3. Code Draw a flowchart using (only) if statements

Turn facing North using while Assume you may use the methods: How to turn so that facing North? 1. Algorithm (in words) 2. Flowchart 3. Code Draw a flowchart using a while

Turn facing North using while Why is this solution more elegant / preferable?

Turn facing North alg into code Assume you may use the methods: Flowchart Code boolean facingnorth ( ) void turnright ( ) How to turn so that facing North? 1. Algorithm (in words) 2. Flowchart 3. Code

Flowchart -> code Note: often a NOT is used in condition (just as in words) code public void facenorth( ) { while (! facingnorth ) { flowchart } } turnright ();

Challenge & problem You must perform two aspects well: 1) Create a problem-solving algorithm (a disciplined and creative process) We use a systematic approach 2) Formulate that algorithm in terms of a programming language (a disciplined and very precise process) Always check that your algorithm is correct by running/testing the implementation!

Steps in creating a solution 1. Think Algorithm 2. Flowchart 3. Code public.. methodname( ) { step1(); step2(); step3();

Debugging (fixing mistakes) 1. Remove compile errors 2. Check if code represents flowchart 3. Check if flowchart represents algorithm 4. Check for thinking-errors in your algorithm public.. methodname( ) { step1(); step2(); step3();

Method with repeating code

Use submethods

Advantages submethods Easier to read / understand Code can easily be adjusted Testing of smaller (code) units Submethods can be re-used in other algorithms

Advice when modifying code After each MINOR adjustment Compile Test if it still works If you do too much at once, and then get an error you re doomed to get frustrated! Remember, from our first lesson: Expect to make mistakes!

Computational thinking Working in a structured manner: Breaking problems down into subproblems Design, solve and test solutions to subproblems Combining these (sub)solutions to solve problem Analyzing the quality of a solution Reflecting on the solution and proces Generalizing and re-use of existing solutions

Wrapping up [1] Save your work! Discuss how/when to finish off and who will turn it in. Homework: Course downloads can be found at: http://www.cs.ru.nl/~s.smetsers/greenfoot/dominicus/ Finish Assignment 2 Finish Assignment 3 Hand via email to sjaaksm@live.com

Wrapping up [2] Quiz: what to expect? Topics: Assignment 1 & 2 Difference between accessor/mutator methods Signature of a method (incl. parameters, results) Types (such as int, boolean, String, void) Explain flowcharts: sequence, selection, repetition Devise an algorithm in words Transform an algorithm into flowchart Reflection/evaluation: tips/tops