cc1007ni: further programming week 8-9 dhruba sen module leader (islington college)

30
CC1007NI: Further Programming Week 8-9 Dhruba Sen Module Leader (Islington College)

Upload: jack-webster

Post on 27-Dec-2015

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CC1007NI: Further Programming Week 8-9 Dhruba Sen Module Leader (Islington College)

CC1007NI: Further Programming

Week 8-9

Dhruba SenModule Leader (Islington College)

Page 2: CC1007NI: Further Programming Week 8-9 Dhruba Sen Module Leader (Islington College)

Exceptions

2.0

Page 3: CC1007NI: Further Programming Week 8-9 Dhruba Sen Module Leader (Islington College)

Main concepts to be coveredException categoriesDefining new exceptionsThrowing exceptionsSimple file processing with exception handling

Page 4: CC1007NI: Further Programming Week 8-9 Dhruba Sen Module Leader (Islington College)

ExceptionsCode can be protected with a try block:

try { Protect one or more statements here.}catch(Exception exception) { Report and recover from the exception here.

}

Page 5: CC1007NI: Further Programming Week 8-9 Dhruba Sen Module Leader (Islington College)

Exceptions

try { ... = Integer.parseInt(...); ... ...}catch(NumberFormatException exception) { ... ...}

1. Exception thrown from here

2. Control transfers to here

Page 6: CC1007NI: Further Programming Week 8-9 Dhruba Sen Module Leader (Islington College)

Exception categoriesChecked exceptions

Subclasses of ExceptionUsed for anticipated failures.Where recovery may be possible.

Unchecked exceptionsSubclasses of RuntimeExceptionUsed for unanticipated failures.Where recovery is unlikely.

Page 7: CC1007NI: Further Programming Week 8-9 Dhruba Sen Module Leader (Islington College)

Examples of Checked ExceptionsIOExceptionFileNotFoundExceptionEOFException

Page 8: CC1007NI: Further Programming Week 8-9 Dhruba Sen Module Leader (Islington College)

Examples of Unchecked ExceptionsNumberFormatExceptionNullPointerExceptionArithmeticExceptionIndexOutOfBoundsExceptionArrayIndexOutOfBoundsException

Page 9: CC1007NI: Further Programming Week 8-9 Dhruba Sen Module Leader (Islington College)

The exception class hierarchy

Page 10: CC1007NI: Further Programming Week 8-9 Dhruba Sen Module Leader (Islington College)

throw and throws keywordsThe throw keyword is used to throw a new exception:throw new NumberFormatException(“...”);

The throws keyword is used to indicate that a method throws an exception:

public static int parseInt(String s)throws NumberFormatException

Page 11: CC1007NI: Further Programming Week 8-9 Dhruba Sen Module Leader (Islington College)

Text input-outputInput-output is particularly error-prone.

It involves interaction with the external environment.

The java.io package supports input-output.java.io.IOException is a checked exception.

Page 12: CC1007NI: Further Programming Week 8-9 Dhruba Sen Module Leader (Islington College)

Text outputUse the FileWriter class.

Open a file.Write to the file.Close the file.

Failure at any point results in an IOException.

Page 13: CC1007NI: Further Programming Week 8-9 Dhruba Sen Module Leader (Islington College)

Text outputtry { FileWriter writer = new FileWriter("name of file"); while(there is more text to write) { ... writer.write(next piece of text); ... } writer.close();}catch(IOException e) { something went wrong with accessing the file}

Page 14: CC1007NI: Further Programming Week 8-9 Dhruba Sen Module Leader (Islington College)

Text inputUse the FileReader class.Augment with BufferedReader for line-based

input.Open a file.Read from the file.Close the file.

Failure at any point results in an IOException.

Page 15: CC1007NI: Further Programming Week 8-9 Dhruba Sen Module Leader (Islington College)

Text inputtry { BufferedReader reader = new BufferedReader( new FileReader("name of file ")); String line = reader.readLine(); while(line != null) { do something with line line = reader.readLine(); } reader.close();}catch(FileNotFoundException e) { the specified file could not be found}catch(IOException e) { something went wrong with reading or closing}

