Casual Discussion, Warm-up Questions, and Lecture Demo Exercises findAccount()

advertisement
Lecture Exercise 09
CS2312 Problem Solving and Programming | www.cs.cityu.edu.hk/~helena
Casual Discussion, Warm-up Questions, and Lecture Demo Exercises
Q1. [Lab07-Q7 Step 5] How to solve?
findAccount()
main()
Last modified: 18‐Mar‐2016 Lecture Exercise 09
CS2312 Problem Solving and Programming | www.cs.cityu.edu.hk/~helena
Q2 [Lab08-Q2] How to write the Fire command class
public class Fire extends RecordedCommand { @Override public void execute(String[] cmdParts) { addUndoCommand(this); clearRedoList(); System.out.println("Done."); } @Override public void undoMe() { addRedoCommand(this); } @Override public void redoMe() { addUndoCommand(this); } } Given: public class Company { .. // getInstance, // addEmployee, findEmployee, etc.. public void removeEmployee(Employee e) { allEmployees.remove(e); } } main():
if (cmdParts[0].equals("fire"))
(new fire()).execute(cmdParts);
Q3. More on for-each loop:
When a for-each loop iterates through a collection, we cannot add/delete elements.
Question: How to implement the following method in the Company class?
public void remove(Employee e) //Remove e from allEmployees { } Last modified: 18‐Mar‐2016 Run-time error!!

Lecture Exercise 09
CS2312 Problem Solving and Programming | www.cs.cityu.edu.hk/~helena
Q4. Suppose we have the classes: Book, Member, Library; where the Library instance, lib, has an array of
books and and array members, we need to work out the borrow book operation.
Compare the following approaches:
Approach 1:
Book b = lib.getBook(bookID);
Member m = lib.getMember(memberID); lib.addBorrowRecord(b,m);
Check for problems like b/m are null, book has been borrowed, member quota exceeded etc.. If okay, change the book's status as borrowed, and store the loan details. Approach 2:
Member m = lib.getMember(memberID);
if (m is not null) //or use try‐catch m.borrowBook(bookID);
Approach 2':
Book b = lib.getBook(bookID);
Member m = lib.getMember(memberID); if (m is not null) //or use try‐catch m.borrowBook(b);
m.borrowBook(bookID) checks the member itself's quota. If OK, lookup for the book b and call b.borrowBy(m). In b.borrowBy(m), check whether the book is available. If OK, change its status as borrowed and store the loan details. Exception handling may be used quite a lot. Your task: choose the correct answers below:
Reference:

Approach 1 / Approach 2 is a top-down approach

Approach 1 is poor/good OO design

Approach 2 is poor/good OO design
[Notes writen by Dr. W.K. Chan / CS2312 [2013‐2014 Sem A]  Executing the typical sequence of statements within a method M of an object may not always be compatible to the current attribute values of the same object at any time. In poor OO programming, an object O1 checks the attribute values of another object O2 before O1 invokes the method M of O2. (or else, whenever O2 change its internal representation, we need to change the coding in O1; otherwise, we may inject bugs into the program!) In good OO programming, an object O1 simply invokes the method M of O2, and let M both determine how to check the attribute values of O2 and determine whether M should affect the current attribute values of O2 (or invoke other methods of this object or some other object) Last modified: 18‐Mar‐2016 Lecture Exercise 09
CS2312 Problem Solving and Programming | www.cs.cityu.edu.hk/~helena
Q5. Review the following slide [Topic 06, slide#25]
Then, complete the following summary about Exception Handling
[ Contents extracted from http://docs.oracle.com/javase/tutorial/essential/exceptions/summary.html]
A program can use exceptions to indicate that an error occurred. To
__________________ an __________________, use the throw statement
and provide it with a error message / an exception object. An exception
object is a descendant of __________________ which aims at
__________________ about the specific error that occurred.
[Given words for blanks] throw
exception
Throwable
providing information
catch the exception
a throws clause
the try, catch, and finally blocks
exception handler Checked exceptions are exceptions which the compiler will reinforce proper handling in your code. A
method that throws a checked / unchecked exception must either __________________ or include
__________________ in its declaration.
A program can catch exceptions by using a combination of __________________________________.
• The try block identifies a block of code in which an exception can occur / be thrown.
• The catch block identifies a block of code, known as an________________, that can handle a particular
type of exception.
• The finally / try block identifies a block of code that is guaranteed to execute, and is the right place to
close files, recover resources, and otherwise clean up after the try-catch / finally block.
The class / instance of the exception object indicates the type of exception thrown. By convention, the
exception object contains at least an error message.
Last modified: 18‐Mar‐2016 Lecture Exercise 09
CS2312 Problem Solving and Programming | www.cs.cityu.edu.hk/~helena
Q6. Advantages of Using Exceptions [ Contents extracted from http://docs.oracle.com/javase/tutorial/essential/exceptions/advantages.html]
Your task: Choose the correct heading for each Advantage below.
Grouping and Differentiating Errors Propagating Errors Up Call Stack Separating Error-Handling from "Regular" Code
Advantage 1: _________________________________________________
Suppose we are to read a file into memory,
the code
Traditional solution:
has potential
• What happens if the file can't be opened?
• What happens if the length of the file can't be determined?
errors: • What happens if enough memory can't be allocated?
• What happens if the read fails?
• What happens if the file can't be closed?
Using Exception Handling:
Advantage 2: _________________________________________________
Using exception handling, it gives us a natural way group or classify exceptions. E.g. Java's IOException
(most general for IO errors) and its subclasses (more specific).
Last modified: 18‐Mar‐2016 Lecture Exercise 09
CS2312 Problem Solving and Programming | www.cs.cityu.edu.hk/~helena
Advantage 3: _________________________________________________
Suppose that the readFile method is the fourth method in a
series of nested method calls made by the main program:
method1 calls method2, which calls method3, which finally
calls readFile.,
Traditional code:
Last modified: 18‐Mar‐2016 method1 {
call method2;
}
method2 {
call method3;
}
method3 {
call readFile;
}
Using Exception Handling:
Download