Week 4 Approaches – Psuedo Code. Last week Introduced the basic concepts Sequential Conditional branching Looping This week Methods to describe problems.

Slides:



Advertisements
Similar presentations
Chapter 8 Testing a class. This chapter discusses n Testing in general. n Testing a single class. n Test plans. n Building a test system.
Advertisements

Introduction to Programming
Lecture 10 Flow of Control: Loops (Part 2) COMP1681 / SE15 Introduction to Programming.
Loops –Do while Do While Reading for this Lecture, L&L, 5.7.
START DEFINITIONS values (3) N1 = (8, 1,-9) i N1 average N3,2 sum N2 = 0 temp N1 Do not guess or assume any values! Follow the values of the variables.
Java Planning our Programs Flowcharts Arithmetic Operators.
 Draft timetable has the same times as this semester: - ◦ Monday 9:00 am to 12:00 noon, 1:00 pm to 3:00 pm. ◦ Tuesday 9:00 am to 12:00 noon, 1:00 pm.
1 9/28/07CS150 Introduction to Computer Science 1 Loops section 5.2, 5.4, 5.7.
Classes, methods, and conditional statements We’re past the basics. These are the roots.
1 Arithmetic in C. 2 Type Casting: STOPPED You can change the data type of the variable in an expression by: (data_Type) Variable_Name Ex: int a = 15;
Loops Chapter 4. It repeats a set of statements while a condition is true. while (condition) { execute these statements; } “while” structures.
COMP 14: I/O and Boolean Expressions May 24, 2000 Nick Vallidis.
10 ThinkOfANumber program1July ThinkOfANumber program CE : Fundamental Programming Techniques.
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
CHAPTER 1 GC 101 Introduction to computers and programs.
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
Java for Robots How to program an NXT robot with a Java Brain Bert G. Wachsmuth Seton Hall University.
Hello AP Computer Science!. What are some of the things that you have used computers for?
CH Programming An introduction to programming concepts.
Programming Concepts Part B Ping Hsu. Functions A function is a way to organize the program so that: – frequently used sets of instructions or – a set.
Karel J Robot An introduction to BlueJ and Object- Oriented Programming.
Advanced Programming LOOP.
11 Chapter 5 METHODS CONT’D. 22 MORE ON PASSING ARGUMENTS TO A METHOD Passing an Object Reference as an Argument to a Method Objects are passed by reference.
CSC 204 Programming I Loop I The while statement.
Lecture 4 Introduction to Programming. if ( grade ==‘A’ ) cout
Basics of Java IMPORTANT: Read Chap 1-6 of How to think like a… Lecture 3.
Repetition & Loops. One of the BIG advantages of a computer: ­It can perform tasks over and over again, without getting bored or making mistakes (assuming.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 5: Software Design & Testing; Revision Session.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Methods (a.k.a. Functions)
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) public static void main(String[]
2008 SBPLI/FIRST Programming Workshop Tom Boehm Patchogue Medford High School, Team 329 Motorola Inc. Mark McLeod Hauppauge High School Team 358 Northrop.
Java Arrays and Methods MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation ©Akhilesh.
Georgia Institute of Technology More on Creating Classes part 2 Barb Ericson Georgia Institute of Technology Oct 2005.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Classes. Student class We are tasked with creating a class for objects that store data about students. We first want to consider what is needed for the.
Indentation & Readability. What does this program do? public class Hello { public static void main ( String[] args ) { //display initial message System.out.println(
Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.
1 Chapter 2: Java Fundamentals cont’d Spring Lory Al Moakar.
Java Review if Online Time For loop Quiz on Thursday.
© The McGraw-Hill Companies, 2006 Chapter 3 Iteration.
Programming with LabVIEW Intro to programming and.
1 Class Chapter Objectives Use a while loop to repeat a series of statements Get data from user through an input dialog box Add error checking.
Java Methods 11/10/2015. Learning Objectives  Be able to read a program that uses methods.  Be able to write a write a program that uses methods.
Files Review For output to a file: –FileOutputStream variable initialized to filename (String) and append/not append (boolean) –PrintWriter variable initialized.
 CSC111 Quick Revision. Problem Write a java code that read a string, then show a list of options to the user to select from them, where:  L to print.
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
TOPIC 8 MORE ON WHILE LOOPS 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B. Ericson,
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
Chapter 2 Clarifications
CSC111 Quick Revision.
using System; namespace Demo01 { class Program
Week of 12/12/16 Test Review.
Counted Loops.
Repetition Chapter 6 12/06/16 & 12/07/16 1 1
Decision statements. - They can use logic to arrive at desired results
Java Fix a program that has if Online time for Monday’s Program
Week 2 – Basic Constructs 1
LRobot Game.
Introduction to Programming
Java Fix a program that has if Online time for Monday’s Program
Building Java Programs
Week 4 Lecture-2 Chapter 6 (Methods).
Python Basics with Jupyter Notebook
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
Random Numbers while loop
DATA TYPES There are four basic data types associated with variables:
Loops CGS3416 Spring 2019 Lecture 7.
Methods/Functions.
CSE 1321 Modules 1-5 Review Spring 2019
Presentation transcript:

Week 4 Approaches – Psuedo Code

Last week Introduced the basic concepts Sequential Conditional branching Looping This week Methods to describe problems

Problem 2.2 A routine to calculate a tip based on the overall service. No tip if the service is poor 10% tip if the service is okay 15% tip if the service is good or better

Problem 2.2 – Problems What is poor, okay or good mean. How do we quantify these? We could use marks out of 10. <3 for poor 3<=marks<=8 for okay >8 for good What is our tip a percentage of? We know this is going to be a percentage of the price of the bill.

Inputs needed Price of the meal Marks for service Outputs Price + tip Rules <3 for poor 3<=marks<=8 for okay >8 for good

User enters the price on the keyboard. User enters the marks out of 10 If mark is less than 3then Tip is 0; If 3<=marks<=8 then Tip is the Price*0.1; If marks>8 then Tip is Price*0.15 Display Tip

Psuedo code Input price Input marks If marks<3for Tip=0; If 3<=marks<=8 then Tip =Price*0.1; If marks>8 then Tip=Price*0.15 Display Tip

This is the start of psuedo code writing out our steps to be taken; in a form that is almost a programming language. We can refine this further

Further refinement Display message to the user to enter the price Input from the keyboard and stored in variable price Display message to the user to enter the marks Input from the keyboard and stored in variable marks If marks<3for Tip=0; If 3<=marks<=8 then Tip =Price*0.1; If marks>8 then Tip=Price*0.15 Display Tip

Set up variables price, marks; Display “Please enter the price”; Input price; Display “Please enter the marks”; Input marks; If marks<3 Tip=0; If 3<=marks<=8 Tip =Price*0.1; If marks>8 Tip=Price*0.15; Display Tip;

Advantages Through incremental change it can fairly intuitive to go from plain English descriptions to almost programming code.

Disadvantages Describing loops and the relationships between statements in loop can be unclear. The way it is used can often be specific to the language used to write the programs.

Problem 4.1 Returning to the square tracing project, but this time lets use a loop. buggy.forward1(1000); buggy.trturn(1000); buggy.forward1(1000); buggy. trturn(1000); buggy. forward1(1000); buggy. trturn(1000); buggy. forward1(1000); buggy. trturn(1000);

The lines below are repeated 4 times: forward1(1000); trturn(1000); So if put these in a loop it could get them to repeat as many times as we want.

First pass Loop four times buggy moves forward for 1000 milliseconds; buggy performs a tight turn to the right for 1000 milliseconds; End loop How does the routine know how may times to loop?

Second pass counter=1; Loop while counter <=4 forward1(1000); trturn(1000); counter=counter+1; End loop This is an acceptable place to stop with this problem.

Java Code public class week4_2{ public static void main(String[] args) {int counter=1; robot_1 buggy=new robot_1(); while (counter <=4) { buggy.forward1(1000); buggy.trturn(1000); counter=counter+1; }

Problem 4.3 Write a routine to get the robot to follow a black line on a white background. Two light sensors (left and right ones) If a sensor is over the line it produces a true output. The sensors are independent of each other so can produce different results depend on line position.

public class line_follower { public static void main (String[] args) { robot2 dick=new robot2(); for(;;) { if ((dick.checkLight(1)==true)&&(dick.checkLight(2)==true)) { //if both sensor are on the line what//action do you want to do? } if ((dick.checkLight(1)==false)&&(dick.checkLight(2)==false)) { //if both sensor are off the line //what action do you want to do? } if ((dick.checkLight(1)==true)&&(dick.checkLight(2)==false)) { //if left sensor is one the line and right sensor is off //what action do you want to do? } if ((dick.checkLight(1)==false)&&(dick.checkLight(2)==true)) { //if right sensor is on the line and left sensor is off //what action do you want to do? }