cc1007ni: further programming week 8-9 dhruba sen module leader (islington college)
TRANSCRIPT
CC1007NI: Further Programming
Week 8-9
Dhruba SenModule Leader (Islington College)
Exceptions
2.0
Main concepts to be coveredException categoriesDefining new exceptionsThrowing exceptionsSimple file processing with exception handling
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.
}
Exceptions
try { ... = Integer.parseInt(...); ... ...}catch(NumberFormatException exception) { ... ...}
1. Exception thrown from here
2. Control transfers to here
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.
Examples of Checked ExceptionsIOExceptionFileNotFoundExceptionEOFException
Examples of Unchecked ExceptionsNumberFormatExceptionNullPointerExceptionArithmeticExceptionIndexOutOfBoundsExceptionArrayIndexOutOfBoundsException
The exception class hierarchy
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
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.
Text outputUse the FileWriter class.
Open a file.Write to the file.Close the file.
Failure at any point results in an IOException.
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}
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.
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}
ActionEvent Methods
2.0
Main concepts to be coveredgetActionCommandgetSource
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");
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.
actionPerformed using getActionCommandpublic void actionPerformed(ActionEvent event){ String command = event.getActionCommand(); if (command.equals("Euros")) { convertToEuros(); } else if (command.equals("Dollars")) { convertToDollars(); }}
actionPerformed using getSourcepublic void actionPerformed(ActionEvent event){ if (event.getSource() == euroButton) { convertToEuros(); } else if (event.getSource() == dollarButton) { convertToDollars(); }}
Casting
2.0
Main concepts to be coveredCasting numbersCasting objects
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;
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
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;
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; }
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)
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();}
THANK YOU.