Presentation is loading. Please wait.

Presentation is loading. Please wait.

 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.

Similar presentations


Presentation on theme: " 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."— Presentation transcript:

1  CSC111 Quick Revision

2 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 the letters in the string (either they are capital or small)  D to print the digit in the string  O to print other characters in the string (any thing except letters and digits) If the user enter another option the program should show an error message This menu will keep showing 4 times

3 Analysis  Input  String  User choice  processing  test choice  if the choice is L then  Test each character in the string if it is a letters or not  Print letters  if the choice is D then  Test each character in the string if it is a Digit or not  Print digits  if the choice is O then  Test each character in the string if it is not a letters neither  Print characters  Output  Letters || digits || other characters

4 Design 2. Else If ch is ‘D’ or ‘d’ For each character in str If character is digit Print it 3. Else If ch is ‘O’ or ‘o’ For each character in str If character is neither letter nor digit Print it 4. EndIf 5. Update counter by 1 6. EndLoop 7. End the Program 1. Start the program 2. Read string and save in variable str 3. Declare and initialize counter to be 0 4. Loop while counter less than 4 1. read user choice (char) and save in variable ch 2. If ch is ‘L’ or ‘l’ For each character in str If character is letter Print it

5  Coding

6 Start the program  /*We want to develop a Java program to classify the characters in a given string to letters, digits, and other characters according the user choice // import Section – import used java libraries public class Classify{ // main method public static void main( String args[] ){ // Declaration section – Declare needed variables // Input section – Enter required data // Processing section – Processing Statements // Output section – Display expected results } // end main } // end class 1.Start by writing the program structure

7 Variable Declaration  What variable do I need ??  input  str  ch  Processing  counter

8 Variable Declaration  what about data type  Input  Str  String  Ch  char  Processing  Counter  int

9 // import Section – import used java libraries public class Classify{ // main method public static void main( String args[] ){ // Declaration section – Declare needed variables String str; char ch; int counter=0; // Input section – Enter required data // Processing section – Processing Statements // Output section – Display expected results } // end main } // end class 2. Declare variable Note: if some variable has initial value initialize them Use appropriate variable name

10

11 3.Input Reading input from the user 4 basic steps  Step 1: import the Scanner class:  import java.util.Scanner;  Step 2 : declaring a reference variable of a Scanner  Scanner input; //we named the object read  Step 3: creating an instance of the Scanner  input = new Scanner (System.in);  Step 4: use specific methods to enter data  int x = input.nextInt();

12 //show the menu to the user System.out.println("Enter your choice: \n L to show the letters in string,\n D to print digits in string, and\n O to print the other characters"); //read user choice ch=input.next().charAt(0);// Processing section – Processing Statements } } / // import Section – import used java libraries import java.util.Scanner; public class Classify{ // main method public static void main( String args[] ){ // Declaration section – String str; //to store user input string char ch; //to store user input choice int counter=0; //initialize counter by zero Scanner input; input=new Scanner (System.in); // read str System.out.println("Enter your string"); str=input.next(); Each input required separate read statement Print message before each read

13 4.Processing 1. Test user choice ch chL For each char in str If it is letter print D For each char in str If it is digit print O For each char in str If it is not L||D print Switch cases For loop to go through all characters in str If to test each char in str

14 4.Processing  Loop that goes through all the characters in str  Str =“Hello”;  How to reach 1st character 01234 Hello

15 4.Processing Str.charAt(0); //H Str.charAt(1); //e Str.charAt(2); //l Str.charAt(3); //l Str.charAt(4); //o Repeated Statement Start from 0 Until 4 ?? What about unknown String str.length()-1; What we can use?? for( int i=0;i<str.length();i++) strCh=Str.charAt(i);

16 4.Processing 1. Test the value of ch 2. Ch =‘L’ || ’l’ 1. Loop that goes through all the characters in str 2. Inside loop testing each char if it is letter or not If (str.charAt(i) >=“A” || str.charAt(i) <=“Z”) If (str.charAt(i) >=“A” && str.charAt(i) <=“Z”) If (str.charAt(i) >=‘A’ && str.charAt(i) <=‘Z’) If (str.charAt(i) >=65&& str.charAt(i) <=90) AB………….YZ 65668990 We can use either the letter it self or ASCII This condition for only capital letters. What about small

17 4.Processing If (str.charAt(i) >=‘a’ && str.charAt(i) <=‘z’) If (str.charAt(i) >=97 && str.charAt(i) <=122 ) Now.. We need one condition that test the letters either small or capital If (str.charAt(i) >=‘A’ && str.charAt(i) =‘a’ && str.charAt(i) <=‘z’) What will happen if we put && instead of ||??? AB………….Yz 9798121122

18 4.Processing 1. Ch =‘D’ || ’d’ 1. Loop that goes through all the characters in str 2. Inside loop testing each char if it is digit or not If (str.charAt(i) >=‘0’ && str.charAt(i) <=‘9’) If (str.charAt(i) >=48&& str.charAt(i) <=57) 01………….89 48495657 We can use either the letter it self or ASCII

19 4.Processing 1. Ch =‘O’ || ’o’ 1. Loop that goes through all the characters in str 2. Inside loop testing each char if it is neither letter nor digit

20 Introduction to OOPDr. S. GANNOUNI & Dr. A. TOUIR Page 20

21 4.Processing 1. Ch =‘O’ || ’o’ 1. Loop that goes through all the characters in str 2. Inside loop testing each char if it is neither letter nor digit Thus, the condition will be: if(!(str.charAt(i)>='0'&&str.charAt(i)<='9'|| str.charAt(i)>='A'&&str.charAt(i)<='Z'|| str.charAt(i)>='a'&&str.charAt(i)<='z')) Digits Capital Small ! Will invert the condition

22 4.Processing This list of options should repeated 4 times ??  Using counter while loop  For loop  Put { showing menu + reading user choice + switch } inside loop

23 import java.util.Scanner; public class Classify { public static void main(String args[]){ Scanner read=new Scanner(System.in); String str; //to store user input string char ch; //to store user input choice int counter=0; //initialize counter by 0 // read str System.out.println("Enter your string"); str=read.next();// read str from user while(counter<4){// this loop will continue until this condition goes false which garneted by updating the counter in the right direction System.out.println("Enter your choice: \n L to show the letters in string,\n D to print digits in string, and\n O to print the other characters"); ch=read.next().charAt(0); switch(ch){ case 'L':// case letters either the user enter L ||l it works ! case 'l': for(int i=0;i ='A'&&str.charAt (i) ='a'&&str.charAt(i)<='z‘) System.out.print(str.charAt(i)); System.out.println(); break;

24 case 'D': case 'd': for(int i=0;i ='0'&&str.charAt(i) ='0'&&str.charAt(i)<='9'|| str.charAt(i)>='A'&&str.charAt(i)<='Z'|| str.charAt(i)>='a'&&str.charAt(i)<='z')) System.out.print(str.charAt(i)); System.out.println(); break; default:// if user enter unlisted character System.out.println("error message"); } // close switch counter++;// update statement (avoiding infinite loop what if we update the counter in wrong direction???) } } }

25 run: CSC111Java!!programming&&Fun Enter your choice: L to show the letters in string, D to print digits in string, and O to print the other characters L CSCJavaprogrammingFun Sample Run Message # 1

26 Enter your choice: L to show the letters in string, D to print digits in string, and O to print the other characters D 111 Enter your choice: L to show the letters in string, D to print digits in string, and O to print the other characters O !!&& Sample Run Message # 2 Message # 3

27 Enter your choice: L to show the letters in string, D to print digits in string, and O to print the other characters d 111 Sample Run Message # 4

28  It keep asking 4 times as the counter starts from [0,4[  Counter <4  An equivalent condition that do the same role  Start from 1: Counter =1  Add = to the condition: Counter<=4  [1,4]

29  Also, it can be done using for loop  For ( int counter=0; counter<4;counter++)  Or  For ( int counter=1; counter<=4;counter++)


Download ppt " 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."

Similar presentations


Ads by Google