Question: can you help me fix my code so it passes alll the tests its failing import java.util.HashSet; / / Sum of Strings [ 3 0

can you help me fix my code so it passes alll the tests its failing
import java.util.HashSet;
// Sum of Strings [30%]
//DO NOT add additional fields
//DO NOT add additional methods
public class Task2{
// DO NOT change this field
private HashSet strings;
// DO NOT change this method
public void setStrings(HashSet stringSet){
strings = stringSet;
}
// DO NOT change the signature for this method
// This method does not need to be recursive
public boolean stringSum(){
for(String string : strings){
if(isConstructable(string, string)){
return true;
}
}
return false;
}
// This method MUST be recursive
// You can change the name and parameters of this method
private boolean isConstructable(String aim, String initial){
return isConstructableHelper(aim, new HashSet<>(), initial);
}
private boolean isConstructableHelper(String aim, HashSet visited, String initial){
if (aim.isEmpty()) return true;
if (visited.contains(aim)) return false;
visited.add(aim);
for (String string : strings){
if (!string.equals(initial) && aim.startsWith(string)){
String remaining = aim.substring(string.length());
if (isConstructableHelper(remaining, visited, initial)){
return true;
}
}
}
visited.remove(aim); // Backtrack
return false;
}
}
public class Task3{
// DO NOT change this field
private int[][] map;
// DO NOT change this method
public void setMap(int[][] grid){
map = grid;
}
// DO NOT change the signature for this method
// This method does not need to be recursive
public int dig(int battery){
int maxGold =0;
for (int col =0; col < map[0].length; col++){
maxGold = Math.max(maxGold, maximumGoldFrom(0, col, battery, new boolean[map.length][map[0].length]));
}
return maxGold;
}
private int maximumGoldFrom(int y, int x, int battery, boolean[][] visited){
if (battery <=0|| y <0|| y >= map.length || x <0|| x >= map[0].length || visited[y][x]){
return 0;
}
visited[y][x]= true; // Mark cell as visited
int gold = map[y][x];
// Explore all directions
int maxGold =0;
maxGold = Math.max(maxGold, gold + maximumGoldFrom(y, x -1, battery -1, visited)); // Left
maxGold = Math.max(maxGold, gold + maximumGoldFrom(y, x +1, battery -1, visited)); // Right
maxGold = Math.max(maxGold, gold + maximumGoldFrom(y +1, x, battery -1, visited)); // Down
visited[y][x]= false; // Backtrack
return maxGold;
}
}
@Test
public void StringSumHelper3(){
Task2 t2= new Task2();
HashSet strings = new HashSet<>();
strings.add("");
strings.add("SEAN");
t2.setStrings(strings);
boolean output = t2.stringSum();
assertEquals(false, output);
}
@Test
public void DigTest1(){
Task3 t3= new Task3();
int[][] map = new int[][]{
new int[]{0,1,1,0,0},
new int[]{2,0,0,0,0},
new int[]{0,0,3,1,0},
new int[]{1,0,0,1,0},
new int[]{0,1,0,0,5}
};
t3.setMap(map);
assertEquals(13, t3.dig(6));
}
@Test
public void DigTest2(){
Task3 t3= new Task3();
int[][] map ={
new int[]{2,3,1},
new int[]{0,0,0},
new int[]{5,4,3}
};
t3.setMap(map);
assertEquals(9, t3.dig(3));
}
public void DigTest3(){
Task3 t3= new Task3();
int[][] map ={
new int[]{1,3,1,4},
new int[]{0,2,5,0},
new int[]{1,0,2,3}
};
t3.setMap(map);
assertEquals(21, t3.dig(5));
}
@Test
public void DigTest5(){
Task3 t3= new Task3();
int[][] map ={
new int[]{1,2,3},
new int[]{4,5,6},
new int[]{7,8,9}
};
t3.setMap(map);
assertEquals(45, t3.dig(6));
}
all these are failing DigTest1() org.opentest4j.AssertionFailedError: Expected :13 Actual :7 DigTest2() org.opentest4j.AssertionFailedError: Expected :9 Actual :7 DigTest3() org.opentest4j.AssertionFailedError: Expected :21 Actual :15 DigTest5() org.opentest4j.AssertionFailedError: Expected :45 Actual :37 StringSumHelper3() org.opentest4j.AssertionFailedError: Expected :false Actual :true

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!