In Java Write a program that checks the properness of a given variable name. More specifically, your program should specify whether a user-entered variable name is (1) illegal, (2) legal but uses poor style, or (3) good. There are different opinions as to what constitutes a good style for a variable name. For this program, check for good style using these rules: · Only use letters and digits. · Use a lowercase letter for the first character. You do not need to check for an uppercase letter for the first letter in the second and third words, ...etc. Your program should repeatedly prompt the user for variable names until the user enters "q" or "Q." the program terminates when the user enters the "q" or "Q." Sample session: This program checks the properness of a proposed Java variable name. Enter a variable name (q or Q to quit): streetAddress2 Good! Enter a variable name (q or Q to quit): street address2 Illegal. Enter a variable name (q or Q to quit): StreetAddress2 Legal, but uses poor style. Enter a variable name (q or Q to quit): 2ndStreetAddress Illegal. Enter a variable name (q or Q to quit): street$address$2 Legal, but uses poor style. Enter a variable name (q or Q to quit): q public class VariableNameChecker { public static void main (String [] args) { String varName; Scanner stdIn = new Scanner (System.in); //prompt the user to enter a variable name boolean valid = VariableChecker(varName); // to check if it it legal if (valid) { // your code to check for good style } else{ // illegal } //repeatedly prompt the user for variable names until the user enters "q" or "Q. }// end main public static boolean VariableChecker(String varName) { // your code } // return your variable. } // end class
In Java
Write a
specifically, your program should specify whether a user-entered variable name
is (1) illegal, (2) legal but uses poor style, or (3) good. There are different
opinions as to what constitutes a good style for a variable name. For this
program, check for good style using these rules:
· Only use letters and digits.
· Use a lowercase letter for the first character.
You do not need to check for an uppercase letter for the first letter in the second
and third words, ...etc.
Your program should repeatedly prompt the user for variable names until the user
enters "q" or "Q." the program terminates when the user enters the "q" or "Q."
Sample session:
This program checks the properness of a proposed Java variable name.
Enter a variable name (q or Q to quit): streetAddress2
Good!
Enter a variable name (q or Q to quit): street address2
Illegal.
Enter a variable name (q or Q to quit): StreetAddress2
Legal, but uses poor style.
Enter a variable name (q or Q to quit): 2ndStreetAddress
Illegal.
Enter a variable name (q or Q to quit): street$address$2
Legal, but uses poor style.
Enter a variable name (q or Q to quit): q
public class VariableNameChecker
{
public static void main (String [] args)
{
String varName;
Scanner stdIn = new Scanner (System.in);
//prompt the user to enter a variable name
boolean valid = VariableChecker(varName); // to check if
it it legal
if (valid)
{
// your code to check for good style
}
else{
// illegal
}
//repeatedly prompt the user for variable names until the
user enters "q" or "Q.
}// end main
public static boolean VariableChecker(String varName)
{
// your code
} // return your variable.
} // end class
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 2 images