cc1007ni: further programming week 8-9 dhruba sen module leader (islington college)
TRANSCRIPT
![Page 1: CC1007NI: Further Programming Week 8-9 Dhruba Sen Module Leader (Islington College)](https://reader038.vdocuments.net/reader038/viewer/2022110402/56649e315503460f94b21c29/html5/thumbnails/1.jpg)
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)](https://reader038.vdocuments.net/reader038/viewer/2022110402/56649e315503460f94b21c29/html5/thumbnails/2.jpg)
Exceptions
2.0
![Page 3: CC1007NI: Further Programming Week 8-9 Dhruba Sen Module Leader (Islington College)](https://reader038.vdocuments.net/reader038/viewer/2022110402/56649e315503460f94b21c29/html5/thumbnails/3.jpg)
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)](https://reader038.vdocuments.net/reader038/viewer/2022110402/56649e315503460f94b21c29/html5/thumbnails/4.jpg)
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)](https://reader038.vdocuments.net/reader038/viewer/2022110402/56649e315503460f94b21c29/html5/thumbnails/5.jpg)
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)](https://reader038.vdocuments.net/reader038/viewer/2022110402/56649e315503460f94b21c29/html5/thumbnails/6.jpg)
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)](https://reader038.vdocuments.net/reader038/viewer/2022110402/56649e315503460f94b21c29/html5/thumbnails/7.jpg)
Examples of Checked ExceptionsIOExceptionFileNotFoundExceptionEOFException
![Page 8: CC1007NI: Further Programming Week 8-9 Dhruba Sen Module Leader (Islington College)](https://reader038.vdocuments.net/reader038/viewer/2022110402/56649e315503460f94b21c29/html5/thumbnails/8.jpg)
Examples of Unchecked ExceptionsNumberFormatExceptionNullPointerExceptionArithmeticExceptionIndexOutOfBoundsExceptionArrayIndexOutOfBoundsException
![Page 9: CC1007NI: Further Programming Week 8-9 Dhruba Sen Module Leader (Islington College)](https://reader038.vdocuments.net/reader038/viewer/2022110402/56649e315503460f94b21c29/html5/thumbnails/9.jpg)
The exception class hierarchy
![Page 10: CC1007NI: Further Programming Week 8-9 Dhruba Sen Module Leader (Islington College)](https://reader038.vdocuments.net/reader038/viewer/2022110402/56649e315503460f94b21c29/html5/thumbnails/10.jpg)
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)](https://reader038.vdocuments.net/reader038/viewer/2022110402/56649e315503460f94b21c29/html5/thumbnails/11.jpg)
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)](https://reader038.vdocuments.net/reader038/viewer/2022110402/56649e315503460f94b21c29/html5/thumbnails/12.jpg)
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)](https://reader038.vdocuments.net/reader038/viewer/2022110402/56649e315503460f94b21c29/html5/thumbnails/13.jpg)
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)](https://reader038.vdocuments.net/reader038/viewer/2022110402/56649e315503460f94b21c29/html5/thumbnails/14.jpg)
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)](https://reader038.vdocuments.net/reader038/viewer/2022110402/56649e315503460f94b21c29/html5/thumbnails/15.jpg)
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)](https://reader038.vdocuments.net/reader038/viewer/2022110402/56649e315503460f94b21c29/html5/thumbnails/16.jpg)
ActionEvent Methods
2.0
![Page 17: CC1007NI: Further Programming Week 8-9 Dhruba Sen Module Leader (Islington College)](https://reader038.vdocuments.net/reader038/viewer/2022110402/56649e315503460f94b21c29/html5/thumbnails/17.jpg)
Main concepts to be coveredgetActionCommandgetSource
![Page 18: CC1007NI: Further Programming Week 8-9 Dhruba Sen Module Leader (Islington College)](https://reader038.vdocuments.net/reader038/viewer/2022110402/56649e315503460f94b21c29/html5/thumbnails/18.jpg)
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)](https://reader038.vdocuments.net/reader038/viewer/2022110402/56649e315503460f94b21c29/html5/thumbnails/19.jpg)
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)](https://reader038.vdocuments.net/reader038/viewer/2022110402/56649e315503460f94b21c29/html5/thumbnails/20.jpg)
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)](https://reader038.vdocuments.net/reader038/viewer/2022110402/56649e315503460f94b21c29/html5/thumbnails/21.jpg)
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)](https://reader038.vdocuments.net/reader038/viewer/2022110402/56649e315503460f94b21c29/html5/thumbnails/22.jpg)
Casting
2.0
![Page 23: CC1007NI: Further Programming Week 8-9 Dhruba Sen Module Leader (Islington College)](https://reader038.vdocuments.net/reader038/viewer/2022110402/56649e315503460f94b21c29/html5/thumbnails/23.jpg)
Main concepts to be coveredCasting numbersCasting objects
![Page 24: CC1007NI: Further Programming Week 8-9 Dhruba Sen Module Leader (Islington College)](https://reader038.vdocuments.net/reader038/viewer/2022110402/56649e315503460f94b21c29/html5/thumbnails/24.jpg)
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)](https://reader038.vdocuments.net/reader038/viewer/2022110402/56649e315503460f94b21c29/html5/thumbnails/25.jpg)
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)](https://reader038.vdocuments.net/reader038/viewer/2022110402/56649e315503460f94b21c29/html5/thumbnails/26.jpg)
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)](https://reader038.vdocuments.net/reader038/viewer/2022110402/56649e315503460f94b21c29/html5/thumbnails/27.jpg)
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)](https://reader038.vdocuments.net/reader038/viewer/2022110402/56649e315503460f94b21c29/html5/thumbnails/28.jpg)
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)](https://reader038.vdocuments.net/reader038/viewer/2022110402/56649e315503460f94b21c29/html5/thumbnails/29.jpg)
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)](https://reader038.vdocuments.net/reader038/viewer/2022110402/56649e315503460f94b21c29/html5/thumbnails/30.jpg)
THANK YOU.