Using loops with the String and Character classes. You can also use the StringBu
ID: 3589231 • Letter: U
Question
Using loops with the String and Character classes. You can also use the StringBuilder class to concatenate on the error messages. Floating point literals can be expressed as digits with one decimal point or using scientific notation. The only valid characters are digits (0 through 9) At most one decimal point. At most one occurrence of the letter ‘E’ At most two positive or negative signs. examples of valid expressions: 3.14159 -2.54 2.453E3 66.3E-5 Write a class definition file that has a String field to hold a possible floating point number. Name this class “ValidateFloat”. Write a method in ValidateFloat that returns true if the String is a valid floating point literal and false if it is not valid. Use other methods in your ValidateFloat class to create a modular solution. Write a Driver class with main( ) that prompts the user to enter candidate strings and prints a message stating whether the entry is a valid floating point number or not. For 10 additional extra credit points, print all the reasons why an expression that is not valid fails the tests. Remember, all the printing is done in main( ). The program ends when the user enters “quit”. Note: the Driver class does all the printing. The ValidateFloat class does no printing. You will be graded on: If the code does not compile the grade is a zero. documentation- Javadoc, inline comments, conventions, proper use of white space, Use of two classes with the classes each performing their separate tasks as described above. (for example- Driver does all input/output, ValidateFloat does all the validation of the String) Style of code- readability, clearness Object oriented design
Explanation / Answer
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
*
* @author Sam
*/
public class ValidateFloat {
public static String validate(String floatNumber) {
boolean dotPresent;
dotPresent = false;
int i;
if (floatNumber.charAt(0) == '-')
i = 1;
else if (floatNumber.charAt(0) >= '0' && floatNumber.charAt(0) <= '9')
i = 0;
else
return floatNumber.charAt(0) + " is not a valid character at start";
for (;i < floatNumber.length(); i++){
if (floatNumber.charAt(i) == '.')
if (dotPresent)
return "Float number does not support multiple dots";
else
dotPresent = true;
else if (floatNumber.charAt(i) == 'E')
return validateInteger(floatNumber, i+1);
else if (floatNumber.charAt(i) < '0' || floatNumber.charAt(i)>= '9')
return floatNumber.charAt(i) + " is not a valid character at position " + i;
}
return null;
}
private static String validateInteger(String floatNumber, int i) {
if (floatNumber.charAt(i) == '-')
i++;
for (;i < floatNumber.length(); i++)
if (floatNumber.charAt(i) < '0' || floatNumber.charAt(i)>= '9')
return floatNumber.charAt(i) + " is not a valid character after 'E' at position " + i;
return null;
}
}
class FloatValidateDriver {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String input, err;
while (true) {
input = br.readLine();
if (input.equals(""))
continue;
if (input.equalsIgnoreCase("quit"))
return;
err = ValidateFloat.validate(input);
if (err == null)
System.out.println(input + " is a valid float number.");
else
System.out.println(input + " is not a valid float because " + err);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.