220 likes | 355 Views
CS1101 Group1. Discussion 9. Lek Hsiang Hui lekhsian @ comp.nus.edu.sg http://www.comp.nus.edu.sg/~lekhsian/cs1101. Lab9 Taxi. When implementing Type c, take note that a “greedy” algorithm will not work i.e. Typical WRONG solution: counterA = passengers/4; passengerA = counterA * 4;
E N D
CS1101 Group1 Discussion 9 Lek Hsiang Hui lekhsian @ comp.nus.edu.sg http://www.comp.nus.edu.sg/~lekhsian/cs1101
Lab9 Taxi • When implementing Type c, take note that a “greedy” algorithm will not work i.e. Typical WRONG solution: counterA = passengers/4; passengerA = counterA * 4; passengerB = passengers – passengerA; lowest = checkPriceA( distance, passengerA); lowest = checkPriceB( distance, passengerB);
Lab9 Taxi • Approach to solving type C: • Try all combinations • e.g. 12 people, try 0 standard, 2 Cab++1 standard, 2 Cab++2 standard, 1 Cab++3 standard, 0 Cab++etc
Exceptions • What are exceptions? • Something used for signaling erroneous situation • E.g. your method should return a valuebut the parameters are wrong, you can throw an exception. (i.e. no need to return anything!)
Exceptions • Different kinds of “Exceptions” • Creating your own user-defined Exceptions • throw new Exception • throws Exception • try-catch-finally
Different kinds of “Exceptions” An Exception is really an Object (subclass of Object)
RuntimeException • What are these? • http://java.sun.com/javase/6/docs/api/java/lang/RuntimeException.html • “A method is not required to declare in its throws clause any subclasses of RuntimeException that might be thrown during the execution of the method but not caught.”
Creating your own user-defined Exceptions • Refer to http://www.comp.nus.edu.sg/~lekhsian/cs1101/d9
throw new Exception • Syntax: throw new ExceptionClassName(…); throw a new object
Handling Exceptions • throws Exception • try-catch-finally
Handling Exceptions : throws • throws Exception • If you do not want to explicitly handle the exception in the method, then put throws ExceptionName at the method
Exceptions • Throwing an exception throw new Exception(“some_exception_message”); • Note that codes you will still have to handle codes in the catch block if there’s any code that throw an exceptioni.etry{ throw new Exception(“”);}catch(Exception e){ throw new Exception(“ “);}
Exceptions : try-catch-finally • Understand try – catch – finally try { age = Integer.parseInt(inputStr); if (age < 0) { throw new Exception ("Negative age is invalid"); } return age; } catch (NumberFormatException e) { … } catch (Exception e) { … } finally{ … } More Specialized More general
Exceptions • Understand try – catch – finally try { age = Integer.parseInt(inputStr); if (age < 0) { throw new Exception ("Negative age is invalid"); } return age; } catch (NumberFormatException e) { JOptionPane.showMessageDialog (null, "‘" + inputStr + "’ is invalid\nPlease enter digits only"); } catch (Exception e) { JOptionPane.showMessageDialog (null, "Error: " + e.getMessage()); } finally{ … } • finally: • Statements that would be executed no matter whether there’s an exception • So is there any difference between placing “ending codes” in the finallyblock and placing it after finally? More Specialized
Exceptions public static void main(String args[]){ try{ return; } catch(Exception e){ System.out.println("in exception"); } finally{ System.out.println("in finally"); } System.out.println("outside"); } • in finally is still being printed even though there’s a return statement in the try block
Exceptions public static void main(String args[]){ try{ System.exit(1); return; } catch(Exception e){ System.out.println("in exception"); } finally{ System.out.println("in finally"); } System.out.println("outside"); } • However it is not always the case that for *all cases*, finally will be executed
Polymorphism • What is polymorphism? • Ability of a type/variable/object to take on different forms, so when you call a method, it will call the method specific to the real type • Example: http://www.comp.nus.edu.sg/~lekhsian/cs1101/d9
Polymorphism • abstract class • A class with at 1 abstract method Or • A class which you don’t want anyone to instantiate i.e. class abstract Animal{} Not allow to do: Animal a = new Animal();
Polymorphism • abstract method • A method whose method body is not implemented • Why is this useful? E.g. abstract class Shape{ public abstract int getArea(); } //we don’t know what this shape will be //so can’t implement getArea
MyString • By now you should have implemented the following methods: • Constructors • append • charAt • ensureCapacity • insert • reverse • toString
MyString • Try implementing : • delete • deleteCharAt • Equals • setCharAt