1. How should catch clauses be ordered? 2. In Java, any method that calls anothe
ID: 3556406 • Letter: 1
Question
1. How should catch clauses be ordered?
2. In Java, any method that calls another method that might possibly throw an exception must either handle the exception or have a throws clause in its method header. Is it possible for Java compilers to enforce this?
3. In designing a bank account class, what exceptions might be appropriate to be defined in the class?
4. Why would you decide to make a class serializable?
5. Applets are restricted from performing certain actions. Why?
6. In order to draw something on a component, you need a reference to that components graphics object. How is that reference typically obtained?
7. Recursive methods have greater overhead than their equivalent iterative methods. Why would a programmer choose to use a recursive method?
8. The quicksort is complicated to implement, so why do we care about it?
Explanation / Answer
1. Think of the inheritance hierarchies of the Exceptions: SQLDataException extends SQLException So if you catch the 'generic' one first (i.e. the topmost base-class of the hierarchy) then every thing 'below' it is of the same type due to polymorphism i.e., SQLDataException isa SQLException
Thus you should capture them in bottom up order w.r.t. the inheritance hierarchy i.e., subclasses first all the way to (generic) base class. This is so because the catch clauses are evaluated in the order you've declared them.
2. If an exception is not caught in your code (which would happen if main was marked as throwing the exception) then the JVM will catch the exception, end that thread of execution, and print a stack trace.
There are cases where the compiler does not enforce these rules. Exceptions that fit this category are called unchecked exceptions.
3. *Transfer money between accounts. Print a message if the origin account
has insufficient funds.
*WITHDRAWING MONEY:
Print a message if insufficient funds in account.
4. *two applications that each have a copy of the same java class to transfer instances of that class; and
* for an application to save an instance to persistent storage, and read it back later.
5. Applets loaded over the network are usually considered to be untrusted code. (The exception, as we'll see in the next section, is when the applet bears the digital signature of an entity that you've specified you trust.) The only way to be sure that an untrusted applet cannot perform any malicious actions (e.g., deleting your files, sending out fake email that looks like it came from you, using your computer as a remote file server) is to run it in a tightly controlled environment. For this reason, Web browsers and applet viewers carefully restrict what an applet is allowed to do.
6. Well there are two issues here 1:
Graphics g1;
a.paint(g1);
And you are getting the error that G1 is not initialized. That's because the variable g1 is never set to anything, and that causes a compile error. To get the code to compile, you would need to, at the very least do this:
Graphics g1 = null;
a.paint(g1);
However, that obviously won't help you out too much. You'll get a NullPointerException when you try to run your code. In order to actually cause your graphics to draw you need to this:
anim1 a=new anim1();
Graphics g1 = anim1.getGraphics();
a.paint(g1);
However, that still won't work because Anim1 will not appear on the screen. To get it to appear on the screen you need something like:
import java.awt.*;
import javax.swing.*;
import java.applet.*;
public class So1 extends Applet{
public void paint (Graphics g)
{
g.drawString("hello",40,30);
}
public static void main(String ad[])
{
JFrame jp1 = new JFrame();
So1 a=new So1 ();
jp1.getContentPane().add(a, BorderLayout.CENTER);
jp1.setSize(new Dimension(500,500));
jp1.setVisible(true);
}
}
Now notice, we don't actually call the paint() function ourselves. That's handled by the awt, which actually picks the graphics context, and calls our paint function for us. If you want, though, you can pass in any graphics object you want and ask it to draw on to that. (so if you want to draw your component onto an image, you can do it)
7.Sometimes recursion helps you to design simpler and more readable code. It is especially relevant for recursive data structures (like trees) or recursive algorithms.
The advantage is that you do not have to preserve state on each iteration. The JVM does it for you in form of call stack.
8. The exact implementation is not very important. But the principle behind quicksort - recursion, partitioning etc, are very basic one and every programmer should understand. These algorithm are actually very simple to describe in words once you understand.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.