Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

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

Material Type: Exam; Class: Data Structures and Algorithms; Subject: Computer Science; University: Marshall ; Term: Unknown 1989;

Typology: Exams

Pre 2010

Uploaded on 07/31/2009

koofers-user-g8z
koofers-user-g8z 🇺🇸

10 documents

1 / 129

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS 210CS 210
Algorithms and Data StructuresAlgorithms and Data Structures
CS
210
CS
210
Algorithms
and
Data
StructuresAlgorithms
and
Data
Structures
Material for Midterm Exam 1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c
pf5d
pf5e
pf5f
pf60
pf61
pf62
pf63
pf64

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