<INSERT_WITTY_QUOTE_HERE>

Slides:



Advertisements
Similar presentations
Week 5: Loops 1.  Repetition is the ability to do something over and over again  With repetition in the mix, we can solve practically any problem that.
Advertisements

Loops (Part 1) Computer Science Erwin High School Fall 2014.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Unit 181 Recursion Definition Recursive Methods Example 1 How does Recursion work? Example 2 Problems with Recursion Infinite Recursion Exercises.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Chapter 15 Recursive Algorithms. 2 Recursion Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
Overview scope - determines when an identifier can be referenced in a program storage class - determines the period of time during which that identifier.
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
Programming Part 1 Armond R. Smith Zhenying Wu. Overview of this Class ● Transition from FTC -> FRC ● Using Your Resources ● Java Keywords o Data Types.
Recursion, Complexity, and Searching and Sorting By Andrew Zeng.
A Computer Science Tapestry 1 Recursion (Tapestry 10.1, 10.3) l Recursion is an indispensable technique in a programming language ä Allows many complex.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
M180: Data Structures & Algorithms in Java
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
1 TCSS 143, Autumn 2004 Lecture Notes Recursion Koffman/Wolfgang Ch. 7, pp ,
Arrays An array is a data structure that consists of an ordered collection of similar items (where “similar items” means items of the same type.) An array.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
Recursion Recursion Chapter 12. Outline n What is recursion n Recursive algorithms with simple variables n Recursion and the run-time stack n Recursion.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Chapter 5: Control Structures II
CPSC1301 Computer Science 1 Chapter 8 Introduction to Processing Digital Sounds part 3.
ITI 1120 Lab #5 Contributors: S. Boyd, R. Plesa, A. Felty, D. Inkpen, A. Williams, D. Amyot.
Intro-Sound-Mod10-part31 Introduction to Processing Digital Sounds part 3 while loop, tracing, for loop, parameters Barb Ericson Georgia Institute of Technology.
CSE 143 Lecture 9 Recursion slides created by Alyssa Harding
Recursion.
Lesson #5 Repetition and Loops.
Recursion Recursion is a fundamental programming technique that can provide an elegant solution certain kinds of problems © 2004 Pearson Addison-Wesley.
Review Array Array Elements Accessing array elements
Lecture 2 D&D Chapter 2 & Intro to Eclipse IDE Date.
4. Java language basics: Function
Recursion DRILL: Please take out your notes on Recursion
CSC 222: Object-Oriented Programming
The switch Statement, and Introduction to Looping
Lesson #5 Repetition and Loops.
5. Function (2) and Exercises
CS1371 Introduction to Computing for Engineers
Creating and Modifying Text part 2
Repetition-Counter control Loop
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Reindeer don't go to public school, they’re elf taught.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Announcements Final Exam on August 17th Wednesday at 16:00.
Manipulating Pictures, Arrays, and Loops part 2
Algorithm design and Analysis
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
PC02 Term 2 Test Recursion and Sorting. PC02 Term 2 Test Recursion and Sorting.
Lesson #5 Repetition and Loops.
Writing Methods AP Computer Science A.
PC02 Consolidation Loading a witty quote…. PC02 Consolidation Loading a witty quote…
Fundamentals of Programming
Unit 6 Working with files. Unit 6 Working with files.
Topic 1: Problem Solving
Console.WriteLine(“Good luck!”);
String and Lists Dr. José M. Reyes Álamo.
Unit 3 Test: Friday.
Introduction to Processing Digital Sounds part 3
What's wrong with Easter jokes? They crack you up
Debugging “Why you were up till 2AM”
Console.WriteLine(“Good luck!”);
The last lesson of this term!
Lesson #5 Repetition and Loops.
Creating and Modifying Text part 3
LCC 6310 Computation as an Expressive Medium
CSE 206 Course Review.
Software Development Techniques
Lecture 20 – Practice Exercises 4
Presentation transcript:

<INSERT_WITTY_QUOTE_HERE> PC02 Consolidation <INSERT_WITTY_QUOTE_HERE>

This is the consolidation for this term Introduction This is the consolidation for this term Has lots of exercises on all the topics covered And some small things that are interesting, but you haven’t seen yet Start at any point you want Put all these in a single project called “Term02Consolidation” And then packages for each topic you do

Lists and Generics Working with files Fixing errors Recursion Index Click me Click me Click me Click me

There is a sorting algorithm that we haven’t used yet Lists and Generics There is a sorting algorithm that we haven’t used yet It’s called random sort or bogoSort It shuffles a list of numbers until they’re sorted Random sort is very easy to make and also extremely inefficient Let’s make it now!

