Comp1202: Building Better Programs

Slides:



Advertisements
Similar presentations
Programming for Beginners
Advertisements

Working With Algorithm and Flowcharts
Basic Programming Concepts INTRO TO PROGRAMMING. Questions to answer  What is a computer program?  What are computer instructions  How is a program.
Algorithms and Problem Solving
Tutorial #6 PseudoCode (reprise)
COEN 352 Data structures and Algorithms R. Dssouli.
 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.
SDP Languages and Environments. Types of Languages and Environments There are 4 main types of language that you must be able to describe at Higher level.
Fibonacci Numbers A simple example of program design.
Lecturer: Fintan Costello Welcome to Hdip 001 Introduction to Programming.
Web Infrastructure Week 3 INFM 603. The Key Ideas Questions Structured Programming Modular Programming Data Structures Object-Oriented Programming.
Lecture Notes 1/21/04 Program Design & Intro to Algorithms.
Stacks. What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
Programming Fundamentals (750113) Ch1. Problem Solving
Welcome to COMP 1001 First Year Computer Science Lecturer: Fintan Costello.
The Project AH Computing. Functional Requirements  What the product must do!  Examples attractive welcome screen all options available as clickable.
Abstraction IS 101Y/CMSC 101 Computational Thinking and Design Tuesday, September 17, 2013 Carolyn Seaman University of Maryland, Baltimore County.
Introduction to Programming Lecture Number:. What is Programming Programming is to instruct the computer on what it has to do in a language that the computer.
CS001 Introduction to Programming Day 5 Sujana Jyothi
Introduction Algorithms and Conventions The design and analysis of algorithms is the core subject matter of Computer Science. Given a problem, we want.
A PowerPoint about Algorithm’s. What is an algorithm?  a process or set of rules to be followed in calculations or other problem-solving operations,
Board Activity Find your seat on the seating chart Login – Remember your login is your first initial your last name and the last three numbers of your.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 5: Software Design & Testing; Revision Session.
What does a computer program look like: a general overview.
Algorithms CS139 – Aug 30, Problem Solving Your roommate, who is taking CS139, is in a panic. He is worried that he might lose his financial aid.
A PowerPoint about Algorithm’s. What is an algorithm? A step by step process of instruction.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
1 CS161 Introduction to Computer Science Topic #9.
Sequence Damian Gordon. Pseudocode When we write programs, we assume that the computer executes the program starting at the beginning and working its.
How Are Computers Programmed? CPS120: Introduction to Computer Science Lecture 5.
Algorithms and Pseudocode
Computer Control Using computers to do tasks. What are Computers used for? ringing your alarm clock. microwaving your breakfast checking for text messages.
Pseudocode Skill Area Materials Prepared by Dhimas Ruswanto, BMm.
1 Structured Programming Arab Academy for Science and Technology CC112 Dr. Sherif Mohamed Tawfik The Course.
Sequences, Modules and Variables David Millard
Introduction to Problem Solving Programming is a problem solving activity. When you write a program, you are actually writing an instruction for the computer.
Comp1004: Programming in Java II Computational Thinking.
Systems in the Small: An Introduction to Algorithms David Millard
Algorithms and Flowcharts
Algorithms 1: Sequences and Modules Yvonne Howard & Rikki Prince
Control Structures: Conditionals, If/Else and Loops David Millard
Building Good Solutions David Millard
JavaScript/ App Lab Programming:
Duncan MacFarlane Professor of Electrical Engineering
Software Development.
INTRODUCTION TO PROBLEM SOLVING
Basics of Computer Programming
Lecture 2 Introduction to Programming
ALGORITHM Basic CONCEPTS of Basic Concepts of Algorithm
Basics of Computer Programming
Basics of Computer Programming
Yenka Portfolio Level for this topic: Student Name : My Levels
CS 240 – Lecture 11 Pseudocode.
CS Programming I Jim Williams, PhD.
Algorithms Today we will look at: what the word algorithm means
Algorithms Y10 Introduction.
A simple example of program design
Comp1202: Introduction III
UNIT 3 CHAPTER 1 LESSON 4 Using Simple Commands.
Programming Fundamentals (750113) Ch1. Problem Solving
Programming Fundamentals (750113) Ch1. Problem Solving
Unit 6 Assignment 2 Chris Boardley.
Building Java Programs
Data Structures and Algorithms CSE 465
Programming Fundamentals (750113) Ch1. Problem Solving
Week 4 Lecture-2 Chapter 6 (Methods).
Algorithms Algorithm. [webster.com] A procedure for solving a mathematical problem (as of finding the greatest common divisor) in a finite number of steps.
Programming Fundamentals (750113) Ch1. Problem Solving
Algorithm: Word comes from Arabic Author Abū ‘Abdallāh Muḥammad ibn Mūsā al-Khwārizmī Algorism Algebra: Kitab al-Mukhtasar fi Hisab al-Jabr wal-Muqabala.
Welcome to the most Amazing course there is 
Presentation transcript:

