Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hey guys having some trouble with code, objective is to have two MasterPassword

ID: 3637888 • Letter: H

Question

Hey guys having some trouble with code, objective is to have two MasterPassword objects, one set as default guest and the other a copy of the guest named manager. You then are to compare the username with the password entered to ensure that the password is valid, (not containing the username in uppercase or lowercase form), and then have a way to compare the guest and manager to report whether or not they are equal. I have the bulk of the code completed, but have hit a road bump with the password validation. I have entered the code below so i would recommend copy and paste to see the problems.


public class MasterPassword {

private String userName;
private String password;

//constructor that accepts two string arguments
public MasterPassword (String userName, String password){
this.userName = userName;
this.password = password;
}

//copy constructor
public MasterPassword (MasterPassword pMasterPassword){
this(pMasterPassword.userName, pMasterPassword.password);
}


//default constructor
public MasterPassword () {
userName = "guest";
password = " ";
}

//method to set UserName
public void setUserName (String uName){
userName = uName;
}

//method to set PassWord
public void setPassword(String pWord){
password = pWord;


}

//toString method to return values in Username and Password
public String toString(){
String logIn = "Username: " + userName + " Password: " + password;
return logIn;
}

//deep copy of MasterPassword Object
public MasterPassword copy(){
MasterPassword copyMasterPassword = new MasterPassword(userName,password);
return copyMasterPassword;
}

//equals method to compare submitted MasterPassword Objects username
//and password with calling objects username and password

public boolean equals(MasterPassword mw){
if(password.equals(mw) && userName.equals(mw))
{
System.out.println("Equal");
return true;
}
else
{
System.out.println("Not Equal");
return false;
}
}

//valid boolean method to check to see if Password contains user name inside
//if username is found in password return false
public boolean valid(){
{
if (password.startsWith(userName))
{
System.out.println("Invalid Password, can not be the same as User Name.");
return false;
}
else
return true;
}
}
}


THIS IS THE DEMO CLASS BELOW TO TEST THE PROGRAM

import java.util.Scanner;

public class MasterPasswodDemo {

static Scanner kb = new Scanner(System.in);

public static void main(String[] args) {
//Create two MasterPassword Objects
MasterPassword guest = new MasterPassword();
guest.setPassword("csci1302");
MasterPassword manager = new MasterPassword(guest);
manager.setUserName("jsmith");

//get input from manager to set new password
System.out.println("Manager enter new password: ");
String pwd = kb.nextLine();
manager.setPassword(pwd);

//check to see if manager user and password equal guest user and password
manager.equals(guest);

//Print out Guest User and Password and Manager User and Password
System.out.println("Guest Info: " + guest);
System.out.println(" Manager Info: " + manager);

}

}

Explanation / Answer

attached is the code. There were two (well, three) issues. The one with the password validation is due to the fact that: 1: first you need to make both the username and the password lowercase (username of jsmith and password JSMITH should be an error) 2: you cannot just do "starts with" because with a username of jsmith and a password of abcjsmith, the password still contains the username which the assignment does not allow. By using indexOf, you can check the entire String. The second issue was with the user comparison (username and password are equal). you were comparing the password String with the entire other MasterPassword object- same with the username the password and username strings is very different than the entire object :-). Instead, you need to write getter methods in MasterPassword.java, and then compare the current password with the other MasterPassword's password, and the same with the username. The third: There's a typo in the class name :-). you missed the r in MasterPasswordDemo class. Here is the code solving the issues: File 1: MasterPassword.java public class MasterPassword { private String userName; private String password; //constructor that accepts two string arguments public MasterPassword (String userName, String password){ this.userName = userName; this.password = password; } //copy constructor public MasterPassword (MasterPassword pMasterPassword){ this(pMasterPassword.userName, pMasterPassword.password); } //default constructor public MasterPassword () { userName = "guest"; password = " "; } //method to set UserName public void setUserName (String uName){ userName = uName; } //method to set PassWord public void setPassword(String pWord){ password = pWord; } //returns the username public String getUserName(){ return userName; } //returns the password public String getPassword(){ return password; } //toString method to return values in Username and Password public String toString(){ String logIn = "Username: " + userName + " Password: " + password; return logIn; } //deep copy of MasterPassword Object public MasterPassword copy(){ MasterPassword copyMasterPassword = new MasterPassword(userName,password); return copyMasterPassword; } //equals method to compare submitted MasterPassword Objects username //and password with calling objects username and password public boolean equals(MasterPassword mw){ if(password.equals(mw.getPassword()) && userName.equals(mw.getUserName())) { System.out.println("Equal"); return true; } else { System.out.println("Not Equal"); return false; } } //valid boolean method to check to see if Password contains user name inside //if username is found in password return false public boolean valid(){ { String lowercasePassword = password.toLowerCase(); String lowercaseUserName = userName.toLowerCase(); if (lowercasePassword.indexOf(lowercaseUserName) >= 0) { System.out.println("Invalid Password, can not contain the User Name."); return false; } else return true; } } } FIle 2: MasterPasswordDemo.java import java.util.Scanner; public class MasterPasswordDemo { static Scanner kb = new Scanner(System.in); public static void main(String[] args) { //Create two MasterPassword Objects MasterPassword guest = new MasterPassword(); guest.setPassword("csci1302"); MasterPassword manager = new MasterPassword(guest); manager.setUserName("jsmith"); //get input from manager to set new password boolean validPw = false; while (!validPw) { System.out.println("Manager enter new password: "); String pwd = kb.nextLine(); manager.setPassword(pwd); validPw = manager.valid(); } //check to see if manager user and password equal guest user and password manager.equals(guest); //Print out Guest User and Password and Manager User and Password System.out.println("Guest Info: " + guest); System.out.println(" Manager Info: " + manager); } }