Professional Documents
Culture Documents
EECS 2030
FA 2020 :: Section C
EECS 2030
Lecture 3 :: Javadoc, Junit, recursion Junit Test,
1
2020-09-18
Types
• What is a type?
▪ Set of values and valid operations that can be performed
with those values
o int: + - * / % == != >
o String: ==, equals, length, charAt, indexOf
2
2020-09-18
Representations in Memory?
• Memory
▪ Primitive types (fixed allocation in memory)
o variable stores a value directly
• Memory diagram
▪ Primitive vs. reference type representation
6
3
2020-09-18
Memory Diagrams
int a = 7;
Student s = new Student();
...
x 100 -1.0
stack
t 108 700a
...
700 Array object 1
heap
704 2 (GCH)
708
x is a double which takes up 8 bytes in
memory, and so…
Array is consecutive in memory, t is allocated the next available byte
8
occupying 2*4=8 bytes
4
2020-09-18
Basics
Accessors and Mutators
Anonymous objects
Object aliasing and pass-by-value
More on methods
object aliasing and call-by-value
public class MyInteger {
Imitate wrapper class
private int value; Integer
public MyInteger(int i) {
this.value = i;
}
MyInteger
public int getValue() {
return value; int value: 30
}
5
2020-09-18
More on methods
object aliasing and call-by-value
MyInteger i = new MyInteger(30); i 60 600a
MyInteger j = i; j 600a
System.out.println(i.getValue()); // ? 30
System.out.println(j.getValue()); // ? 30
j.setValue(100);
System.out.println(i.getValue()); // ? 100
System.out.println(j.getValue()); // ? 100 600 MyInteger
value object
30 100
System.out.println(j==i); // ? True
System.out.println(j.equals(i)); // ?
Heap
(GCH)
i
30
100
j
11
11
More on methods
object aliasing and call-by-value
arrays
a 60 600a
int[] a = {10,20,30}; b 600a
int[] b = a;
System.out.println(Arrays.toString(a)); // ?
System.out.println(Arrays.toString(b)); // ?
10 20 30
10 20 30
600 10
b[1] = 200; // address 600+4 604 20 200
System.out.println(Arrays.toString(a)); // ?
System.out.println(Arrays.toString(b)); // ? 608 30
10 200 30 Heap
System.out.println(j==i); // ? 10 200 30
(GCH)
System.out.println(j.equals(i)); // ?
a
10 20 30
200
b
12
12
6
2020-09-18
More on methods
object aliasing and call-by-value
• aliasing describes a situation in which a data location (Java: object)
in memory can be accessed through different symbolic names (Java:
reference variables) in the program.
• Thus, modifying the data (object) through one name (reference
variables) implicitly modifies the values associated with all aliased
names (variables), which may or may not be expected by the
programmer. (more later)
13
reference variables
13
numbers1[0] = 4;
System.out.println(Arrays.toString(numbers1)); //? 4 2 3
System.out.println(Arrays.toString(numbers2)); //? 1 2 3
14
7
2020-09-18
int a = 1;
int b = 2;
int c = 3;
int[] numbers1 = {a, b, c};
int[] numbers2 = numbers1;
numbers1[0] = 4;
System.out.println(Arrays.toString(numbers1)); //? 4 2 3
System.out.println(Arrays.toString(numbers2)); //? 4 2 3
15
15
numbers1[0].setValue(4);
System.out.println(Arrays.toString(numbers1)); //? 4 2 3
System.out.println(Arrays.toString(numbers2)); //? 4 2 3
16
8
2020-09-18
numbers1[0].setValue(4);
System.out.println(Arrays.toString(numbers1)); //? 4 2 3
System.out.println(Arrays.toString(numbers2)); //? 4 2 3
17
Basics
Accessors and Mutators
Anonymous objects
Object aliasing and pass-by-value
18
18
9
2020-09-18
// tries to double x
this is a different
public static void twice (int x) Doubler.twice x = a;
{ method than the
x = 2 * x; previous example
} int x = a; (now resides at
address 800)
x = 2 * x;
800 Doubler.twice
x 3
a 3 parameter x
is an independent
copy of the value
19 x 3 of argument a
(a primitive)
19
// tries to double x
this is a different
public static void twice (int x) Doubler.twice x = a;
{ method than the
x = 2 * x; previous example
} int x = a; (now resides at
address 800)
x = 2 * x;
800 Doubler.twice
x 3 6
a 3 parameter x
is an independent
copy of the value
20 x 6 of argument a
(a primitive)
20
10
2020-09-18
// tries to double x
public static void twice (MyInteger x) { 500 MyInteger
int i = x.getValue(); object
x.setValue( 2 * i ); x = b;
value 3
}
Call-by
MyInteger x = b;
int i = x.getValue() value
x.setX(2*i)
aliasing Doubler.twice
600
b x 500a
parameter x
3
is an independent
copy of the value
21
x of argument b
(a reference)
21
// tries to double x
public static void twice (MyInteger x) { 500 MyInteger
int i = x.getValue(); object
x.setValue( 2 * i ); x = b;
value 3 6
}
Call-by
MyInteger x = b;
int i = x.getValue() value
x.setX(2*i)
600 Doubler.twice
b x 500a
parameter x
6
is an independent
copy of the value
22
x of argument b
(a reference)
22
11
2020-09-18
// tries to double x
public static void twice (int[] x) {
x[1] = x[1]*2;
} 500 10
int[] x = b; value 20 x = b;
x[1] = x[1]*2;
30 Call-by
value
600 Doubler.twice
b x 500a
10 20 30 parameter x
is an independent
copy of the value
23
x of argument b
(a reference)
23
// tries to double x
public static void twice (int[] x) {
x[1] = x[1]*2;
} 500 10
int[] x = b; value 20 40 x = b;
x[1] = x[1]*2;
30 Call-by
value
600 Doubler.twice
b x 500a
10 20 30 parameter x
is an independent
40 copy of the value
24
x of argument b
(a reference)
24
12
2020-09-18
private Doubler() {
}
// tries to double x
public static void twice (int x) {
x = 2 * x; int a = 3;
} Doubler.twice(a);
System.out.println(a);
// tries to double x
public static int twice (int x) {
int i = 2 * x;
return i;
}
int a = 3;
}
a = Doubler.twice(a);
System.out.println(a); // 6
25
25
private Doubler() {
}
// tries to double x
public static void twice (int x) {
x = 2 * x;
}
// tries to double x
public static void twice (MyInteger x) {
int i = x.getValue();
x = new MyInteger(2 * i );
}
}
26
13
2020-09-18
Basics
Accessors and Mutators
Anonymous objects
Object aliasing and pass-by-value
27
27
static void A ()
{
A_statement1;
B();
A_statement2; static void B ()
} {
B_statement1;
C();
B_statement2;
}
static void D ()
{
D_statement1;
D_statement2;
}
28
28
14
2020-09-18
A()
a A()
600 statememt_1
A()
A method B()
main()
statement_2
29
call/execution/program stack
29
A()
a A()
750 statememt_1
B method C()
statement_2
30
15
2020-09-18
A()
a A()
800 statememt_1
C method D()
statement_2
750 statememt_1
B method C()
statement_2
C()
B() 600 statememt_1
A()
A method B()
main()
statement_2
31
call/execution/program stack
31
950 statememt_1
A()
D method statement_2
a A()
800 statememt_1
C method D()
statement_2
750 statememt_1
B method C()
statement_2
D()
C()
B() 600 statememt_1
A()
A method B()
main()
statement_2
32
call/execution/program stack
32
16
2020-09-18
A()
a A()
800 statememt_1
C method D()
statement_2
750 statememt_1
B method C()
statement_2
C()
B() 600 statememt_1
A()
A method B()
main()
statement_2
33
call/execution/program stack
33
A()
a A()
750 statememt_1
B method C()
statement_2
34
17
2020-09-18
A()
a A()
600 statememt_1
A()
A method B()
main()
statement_2
35
call/execution/program stack
35
A()
a A()
main()
36
call/execution/program stack
36
18
2020-09-18
37
call/execution/program stack
37
38
19
2020-09-18
A()
a A()
600 statememt_1
A()
A method A()
main()
statement_2
39
call/execution/program stack
39
A()
a A()
750 statememt_1
A method A()
statement_2
40
20
2020-09-18
A()
a A()
800 statememt_1
A method A()
statement_2
750 statememt_1
A method A()
statement_2
A()
A() 600 statememt_1
A()
A method A()
main()
statement_2
41
call/execution/program stack
41
950 statememt_1
A()
A method A()
statement_2
100 main method
a A()
800 statememt_1
A method A()
statement_2
750 statememt_1
A method A()
statement_2
A()
A()
A() 600 statememt_1
A()
A method A()
main()
statement_2
42
call/execution/program stack
42
21
2020-09-18
950 statememt_1
A()
A method A()
statement_2
100 main method
a A()
800 statememt_1
A method A()
statement_2
43
Documenting
Javadoc
44
44
22
2020-09-18
• Separation of Concerns
o CLIENT vs. IMPLEMENTER
45
45
• precondition
▪ a condition that the client must ensure is true immediately
before a method is invoked
• postcondition
▪ a condition that the method must ensure is true immediately
after the method is invoked
double sqrtRoot(double i)
46
46
23
2020-09-18
Preconditions
double sqrtRoot(double i)
47
47
Postconditions
double sqrtRoot(double i)
48
48
24
2020-09-18
Documenting
49
49
50
50
25
2020-09-18
Documenting
• Javadoc processes doc comments that immediately precede a
class, attribute, constructor or method declaration
▪ doc comments delimited by /** and */
▪ doc comment written in HTML and made up of two parts
1. a description
first sentence of description gets copied to the summary section
only one description block; can use <p> to create separate paragraphs
2. block tags
begin with @
@param
@return
@throws
….
@pre. (a non-standard (custom tag used in EECS2030) for documenting
51
preconditions)
51
52
52
26
2020-09-18
/**
* @param min
* @param max
* @param value
* @return
*/
53
53
/**
* Returns true if value is strictly greater than min and strictly
* less than max, and false otherwise.
*
* @param min
* @param max
* @param value
* @return
*/
public static boolean isBetween(int min, int max, int value) {
// implementation not shown
}
54
54
27
2020-09-18
/**
* Returns true if value is strictly greater than min and strictly
* less than max, and false otherwise.
*
* @param min a minimum value
* @param max a maximum value
* @param value a value to check
* @return
*/
public static boolean isBetween(int min, int max, int value) {
// implementation not shown
}
55
55
/**
* Returns true if value is strictly greater than min and strictly
* less than max, and false otherwise.
*
* @param min a minimum value
* @param max a maximum value
* @param value a value to check
* @return true if value is strictly greater than min and strictly
* less than max, and false otherwise
*/
public static boolean isBetween(int min, int max, int value) {
// implementation not shown
}
56
56
28
2020-09-18
57
57
29
2020-09-18
• Eclipse
59
59
• Lab0 screeshot
60
60
30
2020-09-18
61
61
62
62
31
2020-09-18
Testing
JUnit
63
63
Testing
64
64
32
2020-09-18
• Imagine you are testing the following methods in the static class
eecs2030.testing.Hello
65
65
// avg
System.out.println(
String.format("average of %d, %d, and %d: ", a, b, c) +
Test.avg(a, b, c));
// swap2
List<Integer> t = new ArrayList<Integer>();
t.add(3);
t.add(5);
String s = t.toString();
Test.swap2(t);
System.out.println(
String.format("swap2(%s): %s", s, t.toString()));
66
66
33
2020-09-18
sum of 1 and 1: 2
67
67
68
68
34
2020-09-18
JUnit
69
69
Unit test
• “A unit test examines the behavior of a distinct unit of
work. Within a Java application, the “distinct unit of work”
is often (but not always) a single method. … A unit of work
is a task that isn't directly dependent on the completion of
any other task.”
▪ from the book JUnit in Action
70
70
35
2020-09-18
Junit Framework?
• Automate the testing of correctness of your Java classes.
• Once you derive the list of tests, translate it into a JUnit test case,
which is just a Java class that you can execute upon.
71
72
72
36
2020-09-18
73
73
74
74
37
2020-09-18
package eecs2030.testing;
75 *https://docs.oracle.com/javase/8/docs/technotes/guides/language/static-import.html
75
package eecs2030.testing;
76
76
38
2020-09-18
package eecs2030.testing;
@Test
public void test_sum() {
int a = 1;
int b = 1;
int expected = 2;
int actual = Hello.sum(a, b); // call static method
Use a Junit built-in method to test if expected and actual
} are equal/same
The JUnit method throws an exception if expected and actual
are not equal. JUnit handles the exception and reports the test
failure to the user.
77
77
78
39
2020-09-18
79
@Test
public void test_swap2() {
List<Integer> actual = new ArrayList<Integer>();
actual.add(-99);
actual.add(88);
List<Integer> expected = new ArrayList<Integer>();
expected.add(88);
expected.add(-99);
Hello.swap2(actual);
assertEquals(expected, actual);
// or
assertTrue(expected.equals(actual));
}
80 assertSame(expected, actual)
80
40
2020-09-18
Run testcases
81
81
82
82
41