Question

Create a simple Java class for a Password with the following requirements:  This program will...

Create a simple Java class for a Password with the following requirements:


 This program will have a header block comment with your name, the course and section, as well as a brief description of what the class does.
 One String property: password (protected to only allow secure passwords)
o A secure password must be at least 8 characters in length
o A secure password must have three of the following four requirements:
 A lower case letter
 An upper case letter
 An integer digit (0-9)
 A keyboard producible symbol (e.g. $, @, |, >)
 A constructor that takes no arguments, and sets the password to: 1newPassword.
 A second constructor that takes in a String argument to set the initial password value for the Password object. Use data protection to ensure the user entered a secure password.
 A setPassword() method that takes in a String argument and uses data protection to ensure the user entered a secure password.
 A getPassword() method that returns the password as a String.
 A toString() method that returns the password as a String (it may call the getPassword() method).
 An equals() method that takes in another Password object as an argument, and then determines if the two objects have the same password value.
 All data protection will throw an appropriate Exception


Create a simple program using Java that demonstrates the Password class with the following requirements:


 Creates a Password object using the no argument constructor.
 Creates a second Password object using the constructor that takes in a String argument.
 Additionally, use either a do or while loop to get the user to enter a secure password using the setPassword() method on the 1st Password object. The loop will continue until they enter a valid secure Password.
 The program will display the password value for both of the objects using the toString() method.
 The program will indicate whether the two password values are the same using the equals() method.
 All thrown Exceptions will handled.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.Objects;

public class Password {
private String password;

Password(){
this.password = "1newPassword";
}

Password(String password){
int upper = 0, lower = 0, digit = 0, special = 0;
for(int i =0;i<password.length();i++){
char c = password.charAt(i);
if(Character.isUpperCase(c)){
upper++;
}
if(Character.isLowerCase(c)){
lower++;
}
if(Character.isDigit(c)){
digit++;
}
if(c>=33&&c<=46||c==64){
special++;
}
}
try {
if(special>=1&&lower>=1&&upper>=1&&digit>=1&&password.length() >= 8){
this.password = password;
}else if(special<1){
throw new IllegalArgumentException("Password must contain atleast one special character");
}else if(lower<1){
throw new IllegalArgumentException("Password must contain atleast one lower case letter");
}else if(upper<1){
throw new IllegalArgumentException("Password must contain atleast one upper case letter");
}else if(digit<1){
throw new IllegalArgumentException("Password must contain atleast one digit");
}
}catch (IllegalArgumentException e){
System.out.println(e);
}

}

public String getPassword() {
return password;
}

public void setPassword(String password) {
int upper = 0, lower = 0, digit = 0, special = 0;
for(int i =0;i<password.length();i++){
char c = password.charAt(i);
if(Character.isUpperCase(c)){
upper++;
}
if(Character.isLowerCase(c)){
lower++;
}
if(Character.isDigit(c)){
digit++;
}
if(c>=33&&c<=46||c==64){
special++;
}
}
try {
if(special>=1&&lower>=1&&upper>=1&&digit>=1&&password.length() >= 8){
this.password = password;
}else if(special<1){
throw new IllegalArgumentException("Password must contain atleast one special character");
}else if(lower<1){
throw new IllegalArgumentException("Password must contain atleast one lower case letter");
}else if(upper<1){
throw new IllegalArgumentException("Password must contain atleast one upper case letter");
}else if(digit<1){
throw new IllegalArgumentException("Password must contain atleast one digit");
}
}catch (IllegalArgumentException e){
System.out.println(e);
}
}

@Override
public String toString() {
return "Password: " + this.getPassword();
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Password password1 = (Password) o;
return Objects.equals(password, password1.password);
}

@Override
public int hashCode() {
return Objects.hash(password);
}
}
PasswordMain.java

import java.util.Scanner;

public class passwordMain {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Password password = new Password();
Password password1 = new Password("NewPAssword12@");

String pass;
boolean status = false;
do{
System.out.println("Enter a Password");
pass = sc.next();
int upper = 0, lower = 0, digit = 0, special = 0;
for(int i =0;i<pass.length();i++){
char c = pass.charAt(i);
if(Character.isUpperCase(c)){
upper++;
}
if(Character.isLowerCase(c)){
lower++;
}
if(Character.isDigit(c)){
digit++;
}
if(c>=33&&c<=46||c==64){
special++;
}
}
if(special>=1&&lower>=1&&upper>=1&&digit>=1&&pass.length() >= 8){
status = true;
password.setPassword(pass);
}

}while (status == false);

if(password.equals(password1)){
System.out.println("Password are same");
}else {
System.out.println("Not Match");
}

System.out.println(password.toString());
System.out.println(password1.toString());

}
}
Output:

