JAVA ONLY PLEASE 1) create a class with the method: public static String repeat(
ID: 3689582 • Letter: J
Question
JAVA ONLY PLEASE
1) create a class with the method:
public static String repeat(String str, int n)
that returns the String str repeated n times. For example, repeat("ho", 3) returns hohoho.
Provide a program that tests your methods from a different class in a static manner.
2) Having a secure password is a very important practice, when much of our information is stored online. Write a program that validates a new password, following these rules:
must be 8 characters long
at least one upper case letter and one lower case letter
at least on digit
Write a program that asks for a password, than asks again to confirm it. If the passwords do not match or the rules are not fulfilled, prompt again. Your program should include a method that checks whether a password is valid.
Explanation / Answer
1)
RepeatString.java
import java.util.Scanner;
public class RepeatString {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
System.out.println("Please enter your string ");
String s = scan.next();
System.out.println("Please enter the n value ");
int n = scan.nextInt();
String finalString = repeat(s, n);
System.out.println(finalString);
}
public static String repeat(String str, int n){
String temp = "";
for(int i=0; i<n; i++){
temp = temp + str;
}
return temp;
}
}
Output:
Please enter your string
ho
Please enter the n value
3
hohoho
PasswordCheck.java
import java.util.Scanner;
public class PasswordCheck {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
while(true){
System.out.println("Please enter the password");
String password = scan.next();
System.out.println("Please enter confirm password");
String confirmPassword = scan.next();
boolean status;
if(password.equals(confirmPassword)){
status = validatePassword(password);
if(status == true){
System.out.println("Valid Password");
break;
}
else{
System.out.println("Invalid Password.. Try Again..");
}
}
else{
System.out.println("Password does not match");
}
}
}
public static boolean validatePassword(String pass){
int upperCount = 0;
int lowerCount = 0;
int digitCount = 0;
if(pass.length() == 8){
for(int i=0; i<pass.length(); i++){
if(Character.isUpperCase(pass.charAt(i))){
upperCount++;
}
else if(Character.isLowerCase(pass.charAt(i))){
lowerCount++;
}
else if(Character.isDigit(pass.charAt(i))){
digitCount++;
}
}
if(upperCount > 0 && lowerCount >0 && digitCount >0){
return true;
}
else{
return false;
}
}
else{
return false;
}
}
}
Output:
Please enter the password
w2e3r4T5
Please enter confirm password
w2e3r4T5
Valid Password
Please enter the password
abcdef12
Please enter confirm password
abcdef12
Invalid Password.. Try Again..
Please enter the password
abcdeF12
Please enter confirm password
abcdeF12
1 5 2
Valid Password
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.