Comp1202: Conclusions Revision Session
Coming up Key Concepts - The Pillars HashMaps Exceptions The Exam Some Last Words
The Three Pillars of OOP EncapsulationInheritancePolymorphism
HashMaps
Maps Maps are a collection type that map a key to a value put(“Alfie”,“407351”); and so on Memory Map: Name -> Phone number Alfie Jenny
Lookup Lookup is the act of supplying the key and having the value returned String num = myHashMap.get(“Alfie”); Memory myHashMap Alfie
Like a more generic array In an array you map an integer to an object –myarray[0] = “Plumber”; –System.out.println(“Job type “ + myarray[0]);
Like a more generic array In an array you map an integer to an object –myarray[0] = “Plumber”; –System.out.println(“Job type “ + myarray[0]); In a hashmap you map an object to an object E.g. String to String –myhashmap.put(“Andy”, “Plumber”); –System.out.println(“Job type “ + myhashmap.get(“Andy”));
Like a more generic array In an array you map an integer to an object –myarray[0] = “Plumber”; –System.out.println(“Job type “ + myarray[0]); In a hashmap you map an object to an object E.g. String to String –myhashmap.put(“Andy”, “Plumber”); –System.out.println(“Job type “ + myhashmap.get(“Andy”)); E.g. Names to Jobs –Name n = new Name(“Andy”, “English”); –Job j = new Job(“Plumber”, 25000); –myhashmap.put(n, j); –System.out.println(“Salary “ + myhashmap.get(n).getSalary());
Bringing it together… import java.util.HashMap; //code omitted HashMap marks; marks = new HashMap (); marks.put("Alice", 75); marks.put("Bob", 62); marks.put("Colin", 68); System.out.println("Bob got " + marks.get("Bob"));
Bringing it together… import java.util.HashMap; //code omitted HashMap marks; marks = new HashMap (); marks.put("Alice", 75); marks.put("Bob", 62); marks.put("Colin", 68); System.out.println("Bob got " + marks.get("Bob")); What is this?
Bringing it together… import java.util.HashMap; //code omitted HashMap marks; marks = new HashMap (); marks.put("Alice", 75); marks.put("Bob", 62); marks.put("Colin", 68); System.out.println("Bob got " + marks.get("Bob")); What is this? HashMap is a generic class, this means we should tell it what two types it maps together
Bringing it together… import java.util.HashMap; //code omitted HashMap marks; marks = new HashMap (); marks.put("Alice", 75); marks.put("Bob", 62); marks.put("Colin", 68); System.out.println("Bob got " + marks.get("Bob")); What is this? HashMap is a generic class, this means we should tell it what two types it maps together What is happening here?
Bringing it together… import java.util.HashMap; //code omitted HashMap marks; marks = new HashMap (); marks.put("Alice", 75); marks.put("Bob", 62); marks.put("Colin", 68); System.out.println("Bob got " + marks.get("Bob")); What is this? HashMap is a generic class, this means we should tell it what two types it maps together What is happening here? This is autoboxing – the ints are automatically turned into Integers for us
Bringing it together… import java.util.HashMap; //code omitted HashMap marks; marks = new HashMap (); marks.put("Alice", 75); marks.put("Bob", 62); marks.put("Colin", 68); System.out.println("Bob got " + marks.get("Bob")); What is this? HashMap is a generic class, this means we should tell it what two types it maps together What is happening here? This is autoboxing – the ints are automatically turned into Integers for us Which type of String comparison is being used?
Bringing it together… import java.util.HashMap; //code omitted HashMap marks; marks = new HashMap (); marks.put("Alice", 75); marks.put("Bob", 62); marks.put("Colin", 68); System.out.println("Bob got " + marks.get("Bob")); What is this? HashMap is a generic class, this means we should tell it what two types it maps together What is happening here? This is autoboxing – the ints are automatically turned into Integers for us Which type of String comparison is being used? Equality (not Identity). It is using the.equals method
Exceptions
Code that takes risks can throw Exceptions to show that something has gone wrong We can then catch that Exception and tidy up the mess try { //some code that might throw Exception throw new Exception (“Oops – cannot open file”); } catch (Exception e) { //code to run in case an Exception is thrown }
Exceptions Rather than catching an Exception you can pass it on down the call stack This allows the Exception to be handled at the best possible place But remember it must be caught somewhere!
public void processData(int data[]) { for(int i = 0; i < data.length; i++) { data[i] = processItem(data[i]); } public void processAllData() { processData(getPrimaryDataSet()); processData(getSecondaryDataSet()); } public static void main(String [] args) { processAllData(); } Exceptions Exceptions are objects that contain information about an error They are thrown by one bit of code And caught and handled by another
Exceptions Exceptions are objects that contain information about an error They are thrown by one bit of code And caught and handled by another public void processData(int data[]) { try { if(data == null) throw new Exception(“No Data to Process”); for(int i = 0; i < data.length; i++) { data[i] = processItem(data[i]); } catch(Exception e) { System.out.println(e); } public void processAllData() { processData(getPrimaryDataSet()); processData(getSecondaryDataSet()); } public static void main(String [] args) { processAllData(); }
Exceptions Exceptions are objects that contain information about an error They are thrown by one bit of code And caught and handled by another public void processData(int data[]) throws Exception { if(data == null) throw new Exception(“No Data to Process”); for(int i = 0; i < data.length; i++) { data[i] = processItem(data[i]); } public void processAllData() { try { processData(getPrimaryDataSet()); processData(getSecondaryDataSet()); } catch(Exception e) { System.out.println(e); } public static void main(String [] args) { processAllData(); }
Exceptions Exceptions are objects that contain information about an error They are thrown by one bit of code And caught and handled by another public void processData(int data[]) throws Exception { if(data == null) throw new Exception(“No Data to Process”); for(int i = 0; i < data.length; i++) { data[i] = processItem(data[i]); } public void processAllData () throws Exception { processData(getPrimaryDataSet()); processData(getSecondaryDataSet()); } public static void main(String [] args) { try { processAllData(); } catch(Exception e) { System.out.println(e); }
Exceptions Exceptions are objects that contain information about an error They are thrown by one bit of code And caught and handled by another public void processData(int data[]) throws Exception { if(data == null) throw new Exception(“No Data to Process”); for(int i = 0; i < data.length; i++) { data[i] = processItem(data[i]); } public void processAllData () throws Exception { try { processData(getPrimaryDataSet()); processData(getSecondaryDataSet()); } catch(Exception e) { resetDataSets(); throw e; } public static void main(String [] args) { try { processAllData(); } catch(Exception e) { System.out.println(e); }
Exceptions The Java API throws many different types of Exceptions You can catch them when something goes wrong (for example ArrayIndexOutOfBoun dsException) catch(Exception e) will catch all of them Next Semester you will see how to handle Exceptions in a more sophisticated way And to define your own! public void processData(int data[]) throws Exception { if(data == null) throw new Exception(“No Data to Process”); for(int i = 0; i < data.length; i++) { data[i] = processItem(data[i]); } public void processAllData () throws Exception { try { processData(getPrimaryDataSet()); processData(getSecondaryDataSet()); } catch(Exception e) { resetDataSets(); throw e; } public static void main(String [] args) { try { processAllData(); } catch(Exception e) { System.out.println(e); }
Exam
The Good News Three hour exam – so lots of time – held in one of the University lab rooms – done on a PC using the tools you are used to Open Book – you can take in textbooks and notes – you can access the web (e.g. look at help pages and/or the Java API)
The Bad News But it is still an Exam – No communication allowed (e.g. IM, Facebook, file sharing) – No pre-prepared solutions allowed – No de-compilers allowed Invigilators will be present to enforce the rules and make sure the machines and software are working properly – They cannot answer questions about your code!
The Questions There are three questions, you must try and answer all of them Question 1 (20%) – Is the easiest question – Explores your ability to read code and understand programming terms Question 2 (50%) – Is the main question – Explores your ability to write code and solve programming problems Question 3 (30%) – Is the advanced question – Explores your ability to understand a program design, write more advanced code and think creatively
Some advice Most people will not finish the whole exam – Do not worry – Stay calm – Start at the beginning – Complete as much as you can Be defensive – Read each question thoroughly before you start it – Save often – Submit your solutions as you go (via handin machine) Check out the past exam papers (under comp1004): –
Feedback
How was it for you? An online questionnaire will allow you to give anonymous feedback – We read and take notice of what you say! But are their any comments you want to make on: – lectures – courseworks – labs – space cadets or ground controllers – dojo What the best and worst things about the course?
Some Last Words
A Dirty Secret No matter how we teach you will mainly learn through practice! Programming is the single most important skill for a computer scientist or software engineer – Systematic thinking and problem solving – Abstraction and data modeling Did we mention that you need to practice?
PRACTICE! “I've often thought that sucking less every year is how humble programmers improve. You should be unhappy with code you wrote a year ago.” - Jeff Atwood, “I have no talent. What I do have is a lot of practice. And I am not talking about occasionally dabbling in Ruby on the weekends. I am talking about the kind of practice where I beat code that isn’t working into submission (though often times the code wins).” - John Nunemaker,
Best of Luck and Have Fun!