Add a comment
Know the answer?
Add Answer to:
Create a simple Java class for a Password with the following requirements:  This program will...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Create a simple Java class for a Month object with the following requirements:  This program...

    Create a simple Java class for a Month object with the following requirements:  This program will have a header block comment with your name, the course and section, as well as a brief description of what the class does.  All methods will have comments concerning their purpose, their inputs, and their outputs  One integer property: monthNumber (protected to only allow values 1-12). This is a numeric representation of the month (e.g. 1 represents January, 2 represents February,...

  • Modify the below Java program to use exceptions if the password is wrong (WrongCredentials excpetion). import java.util....

    Modify the below Java program to use exceptions if the password is wrong (WrongCredentials excpetion). import java.util.HashMap; import java.util.Map; import java.util.Scanner; class Role { String user, password, role; public Role(String user, String password, String role) { super(); this.user = user; this.password = password; this.role = role; } /** * @return the user */ public String getUser() { return user; } /** * @param user the user to set */ public void setUser(String user) { this.user = user; } /** *...

  • 1.     This project will extend Project 3 and move the encryption of a password to a...

    1.     This project will extend Project 3 and move the encryption of a password to a user designed class. The program will contain two files one called Encryption.java and the second called EncrytionTester.java. 2.     Generally for security reasons only the encrypted password is stored. This program will mimic that behavior as the clear text password will never be stored only the encrypted password. 3.     The Encryption class: (Additionally See UML Class Diagram) a.     Instance Variables                                                i.     Key – Integer...

  • This project will use The Vigenere Cipher to encrypt passwords. Simple letter substitution ciphers are ones...

    This project will use The Vigenere Cipher to encrypt passwords. Simple letter substitution ciphers are ones in which one letter is substituted for another. Although their output looks impossible to read, they are easy to break because the relative frequencies of English letters are known. The Vigenere cipher improves upon this. They require a key word and the plain text to be encrypted. Create a Java application that uses: decision constructs looping constructs basic operations on an ArrayList of objects...

  • create a class in Java for a Plane the class will include String Data Fields for...

    create a class in Java for a Plane the class will include String Data Fields for the make, model, and year. (as well as mutators and accessors) Defined constructor and copy constructor. there must be a toString method as well as a copy method. runner program the program will create an array for Plane class. The size of the array will be set to user input. then the user will input the information into each plane object in the array....

  • Java Create four classes: 1.      An Animal class that acts as a superclass for Dog and...

    Java Create four classes: 1.      An Animal class that acts as a superclass for Dog and Bird 2.      A Bird class that is a descendant of Animal 3.      A Dog class that is a descendant of Animal 4.      A “driver” class named driver that instantiates an Animal, a Dog, and a Bird object. The Animal class has: ·        one instance variable, a private String variable named   name ·        a single constructor that takes one argument, a String, used to set...

  • Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the...

    Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the concept of a college course, assuming that a course has following attributers: code (for instance COSC1337), a description, and a number of credits (for instance 3). Include a constructor, the accessors, mutators and methods ‘toString’, ‘equals’, and ‘finalize’. Write a client class to test the behavior of the class and its methods. The outline of the class is given as follows: public class Course...

  • In JAVA Design a class named Point that meets the following requirements: Two data fields x...

    In JAVA Design a class named Point that meets the following requirements: Two data fields x and y for representing a point with getter methods. A no-arg constructor that constructs a point for (0, 0). A constructor that constructs a point with the specified x and y values. Override the equals method. Point p1 is said to be greater than point p2 if p1.x == p2.x and p1.y == p2.y. Implement the Comparable<Point> interface and the compareTo method. Point p1...

  • Write a program that meets the following requirements: Sandwich Class Create a class called Sandwich which has the following instance variables. No other instance variables should be used: - ingredi...

    Write a program that meets the following requirements: Sandwich Class Create a class called Sandwich which has the following instance variables. No other instance variables should be used: - ingredients (an array of up to 10 ingredients, such as ham, capicola, American cheese, lettuce, tomato) -condiments (an array of up to 5 condiments, such as mayonnaise, oil, vinegar and mustard) Create a two argument constructor Write the getters and setters for the instance variables Override the toString method using the...

  • Create the following program in java please Write a class Store which includes the attributes: store...

    Create the following program in java please Write a class Store which includes the attributes: store name and sales tax rate. Write another class encapsulating a Book Store, which inherits from Store. A Book Store has the following additional attributes: how many books are sold every year and the average price per book. Code the constructor, accessors, mutators, toString and equals method of the super class Store and the subclass Book Store; In the Book Store class, also code a...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT