




























































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Material Type: Exam; Class: Data Structures and Algorithms; Subject: Computer Science; University: Marshall ; Term: Unknown 1989;
Typology: Exams
1 / 129
This page cannot be seen from the preview
Don't miss anything!
Material for Midterm Exam 1
Marshall University Department of Engineering and
Computer Science
The terms
defect
error
and
The
terms defect, error and
bug are used synonymously
They all mean there is
y
something wrong that causesan undesired result
Some people in the field
Some people in the fieldrefuse to use the word “bug”since it implies some“
sinister force
” is at work.
They insist that any error isThey insist that any error isreally the fault of thedeveloper and not someoutside entity.
Marshall University Department of Engineering and
Computer Science
There are 3 major categories or types of errorsThere
are 3 major categories or types of errors
Syntax
errors
Runtime
errors
i
Logic
errors
The diagram below shows the percentages for source of errors insoftware
Marshall University Department of Engineering and
Computer Science
If we compile this Java file and runthe resulting class file, the outputproves that the program has done itsproves that the program has done itsjob.
No errors!
But, what about replacing
,^
p
g
by the following: s
ort(new
String[]
{},
2);
(
g[]
{},
);
or
sort(new
String[]
{null},
2);
or
sort(new
String[]
{null,
null},
2);
???
Marshall University Department of Engineering and
Computer Science
7
What we see when a runtime error occurs.We must learn to read theseb t it i
b tt
t
i^
th t
but it is better to insure thatthey do not occur.
Marshall University Department of Engineering and
Computer Science
The constructor for a
Triangle
has double values for
base
and
height
. The developer inadvertently reverses them when
he tries to calculate cosines and sines of the angleshe tries to calculate cosines and sines of the angles.
Height=y
Hypotenuse=r θ
Marshall University Department of Engineering and
Computer Science
Base=x
θ
To calculate the roots of a quadratic equation, the developererroneously codes
erroneously codes
r1=-b+Math.sqrt(bb-4ac)/(2a);r1=[-b+Math.sqrt(bb-4ac)]/(2a);**
Is there an error in the logic below that will not be detected by thecompiler or the Java virtual machine?
To find the volume of a sphere, we use the formula
π
r
3
A programmer codes
return 4/3Math.PIMath.pow(r,3);**
Marshall University Department of Engineering and
Computer Science
Marshall University Department of Engineering and
Computer Science
The
Throwable
class is the superclass of all errors and
exceptions in the Java language.
Only this class or one of its subclasses can be the argument type in
y
g
yp
a
catch
clause.
Only objects that are instances of this class
(or one of its
subclasses
are thrown by the Java Virtual Machine or can be )
y
thrown by the Java
throw
statement.
try{
addOrChangeEntry(myName,
myNumber);}
catch
(IllegalArgumentException
ex){
(
g
g
p
){
System.err.println(ex.getMessage());}
if
(!isPhoneNumberFormat(number)){throw
new
IllegalArgumentException(“Invalid
phone
number:
“
+
number);
Marshall University Department of Engineering and
Computer Science
}
Throwable
ErrorErrorException
IOException
EOFExceptionFileNotFoundExceptionMalformedURLExceptionUnknownHostException
ClassNotFoundExceptionCloneNotSupportedExceptionR
ti
E
ti
We are
RuntimeException
ArithmeticExceptionClassCastExceptionIllegalArgumentException
NumberFormatException
interestedin theseClasses.
NumberFormatException
IllegalStateExceptionIndexOutOfBoundsException
ArrayIndexOutOfBoundsException
NoSuchElementException
InputMismuchException
NullPointerException
Marshall University Department of Engineering and
Computer Science
tM
t
St
i
getMessage
returns a
St
ring
printStackTrace
prints the run time stack from the point
where the exception occurred back to the main program
toString
returns a
tring
with a detailed message and the
name of the exception
Marshall University Department of Engineering and
Computer Science
try{
System.out.println(str.charAt(3));
} catch(StringIndexOutOfBoundsException
ex){
ex.printStackTrace();System.exit(1);
}
Can be replaced by
Marshall University Department of Engineering and
Computer Science
19
i
f ll i
i^
h
k d
d
h
k d
xceptions fall into two categories: checked and unchecked
checked
exception is an exception that is not the fault of the
programmer.p og a
e.
For example
User enters invalid data for a numeric fieldUser asks to open non e istent file
User
asks to open non-existent file
The compiler checks to insure that the programmer hashandled these exceptions.
Other exceptions are referred to as
unchecked
. The compiler does
not force the programmer to catch these exceptions because he/shehas other methods to protect against them.
Marshall University Department of Engineering and
Computer Science
p
g