Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Do NOT use any buffers(haven\'t learned those yet!!!) PLEASE use stringBuilders

ID: 3591218 • Letter: D

Question

Do NOT use any buffers(haven't learned those yet!!!)
PLEASE use stringBuilders and appends where possible!
..000 Verizon 1:46 AM * 100%-. Need this done in java please comment thoroughly, I need to understand what is happening at crucial parts of the program. Using loops with the String and Character classes. You can also use the StrimgBuilder 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 453E3 663E-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 ValidateFleat 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.

Explanation / Answer

public class ValidateFloat {

private String value;

private String errorMsg;

public ValidateFloat(String num)

{

value = num;

}

public boolean isValid()

{

errorMsg = "";

StringBuffer errors = new StringBuffer("");

int signs = 0, numE = 0, numDeciPt = 0;

boolean sing2beforeE = false;

for(int i = 0; i < value.length(); i++)

{

char ch = value.charAt(i);

if(!Character.isDigit(ch)) //if its not a digit

{

if(ch == '.')

{

numDeciPt++;

if(numDeciPt > 1)

errors.append("Decimal point already used. Not allowed again at index " + i + " ");

}

else if(ch == 'E')

{

numE++;

if(numE > 1)

errors.append("Letter E already used. Not allowed again at index " + i + " ");

}

else if(ch == '+' || ch == '-')

{

signs++;

if(signs == 2 && numE == 0) //check if the 2nd sign is coming before an E appears, then its error

{

errors.append("sign " + ch + " at index " + i + " appearing before E ");

}

else if (signs > 2)

errors.append("More than 2 signs. " + ch + " appearing at index " + i + " ");

}

else

errors.append("Invalid character " + ch + " index " + i + " ");

}

}

errorMsg = errors.toString();

if(errorMsg.isEmpty())

return true;

else

return false;

}

public String getErrorMessage()

{

return errorMsg;

}

public String getValue()

{

return value;

}

}

public class FloatTest {

private static void check(ValidateFloat f)

{

System.out.print(" Validating " + f.getValue());

if(f.isValid())

System.out.print(" <=> Valid");

else

{

System.out.println(" <=> NOT valid");

System.out.println(f.getErrorMessage());

}

System.out.println();

}

public static void main(String[] args) {

ValidateFloat f1 = new ValidateFloat("3.14159");

ValidateFloat f2 = new ValidateFloat("-2.54");

ValidateFloat f3 = new ValidateFloat("2.453E3");

ValidateFloat f4 = new ValidateFloat("66.3E-5");

ValidateFloat f5 = new ValidateFloat("+3.12+42+E-5");

ValidateFloat f6 = new ValidateFloat("1.44.77");

ValidateFloat f7 = new ValidateFloat("4.5E69%E3");

check(f1);

check(f2);

check(f3);

check(f4);

check(f5);

check(f6);

check(f7);

}

}

output


Validating 3.14159 <=> Valid

Validating -2.54 <=> Valid

Validating 2.453E3 <=> Valid

Validating 66.3E-5 <=> Valid

Validating +3.12+42+E-5 <=> NOT valid
sign + at index 5 appearing before E
More than 2 signs. + appearing at index 8
More than 2 signs. - appearing at index 10

Validating 1.44.77 <=> NOT valid
Decimal point already used. Not allowed again at index 4

Validating 4.5E69%E3 <=> NOT valid
Invalid character % index 6
Letter E already used. Not allowed again at index 7

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote