I have a java program I am trying to complete. The class instance field text is
ID: 3771078 • Letter: I
Question
I have a java program I am trying to complete.
The class instance field text is an array of strings containg:
user ID
password
email address
state (two upper letters)
and zip code (five decimal digits).
Mutators of the class are:
setUserID – changes the user ID.
setPsw – changes the user password. The password must be strong password, created acording the rules (see below the definition for a strong password).
setEmail – changes the email address.
setState – changes the state (two upper letters).
setZip – changes the zip code (five decimal digits).
Selectors of the class are:
getUserID – returns the user ID.
getPsw – returns the user password.
getEmail – returns the email address.
getState – returns the state.
getZip – returns the zip code.
toSting - returns all data of a user as one string.
genStrongPsw - generates and returns a strong password with len characters in length.
isStrong – returns true if the password is strong and it returns false if the password is not strong.
Definition: A password is strong if it satisfies the following rules:
It must be at least eight characters in length.
There are two decimal digits in the first eight characters: 0 9
There is at least one lowercase letter: a z
There is at least one uppercase letter: A Z
There is least one special character: { $ . ! % ^ * }
This is the UML diagram:
---------------------------
ProcPsw
- text : String[]
+ ProcPsw(txt : String[])
+ setUserID(uid:String):void
+ setPsw(psw:String):void
+ setEmail(eMail:String):void
+ setState(st:String):void
+ setZip(zip:String):void
+ getUserID(): String
+ getPsw(): String
+ getEmail(): String
-----------------------------
------------------------------------------------
+ getState(): String
+ getZip():String
+ toSting(): String
+ genStrongPsw(int len): String
+ isStrong(): boolean
------------------------------------
Explanation / Answer
/**
* The java class ProcPsw that contains methods
* setter and getter methods to set id,password, email,state and zip.
* The class contains method to check strong password and
* to generate a strong password. The toStrong
* method that returns string array as string return type
* */
//ProcPsw.java
import java.util.Random;
public class ProcPsw
{
final int size=5;
private String text[]=new String[size];
//Constructor that takes an array of string type
public ProcPsw(String text[])
{
this.text=text;
setUserID(text[0]);
setPsw(text[1]);
setEmail(text[2]);
setState(text[3]);
setZip(text[4]);
}
//Set zip
public void setZip(String zip)
{
this.text[4]=zip;
}
//Set state
public void setState(String st)
{
this.text[3]=st;
}
//Set email
public void setEmail(String eMail)
{
this.text[2]=eMail;
}
//Set password
public void setPsw(String psw)
{
this.text[1]=psw;
}
//Set user id
public void setUserID(String uid)
{
this.text[0]=uid;
}
//Returns zip
public String getZip()
{
return text[4];
}
//Returns state
public String getState()
{
return text[3];
}
//Returns email
public String getEmail()
{
return text[2];
}
//Returns pass word
public String getPsw()
{
return text[1];
}
//Returns user id
public String getUserID()
{
return text[0];
}
//Returns string reprsentation of ProcPsw object
@Override
public String toString()
{
return "User ID :"+text[0]+
" Password :"+text[1]+
" Email :"+text[2]+
" State :"+text[3]+
" Zip :"+text[4];
}
/*The method genStrongPsw that takes lenght as input argument
* that generates a strong password and returns strong password
* of fiven lenght*/
public String genStrongPsw(int len)
{
Random rand=new Random();
char[] password=new char[len];
int dec1=rand.nextInt(10);
password[0]=(char)(dec1+49);
int dec2=rand.nextInt(10);
password[1]=(char)(dec2+49);
char lowercase= (char)('a' + Math.random() * 26);
password[2]=lowercase;
char uppercase= (char)('A' + Math.random() * 26);
password[3]=uppercase;
char symbols[]={'$','.','%','^','*'};
char symbol= symbols[rand.nextInt(symbols.length)];
password[4]=symbol;
for (int i = 5; i < len; i++)
{
lowercase= (char)('a' + Math.random() * 26);
password[i]=lowercase;
}
return String.valueOf(password);
}
/*The method isStrong that check if the password is strong
otherwise returns false */
public boolean isStrong()
{
boolean strong=true;
String password=text[1];
if(password.length()!=8)
strong=false;
else
{
int decimalCount=0;
int lowercaseCount=0;
int uppercaseCount=0;
int specialChar=0;
for (int i = 0; i < password.length();
i++)
{
char ch=password.charAt(i);
if(Character.isDigit(ch))
decimalCount++;
else if(Character.isLowerCase(ch))
lowercaseCount++;
else if(Character.isUpperCase(ch))
uppercaseCount++;
else if(ch=='$'||ch=='.'||
ch=='!'||ch=='%'||
ch=='^'||ch=='*')
specialChar++;
}
if(decimalCount<2)
strong=false;
if(lowercaseCount<1)
strong=false;
if(uppercaseCount<1)
strong=false;
if(specialChar<1)
strong=false;
}
return strong;
}
}
----------------------------------------------------------------
//Driver class to test methods of ProcPsw class
//PasswordDriver.java
public class PasswordDriver
{
public static void main(String[] args)
{
//Create an array of string type of size 5
String text[]=new String[5];
//set fields
text[0]="1234";
text[1]="12A$abc";
text[2]="abc@dummy.com";
text[3]="Michigon";
text[4]="452452";
//Create an instance of ProcPsw with string array
ProcPsw proc=new ProcPsw(text);
System.out.println("ProcPsw object");
//Print proc object
System.out.println(proc.toString());
//Check if password is strong
System.out.println("Is Password valid ?"+proc.isStrong());
System.out.println("Generating strong password");
//Generate a password of lenght 8
String password=proc.genStrongPsw(8);
System.out.println("Strong Password :"+password);
//Set strong password
text[1]=password;
System.out.println("Is Password valid ?"+proc.isStrong());
System.out.println("ProcPsw object");
System.out.println(proc.toString());
}
}
------------------------------------------------------------------------------
Sample Output :
ProcPsw object
User ID :1234
Password :12A$abc
Email :abc@dummy.com
State :Michigon
Zip :452452
Is Password valid ?false
Generating strong password
Strong Password :53xE^upe
Is Password valid ?true
ProcPsw object
User ID :1234
Password :53xE^upe
Email :abc@dummy.com
State :Michigon
Zip :452452
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.