Spaces:
Running
Running
| 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 | |