TA: Nouf Al-Harbi NoufNaief.net :::

Slides:



Advertisements
Similar presentations
Computer Programming Lab(7).
Advertisements

CS110 Programming Language I
Computer Programming Lab 8.
CS110 Programming Language I Lab 10: Arrays I Computer Science Department Spring 2014.
Programming 2 CS112- Lab 2 Java
TA: Nouf Al-Harbi NoufNaief.net :::
Primitive Data Types and Operations. Introducing Programming with an Example public class ComputeArea { /** Main method */ public static void main(String[]
1 More About Methods in Java CSC 1401: Introduction to Programming with Java Week 7 – Lecture 3 Wanda M. Kunkle.
Introduction to Computer Programming Decisions If/Else Booleans.
Some basic I/O.
Loop variations do-while and for loops. Do-while loops Slight variation of while loops Instead of testing condition, then performing loop body, the loop.
Loops Chapter 4. It repeats a set of statements while a condition is true. while (condition) { execute these statements; } “while” structures.
LAB 10.
Computer Programming Lab(4).
Computer Programming Lab(5).
JAVA Control Structures: Repetition. Objectives Be able to use a loop to implement a repetitive algorithm Practice, Practice, Practice... Reinforce the.
CPS 2231 Computer Organization and Programming Instructor: Tian (Tina) Tian.
Chapter 6: Iteration Part 2. Create triangle pattern [] [][] [][][] [][][][] Loop through rows for (int i = 1; i
Java Programming: From the Ground Up
 The pool rack example could be implemented using a for loop.  It is also possible to write recursive methods that accomplish things that you might.
Lecture 10 Recursion. public class recursionDemo { public static void main(String[] args) { System.out.println("TriCount for n = 5 is... " + triCount(5));
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
Chapter 2 Elementary Programming
Chapter 5 Case Study. Chapter 5 The RPS Flowchart.
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
Lecture 3 Decisions (Conditionals). One of the essential features of computer programs is their ability to make decisions. Like a train that changes tracks.
1 while loops. 2 Definite loops definite loop: A loop that executes a known number of times.  The for loops we have seen so far are definite loops. We.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Chapter 4: Control Structures II
COM S 207 Method Instructor: Ying Cai Department of Computer Science Iowa State University
Chapter 6. else-if & switch Copyright © 2012 Pearson Education, Inc.
1 Reasoning about assertions Readings: Assertions assertion: A statement that is either true or false. Examples:  Java was created in (true)
CS110 Programming Language I Lab 4: Control Statements I Computer Science Department Spring 2014.
Topics Logical Operators (Chapter 5) Comparing Data (Chapter 5) The conditional operator The switch Statement The for loop Nested Loops.
Java Variables, Types, and Math Getting Started Complete and Turn in Address/Poem.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Decisions Bush decision making.
Starting Out with Java: From Control Structures through Objects 5 th edition By Tony Gaddis Source Code: Chapter 9.
CS1101: Programming Methodology Preparing for Practical Exam (PE)
 CSC111 Quick Revision. Problem Write a java code that read a string, then show a list of options to the user to select from them, where:  L to print.
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC 110 – INTRO TO COMPUTING - PROGRAMMING Switch Statement.
Computer Programming Lab 9. Exercise 1 Source Code package excercise1; import java.util.Scanner; public class Excercise1 { public static void main(String[]
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
TOPIC 8 MORE ON WHILE LOOPS 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B. Ericson,
(Dreaded) Quiz 2 Next Monday.
CS0007: Introduction to Computer Programming
Exercise 1- I/O Write a Java program to input a value for mile and convert it to kilogram. 1 mile = 1.6 kg. import java.util.Scanner; public class MileToKg.
Computer Programming Methodology Input and While Loop
Repetition-Sentinel,Flag Loop/Do_While
Repetition.
TK1114 Computer Programming
The switch Statement The switch statement provides another way to decide which statement to execute next The switch statement evaluates an expression,
Something about Java Introduction to Problem Solving and Programming 1.
Control Statement Examples
Building Java Programs
Building Java Programs
Building Java Programs
SELECTION STATEMENTS (2)
Java Variables, Types, and Math Getting Started
Building Java Programs
class PrintOnetoTen { public static void main(String args[]) {
SELECTIONS STATEMENTS
Lecture Notes – Week 2 Lecture-2
CSC 1051 – Data Structures and Algorithms I
Building Java Programs
More on iterations using
Computer Science Club 1st November 2019.
Presentation transcript:

TA: Nouf Al-Harbi NoufNaief.net ::: nouf200@hotmail.com Programming 2 LAB TA: Nouf Al-Harbi NoufNaief.net ::: nouf200@hotmail.com

The String Class Applications Lab 2 The String Class Applications

Program Phone keypads

Phone keypads Problem Description: The international standard letter/number mapping found on the telephone is shown below: Phone keypads

public static int getNumber(char uppercaseLetter) 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

Phone keypads <Output> Enter a string: 1-800-Flowers 1-800-3569377 <End Output> Enter a string: 1800flowers 18003569377 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) { 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; 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;

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 Password Sample 1 Sample 2 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");

// Count the number of digits int count = 0; /** Check if a string is a valid password */ public static boolean isValidPassword(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))) return false; } // Check length if (s.length() < 8) // 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) return true; else return false;

End of Lab 2