Comp1202: Building Better Programs Computational Thinking

Reminder: Getting Help Now is about the time when some people realise they are getting lost If this is you, Act! Check you are practicing enough (6 hours per week + lectures and labs) Attend Ground Controllers Ask the demonstrators in the Lab See helpdesk with specific topic/questions Form a study group To help we will spend this week consolidating some of the ideas we have encountered so far in order to answer a not-so-simple question: How do you write good code? In the next lecture this will take us to one of the fundamental principles of Object Oriented Programming: Encapsulation

Coming up Introduction to Algorithms Problems to Solutions Definition Characteristics Problems to Solutions The Difficulties From Steps to Methods Object Oriented Solutions

Algorithms

Definitions - Etymology Algorism (n) Persian mathematician Abu Abdullah Muhammad ibn Musa al-Khwarizmi (early 9th century) Europe became aware of his work on Algebra Arab numerals became associated with his name Has since evolved to mean all processes for solving tasks

Definitions - Dictionary Algorithm (n) “An algorithm is a sequence of finite instructions, often used for calculation and data processing…” Wikipedia "A step-by-step problem-solving procedure, especially an established, recursive computational procedure for solving a problem in a finite number of steps.” Answers.com “A step-by-step procedure for solving a problem or accomplishing some end especially by a computer” Merriam Webster Dictionary

Definitions - Dictionary Algorithm (n) “An algorithm is a sequence of finite instructions, often used for calculation and data processing…” Wikipedia "A step-by-step problem-solving procedure, especially an established, recursive computational procedure for solving a problem in a finite number of steps.” Answers.com “A step-by-step procedure for solving a problem or accomplishing some end especially by a computer” Merriam Webster Dictionary

Definitions - Dictionary Algorithm (n) “An algorithm is a sequence of finite instructions, often used for calculation and data processing…” Wikipedia "A step-by-step problem-solving procedure, especially an established, recursive computational procedure for solving a problem in a finite number of steps.” Answers.com “A step-by-step procedure for solving a problem or accomplishing some end especially by a computer” Merriam Webster Dictionary

Definitions - Dictionary Algorithm (n) “An algorithm is a sequence of finite instructions, often used for calculation and data processing…” Wikipedia "A step-by-step problem-solving procedure, especially an established, recursive computational procedure for solving a problem in a finite number of steps.” Answers.com “A step-by-step procedure for solving a problem or accomplishing some end especially by a computer” Merriam Webster Dictionary

Characteristics of an Algorithm Performance What else?

Characteristics of an Algorithm Performance Efficiency Understandability Scalability Reusability Reliability Elegance Elegance (n). Of scientific processes, demonstrations, inventions, etc.: ‘Neatness’, ingenious simplicity, convenience, and effectiveness - OED

From Problem to Solution

Example: Making a Cup of Tea Get into small groups of 3 or 4 and write down the steps that you need to do in order to make a cup of tea Use a sequence of simple statements like ‘Boil Water’ or ‘Put Milk in Cup’ Try and put down an appropriate level of detail so that a person could follow your instructions unambiguously Can you group certain steps together (for example, are the first few about preparation)? Give these groups sensible names.

Swap with Another Group Take a look at the tea-making instructions of another group Annotate their instructions if: Any instruction is ambiguous They have missed something out They have made an assumption They have created a group that doesn’t make sense to you Also if they have done anything you really like :-)

Go Back to Your Own Instructions Take a few moments to see what they have said Are the comments fair? Any unexpected ones? Would you make any changes now you have seen someone else’s instructions?

Writing Sequences is Easy… … But getting the sequence right is hard Often the specification is inadequate It is easy to make assumptions without realising it Making it complete is challenging Making sure not to miss smaller, less-obvious steps Creating unambiguous instructions Machines are very unforgiving, they do exactly what you ask – nothing more, nothing less

