This is a Java question Write the class Password which contains a constructor an
ID: 3678021 • Letter: T
Question
This is a Java question
Write the class Password which contains a constructor and 4 methods:
? The shortPassword method returns a random String password which consists of 3 lower case letters.
? The longPassword method returns a random String password of 25 characters. Each character in the password can be a lower case letter, upper case letter, or digit.
? The nextPassword method returns a String password which consists of 3 lower case letters. This method has the property that if it is called enough times, it will return every possible 3 lower case letter passwords exactly once without repeating any. You can generate the passwords in any order. Once all the passwords have been generated, return “???”.
? The reset method returns nothing. This method re-initializes things for the nextPassword method so that nextPassword can re-start generating passwords from the beginning.
Explanation / Answer
Code:
class TestClass {
public static void main(String args[] ) throws Exception {
System.out.println("Hello World!");
Password pw = new Password();
int count=0;
System.out.println("A short password :"+ pw.shortPassword());
System.out.println("Another short password :"+pw.shortPassword());
System.out.println("A long password :"+pw.longPassword());
System.out.println("Another long password : "+pw.longPassword());
System.out.println();
System.out.println("Assume i am trying to break into a system which has the password dog");
while(!pw.nextPassword().equals("dog"))
count++;
System.out.println("It would took me "+count+" guesses to break in.");
System.out.println();
System.out.print("Total number of possible 3 lower case letter password: ");
count=0;
pw.reset();
while(!pw.nextPassword().equals("???"))
count++;
System.out.println(count);
}
}
class Password
{
String[] pas;
int pos;
private char[] l={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','0','1','2','3','4','5','6','7','8','9'};
Password()
{
pas=new String[17576];
for(int i=0;i<26;i++)
{
for(int j=0;j<26;j++)
{
for(int k=0;k<26;k++)
{
pas[i*26*26+j*26+k]=""+l[i]+l[j]+l[k];
}
}
}
shuffle();
/* for(int i=0;i<17576;i++)
{
System.out.println(pas[i]);
}
*/
pos=0;
}
private void shuffle()
{
int i,j;
String temp;
for(i=0;i<17576;i++)
{
j=(int)(Math.random()*17576);
temp = pas[i];
pas[i]=pas[j];
pas[j]=temp;
}
}
public String shortPassword()
{
return pas[(int)(Math.random()*17576)];
}
public String longPassword()
{
int j;
String res="";
for(int i=0;i<25;i++)
{
res+=l[(int)(Math.random()*62)];
}
return res;
}
public String nextPassword()
{
if(pos>17575)
return "???";
return pas[pos++];
}
public void reset()
{
shuffle();
pos=0;
}
}
Output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.