KaiquanMah commited on
Commit
4c632a6
·
verified ·
1 Parent(s): db7ae7d

Create 13B. Fix report card

Browse files
Week 2: Methods, strings and lists/13B. Fix report card ADDED
@@ -0,0 +1,133 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Ethan has been slacking off at school, and the certificate doesn't look like something he'd dare show his parents.
2
+ Write the method
3
+
4
+ String fix(String rcard)
5
+
6
+ which Ethan can use to get the certificate corrected to be salon-qualified.
7
+
8
+
9
+ The grades should be replaced as follows:
10
+ four --> eight
11
+ five --> nine
12
+ six --> ten
13
+
14
+
15
+ Example method call:
16
+ public static void main(String[] args) {
17
+ String report = "Math: four, Geography: six, Biology: seven";
18
+ String fixed= fix(report);
19
+ System.out.println(fixed);
20
+ }
21
+ Program outputs:
22
+ Math: eight, Geography: ten, Biology: seven
23
+
24
+
25
+
26
+ ============================
27
+
28
+
29
+
30
+
31
+ import java.util.Random;
32
+
33
+ public class Test{
34
+ public static void main(String[] args){
35
+ final Random r = new Random();
36
+
37
+ String[] subjects = "Math Chemisty English Physics PE Religion Biology Geography".split(" ");
38
+ String[] g = "four five six seven eight".split(" ");
39
+
40
+ for (int i=1; i<=3; i++) {
41
+ System.out.println("Test " + i);
42
+ String rCard = "";
43
+ for (String s : subjects) {
44
+ // r.nextInt(5) - random int from 0 to 4 (inclusive)
45
+ rCard += s + ": " + g[r.nextInt(5)];
46
+ rCard +=", ";
47
+ }
48
+
49
+ // removes the trailing ", " from the LAST ENTRY (<subjects>: <g>)
50
+ // OF THE LONG STRING
51
+ rCard = rCard.substring(0, rCard.length() -2);
52
+
53
+ System.out.println("Report card before: ");
54
+ System.out.println("" + rCard);
55
+ System.out.println("Report card fixed: ");
56
+ System.out.println(fix(rCard));
57
+ System.out.println("");
58
+
59
+ }
60
+ }
61
+
62
+ //ADD
63
+ // public static String fix(String rcard) {
64
+ // String fixed = "";
65
+
66
+ // if (rcard.contains("four")) {
67
+ // fixed = rcard.replace("four", "eight");
68
+ // }
69
+ // if (rcard.contains("five")) {
70
+ // fixed = rcard.replace("five", "nine");
71
+ // }
72
+ // if (rcard.contains("six")) {
73
+ // fixed = rcard.replace("six", "ten");
74
+ // }
75
+ // else {
76
+ // fixed = rcard;
77
+ // }
78
+
79
+ // return fixed;
80
+ // }
81
+
82
+ public static String fix(String rcard) {
83
+ // String fixed = "";
84
+
85
+ if (rcard.contains("four")) {
86
+ rcard = rcard.replace("four", "eight");
87
+ }
88
+ if (rcard.contains("five")) {
89
+ rcard = rcard.replace("five", "nine");
90
+ }
91
+ if (rcard.contains("six")) {
92
+ rcard = rcard.replace("six", "ten");
93
+ }
94
+
95
+ return rcard;
96
+ }
97
+
98
+
99
+
100
+ }
101
+
102
+
103
+
104
+
105
+
106
+
107
+
108
+
109
+
110
+ Test 1
111
+ Report card before:
112
+ Math: five, Chemisty: six, English: eight, Physics: seven, PE: eight, Religion: eight, Biology: eight, Geography: five
113
+ Report card fixed:
114
+ Math: nine, Chemisty: ten, English: eight, Physics: seven, PE: eight, Religion: eight, Biology: eight, Geography: nine
115
+
116
+ Test 2
117
+ Report card before:
118
+ Math: four, Chemisty: five, English: five, Physics: five, PE: seven, Religion: eight, Biology: five, Geography: seven
119
+ Report card fixed:
120
+ Math: eight, Chemisty: nine, English: nine, Physics: nine, PE: seven, Religion: eight, Biology: nine, Geography: seven
121
+
122
+ Test 3
123
+ Report card before:
124
+ Math: six, Chemisty: six, English: four, Physics: five, PE: six, Religion: eight, Biology: five, Geography: seven
125
+ Report card fixed:
126
+ Math: ten, Chemisty: ten, English: eight, Physics: nine, PE: ten, Religion: eight, Biology: nine, Geography: seven
127
+
128
+
129
+
130
+
131
+
132
+
133
+