Comp1004: Programming in Java II
description
Transcript of Comp1004: Programming in Java II
![Page 1: Comp1004: Programming in Java II](https://reader035.fdocuments.us/reader035/viewer/2022062400/568168f5550346895de001c9/html5/thumbnails/1.jpg)
Comp1004: Programming in Java IIComputational Thinking
![Page 2: Comp1004: Programming in Java II](https://reader035.fdocuments.us/reader035/viewer/2022062400/568168f5550346895de001c9/html5/thumbnails/2.jpg)
Coming up
• Introduction to Algorithms– Definition– Characteristics
• Problems to Solutions– The Difficulties– From Steps to Methods– Object Oriented Solutions
![Page 3: Comp1004: Programming in Java II](https://reader035.fdocuments.us/reader035/viewer/2022062400/568168f5550346895de001c9/html5/thumbnails/3.jpg)
Algorithms
![Page 4: Comp1004: Programming in Java II](https://reader035.fdocuments.us/reader035/viewer/2022062400/568168f5550346895de001c9/html5/thumbnails/4.jpg)
Definitions - EtymologyAlgorism (n)
• Arab 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
![Page 5: Comp1004: Programming in Java II](https://reader035.fdocuments.us/reader035/viewer/2022062400/568168f5550346895de001c9/html5/thumbnails/5.jpg)
Definitions - DictionaryAlgorithm (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
![Page 6: Comp1004: Programming in Java II](https://reader035.fdocuments.us/reader035/viewer/2022062400/568168f5550346895de001c9/html5/thumbnails/6.jpg)
Definitions - DictionaryAlgorithm (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
![Page 7: Comp1004: Programming in Java II](https://reader035.fdocuments.us/reader035/viewer/2022062400/568168f5550346895de001c9/html5/thumbnails/7.jpg)
Definitions - DictionaryAlgorithm (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
![Page 8: Comp1004: Programming in Java II](https://reader035.fdocuments.us/reader035/viewer/2022062400/568168f5550346895de001c9/html5/thumbnails/8.jpg)
Definitions - DictionaryAlgorithm (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
![Page 9: Comp1004: Programming in Java II](https://reader035.fdocuments.us/reader035/viewer/2022062400/568168f5550346895de001c9/html5/thumbnails/9.jpg)
Characteristics of an Algorithm• Performance
What else?
![Page 10: Comp1004: Programming in Java II](https://reader035.fdocuments.us/reader035/viewer/2022062400/568168f5550346895de001c9/html5/thumbnails/10.jpg)
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
![Page 11: Comp1004: Programming in Java II](https://reader035.fdocuments.us/reader035/viewer/2022062400/568168f5550346895de001c9/html5/thumbnails/11.jpg)
From Problem to Solution
![Page 12: Comp1004: Programming in Java II](https://reader035.fdocuments.us/reader035/viewer/2022062400/568168f5550346895de001c9/html5/thumbnails/12.jpg)
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.
![Page 13: Comp1004: Programming in Java II](https://reader035.fdocuments.us/reader035/viewer/2022062400/568168f5550346895de001c9/html5/thumbnails/13.jpg)
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 :-)
![Page 14: Comp1004: Programming in Java II](https://reader035.fdocuments.us/reader035/viewer/2022062400/568168f5550346895de001c9/html5/thumbnails/14.jpg)
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?
![Page 15: Comp1004: Programming in Java II](https://reader035.fdocuments.us/reader035/viewer/2022062400/568168f5550346895de001c9/html5/thumbnails/15.jpg)
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
![Page 16: Comp1004: Programming in Java II](https://reader035.fdocuments.us/reader035/viewer/2022062400/568168f5550346895de001c9/html5/thumbnails/16.jpg)
Example: Making a Cup of TeaMake a Cup of TeaGet CupGet KettleGet TeaGet MilkGet Sugar LumpsEmpty KettleFill Kettle with WaterSwitch Kettle onWait until Kettle BoilsPut Tea in PotPut Boiling Water in PotWait 2 MinutesPut Milk in CupPour Tea in CupPut 1 Sugar Lump in CupStir Tea in CupGive Cup of Tea to User
Pseudocode
Assumptions?
![Page 17: Comp1004: Programming in Java II](https://reader035.fdocuments.us/reader035/viewer/2022062400/568168f5550346895de001c9/html5/thumbnails/17.jpg)
Example: Making a Cup of TeaMake a Cup of TeaGet CupGet KettleGet TeaGet MilkGet Sugar LumpsEmpty KettleFill Kettle with WaterSwitch Kettle onWait until Kettle BoilsPut Tea in PotPut Boiling Water in PotWait 2 MinutesPut Milk in CupPour Tea in CupPut 1 Sugar Lump in CupStir Tea in CupGive Cup of Tea to User
Pseudocode
Assumptions:
• Electric Kettle• User wants Milk and Sugar• Only making 1 cup of tea• Nothing goes wrong!
![Page 18: Comp1004: Programming in Java II](https://reader035.fdocuments.us/reader035/viewer/2022062400/568168f5550346895de001c9/html5/thumbnails/18.jpg)
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)
![Page 19: Comp1004: Programming in Java II](https://reader035.fdocuments.us/reader035/viewer/2022062400/568168f5550346895de001c9/html5/thumbnails/19.jpg)
Example: Making a Cup of TeaMake a Cup of TeaGet CupGet KettleGet TeaGet MilkGet Sugar LumpsEmpty KettleFill Kettle with WaterSwitch Kettle onWait until Kettle BoilsPut Tea in PotPut Boiling Water in PotWait 2 MinutesPut Milk in CupPour Tea in CupPut 1 Sugar Lump in CupStir Tea in CupGive Cup of Tea to User
![Page 20: Comp1004: Programming in Java II](https://reader035.fdocuments.us/reader035/viewer/2022062400/568168f5550346895de001c9/html5/thumbnails/20.jpg)
Fetch Utensils and Ingredients
Boil Water in Kettle
Make Tea in Pot
Example: Making a Cup of Tea
Add Tea, Milk and Sugar to Cup
Make a Cup of TeaGet CupGet KettleGet TeaGet MilkGet Sugar LumpsEmpty KettleFill Kettle with WaterSwitch Kettle onWait until Kettle BoilsPut Tea in PotPut Boiling Water in PotWait 2 MinutesPut Milk in CupPour Tea in CupPut 1 Sugar Lump in CupStir Tea in CupGive Cup of Tea to User
![Page 21: Comp1004: Programming in Java II](https://reader035.fdocuments.us/reader035/viewer/2022062400/568168f5550346895de001c9/html5/thumbnails/21.jpg)
Example: Making a Cup of TeaMake a Cup of TeaFetch Utensils and IngredientsBoil Water in KettleMake Tea in PotAdd Tea, Milk and Sugar to CupGive Cup of Tea to User
Fetch Utensils and IngredientsGet CupGet KettleGet TeaGet MilkGet Sugar Lumps
Boil Water in KettleEmpty KettleFill Kettle with WaterSwitch Kettle onWait until Kettle Boils
Make Tea in PotPut Tea in PotPut Boiling Water in PotWait 2 Minutes
Add Tea, Milk and Sugar to CupPut Milk in CupPour Tea in CupPut 1 Sugar Lump in CupStir Tea in CupGive Cup of Tea to User
![Page 22: Comp1004: Programming in Java II](https://reader035.fdocuments.us/reader035/viewer/2022062400/568168f5550346895de001c9/html5/thumbnails/22.jpg)
A Procedural Approach? What about Object Oriented Solutions?
Make a Cup of TeaFetch Utensils and IngredientsBoil Water in KettleMake Tea in PotAdd Tea, Milk and Sugar to CupGive Cup of Tea to User
Fetch Utensils and IngredientsGet CupGet KettleGet TeaGet MilkGet Sugar Lumps
Boil Water in KettleEmpty KettleFill Kettle with WaterSwitch Kettle onWait until Kettle Boils
Make Tea in PotPut Tea in PotPut Boiling Water in PotWait 2 Minutes
Add Tea, Milk and Sugar to CupPut Milk in CupPour Tea in CupPut 1 Sugar Lump in CupStir Tea in CupGive Cup of Tea to User
What are the objects?
Remember: objects should have behaviour and/or bring together a particular set of data
![Page 23: Comp1004: Programming in Java II](https://reader035.fdocuments.us/reader035/viewer/2022062400/568168f5550346895de001c9/html5/thumbnails/23.jpg)
A Procedural Approach? What about Object Oriented Solutions?
Make a Cup of TeaFetch Utensils and IngredientsBoil Water in KettleMake Tea in PotAdd Tea, Milk and Sugar to CupGive Cup of Tea to User
Fetch Utensils and IngredientsGet CupGet KettleGet TeaGet MilkGet Sugar Lumps
Boil Water in KettleEmpty KettleFill Kettle with WaterSwitch Kettle onWait until Kettle Boils
Make Tea in PotPut Tea in PotPut Boiling Water in PotWait 2 Minutes
Add Tea, Milk and Sugar to CupPut Milk in CupPour Tea in CupPut 1 Sugar Lump in CupStir Tea in CupGive Cup of Tea to UserTeasmaid?
What are the objects?
Remember: objects should have behaviour and/or bring together a particular set of data
![Page 24: Comp1004: Programming in Java II](https://reader035.fdocuments.us/reader035/viewer/2022062400/568168f5550346895de001c9/html5/thumbnails/24.jpg)
Make a Cup of TeaFetch Utensils and IngredientsBoil Water in KettleMake Tea in PotAdd Tea, Milk and Sugar to CupGive Cup of Tea to User
Teasmaid?
public class Teasmaid {
Kettle k;Teapot p;
public static void main(String[] args){Teasmaid maid = new Teasmaid();Cup c = maid.makeTea(true, 1);
}
public Cup makeTea(boolean milk, int sugars){if(haveIngredients(milk, sugars)) {
k = new Kettle();p = new Teapot();k.boilWater();p.addTeaBags();k.pourWaterInto(p);
Cup c = prepareCup(milk, sugars);
p.pourTeaInto(c);return c;
}}
}
Warning: Kettle, Teapot and Cup classes need to be defined, and some other methods are needed in Teasmaid
![Page 25: Comp1004: Programming in Java II](https://reader035.fdocuments.us/reader035/viewer/2022062400/568168f5550346895de001c9/html5/thumbnails/25.jpg)
Summary
• Introduction to Algorithms– Definition– Characteristics
• Problems to Solutions– The Difficulties– From Steps to Methods– Object Oriented Solutions