cs1101 group1

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

Upload: beau-shepherd

Post on 31-Dec-2015

69 views

Category:

Documents


1 download

DESCRIPTION

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; - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: CS1101 Group1

CS1101 Group1

Discussion 9

Lek Hsiang Hui

lekhsian @ comp.nus.edu.sg

http://www.comp.nus.edu.sg/~lekhsian/cs1101

Page 2: CS1101 Group1

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

Page 3: CS1101 Group1

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

Page 4: CS1101 Group1

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

Page 5: CS1101 Group1

Exceptions

• Different kinds of “Exceptions”

• Creating your own user-defined Exceptions

• throw new Exception

• throws Exception

• try-catch-finally

Page 6: CS1101 Group1

Different kinds of “Exceptions”

An Exception is really an Object (subclass of Object)

Page 7: CS1101 Group1

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

Page 8: CS1101 Group1

Creating your own user-defined Exceptions

• Refer to http://www.comp.nus.edu.sg/~lekhsian/cs1101/d9

Page 9: CS1101 Group1

throw new Exception

• Syntax:throw new ExceptionClassName(…);

throw a new object

Page 10: CS1101 Group1

Handling Exceptions

• throws Exception

• try-catch-finally

Page 11: CS1101 Group1

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

Page 12: CS1101 Group1

Exceptions

• Throwing an exceptionthrow 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(“ “);}

Page 13: CS1101 Group1

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 SpecializedMore Specialized

More genera

l

More genera

l

Page 14: CS1101 Group1

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{ …}

More SpecializedMore Specialized

•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?

Page 15: CS1101 Group1

Exceptionspublic 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

Page 16: CS1101 Group1

Exceptionspublic 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

Page 17: CS1101 Group1

Access rights

Member Restriction

this Subclass Package General

public

protected

default

private

Page 18: CS1101 Group1

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

Page 19: CS1101 Group1

Polymorphism

• abstract class• A class with at 1 abstract methodOr• A class which you don’t want anyone to

instantiatei.e.class abstract Animal{}Not allow to do:Animal a = new Animal();

Page 20: CS1101 Group1

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

Page 21: CS1101 Group1

MyString

• By now you should have implemented the following methods:

– Constructors– append– charAt– ensureCapacity– insert– reverse– toString

Page 22: CS1101 Group1

MyString

• Try implementing :– delete– deleteCharAt– Equals– setCharAt