Users are required to enter the correct passcode to start the application. The s
ID: 3815373 • Letter: U
Question
Users are required to enter the correct passcode to start the application. The system uses a scramble keypad with a randomly allocated set of numbers from 0 to 9. The display shall show "Enter passcode" initially, and show an asterisk (*) for each number entered. Upon pushing the "Enter" button, the system verifies the passcode. If the passcode is correct, the system invokes a method called startApp() to start the application. Otherwise, it displays "Wrong passcode". The "Clear" button shall clear the display. Assume that the following methods are available:
// return the passcode
public String getPasscode(){
//Random rnd = new Random();
//String passcode4digit = rnd.nextInt(10) + “” + rnd.nextInt(10) + “” + // rnd.nextInt(10) + “” + rnd.nextInt(10);
String passcode4digit =”7312”;
return passcode4digit;
}
// Start the application public void startApp(){
System.out.println(“Application started”); }
// Shuffle (Randomize) the given int array, e.g.,
// int[] numbers = {1, 2, 3, 4, 5};
// shuffleArray(numbers);
// randomize the elements
public void shuffleArray(int[] ar)
{
Random rnd = new Random();
for (int i = ar.length - 1; i > 0; i--) {
int index = rnd.nextInt(i + 1);
// Simple swap
int a = ar[index];
ar[index] = ar[i];
ar[i] = a;
}
}
Explanation / Answer
Okay so for this lets write main:
// This is a password masking based Authentication
// start a thread that wuld make asterisk appear in the password field.
// make other thins work as usual
//To get the value of password, use:
// String.valueOf(password);
PSB code snippet:
import java.io.*;
public class PwdMask {
public static void main(String[] args) {
char password[] = null;
try {
passcode = PasscodeClass.retreive(System.in, "Enter passcode: ");
} catch(IOException io) {
io.printStackTrace();
}
if(String.valueOf(password)=="passcode") // define your own and act acoordingly
{
}
}
}
}
---Now Passcode.class whih would implement ther class thread----
public class PasswordField {
public static final char[] getPassword(InputStream in, String message) throws IOException {
PutAsterik ast = new PutAsterisk(message);
Thread thread = new Thread(ast);
thread.start(); // for printing asterisks
//...print messgae and do comparing
//stop masking here
}
}
---------------------PutAsterisk class--------------
class PutAsterisk extends Thread {
public PutAsterisk(String msg) {
System.out.print(msg);
}
public void run() {
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
try {
while(stop) {
System.out.print("*");
try {
Thread.currentThread().sleep(1);//for producing delay
}catch (InterruptedException iex) {
Thread.currentThread().interrupt();
return;
}
}
} finally {
Thread.currentThread().setPriority(priority);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.