Instructions:Omitted
Getting Ready:Omitted
Example1.java
in the editor (e.g. jGrasp).
Example1
.
The answer is: 2 Done.
denominator
to 0.
Example1
.
Exception in thread "main" java.lang.ArithmeticException: / by zero at Example1.main(Example1.java:11)
try-catch
statement. Specifically,
put only the statement that generated the exception inside of the
try
block and put no statments in the
catch
block. (Hint: You should be able to determine
what exception to catch and what line generated the exception
from the error message that you received during
the previous step.)
Example1
.
Example.java:17: variable ratio might not have been initialized System.out.println("The answer is: "+ratio); ^
This error is generated because ratio
is initialized in
the try
block and this assignment will not take place if
an exception is thrown before the assignment can be completed.
try
block (as well).
System.out.println("Divide by 0.");
to the catch
block.
Example1
.
Divide by 0. Done.
printStackTrace()
method of the
ArithmeticException
to the end of the
catch
block.
Example1
.
Divide by 0. java.lang.ArithmeticException: / by zero at Example.main(Example.java:13) Done.
ArithmeticException
was thrown when the
division was attempted but it was caught. After it was caught some
messages were printed and then the application terminated.
Example2.java
in the editor (e.g. jGrasp).
(Note: Different editors handle tabs/spaces differently. As a
result, you may need to fix the indentation of files created by
other people. In jGrasp you can do this by first generating a
Control Structure Diagram (F2) and then removing the CSD
(Shift-F2).)
Example2
.
Example.java:20: variable i might not have been initialized numbers[i]+"/"+numbers[i+1]); ^
i
to 0 inside of the try
block
(but before the for
loop).
Example2
.
Example.java:21: variable i might not have been initialized numbers[i]+"/"+numbers[i+1]); ^
i
to be used before it is
initialized. Why is this error generated anyway? (Hint: Think about
block statements.)
try
block is treated as a single (block) statement.
This block statement may throw an exception causing the catch
block to be entered before the assignment is completed.
i
before the
try
block.
Example2
.
100/10=10 Couldn't calculate 10/0
try
block and enters the catch
block. Control then leaves the catch
block and then leaves
main
which causes the application to terminate.
Example2
so that it executes properly.
(Hint: Move the try-catch
block inside of the
for
block.) What did you change?
public class Example2 { public static void main(String[] args) { int i, ratio; int[] numbers = {100,10,0,5,2,8,0,30}; for (i=0; i < numbers.length-1; i++) { try { ratio = numbers[i] / numbers[i+1]; System.out.println(numbers[i]+"/"+numbers[i+1]+"="+ratio); } catch (ArithmeticException ae) } System.out.println("Couldn't calculate "+ numbers[i]+"/"+numbers[i+1]); } } } }
Example3
and verify that
it outputs all of the values followed by the word "Done".
Example3
so that it loops "properly" and
does not need to use a try-catch
statement. (Note: The output
should not change.) What did you change?
public class Example3 { public static void main(String[] args) { int i; int[] data = {50, 320, 97, 12, 2000}; for (i=0; i < data.length; i++) { System.out.println(data[i]); } } }
StringTokenizer
object provide?
String
into component parts,
called tokens.
StringTokenizer
class?
String
that is going to be tokenized, the delimiters
to use while tokenizing, and whether or not the delimiters should be
returned as tokens.
Example4.java
.
Example4
as follows: java Example4 5.3+9.2
.
Example4
as follows: java Example4 5.3+
.
NoSuchElementException
is thrown by the statement
rightString = tokenizer.nextToken();
Example4
as follows: java Example4 5.3+a
.
NumberFormatException
is thrown by the statement
rightOperand = Double.parseDouble(rightString);
Example4.java
so that it supports addition (+),
subtraction (-), multiplication (*), and division (/).
Example4.java
so that it processes all of the
command line arguments rather than just args[0]
.
(Your program should treat each command line argument as a
different expression to evaluate. So, for example, it should
be able to be executed as follows:
java Example4 5.0+4.1 3.2*9.1
.
Example4.java
so that it tells you which
operand is not a number. (Hint: You may need to use nested
try-catch
blocks.)
when
Copyright 2011