Example: Making a Cup of Tea Make a Cup of Tea Get Cup Get Kettle Get Tea Get Milk Get Sugar Lumps Empty Kettle Fill Kettle with Water Switch Kettle on Wait until Kettle Boils Put Tea in Pot Put Boiling Water in Pot Wait 2 Minutes Put Milk in Cup Pour Tea in Cup Put 1 Sugar Lump in Cup Stir Tea in Cup Give Cup of Tea to User Pseudocode Assumptions?

Example: Making a Cup of Tea Make a Cup of Tea Get Cup Get Kettle Get Tea Get Milk Get Sugar Lumps Empty Kettle Fill Kettle with Water Switch Kettle on Wait until Kettle Boils Put Tea in Pot Put Boiling Water in Pot Wait 2 Minutes Put Milk in Cup Pour Tea in Cup Put 1 Sugar Lump in Cup Stir Tea in Cup Give Cup of Tea to User Pseudocode Assumptions: Electric Kettle User wants Milk and Sugar Only making 1 cup of tea Nothing goes wrong!

Modules Modules break an algorithm into logical parts (like your groups) Helps with Clarity and Understandability Modules can be reused Within the same algorithm In a different algorithm In Programming Modules can be called: Sub-routines (in older languages) Functions (in procedural languages like C) Methods (in object oriented languages like Java)

Example: Making a Cup of Tea Make a Cup of Tea Get Cup Get Kettle Get Tea Get Milk Get Sugar Lumps Empty Kettle Fill Kettle with Water Switch Kettle on Wait until Kettle Boils Put Tea in Pot Put Boiling Water in Pot Wait 2 Minutes Put Milk in Cup Pour Tea in Cup Put 1 Sugar Lump in Cup Stir Tea in Cup Give Cup of Tea to User

Example: Making a Cup of Tea Make a Cup of Tea Get Cup Get Kettle Get Tea Get Milk Get Sugar Lumps Empty Kettle Fill Kettle with Water Switch Kettle on Wait until Kettle Boils Put Tea in Pot Put Boiling Water in Pot Wait 2 Minutes Put Milk in Cup Pour Tea in Cup Put 1 Sugar Lump in Cup Stir Tea in Cup Give Cup of Tea to User Fetch Utensils and Ingredients Boil Water in Kettle Make Tea in Pot Add Tea, Milk and Sugar to Cup

Example: Making a Cup of Tea Make a Cup of Tea Fetch Utensils and Ingredients Boil Water in Kettle Make Tea in Pot Add Tea, Milk and Sugar to Cup Give Cup of Tea to User Fetch Utensils and Ingredients Get Cup Get Kettle Get Tea Get Milk Get Sugar Lumps Boil Water in Kettle Empty Kettle Fill Kettle with Water Switch Kettle on Wait until Kettle Boils Make Tea in Pot Put Tea in Pot Put Boiling Water in Pot Wait 2 Minutes Add Tea, Milk and Sugar to Cup Put Milk in Cup Pour Tea in Cup Put 1 Sugar Lump in Cup Stir Tea in Cup Give Cup of Tea to User

Comp1202: Building Better Programs A Note on Coding Style

Naming Data and Methods should have meaningful names Classes – UpperCamelCase Variables/Methods/Parameters – lowerCamelCase Constants – UPPERCASE Java is a verbose language - Embrace it! Don’t be afraid to be explicit (within reason!) avMk averageMark averageMarkOfAnIndividualStudentInProgramming1Cohort

Layout Code should be indented so that the structure is clear Use brackets to be explicit about grouping statements public static void main(String[] args) { Account myAccountObject = new Account(); myAccountObject.withdraw(5); myAccountObject.withdraw(10); } vs. public static void main(String[] args) {Account myAccountObject = new Account(); myAccountObject.withdraw(5); myAccountObject.withdraw(10);}

Comments Comments should explain appropriate blocks of code Every method / class Every logical section of a method There is no need to explain the obvious! //add 100 to the variable i int i = i + 100; Java has an advanced comment system called JavaDoc Compiles comments into documentation The Java API is entirely generated by JavaDoc

Style Guide Google has a good style guide: https://google.github.io/styleguide/javaguide.html Note that everyone has minor differences (quirks) to the way they program Most important to be consistent, and to be clear (check out the Obfuscated Code Contest – it is worth a quick Google – and is everything that you should not do!)

Summary Introduction to Algorithms Problems to Solutions Definition Characteristics Problems to Solutions The Difficulties From Steps to Methods Object Oriented Solutions Java Coding Style