OK THIS IS THE PASSWORD FILE THIS ONE IS OK BUT THE FILE IM GETTING THE ERRORS I
ID: 3887264 • Letter: O
Question
OK THIS IS THE PASSWORD FILE THIS ONE IS OK BUT THE FILE IM GETTING THE ERRORS IN IS THE USER FILE
/*
Chapter 9 The Password Class
Programmer:
Date: September 9, 2017
File Name: Password.java
Purpose: To provide a reusable Password Class
*/
import java.util.*;
public class Password
{
final static int MIN_SIZE = 6;
final static int MAX_SIZE = 15;
static int maxHistory = 4;
static int expiresNotifyLimit = 3;
private int maxUses = 120;
private int remainingUses = maxUses;
private boolean autoExpires = true;
private boolean expired = false;
private ArrayList pswdHistory;
//Constructors for objects of class Password
public Password(String newPassword) throws Exception
{
pswdHistory = new ArrayList(maxHistory);
set(newPassword);
}
public Password(String newPassword, int numMaxUses) throws Exception
{
pswdHistory = new ArrayList(maxHistory);
maxUses = numMaxUses;
remainingUses = numMaxUses;
set(newPassword);
}
public Password(String newPassword, boolean pswdAutoExpires) throws Exception
{
pswdHistory = new ArrayList(maxHistory);
autoExpires = pswdAutoExpires;
set(newPassword);
}
public Password(String newPassword, int numMaxUses, boolean pswdAutoExpires) throws Exception
{
pswdHistory = new ArrayList(maxHistory);
maxUses = numMaxUses;
remainingUses = numMaxUses;
autoExpires = pswdAutoExpires;
set(newPassword);
}
public boolean getAutoExpires()
{
return autoExpires;
}
public void setAutoExpires(boolean autoExpires)
{
this.autoExpires = autoExpires;
if(autoExpires)
remainingUses = maxUses;
}
public boolean isExpired()
{
return expired;
}
public void setExpired(boolean newExpired)
{
expired = newExpired;
}
public int getExpiresNotifyLimit()
{
return expiresNotifyLimit;
}
public void setExpiresNotifyLimit(int newNotifyLimit)
{
if(newNotifyLimit >= 2 && newNotifyLimit <= 20)
expiresNotifyLimit = newNotifyLimit;
}
public int getMaxHistory()
{
return maxHistory;
}
public void setMaxHistory(int newMaxHistory)
{
int overage = 0;
if(newMaxHistory >= 1 && newMaxHistory <= 10)
{
maxHistory = newMaxHistory;
overage = getHistorySize() - maxHistory;
if(overage > 0) // if size > max allowed
{
do {
pswdHistory.remove(0); // then remove overage number
overage--; // of oldest pswds from list
} while(overage > 0);
pswdHistory.trimToSize(); // resize capacity to max allowed
}
}
}
public int getRemainingUses()
{
return remainingUses;
}
public int getHistorySize()
{
return pswdHistory.size();
}
public boolean isExpiring()
{
boolean expiring = false;
if(autoExpires && remainingUses <= expiresNotifyLimit)
expiring = true;
return expiring;
}
// Sets password to a new value; keeps current & previous values in history up to max number
public void set(String pswd) throws Exception
{
String encryptPswd;
boolean pswdAdded = true;
pswd = pswd.trim();
verifyFormat(pswd);
encryptPswd = encrypt(pswd);
if(!pswdHistory.contains(encryptPswd))
{
if(pswdHistory.size() == maxHistory)
pswdHistory.remove(0);
pswdAdded = pswdHistory.add(encryptPswd);
if(!pswdAdded)
throw new Exception("Internal list error - Password not accepted");
if(expired)
expired = false;
if(autoExpires)
remainingUses = maxUses;
}
else
throw new Exception("Password recently used");
}
// Validates entered password against most recently saved value
public void validate(String pswd) throws Exception
{
String encryptPswd;
String currentPswd;
int currentPswdIndex;
verifyFormat(pswd); // verify password was entered properly
encryptPswd = encrypt(pswd); // convert to encrypted form
if(!pswdHistory.isEmpty()) // at least one password entry is in history
{
currentPswdIndex = pswdHistory.size()-1;
currentPswd = (String)pswdHistory.get(currentPswdIndex);
if(!encryptPswd.equals(currentPswd)) // if not most recent pswd
throw new Exception("Password is invalid");
if(expired)
throw new Exception("Password has expired - please change");
if(autoExpires)
{
--remainingUses;
if(remainingUses <= 0)
expired = true;
}
}
else
throw new Exception("No password on file - list corrupted!"); // Should never happen
}
// Verifies password has proper format
private void verifyFormat(String pswd) throws Exception
{
boolean numFound = false;
if(pswd.length() == 0)
throw new Exception("No password provided!");
if(pswd.length() < MIN_SIZE)
throw new Exception("Password must be at least "+MIN_SIZE+" characters in length");
if(pswd.length() > MAX_SIZE)
throw new Exception("Password cannot be greater than "+MAX_SIZE+" characters in length");
// scan through passord to find if at least 1 number is used
for(int i=0; i < pswd.length() && !numFound; ++i)
if(Character.isDigit(pswd.charAt(i)))
numFound = true;
if(!numFound)
throw new Exception("Passord is invalid - must have at least one numeric digit");
}
// Encrypts original password returning new encrypted String
private String encrypt(String pswd)
{
StringBuffer encryptPswd;
int pswdSize = 0;
int midpoint = 0;
int hashCode = 0;
// swap frst and last half of password
pswdSize = pswd.length();
midpoint = pswdSize/2;
encryptPswd = new StringBuffer(pswd.substring(midpoint) // get last half of pswd
+ pswd.substring(0,midpoint)); // and concatenate first half
encryptPswd.reverse(); // reverses order of characters in password
for(int i=0; i < pswdSize; ++i) // encrypt each character
encryptPswd.setCharAt(i, (char)(encryptPswd.charAt(i) & pswd.charAt(i)) );
hashCode = pswd.hashCode(); // hash code for original password
encryptPswd.append(hashCode);
return encryptPswd.toString();
}
} the user file is the one with the errors
/*
Chapter 10: The User Class
Programmer:
Date: September 16, 2017
Filename: User.java
Purpose: To provide a User class to test the Password class
*/
public class User
{
private String name;
private Password pswd;
public User(String aName, String password) throws PasswordException
{
name = new String(aName);
pswd = new Password(password);
}
public User(String aName, String password, int pswdUses) throws PasswordException
{
name = new String(aName);
pswd = new Password(password,pswdUses);
}
public User(String aName, String password, boolean autoExpirePswd) throws PasswordException
{
name = new String(aName);
pswd = new Password(password,autoExpirePswd);
}
public String getName()
{
return new String(name);
}
public boolean pswdAutoExpires()
{
return pswd.getAutoExpires();
}
public boolean pswdIsExpiring()
{
return pswd.isExpiring();
}
public int getPswdUses()
{
return pswd.getRemainingUses();
}
public void validate(String password) throws PasswordException
{
pswd.validate(password);
}
public void changePassword(String oldPassword, String newPassword) throws PasswordException
{
try
{
pswd.validate(oldPassword);
}
catch(PasswordExpiredException ex)
{}
pswd.set(newPassword);
}
} the errors are line 17,22,57,27,59 and 64 that reads error unreported exception Exception; must be caught or declared to be thrown
Explanation / Answer
You were getting errors because you did not create a custom exception called PasswordException. That is why the errors are Exception; must be caught or declared to be thrown. There was no implementation of this exception class. I implemented this exception class and included the necessary try-catch block statements in the User file. Now It is now showing any errors.
**PasswordException.java
public class PasswordException extends Exception {
PasswordException(String s){
super(s);
}
}
**User.java
public class User
{
private String name;
private Password pswd;
public User(String aName, String password) throws PasswordException
{
name = new String(aName);
try {
pswd = new Password(password);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public User(String aName, String password, int pswdUses) throws PasswordException
{
name = new String(aName);
try {
pswd = new Password(password,pswdUses);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public User(String aName, String password, boolean autoExpirePswd) throws PasswordException
{
name = new String(aName);
try {
pswd = new Password(password,autoExpirePswd);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public String getName()
{
return new String(name);
}
public boolean pswdAutoExpires()
{
return pswd.getAutoExpires();
}
public boolean pswdIsExpiring()
{
return pswd.isExpiring();
}
public int getPswdUses()
{
return pswd.getRemainingUses();
}
public void validate(String password) throws PasswordException
{
try {
pswd.validate(password);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void changePassword(String oldPassword, String newPassword) throws PasswordException
{
try
{
pswd.validate(oldPassword);
}
catch(Exception ex)
{}
try {
pswd.set(newPassword);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
**Password.java
import java.util.*;
public class Password
{
final static int MIN_SIZE = 6;
final static int MAX_SIZE = 15;
static int maxHistory = 4;
static int expiresNotifyLimit = 3;
private int maxUses = 120;
private int remainingUses = maxUses;
private boolean autoExpires = true;
private boolean expired = false;
private ArrayList pswdHistory;
//Constructors for objects of class Password
public Password(String newPassword) throws Exception
{
pswdHistory = new ArrayList(maxHistory);
set(newPassword);
}
public Password(String newPassword, int numMaxUses) throws Exception
{
pswdHistory = new ArrayList(maxHistory);
maxUses = numMaxUses;
remainingUses = numMaxUses;
set(newPassword);
}
public Password(String newPassword, boolean pswdAutoExpires) throws Exception
{
pswdHistory = new ArrayList(maxHistory);
autoExpires = pswdAutoExpires;
set(newPassword);
}
public Password(String newPassword, int numMaxUses, boolean pswdAutoExpires) throws Exception
{
pswdHistory = new ArrayList(maxHistory);
maxUses = numMaxUses;
remainingUses = numMaxUses;
autoExpires = pswdAutoExpires;
set(newPassword);
}
public boolean getAutoExpires()
{
return autoExpires;
}
public void setAutoExpires(boolean autoExpires)
{
this.autoExpires = autoExpires;
if(autoExpires)
remainingUses = maxUses;
}
public boolean isExpired()
{
return expired;
}
public void setExpired(boolean newExpired)
{
expired = newExpired;
}
public int getExpiresNotifyLimit()
{
return expiresNotifyLimit;
}
public void setExpiresNotifyLimit(int newNotifyLimit)
{
if(newNotifyLimit >= 2 && newNotifyLimit <= 20)
expiresNotifyLimit = newNotifyLimit;
}
public int getMaxHistory()
{
return maxHistory;
}
public void setMaxHistory(int newMaxHistory)
{
int overage = 0;
if(newMaxHistory >= 1 && newMaxHistory <= 10)
{
maxHistory = newMaxHistory;
overage = getHistorySize() - maxHistory;
if(overage > 0) // if size > max allowed
{
do {
pswdHistory.remove(0); // then remove overage number
overage--; // of oldest pswds from list
} while(overage > 0);
pswdHistory.trimToSize(); // resize capacity to max allowed
}
}
}
public int getRemainingUses()
{
return remainingUses;
}
public int getHistorySize()
{
return pswdHistory.size();
}
public boolean isExpiring()
{
boolean expiring = false;
if(autoExpires && remainingUses <= expiresNotifyLimit)
expiring = true;
return expiring;
}
// Sets password to a new value; keeps current & previous values in history up to max number
public void set(String pswd) throws Exception
{
String encryptPswd;
boolean pswdAdded = true;
pswd = pswd.trim();
verifyFormat(pswd);
encryptPswd = encrypt(pswd);
if(!pswdHistory.contains(encryptPswd))
{
if(pswdHistory.size() == maxHistory)
pswdHistory.remove(0);
pswdAdded = pswdHistory.add(encryptPswd);
if(!pswdAdded)
throw new Exception("Internal list error - Password not accepted");
if(expired)
expired = false;
if(autoExpires)
remainingUses = maxUses;
}
else
throw new Exception("Password recently used");
}
// Validates entered password against most recently saved value
public void validate(String pswd) throws Exception
{
String encryptPswd;
String currentPswd;
int currentPswdIndex;
verifyFormat(pswd); // verify password was entered properly
encryptPswd = encrypt(pswd); // convert to encrypted form
if(!pswdHistory.isEmpty()) // at least one password entry is in history
{
currentPswdIndex = pswdHistory.size()-1;
currentPswd = (String)pswdHistory.get(currentPswdIndex);
if(!encryptPswd.equals(currentPswd)) // if not most recent pswd
throw new Exception("Password is invalid");
if(expired)
throw new Exception("Password has expired - please change");
if(autoExpires)
{
--remainingUses;
if(remainingUses <= 0)
expired = true;
}
}
else
throw new Exception("No password on file - list corrupted!"); // Should never happen
}
// Verifies password has proper format
private void verifyFormat(String pswd) throws Exception
{
boolean numFound = false;
if(pswd.length() == 0)
throw new Exception("No password provided!");
if(pswd.length() < MIN_SIZE)
throw new Exception("Password must be at least "+MIN_SIZE+" characters in length");
if(pswd.length() > MAX_SIZE)
throw new Exception("Password cannot be greater than "+MAX_SIZE+" characters in length");
// scan through passord to find if at least 1 number is used
for(int i=0; i < pswd.length() && !numFound; ++i)
if(Character.isDigit(pswd.charAt(i)))
numFound = true;
if(!numFound)
throw new Exception("Passord is invalid - must have at least one numeric digit");
}
// Encrypts original password returning new encrypted String
private String encrypt(String pswd)
{
StringBuffer encryptPswd;
int pswdSize = 0;
int midpoint = 0;
int hashCode = 0;
// swap frst and last half of password
pswdSize = pswd.length();
midpoint = pswdSize/2;
encryptPswd = new StringBuffer(pswd.substring(midpoint) // get last half of pswd
+ pswd.substring(0,midpoint)); // and concatenate first half
encryptPswd.reverse(); // reverses order of characters in password
for(int i=0; i < pswdSize; ++i) // encrypt each character
encryptPswd.setCharAt(i, (char)(encryptPswd.charAt(i) & pswd.charAt(i)) );
hashCode = pswd.hashCode(); // hash code for original password
encryptPswd.append(hashCode);
return encryptPswd.toString();
}
}
**Comment for any further help
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.