Page 16: CC1007NI: Further Programming Week 8-9 Dhruba Sen Module Leader (Islington College)

ActionEvent Methods

2.0

Page 17: CC1007NI: Further Programming Week 8-9 Dhruba Sen Module Leader (Islington College)

Main concepts to be coveredgetActionCommandgetSource

Page 18: CC1007NI: Further Programming Week 8-9 Dhruba Sen Module Leader (Islington College)

CurrencyConverter ButtonsTwo buttons are declared: private JButton euroButton; private JButton dollarButton;

Text is placed on the buttons when they are created: euroButton = new JButton("Euros"); dollarButton = new JButton("Dollars");

Page 19: CC1007NI: Further Programming Week 8-9 Dhruba Sen Module Leader (Islington College)

getActionCommandThe actionPerformed method of CurrencyConverter

uses the getActionCommand method of the ActionEvent object to determine which button triggered the event.

Alternatively, the getSource method of the ActionEvent object can be used.

The getActionCommand returns the text written on the component whereas the getSource method returns the object that triggered the event.

Page 20: CC1007NI: Further Programming Week 8-9 Dhruba Sen Module Leader (Islington College)

actionPerformed using getActionCommandpublic void actionPerformed(ActionEvent event){ String command = event.getActionCommand(); if (command.equals("Euros")) { convertToEuros(); } else if (command.equals("Dollars")) { convertToDollars(); }}

Page 21: CC1007NI: Further Programming Week 8-9 Dhruba Sen Module Leader (Islington College)

actionPerformed using getSourcepublic void actionPerformed(ActionEvent event){ if (event.getSource() == euroButton) { convertToEuros(); } else if (event.getSource() == dollarButton) { convertToDollars(); }}

Page 22: CC1007NI: Further Programming Week 8-9 Dhruba Sen Module Leader (Islington College)

Casting

2.0

Page 23: CC1007NI: Further Programming Week 8-9 Dhruba Sen Module Leader (Islington College)

Main concepts to be coveredCasting numbersCasting objects

Page 24: CC1007NI: Further Programming Week 8-9 Dhruba Sen Module Leader (Islington College)

Casting numbersWe can easily assign an int to a double because

there is no possibility of a loss of precisionIn the following example, the value 3.0 will be

assigned to dint i = 3;double d = i;

Page 25: CC1007NI: Further Programming Week 8-9 Dhruba Sen Module Leader (Islington College)

Casting numbersThere would be a compilation error though if we

tried to write:double d = 3.9;int i = d;

If we want to assign a double to an int (or, for example, to a float) then we need to tell the compiler that we accept the possible loss of precision

Page 26: CC1007NI: Further Programming Week 8-9 Dhruba Sen Module Leader (Islington College)

Casting numbersThis process is known as castingWe need to cast the number to an int by writing (int) in front of it

In the following example, the value 3 will be assigned to i

double d = 3.9;int i = (int) d;

Page 27: CC1007NI: Further Programming Week 8-9 Dhruba Sen Module Leader (Islington College)

Casting objectsConsider the dome-v2 project from Weeks 1 & 2The CD class has the following method to return

the number of tracks

public int getNumberOfTracks() { return numberOfTracks; }

Page 28: CC1007NI: Further Programming Week 8-9 Dhruba Sen Module Leader (Islington College)

Casting objectsSuppose we wanted to write a method in the

Database class to return the number of tracks of a CD that occurred at a particular position in the array list

The method might have the following signature

public int getCDTracks(int index)

Page 29: CC1007NI: Further Programming Week 8-9 Dhruba Sen Module Leader (Islington College)

Casting objectsThe Item object returned from the get

method would need to be cast into a CD object in order to call the getNumberOfTracks method

public int getCDTracks(int index){ CD cd = (CD) items.get(index); return cd.getNumberOfTracks();}

Page 30: CC1007NI: Further Programming Week 8-9 Dhruba Sen Module Leader (Islington College)

THANK YOU.