Goal: Practice writing pseudocode and understand how pseudocode translates to real code.



Similar documents
Turtle Power. Introduction: Python. In this project, you ll learn how to use a turtle to draw awesome shapes and patterns. Activity Checklist

Introduction to Ethics for Health Care Aides Online course presented by the Manitoba Provincial Health Ethics Network Frequently asked questions

The Rules 1. One level of indentation per method 2. Don t use the ELSE keyword 3. Wrap all primitives and Strings

Why should I back up my certificate? How do I create a backup copy of my certificate?

CS170 Lab 11 Abstract Data Types & Objects

Repetition Using the End of File Condition

How to Pass Physics 212

Lesson #436. The Animals` Guide to Sports Betting

Week 2 Practical Objects and Turtles

HOW TO SELL A STOCK BY KELLY GREEN

Netbeans IDE Tutorial for using the Weka API

Part 1 Foundations of object orientation

CS 51 Intro to CS. Art Lee. September 2, 2014

Coin Flip Questions. Suppose you flip a coin five times and write down the sequence of results, like HHHHH or HTTHT.

How To Create A Digital Signature And Sign A Document With Adobe Reader XI

INTRODUCTION: SQL SERVER ACCESS / LOGIN ACCOUNT INFO:

Objectives. Python Programming: An Introduction to Computer Science. Lab 01. What we ll learn in this class

Introduction to Open Atrium s workflow

Introduction to the course, Eclipse and Python

CS 40 Computing for the Web

Wind River Financial iprocess Setup Guide for Android Devices

Collaboration Policy Fall 2015

ACADEMIC SUPPORT TUTORING HANDBOOK: GUIDELINES & SUGGESTIONS

Program to solve first and second degree equations

ORACLE WEB CONTENT MANAGEMENT SYSTEM 2010

Pseudo code Tutorial and Exercises Teacher s Version

OHIO BUSINESS GATEWAY USER ACCOUNT UPDATE GUIDE FOR PASSWORD RESET AND ACCOUNT SECURITY FUNCTIONALITY


Creating Your Teacher Website using WEEBLY.COM

Android: Setup Hello, World: Android Edition. due by noon ET on Wed 2/22. Ingredients.

Mobile App Design Project #1 Java Boot Camp: Design Model for Chutes and Ladders Board Game

Club Accounts Question 6.

Virtual Desktop Frequently Asked Questions (FAQs)

A Python Tour: Just a Brief Introduction CS 303e: Elements of Computers and Programming

Name: Address: Phone Number Cell Phone Introduction

Preparing your Domain to transfer from Go Daddy

Algorithm & Flowchart & Pseudo code. Staff Incharge: S.Sasirekha

Algorithms Abstraction

Managing Your Network Password Using MyPassword

Practical Study Tips

Class Assignment. College Bus Graphics Design VCP DIGITAL IMAGING III ASSIGNMENT DUE OCTOBER 25 TH FALL 2010

Advanced Tornado TWENTYONE Advanced Tornado Accessing MySQL from Python LAB

LAB 3 Part 1 OBJECTS & CLASSES

CS Matters in Maryland CS Principles Course

Step One Check for Internet Connection

Free Medical Billing. Insurance Payment Posting: The following instructions will help guide you through Insurance Payment Posting Procedures.

Dreamweaver and Fireworks MX Integration Brian Hogan

FSA Infrastructure Trial Guide

LAB4 Making Classes and Objects

Using Karel with Eclipse

Lottery Looper. User Manual

Python Programming: An Introduction to Computer Science

Stage One - Applying For an Assent Remote Access Login

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

Python for Rookies. Example Examination Paper

WorldPay Mobile Demonstration

Ready to get started? Click the button below to tell us which account number you currently have:

Welcome to Online Introductory Biology 101

CS 1133, LAB 2: FUNCTIONS AND TESTING

Quick Start Guide. Microsoft OneNote 2013 looks different from previous versions, so we created this guide to help you minimize the learning curve.

SOFTWARE SELECTION GUIDE. How to Find the Right Software for Your Organization

Wellesley College Alumnae Association. Volunteer Instructions for Template

Learning From Lectures:

Easy Casino Profits. Congratulations!!

