Download case - Weebly

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Programming 2 LAB
TA: Nouf Al-Harbi
NoufNaief.net ::: [email protected]
LAB 2
The String Class Applications
PROGRAM
Phone keypads
Problem Description:
• The international standard letter/number mapping found
on the telephone is shown below:
Phone keypads
• Write a method that returns a number, given an
uppercase letter, as follows:
public static int getNumber(char uppercaseLetter)
• Write a test program that prompts the user to enter a phone
number as a string. The input number may contain letters. The
program translates a letter (upper- or lowercase) to a digit and
leaves all other characters intact.
Phone keypads
•
•
•
•
<Output>
Enter a string: 1-800-Flowers
1-800-3569377
<End Output>
•
•
•
•
<Output>
Enter a string: 1800flowers
18003569377
<End Output>
Phone keypads
SOLUTION..
import java.util.Scanner;
public class Exercise9_7 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// Prompt the user to enter a string
System.out.print("Enter a string: ");
String s = input.nextLine();
for (int i = 0; i < s.length(); i++)
{
if (Character.isLetter(s.charAt(i)))
System.out.print(getNumber(Character.toUpperCase(s.charAt(i))
));
else
System.out.print(s.charAt(i));
}
}
public static int getNumber(char uppercaseLetter)
{
case 'M':
int number = 0;
case 'N':
switch (uppercaseLetter)
case 'O': number
{
case 'P':
case 'A':
case 'Q':
case 'B':
case 'R':
case 'C': number = 2; break;
case 'S': number
case 'D':
case 'T':
case 'E':
case 'U':
case 'F': number = 3; break;
case 'V': number
case 'G':
case 'W':
case 'H':
case 'X':
case 'I': number = 4; break;
case 'Y':
case 'J':
case 'Z': number
case 'K':
}
case 'L': number = 5; break;
return number;
}
}
= 6; break;
= 7; break;
= 8; break;
= 9; break;
PROGRAM
Checking Password
Problem Description :
• Some Websites impose certain rules for passwords. Write
a method that checks whether a string is a valid password.
Suppose the password rule is as follows:
• A password must have at least eight characters.
• A password consists of only letters and digits.
• A password must contain at least two digits.
• Write a program that prompts the user to enter a password
and displays "valid password" if the rule is followed or
"invalid password" otherwise.
Checking Password
• Sample 1
• Enter a string for password: wewew43x
• valid password
• Sample 2
• Enter a string for password: 343a
• invalid password
Checking Password
SOLUTION..
public class Test {
public static void main(String[] args) {
// Prompt the user to enter a password
java.util.Scanner input = new
java.util.Scanner(System.in);
System.out.print("Enter a string for
password: ");
String s = input.nextLine();
if (isValidPassword(s)) {
System.out.println("Valid password");
}
else {
System.out.println("Invalid password");
}
}
/** Check if a string is a valid
password */
public static boolean
isValidPassword(String s) {
// Count the number of digits
// Only letters and digits?
for (int i = 0; i < s.length();
int count = 0;
i++) {
for (int i = 0; i <
if
s.length(); i++) {
(!Character.isLetter(s.charAt(i))
if
&&
!Character.isDigit(s.charAt(i)))
return false;
}
(Character.isDigit(s.charAt(i)))
count++;
}
if (count >= 2)
return true;
else
return false;
// Check length
if (s.length() < 8)
return false;
}
}
End of Lab 2