Midterm Review
Midterm Review
1 review
Concepts
1.
What
items
should
be
included
in
a
complete
method
specica:on?
(In
Quiz1)
(modifier, return value, method name, parameter list 2.
What
is
a
precondi:on?
A condition that is supposed to be true when the method begins. A condition that is supposed to be true when the method ends. 3.
What
is
a
postcondi:on?
4.
What
is
no-argument
constructor?
And
what
does
it
do?
(In
Quiz2)
A no argument constructor has no arguments and effectively initializes the instance variables. 5.
What
is
the
invariant
for
IntArrayBag?
1. The number of elements in the bag is stored in the instance variable manyItems, which is no longer than data.length. 2. For an empty bag, we do not care what is stored in data[0] through data[manyItems - 1], and we don't care what is stored in the rest of the data.
Big-O
6. Given a formula, get Big-O nota:on (Quiz1 Q1) 7. 1+2+3++n, derive formula and get Big-O nota:on. 8. Compare Big O (Quiz1 Q4)
const O(x) Log O(log n) Linear O(n) quad O(n^2) exp O(n^n)
f(n) =
Variable
9.
Test
using
variable
for
primi:ve
data
type
as
parameter
public
class
Test{
public
sta+c
void
incValue(int
v){
v++;
System.out.println(v);
}
public
sta+c
void
main(String
args[]){
int
x
=
1;
System.out.println(x);
Test.incValue(x);
System.out.println(x);
}
}
IntArrayBag
17.
IntArrayBag
Constructor
18.
Add
method
(3
example
ques:ons)
19.
Remove
method
(3
similar
example
ques:ons)
20.
EnsureCapacity
method(3
similar
example
ques:ons)
Example
ques:on
1
Ref:
Quiz
3,
Complete
codes
public
void
add(int
element){
if(manyItems==data.length){
EnsureCapacity((manyItems+1)*2);
}
data[manyItems]=element;
manyItems++
}
Example
ques:on
2
Give
the
add
method
public
void
add(int
element){
if(manyItems==data.length){
EnsureCapacity((manyItems+1)*2);
}
data[manyItems]=element;
manyItems++
}
IntArrayBag
bag
=
new
IntArrayBag(2);
bag.add(1);
bag.add(2);
bag.add(2);
//what
does
the
memory
look
like
Example
ques:on
3
Debug
public
void
add(int
element)
{
if(manyItems<data.length){
data[manyItems]
=
element;
manyItems++;
}else{
ensureCapacity(manyItems*2+1);
}
}
Any
problem?
Yes/No?
Linked
List
21.
IntNode
constructor
22.
IntNode
ListLength
(test
two
ways
to
loop
a
list)
23.
IntNode
listSearch
(similar
to
22)
24.
IntNode
listPosi:on
(similar
to
22)
25.
IntNode
addNodeAeer
(Example
ques:ons,
see
later
notes)
26.
IntNode
removeNodeAeer
(Example
ques:ons,
see
later
notes)
25-28
Func:ons
in
lab5:
Reverse,
removeDuplicate,
sumLast,ndMiddle
Example
ques:ons:
write
one
method
listLength
How
to
loop
a
list
using
for
public
sta+c
int
listLength(IntNode
head)
{
IntNode
cursor;
int
answer;
answer
=
0;
for
(cursor
=
head;
cursor
!=
null;
cursor
=
cursor.link)
answer++;
return
answer;
}
addNodeAeer
public
void
addNodeAPer(int
item)
{
link
=
new
IntNode(item,
link);
}
Possible
Ques:on
1:
write
this
method
Possible
Ques:on
2:
use
this
method
to
implement
another
method
Create
a
list
with
n
numbers
Possible
Ques:on
3:
What
error
you
may
get
Possible
Ques:on
4:
what
is
the
:me
complexity
of
this
method?
removeNodeAeer
public
void
removeNodeAPer(
)
{
link
=
link.link;
}
Possible
Ques:on
1:
write
this
method
Possible
Ques:on
2:
use
this
method
to
implement
another
method
Empty
a
list
removeDuplicate
Possible
Ques:on
3:
What
Excep:on
you
may
get
Possible
Ques:on
4:
what
is
the
:me
complexity
of
this
method?