FRQ 4: ----------+ Candy.java| ----------+ import java.awt.*; /** * 2023 FRQ 4 has two classes: Candy and BoxOfCandy. * Implementation by Chris Thiel, OFMCap. */ public class Candy { private String flavor; /** * Constructor for objects of class Candy */ public Candy(String f) { flavor = f; } public String getFlavor() { return flavor; } public String toString() { String f = flavor; if (f.length() == 4) f= " "+f+" "; if (f.length() == 5) f= f+" "; return f; } } ---------------+ BoxOfCandy.java| ---------------+ /** * 2023 FRQ 4 has three classes: Candy, BoxOfCandy, and BoxOfCandyTester. * Implementation by Chris Thiel, OFMCap. */ public class BoxOfCandy { private Candy[][] box; /** * part (a) */ return false; //replace with your code } /** * part (b) * * Search the from the last row the box from left to right * then the next last row of the box from left to right until * a piece of candy with the desired flavor is found. * * Return null if the desired candy is not found */ public Candy removeNextByFlavor(String flavor) { return null; // replace with your code } public BoxOfCandy(int rows, int cols ) { box = new Candy[rows][cols]; } public BoxOfCandy(BoxOfCandy b){ int rows = b.getBox().length; int cols = b.getBox()[0].length; box = new Candy[rows][cols]; for(int r=0; r< rows; r++) for(int c = 0; c < cols; c++) box[r][c] = b.getBox()[r][c]; } public Candy[][] getBox(){ return box; } public void newCandy(int r, int c, String f) { box[r][c] = new Candy(f); } public boolean equals(BoxOfCandy o) { int rows = box.length; int cols = box[0].length; if (rows != o.box.length) return false; if (cols != o.box[0].length) return false; for(int r = 0; r < rows; r++) { for(int c = 0; c < cols; c++) { if (this.box[r][c] == null && o.box[r][c] != null) return false; if (box[r][c] != null && o.box[r][c] == null) return false; if (this.box[r][c] != null && o.box[r][c] != null && !this.box[r][c].getFlavor().equals(o.box[r][c].getFlavor()) ) return false; } } return true; } public String toString(){ String result=""; for(int r=0; r< box.length; r++){ for(int c = 0; c < box[r].length; c++) { if(box[r][c] == null) result+= "[ ]"; else result += "["+box[r][c].toString()+"]"; } result +="\n"; } return result; } } ---------------------+ BoxOfCandyTester.java| ---------------------+ /** * 2023 FRQ 4: Box of Candy Tester * Requires Candy and BoxOfCandy classes * with your answer to parts (a) and (b) in * the Box of Candy class */ public class BoxOfCandyTester { public static void main( String[] args ) { BoxOfCandy boxA = new BoxOfCandy(4, 3); boxA.newCandy(0,1,"lime"); boxA.newCandy(2,2,"cherry"); boxA.newCandy(1,1,"orange"); boxA.newCandy(3,1,"lemon"); boxA.newCandy(3,2,"grape"); System.out.println("Part A begins as\n"+boxA); BoxOfCandy boxA2 = new BoxOfCandy(boxA); System.out.println(" moveCandyToFirstRow(0) returns "+ boxA.moveCandyToFirstRow(0) +"(should be false)"); System.out.println(" moveCandyToFirstRow(1) returns "+ boxA.moveCandyToFirstRow(1) +"(should be true)"); System.out.println(" moveCandyToFirstRow(2) returns "+ boxA.moveCandyToFirstRow(2) +"(should be true)"); System.out.println("after moveCandyToFirstRow(2) \n"+boxA + partACheck(boxA)); BoxOfCandy boxB = new BoxOfCandy(3, 5); boxB.newCandy(0,0,"lime"); boxB.newCandy(0,1,"lime"); boxB.newCandy(0,3,"lemon"); boxB.newCandy(1,0,"orange"); boxB.newCandy(1,3,"lime"); boxB.newCandy(1,4,"lime"); boxB.newCandy(2,0,"cherry"); boxB.newCandy(2,2,"lemon"); boxB.newCandy(2,4,"orange"); System.out.println("\nPart B begins as\n"+boxB); System.out.println ("removeNextByFlavor(\"cherry\") returns " + checkPartB("cherry", boxB) ); System.out.println("\nPart B now is\n"+boxB); System.out.println ("removeNextByFlavor(\"lime\") returns " + checkPartB("lime", boxB) ); System.out.println("\nPart B now is\n"+boxB); System.out.println ("removeNextByFlavor(\"grape\") returns " + checkPartB("grape", boxB) ); System.out.println("\nPart B now is\n"+boxB); System.out.println(isPartBCorrect()); } public static String partACheck(BoxOfCandy b) { Candy[][] guess = b.getBox(); int rows = guess.length; int cols = guess[0].length; BoxOfCandy a1 = new BoxOfCandy(rows, cols); BoxOfCandy a2 = new BoxOfCandy(rows, cols); a1.newCandy(0,1,"lime"); a1.newCandy(0,2,"cherry"); a1.newCandy(1,1,"orange"); a1.newCandy(3,1,"lemon"); a1.newCandy(3,2,"grape"); a2.newCandy(0,1,"lime"); a2.newCandy(2,2,"cherry"); a2.newCandy(1,1,"orange"); a2.newCandy(3,1,"lemon"); a2.newCandy(0,2,"grape"); Candy[][] ans1 = a1.getBox(); Candy[][] ans2 = a2.getBox(); //System.out.println("a1\n"+a1); //System.out.println("a2\n"+a2); if(a1.equals(b) || a2.equals(b)) return "CORRECT"; return "NOT CORRECT"; } public static String checkPartB(String f,BoxOfCandy b ) { Candy result = b.removeNextByFlavor(f); if (result == null) return " a null"; return "a "+result.getFlavor() + " candy"; } public static String isPartBCorrect(){ String result =""; int rows = 3; int cols = 5; BoxOfCandy aCherry = new BoxOfCandy(rows, cols); BoxOfCandy aLime = new BoxOfCandy(rows, cols); aCherry.newCandy(0,0,"lime"); aCherry.newCandy(0,1,"lime"); aCherry.newCandy(0,3,"lemon"); aCherry.newCandy(1,0,"orange"); aCherry.newCandy(1,3,"lime"); aCherry.newCandy(1,4,"lime"); aCherry.newCandy(2,2,"lemon"); aCherry.newCandy(2,4,"orange"); aLime.newCandy(0,0,"lime"); aLime.newCandy(0,1,"lime"); aLime.newCandy(0,3,"lemon"); aLime.newCandy(1,0,"orange"); aLime.newCandy(1,4,"lime"); aLime.newCandy(2,2,"lemon"); aLime.newCandy(2,4,"orange"); BoxOfCandy boxB = new BoxOfCandy(3, 5); boxB.newCandy(0,0,"lime"); boxB.newCandy(0,1,"lime"); boxB.newCandy(0,3,"lemon"); boxB.newCandy(1,0,"orange"); boxB.newCandy(1,3,"lime"); boxB.newCandy(1,4,"lime"); boxB.newCandy(2,0,"cherry"); boxB.newCandy(2,2,"lemon"); boxB.newCandy(2,4,"orange"); Candy c = boxB.removeNextByFlavor("cherry"); boolean cherryPass = c !=null && c.getFlavor().equals("cherry") && aCherry.equals(boxB); Candy l = boxB.removeNextByFlavor("lime"); boolean limePass = l !=null && l.getFlavor().equals("lime") && aLime.equals(boxB) ; Candy g = boxB.removeNextByFlavor("grape"); boolean grapePass = g == null && aLime.equals(boxB); if( cherryPass && limePass && grapePass) return "Part(b) is CORRECT"; return "part (b) is NOT CORRECT"; } }