Java Object oriented design Use loops with the String and C

Java -- Object oriented design Use loops with the String and Character classes. 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 file that has a String field to hold a possible floating point number. Name this class ValidateFloat. Write a method that returns true if the String is a valid floating point literal and false if it is not valid. Use other methods in your class to create a modular solution. Write a Driver class with main( ) that prompts the user to enter a candidate string and prints a message stating whether the entry is a valid floating point number or not. For extra credit, print the reasons why an expression that is not valid fails the tests. Use the StringBuilder class to concatenate on the error messages. Note: the Driver class does all the printing. The ValidateFloat class does no printing.

Solution

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author USER
*/

import java.util.*;

public class ValidateFloat
{
String str1;

public static boolean isNumberWith2Decimals(String string)
{
if(string.matches(\"^\\\\d+\\\\.\\\\d{2}$\"))
{
return true;
}
return false;
}
  
public static boolean isNumeric(String string)
{
  
if(string.matches(\"^[-+]?\\\\d+(\\\\.\\\\d+)?$\"))
{
return true;
}
return false;
}

public static boolean isNumber(String string)
{
if(string.matches(\"^\\\\d+$\"))
{
return true;
}
return false;
}

String flag=\"false\";

public String validate(String str)
{


for(int i=0;i<str.length();i++)
{
if(str.matches(\"[ 0-9 A-Z a-z _]+ - .\"))
{
flag=\"true\";
}
else
{
flag=\"false\";
}
}
  
if(flag==\"true\")
{
return \"true\";
}
else
return \"false\";

}


}


class demo
{
public static void main(String args[])
{
String s;
System.out.println(\"enter the literal:\");
Scanner scan=new Scanner(System.in);
s=scan.next();
  
ValidateFloat v=new ValidateFloat();
v.validate(s);
boolean b= v.isNumberWith2Decimals(s);
boolean b1=v.isNumeric(s);
if(b&&b1)
{
System.out.println(\"valid lietral\");
}
else
{
System.out.println(\"not valid literal\");
}
  
}
}

Java -- Object oriented design Use loops with the String and Character classes. Floating point literals can be expressed as digits with one decimal point or usi
Java -- Object oriented design Use loops with the String and Character classes. Floating point literals can be expressed as digits with one decimal point or usi

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site