1 / 22

CS1101 Group1

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;

Download Presentation

CS1101 Group1

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS1101 Group1 Discussion 9 Lek Hsiang Hui lekhsian @ comp.nus.edu.sg http://www.comp.nus.edu.sg/~lekhsian/cs1101

  2. 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);

  3. 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

  4. 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!)

  5. Exceptions • Different kinds of “Exceptions” • Creating your own user-defined Exceptions • throw new Exception • throws Exception • try-catch-finally

  6. Different kinds of “Exceptions” An Exception is really an Object (subclass of Object)

  7. 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.”

  8. Creating your own user-defined Exceptions • Refer to http://www.comp.nus.edu.sg/~lekhsian/cs1101/d9

  9. throw new Exception • Syntax: throw new ExceptionClassName(…); throw a new object

  10. Handling Exceptions • throws Exception • try-catch-finally

  11. 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

  12. 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(“ “);}

  13. 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

  14. 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

  15. 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

  16. 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

  17. Access rights

  18. 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

  19. 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();

  20. 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

  21. MyString • By now you should have implemented the following methods: • Constructors • append • charAt • ensureCapacity • insert • reverse • toString

  22. MyString • Try implementing : • delete • deleteCharAt • Equals • setCharAt

More Related