JAVA (20 points) Complete the body of main so it prompts the user for a password
ID: 3789085 • Letter: J
Question
JAVA
(20 points) Complete the body of main so it prompts the user for a password candidate until either the password candidate is the empty string, or the password candidate is “acceptable” according to isOK. (In our formal specifications, for a mathematical string such as pwd, |pwd| means the length of pwd.)
/** * Checks whether the given string satisfies the criteria for * an acceptable password. *
* @param pwd * the string to check for acceptability
* @requires * |pwd| > 0 * @ensures * isOK = [pwd satisfies the criteria for an acceptable password]
*/ private static boolean isOK(String pwd)
{...}
/** * Main method. * * @param args * the command line arguments */
public static void main(String[] args)
{
SimpleReader in = new SimpleReader1L();
SimpleWriter out = new SimpleWriter1L();
in.close();
out.close();
}
Explanation / Answer
Hi, You have not posted SimpleReader and SimpleWriter class, So I have implemented using Scanner class.
Please let me know in case of any issue
public class test {
/** * Checks whether the given string satisfies the criteria for * an acceptable password. *
* @param pwd * the string to check for acceptability
* @requires * |pwd| > 0 * @ensures * isOK = [pwd satisfies the criteria for an acceptable password]
*/ private static boolean isOK(String pwd)
{
if(pwd != null && pwd.length() > 0)
return true;
return false;
}
/** * Main method. * * @param args * the command line arguments */
public static void main(String[] args)
{
//SimpleReader in = new SimpleReader1L();
//SimpleWriter out = new SimpleWriter1L();
Scanner sc = new Scanner(System.in);
String pwd = sc.next();
while(!isOK(pwd))
pwd = sc.next();
}
//in.close();
//out.close();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.