Create a new public static function Lists and Generics Exercise Create a new public static function It’s supposed to look like this: public static <Type extends Comparable<Type>> List<Type> randomSort(List<Type> list){} The first bit before the return type means that it’s generic and works for anything that implements comparable It also returns and accepts a generic list Declare a boolean variable called sorted and set it to true

Add a while loop that keeps going while sorted is false Lists and Generics Exercise Add a while loop that keeps going while sorted is false Inside, use Collections.shuffle() on the list Then, set sorted to true After that, add a for loop that starts at zero and stops at list.size()-2 (inclusive!) Add an if statement that compares list.get(i) with list.get(i+1) You will need to use compareTo() and check that the result is positive

Set sorted to false inside the if statement Lists and Generics Exercise Set sorted to false inside the if statement Return the list after the while loop Add main() Inside, make a random number generator Then, declare an ArrayList of integers and call it list Add a for loop that adds 10 random numbers to the list

Declare two long variables, startTime and endTime Lists and Generics Exercise Declare two long variables, startTime and endTime Set startTime to System.currentTimeMillis(); Sort list using your randomSort method Set endTime to System.currentTimeMillis(); Print their difference to the console Test the program Don’t worry if it takes several seconds to sort your numbers - as was said before, it’s very inefficient

Let’s make a program for counting letter occurrences Working with files Let’s make a program for counting letter occurrences It will need to read text from a file Save it in a variable And then count frequencies for each letter of the alphabet

In this method, create a new integer variable Working with files Exercise Create a new method Call it countOccurrences Should be static Return an integer And accept two parameters: Sring text and char letter In this method, create a new integer variable Call it count and set to zero

Inside the loop, add an if statement Working with files Exercise Still inside countOccurrences, add a for loop that goes from 0 up to text.length() Inside the loop, add an if statement It should check that text.charAt(i) is equal to letter If it is, add one to your counter Return the counter after the for loop

Inside, add a try-catch block Working with files Exercise Add main() Inside, add a try-catch block It should catch all exceptions and print a stack trace to the console Declare a new BufferedReader inside the try block It’s constructor should use newFileReader(input.txt) as a parameter

Declare two string variables, line and text Working with files Exercise Declare two string variables, line and text Set both of them to an empty string Add a while loop that reads lined until the end of the file It should use “(line = reader.readLine()) != null” as it’s condition Add line.toLowerCase() to text inside the loop Close the reader after the loop

Working with files Exercise Add a for loop This loop will be a bit unusal because it will use char letter for it’s counter It should start with ‘a’ And end when letter is less or equal to ‘z’ You can use letter++ to increment your counter Inside the loop you should print the letter and the result of countOccurences(text,letter) Create a text file in your project directory, fill it with text and test your program!

Debugging is a vital skill for any programmer Fixing errors Debugging is a vital skill for any programmer It’s time for you to get a bit of practise! Click here: … or type this link: goo.gl/EGN4nL In eclipse, click on File -> Import -> General -> Existing Projects into Workspace Then, tick “Select archive file” and click on “Browse…” Download the program

Recursion is a technique where a function calls itself All recursive functions need a terminating condition, otherwise they just won’t stop In most cases, recursion is a bit slower than loops, but sometimes it can do things that loops can’t do easily

Let’s solve a problem using recursion Imagine that you have a 1xN (where N is a positive number) grid with a bunny at the beginning The bunny can jump either one or three cells forward (but not two, it also can’t go back) How many ways are there for the bunny to reach the end?

Let’s look at what happens when we have a 1x4 grid Recursion Let’s look at what happens when we have a 1x4 grid The bunny starts in the first cell Then, it can either move forward once Or hop straight to finish 🐰 🐰 Finish 🐰 Finish

What if we look from the end? Recursion If it moves forward once, then the only path left for it is to move forward twice more For N =4 the answer is two What if we look from the end? We can get to finish from either the 5th cell or the 3rd one If there is one way to get to the 3rd cell and three to the 5th one, then the answer is their sum, 5+3 Sounds familiar? 1 2 3 🐰 Finish

It’s very similar to Fibonacci Numbers Recursion It’s very similar to Fibonacci Numbers With Fibonacci numbers, the formula is F(n) = F(n-1) + F(n-2) Our formula is F(n)= F(n-1)+ F(n-3) Where F(n) is the number of paths to get to the nth cell Let’s write a recursive function to solve this!

Create a new static function and call it bunnyCount Recursion Exercise Create a new static function and call it bunnyCount It should accept one parameter called n– the cell index If this index is less than zero it should return one If it’s 0, 1 or 2 it should return 1 because there is only one way to get there For everything else it should return bunnyCount(n-1) + bunnyCount(n-3) Test your work!

THE END Have a nice holiday!