Simple Linear Regression

Making the Right Choice

welcome to my Randstad web timesheets!

Get the Most Out of Class

02-201: Programming for Scientists

SLA Online User Guide

Applitools Eyes Web Application Guide

Programming in Access VBA

How to Set-Up your Pay Pal Account and Collect Dues On-Line

Welcome to e2020! Included in this Guide. Tips to Help You Succeed with e2020. Focus on 3 Things: Space, Time, Productivity

Handling of "Dynamically-Exchanged Session Parameters"

How to Write a Good, if not Perfect Lab Notebook

Wiley PLUS Student User Guide

How to Run a Test Trial of New B2B Distribution Channels

Sending on Blue Hornet

Assignment 1: Matchismo

Voice Driven Animation System

W-2 DUPLICATE OR REPRINT PROCEDURE/ IMPORTING W-2 INFORMATION INTO TAX RETURN

Quick Start Guide. Microsoft OneNote 2013 looks different from previous versions, so we created this guide to help you minimize the learning curve.

CS 2302 Data Structures Spring 2015

Cloud Computing. Thin Client

Nursing School 101: What Professors Won t Tell You

Sharpen Solutions. 1 Part One. Sometimes there s more than one right answer. And sometimes the

Agile ICT Website Starter Guides

Online Web Learning University of Massachusetts at Amherst

How to get Office 365 through your Student

Employee Time Clock Training elearning Course Notes

Remote Access Password Tips

Transcription:

Lab 7: Pseudocode Pseudocode is code written for human understanding not a compiler. You can think of pseudocode as English code that can be understood by anyone (not just a computer scientist). Pseudocode is not language specific, which means that given a block of pseudocode you could convert it to Java, Python, C++, or whatever language you so desire. Let us tell you now, pseudocode will be very important to your future in Computer Science (most immediately in CS16 if you choose to take it). Typically pseudocode is used to write a high level outline of an algorithm. As you may already know, an algorithm is a series of steps that a program takes to complete a specific task. The algorithms you will be asked to write can get very complicated to code without a detailed plan, so writing pseudocode before you code will be very beneficial. Goal: Practice writing pseudocode and understand how pseudocode translates to real code. How to Write Pseudocode There are no concrete rules that dictate how to write pseudocode. However, there are standards. A reader should be able to follow the pseudocode and hand simulate what is going to happen at each step. After writing pseudocode, you should be able to easily convert your pseudocode into any programming language you like. We use indentation to delineate blocks of code, so it is clear which lines are inside of which method, loop, etc. Indentation is crucial to writing pseudocode. Java may not care if you don't indent inside your if statements, but a human reader would be completely lost without indentation cues. Remember: Human comprehension is the whole point of pseudocode. So, what does pseudocode look like? Consider an algorithm to complete your math homework. On the next page we ve included examples of both good and bad pseudocode.

