Download and import the Lab 2 Eclipse project from D2L. In the lab 2 project cre
ID: 3786167 • Letter: D
Question
Download and import the Lab 2 Eclipse project from D2L. In the lab 2 project create a class to complete the following. All work should be done in a class of static methods (Tools.java) that are called from your Tester class (PasswordCheck) Prompt the user to enter a String representing a password. The method returns true if the password String has at least one special (non-letter) character and at least one number (StringStats) Prompt the user to enter a sentence (String) (eg "Eclipse is much better than Notepad++ because it does the work for me!") and print the number of vowels and the number of words in the sentence that are shorter than five characters (CharReplace) Using the same String as the previous task, return a new String with all occur of O' (zero) replaced by with O' and print the result BONUS (CaesarCipher) write an 'encryption' method that changes all characters by of will change a to by will change 'a' to 'f' and 'l' to 'q'). The method returns the" which is printed in main().Explanation / Answer
/**
* The java class Tools that implements two
* methods called PasswordCheck and StringStats
* and print results to console.
* */
//Tools.java
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Tools
{
/*The method PasswordCheck that prompts user
* to etner password string and returns true
* if the password is valid */
public static boolean PasswordCheck()
{
String pwd;
Scanner scanner=new Scanner(System.in);
System.out.println("Enter password string : ");
pwd=scanner.nextLine();
boolean nonletter=false;
boolean digit=false;
Pattern p = Pattern.compile("[^A-Za-z0-9]");
Matcher m = p.matcher(pwd);
if (m.find())
nonletter=true;
p = Pattern.compile("([0-9])");
m = p.matcher(pwd);
if (m.find())
digit=true;
return nonletter && digit;
}
/*
* The method StringStats that prompts string
* and prints number of vowels and number of words
* in the string and print results to console.
* */
public static void StringStats()
{
String str;
Scanner scanner=new Scanner(System.in);
System.out.println("Enter a string : ");
str=scanner.nextLine();
int vowelCount=0;
for (int i = 0; i < str.length(); i++)
{
switch (str.charAt(i))
{
case 'A': case 'E': case 'I': case 'O': case 'U':
case 'a': case 'e': case 'i': case 'o': case 'u':
vowelCount++;
/*vowel*/
break;
}
}
String words[]=str.split(" ");
int wordcount=words.length;
System.out.println("Number of vowels : "+vowelCount);
System.out.println("Number of words : "+wordcount);
}
}
---------------------------------------------------------------------------------------------------------------------
/**
* The java program Tester that tests the
* static methods of the Tools class.
* */
//Tester.java
public class Tester
{
public static void main(String[] args)
{
//calling static method PaswordCheck method
if(Tools.PasswordCheck())
System.out.println("Valid password");
else
System.out.println("Invalid password");
//calling StringStats method
Tools.StringStats();
}
}
---------------------------------------------------------------------------------------------------------------------
Sample output:
Enter password string :
abc!2
Valid password
Enter a string :
hellojava
Number of vowels : 4
Number of words : 1
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.