programming 2 lab n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Programming 2 LAB PowerPoint Presentation
Download Presentation
Programming 2 LAB

Loading in 2 Seconds...

play fullscreen
1 / 16

Programming 2 LAB - PowerPoint PPT Presentation


  • 232 Views
  • Uploaded on

Programming 2 LAB. TA: Nouf Al-Harbi NoufNaief.net ::: nouf200@hotmail.com. 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.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Programming 2 LAB' - teagan-stephens


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
programming 2 lab

Programming 2 LAB

TA: Nouf Al-Harbi

NoufNaief.net ::: nouf200@hotmail.com

lab 2
Lab 2

The StringClass Applications

program
Program

Phone keypads

phone keypads

Problem Description:

  • The international standard letter/number mapping found on the telephone is shown below:
Phone keypads
phone keypads1

Write a method that returns a number, given an uppercase letter, as follows:

public static intgetNumber(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
phone keypads2

<Output>

  • Enter a string: 1-800-Flowers
  • 1-800-3569377
  • <End Output>
  • <Output>
  • Enter a string: 1800flowers
  • 18003569377
  • <End Output>
Phone keypads
slide8

importjava.util.Scanner;

publicclass Exercise9_7 {

publicstaticvoid main(String[] args) {

Scanner input = newScanner(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));

}

}

slide9

case'M':

case'N':

case'O': number = 6; break;

case'P':

case'Q':

case'R':

case'S': number = 7; break;

case'T':

case'U':

case'V': number = 8; break;

case'W':

case'X':

case'Y':

case'Z': number = 9; break;

}

return number;

}

}

publicstaticintgetNumber(charuppercaseLetter)

{

int number = 0;

switch (uppercaseLetter)

{

case'A':

case'B':

case'C': number = 2; break;

case'D':

case'E':

case'F': number = 3; break;

case'G':

case'H':

case'I': number = 4; break;

case'J':

case'K':

case'L': number = 5; break;

program1
Program

Checking Password

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
checking password1

Sample 1

    • Enter a string for password: wewew43x
    • valid password
  • Sample 2
    • Enter a string for password: 343a
    • invalid password
Checking Password
slide14

publicclass Test {

publicstaticvoid main(String[] args) {

// Prompt the user to enter a password

java.util.Scanner input = newjava.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");

}

}

slide15

/** Check if a string is a valid password */

publicstaticbooleanisValidPassword(String s) {

// Only letters and digits?

for (int i = 0; i < s.length(); i++) {

if (!Character.isLetter(s.charAt(i)) &&

!Character.isDigit(s.charAt(i)))

returnfalse;

}

// Check length

if (s.length() < 8)

returnfalse;

// Count the number of digits

int count = 0;

for (int i = 0; i < s.length(); i++) {

if (Character.isDigit(s.charAt(i)))

count++;

}

if (count >= 2)

returntrue;

else

returnfalse;

}

}