Write a short program in the main to generate the following
Solution
A. The Java Program to generate the dialog box is as follows ,
import javax.swing.JOptionPane;
public class Main {
public static void main(String[] argv) throws Exception {
JOptionPane.showMessageDialog(null, \"C:/data/201701/csc110/labs/lab3>java Lab3\ \"
+ \"What\'s your name? Victoria\ \"
+ \"Hi, Victoria!\ \"
+ \"Enter a natural number:75\ \"
+ \"75 is an odd number\ \");
}
}
B. The two string classes are declared as follows,
C. There are three methods of comparing strings in java,
They are , equals() method , compareTo() method and by using \" == \" operator .
In the below program the two methods equals() and equalsIgnoreCase() are given,
The Program below gives the example for charAt() method,
The Java Program below gives the example for length() method,
The \" == \" operator compares the object references between the given two objects or strings.
D. The Java program to validate a password is given as ,
class PasswordValidator
{
public PasswordValidator()
{
super();
}
public static void main(String[] args)
{
PasswordValidator passwordValidator = new PasswordValidator();
String userName;
userName = \" ABC \";
String passWord = \" java \";
System.out.println(\"Input : UserName \"+userName+\" PassWord -> \"+passWord);
passwordValidator.passwordValidation(userName,passWord);
System.out.println();
passWord = \" Java10* \";
System.out.println(\"Input : UserName \"+userName+\" PassWord -> \"+passWord);
passwordValidator.passwordValidation(userName,passWord);
}
/*
* Password should contain more than 8 characters in length.
* Password should contain at least one upper case and one lower case alphabet.
* Password should contain at least one digit.
*/
public void passwordValidation(String userName, String password)
{
boolean valid = true;
if (password.length() > 15 || password.length() < 8)
{
System.out.println(\"Password should be less than 15 and more than 8 characters in length.\");
valid = false;
}
if (password.indexOf(userName) > -1)
{
System.out.println(\"Password Should not be same as user name\");
valid = false;
}
String upperCaseChars = \"(.*[A-Z].*)\";
if (!password.matches(upperCaseChars ))
{
System.out.println(\"Password should contain atleast one upper case alphabet\");
valid = false;
}
String lowerCaseChars = \"(.*[a-z].*)\";
if (!password.matches(lowerCaseChars ))
{
System.out.println(\"Password should contain atleast one lower case alphabet\");
valid = false;
}
String numbers = \"(.*[0-9].*)\";
if (!password.matches(numbers ))
{
System.out.println(\"Password should contain atleast one digit.\");
valid = false;
}
if (valid)
{
System.out.println(\"Password is valid.\");
}
}
}

