Docsity

Materials for Midterm Exam 1 - Data Structures and Algorithms | CS 210, Exams of Computer Science

Partial preview of the text

Download Materials for Midterm Exam 1 - Data Structures and Algorithms | CS 210 and more Exams Computer Science in PDF only on Docsity!

CS 210CS 210

Algorithms and Data StructuresAlgorithms and Data Structures

CS

CS 210

Algorithms and Data StructuresAlgorithms and Data Structures

Material for Midterm Exam 1

Author

’s Objectives for Chapter II

Author’s Objectives for Chapter IIAuthor s Objectives for Chapter IIAuthor s Objectives for Chapter II

Marshall University Department of Engineering and

Computer Science

Defects, errors, and bugsDefects, errors, and bugs

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

Types of errorsTypes of errors

There are 3 major categories or types of errorsThere

are 3 major categories or types of errors

Syntax

errors

Runtime

errors

L

i

.^

Logic

errors

The diagram below shows the percentages for source of errors insoftware

Marshall University Department of Engineering and

Computer Science

Runtime ErrorsRuntime Errors

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

Runtime ErrorsRuntime Errors

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

Logic ErrorsLogic ErrorsLogic

Errors

Logic Errors

Logic errors result from a faulty algorithm.

Logic errors may be the most difficult to correct

Have the potential to be devastating, especially ifth

ft

th

d

t h

b

d li

d

they occur after the product has been delivered

For example

For example

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 θ

cos

= x/r

i

Marshall University Department of Engineering and

Computer Science

Base=x

θ

sin

= y/r

Logic ErrorsLogic Errors

To calculate the roots of a quadratic equation, the developererroneously codes

Logic ErrorsLogic Errors

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

V=4/

π

r

3

A programmer codes

return 4/3Math.PIMath.pow(r,3);**

Marshall University Department of Engineering and

Computer Science

OutlineOutline

D f

t

d b

D

efects, errors, and bugs

The

Throwable

class

The

try-catch-finally

sequence,

the

throw

statement, and the

throws

l

clause

Removing logic errors, testingprograms

Efficiency of algorithms

Marshall University Department of Engineering and

Computer Science

TheThe

T

hrowable

Throwable

ClassClass

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

}

Exception Classes HierarchyException Classes Hierarchy

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

Common methods ofCommon methods of

T

hrowable

Throwable

classclass

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

S

tring

with a detailed message and the

name of the exception

Marshall University Department of Engineering and

Computer Science

The stack traceThe stack traceThe

stack trace

The stack trace

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

Checked and unchecked exceptionsChecked and unchecked exceptions

E

i

f ll i

i^

h

k d

d

h

k d

E

xceptions fall into two categories: checked and unchecked

A

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