Good Pseudocode Real Code (in Java) Bad Pseudocode method domathhomework(): Get pencil Open textbook and notebook Go through all the problems: Complete problem while the problem is wrong: Try again Clean up your desk Submit your homework public void domathhomework(){ this.getpencil(); _textbk.open(); _notebk.open(); } for(int i = 0; i < _problems.length(); i++){ _problems[i].solve(); while(!_problems[i].isright()){ this.erasesolution(i); _problems[i].solve(); this.cleandesk(); this.submit(); method domathhomework(): Get things for homework Do the problems correctly Finish the homework If we were provided the bad pseudocode and asked to convert it into Java, we would have to put a lot of thought into each line in order to convert it, which defeats the original purpose of writing it. When reading the bad pseudocode, a reader might (should) wonder, what things do I need to do homework? or how do I do my homework correctly?. This is a good indicator that your pseudocode isn t specific enough. The good pseudocode is detailed enough that in order to convert it into code, we simply had to read line by line and turn it into code. However, you ll notice that not every line has a 1:1 ratio with real code. For example, Try again is actually implemented as this.erasesolution(i); problems[i].solve();. Because this was a small and simple implementation detail, we could omit it in the pseudocode for the sake of seeing the bigger picture. That said, the more specific your pseudocode is, the more useful it will be when you have to translate it into code going too high level will leave you with pseudocode like the bad example. Also, it s noteworthy that although this pseudocode was converted into Java, it could have just as easily been converted into Python or C++. Remember that pseudocode is not language specific so we are not looking for almost Java code. Instead, we are looking for a strong understanding of the algorithm at hand. Differences in Pseudocode Style In the example above, the pseudocode was very close to prose English. Yet we could still see how to translate it into object oriented code. In some cases, however, the pseudocode could very much resemble code. Consider an algorithm that prints out all odd numbers between 1 and 10 (inclusive): method printoddnumbers():

i = 1 while i < 11: if i is not divisible by 2: print i i++ This pseudocode style was much more appropriate for the given algorithm, and it wouldn t make much sense to have tried to make it better resemble prose English. Pseudocode style depends greatly on personal preference, the problem you re asked to solve, and on what you're optimizing for: speed, readability, aesthetics, etc. As you write more pseudocode, you will discover what style comes most naturally to you. In summary: Roses are red, violets are blue, pseudocode comes in many shapes and sizes, just make sure it appropriately outlines the algorithm you re writing. Drawing Turtles Remember the turtle demo from lecture? You are going to create your own! Your turtle can do the following things: move forward a number of steps (each step is one pixel long) turn left 90 degrees turn right 90 degrees put its pen down, which starts drawing the turtle's path lift its pen up, which stops drawing the turtle's path Pseudocode You will be writing pseudocode for an algorithm that directs the turtle to draw a chain of squares decreasing in size by 10 pixels. The width of the turtle's pen is one pixel. The initial direction of the turtle is facing north. Example: The chain should look like this for a starting length of 50: Fill in the following pseudocode method: method drawsquares(turtle, sidelen){

} // write pseudocode here! Note: sidelen is the starting side length of the squares. Follow up Note: The side length of a square should never be less than 0, right? Checkpoint 1: Show your pseudocode to a TA before moving on. Now that you have finished writing your pseudocode, put your skills to the test and go code your algorithm. If you have written your pseudocode well, it should be a trivial step to translate it line by line into Java. But first... Running the App Run cs015_install lab7. This will install stencil code in /home/<yourlogin>/course/cs015/lab7/. Open up eclipse, right click on App.java, and choose Run as >> Java Application What s this?! You should notice that your program will not run Continue reading to see how to fix this. Oh no! It appears that Spy Turtles have locked the Turtle Drawer, which is preventing you from being able to execute your code. If you inspect the code in MyTurtleDrawer.java you ll notice that the program can be unlocked by successfully passing in a password (that is dependent on your username) to the checkpassword(string username, String password) method. Due to the cunning of the Spy Turtles, our human eye is unable to see the inner workings of this method. So this looks like a job for the Eclipse Debugger (and your super spy abilities). Your mission: Unlock this program.

Mission Impossible checkpassword(string username, String password) works by using your login to generate a password. It then stores this in the variable correctpassword and checks whether your inputted password equals correctpassword. To unlock MyTurtleDrawer you have to figure out what this password is! You could try to figure out how checkpassword(...) generates the password, but this is pretty hard. Instead, use the Eclipse Debugger to find the value of correctpassword! (See Lab 5 for a refresher on the Debugger.) Hint: We should use the Debugger to see the inner workings of the method. Once you get the message Yes! You got the right password! you ve completed Mission Impossible and can move on to making the turtles do your bidding. From Pseudocode to Actual Code Now let s take the pseudo out of pseudocode! You will be implementing your psuedocode in MyTurtleDrawer to make your turtle minions draw a chain of squares. Translate your pseudocode into real code in the drawsquares(turtle turtle, int sidelen) method in the MyTurtleDrawer.java file. Look at the comments in the file to see what methods of Turtle you can call. Note: You do not need to modify the other files, App.java and AbstractTurtleDrawer.java. Note: The Turtle draws from a random point on the screen. Run your program. If your turtle does not draw a chain of squares as expected, go back and hand simulate your pseudocode again! What is going wrong? Checkpoint 2: Show your program to a TA to get checked off for today s lab! Congratulations on finishing the lab. If you finish early, feel free to play around with the drawing turtles and make some cool designs (maybe try a spiral or a filled in square); this is a great way to practice writing loops.