Spaces:
Running
Running
File size: 3,511 Bytes
4c632a6 | 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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | Ethan has been slacking off at school, and the certificate doesn't look like something he'd dare show his parents.
Write the method
String fix(String rcard)
which Ethan can use to get the certificate corrected to be salon-qualified.
The grades should be replaced as follows:
four --> eight
five --> nine
six --> ten
Example method call:
public static void main(String[] args) {
String report = "Math: four, Geography: six, Biology: seven";
String fixed= fix(report);
System.out.println(fixed);
}
Program outputs:
Math: eight, Geography: ten, Biology: seven
============================
import java.util.Random;
public class Test{
public static void main(String[] args){
final Random r = new Random();
String[] subjects = "Math Chemisty English Physics PE Religion Biology Geography".split(" ");
String[] g = "four five six seven eight".split(" ");
for (int i=1; i<=3; i++) {
System.out.println("Test " + i);
String rCard = "";
for (String s : subjects) {
// r.nextInt(5) - random int from 0 to 4 (inclusive)
rCard += s + ": " + g[r.nextInt(5)];
rCard +=", ";
}
// removes the trailing ", " from the LAST ENTRY (<subjects>: <g>)
// OF THE LONG STRING
rCard = rCard.substring(0, rCard.length() -2);
System.out.println("Report card before: ");
System.out.println("" + rCard);
System.out.println("Report card fixed: ");
System.out.println(fix(rCard));
System.out.println("");
}
}
//ADD
// public static String fix(String rcard) {
// String fixed = "";
// if (rcard.contains("four")) {
// fixed = rcard.replace("four", "eight");
// }
// if (rcard.contains("five")) {
// fixed = rcard.replace("five", "nine");
// }
// if (rcard.contains("six")) {
// fixed = rcard.replace("six", "ten");
// }
// else {
// fixed = rcard;
// }
// return fixed;
// }
public static String fix(String rcard) {
// String fixed = "";
if (rcard.contains("four")) {
rcard = rcard.replace("four", "eight");
}
if (rcard.contains("five")) {
rcard = rcard.replace("five", "nine");
}
if (rcard.contains("six")) {
rcard = rcard.replace("six", "ten");
}
return rcard;
}
}
Test 1
Report card before:
Math: five, Chemisty: six, English: eight, Physics: seven, PE: eight, Religion: eight, Biology: eight, Geography: five
Report card fixed:
Math: nine, Chemisty: ten, English: eight, Physics: seven, PE: eight, Religion: eight, Biology: eight, Geography: nine
Test 2
Report card before:
Math: four, Chemisty: five, English: five, Physics: five, PE: seven, Religion: eight, Biology: five, Geography: seven
Report card fixed:
Math: eight, Chemisty: nine, English: nine, Physics: nine, PE: seven, Religion: eight, Biology: nine, Geography: seven
Test 3
Report card before:
Math: six, Chemisty: six, English: four, Physics: five, PE: six, Religion: eight, Biology: five, Geography: seven
Report card fixed:
Math: ten, Chemisty: ten, English: eight, Physics: nine, PE: ten, Religion: eight, Biology: nine, Geography: seven
|