Question: What would the code be for this? public String moveLeft(String row) If the row is not valid (you must invoke the validateRow method to determine
What would the code be for this?
public String moveLeft(String row)
If the row is not valid (you must invoke the validateRow method to determine this), return the string unmodified. Otherwise, return a new string with all of the non-blank spaces (i.e. '2's and '4's) shifted all the way to the left. This method does not combine blocks.
Given this:
import java.util.*;
class Combine { //implements combinable
public boolean validateChar(char ch) {
if(ch=='4' || ch=='2' || ch=='_') {
return true;
}
return false;
}
public String CheckString(String s) {
if(s.length()!=3) {
return s;
}
else if(validateChar(s.charAt(0))==true && validateChar(s.charAt(1))==true && validateChar(s.charAt(2))==true) {
assert validateChar(s.charAt(0)) : s.charAt(0)+" should be a valid char";
assert validateChar(s.charAt(1)) : s.charAt(0)+" should be a valid char";
assert validateChar(s.charAt(2)) : s.charAt(0)+" should be a valid char";
return "Formatted String";
}
else {
return s;
}
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String str=sc.next();
System.out.println(new Combine().CheckString(str));
sc.close();
}
public boolean validateRow(String row){
// if the length of the row is not 3
if(row.length()!=3) {
return false;
}
// check all charatcters are valid
else if(validateChar(row.charAt(0))==true && validateChar(row.charAt(1))==true && validateChar(row.charAt(2))==true)
// return true
return true;
else
// else return false
return false;
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
