Download presentation
Presentation is loading. Please wait.
2
<INSERT_WITTY_QUOTE_HERE>
PC02 Consolidation <INSERT_WITTY_QUOTE_HERE>
3
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
4
Lists and Generics Working with files Fixing errors Recursion Index
Click me Click me Click me Click me
5
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!
6
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
7
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
8
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
9
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
10
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
11
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
12
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
13
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
14
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
15
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!
16
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
17
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
18
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?
19
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
20
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
21
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!
22
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!
23
THE END Have a nice holiday!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.