What is an exception Explain how to use trycatch blocks to p

What is an exception? Explain how to use try/catch blocks to prevent a program from crashing.

What is refactoring? What are some of the ways that Eclipse can help you with refactoring?

What is the output? Explain how you obtain this answer by hand, not using a computer.

What is the output? Explain how you obtain this answer by hand, not using a computer.

A Java Swing application contains this paintComponent method for a panel. Sketch what is drawn by this method.

Solution

What is an exception? Explain how to use try/catch blocks to prevent a program from crashing.

Exception means abnormal conditions that arsies while a program gets executed.

Exception Handling is a way through which these exceptions are handled.
If these exceptions are not handled then, program crashes and abruptly ends the program in between.

To handle Exception, try/catch blocks are written in code of java.

These are nothing but statements. \'try\' enclose that part of code which throws the exception. A try block must be followed by catch/finally block.
\'catch\' block is the block which actually handles the exception produced.

Syntax of java try-catch

try{
//code that may throw exception
}catch(Exception_class_Name ref){}
If in our program, the try-catch block is written properly, then it prevents system from craching abruptly.

What is refactoring? What are some of the ways that Eclipse can help you with refactoring?
The goal of Java program refactoring is to make system-wide code changes without affecting the behavior of the program. The Java tools provide assistance in easily refactoring code.
Refactoring commands are available from the context menus of several Java views (e.g. Package Explorer, Outline) and editors. Many \"apparently simple\" commands, such as Move and Rename,

Foilowing are the some ways below as captured from eclipse:

Rename: Renames the selected element and (if enabled) corrects all references to the elements (also in other files).
Shortcut: Alt + Shift + R
Options:
1.Renaming a type does allow to rename similarly named variables and methods. Enable \'Update similarly named variables and methods\' in the Rename Type dialog. Select \'Configure...\' to

configure the strategy for matching type names.
2. Renaming a package does allow to rename its subpackages. Enable \'Rename subpackages\' in the Rename Package dialog.

3. Enable \'Keep original method as delegate to changed method\' to keep the original method. Optionally you can deprecate the old method.

Move: Moves the selected elements and (if enabled) corrects all references to the elements (also in other files).

Shortcut: Alt + Shift + V
Options: You can use Drag & Drop in the Package Explorer to start this refactoring.

What is the output? Explain how you obtain this answer by hand, not using a computer.

String alphabet = \"ABCDEFGHIJKLMNOPQRSTUVWXYZ\";
for(int i = 1; i <= 26; i *= 2) {
System.out.print(alphabet.charAt(i - 1));
}

The output will be:
ABDHP
Explaination: First of all the value of i will be 1, so, from alphabet string, character \'A\' will be picked.
Second time: i=2, \'B\' will be picked.
Third time: i=4, \'D\' will be picked.
Fourth time:i=8, \'H\' will be picked.
Fivth time:i=16, \'P\' will be picked.
Sixth time: i=32, Program will skip.


What is the output? Explain how you obtain this answer by hand, not using a computer.

int n = 500;
int count = 0;
while (n > 1) {
if (n % 2 == 0) {
n /= 3;
}
else {
n /= 2;
}
count++;
}
System.out.println(count);
Output:7
Explaination:
First time: n=500
count=0
n/3=166
count=1
Second time: n=166
count=1
n/3=55
count=2
Third time= n=55
count=2
n/3=27
count=3
fourth time= n=27
count=3
n/2=13
n/3=27
count=4
Fivh time= n=13
count=4
n/2=6
count=5
Sixth time= n=6
count=5
n/3=2
count=6
Seventh time= n=2
count=6
n/3=0
count=7

As n>0 so it exits and count is 7.

What is an exception? Explain how to use try/catch blocks to prevent a program from crashing. What is refactoring? What are some of the ways that Eclipse can he
What is an exception? Explain how to use try/catch blocks to prevent a program from crashing. What is refactoring? What are some of the ways that Eclipse can he

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site