-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCounter1ConstructorGrade.java
More file actions
74 lines (67 loc) · 2.56 KB
/
Copy pathCounter1ConstructorGrade.java
File metadata and controls
74 lines (67 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import java.util.Random;
/**
*
* This program tests and scores the Counter1Constructor class.
*
* @author {user}
* Course: COMP B11
* Created: Apr 19, 2017, 12:41:21 PM
* Source File: Counter1ConstructorGrade.java
*/
public class Counter1ConstructorGrade {
public static int testCount(Counter1Constructor counter, int numTimesToCount) {
int initialCount = counter.getValue();
int count;
int scoreDeduction = 0;
for (int i = 0; i < numTimesToCount; ++i)
counter.count();
System.out.printf("Calling count() %d times\n", numTimesToCount);
count = counter.getValue();
if (count != initialCount + numTimesToCount) {
scoreDeduction = 50;
System.out.printf("Incorrect count getValue() returns %d, but "
+ "should return %d (-50 pts)\n",
count, initialCount + numTimesToCount);
} else {
System.out.printf("\tCount correct\n");
}
return scoreDeduction;
}
public static void main(String[] args) {
int numTimesToCount;
int count;
int score = 100;
Random random = new Random();
numTimesToCount = random.nextInt(12) + 3;
System.out.printf("Testing no-arg constructor\n");
Counter1Constructor counter = new Counter1Constructor();
if ((count = counter.getValue()) != 0) {
score -= 50;
System.out.printf("No-arg constructor incorrect: getValue() returns"
+ " %d, but should return 0 (-50 pts)\n", count);
} else
score -= testCount(counter, numTimesToCount);
int argument = numTimesToCount + 1;
System.out.printf("\nTesting constructor with argument of %d\n",
argument);
counter = new Counter1Constructor(argument);
if ((count = counter.getValue()) != argument) {
score -= 50;
System.out.printf("Constructor incorrect: getValue() returns"
+ " %d, but should return %d (-50 pts)\n", count, argument);
} else
score -= testCount(counter, numTimesToCount);
System.out.printf("\nCalling reset()\n");
counter.reset();
count = counter.getValue();
if (count != 0) {
score -= 50;
System.out.printf("Incorrect count after reset() - getValue() "
+ "returns %d, but should return 0 (-50 pts)\n",
count);
} else {
System.out.printf("reset() works correctly.\n");
}
System.out.printf("\nScore: %d%%\n", score);
}
}