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

Slides:



Advertisements
Similar presentations
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
Advertisements

Introduction to Computers and Programming Lecture 7:
Chapter 2: Java Fundamentals Input and Output statements.
Loops –For For Reading for this Lecture, L&L, Part of 5.8.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 7 User-Defined Methods.
Chapter 7: User-Defined Methods
Chapter 2: Java Fundamentals Java Program Structure.
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
The switch Statement, DecimalFormat, and Introduction to Looping
LAB 10.
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
Chapter 4: Control Structures II
Introduction to Java. Main() Main method is where the program execution begins. There is only one main Displaying the results: System.out.println (“Hi.
Week #2 Java Programming. Enable Line Numbering in Eclipse Open Eclipse, then go to: Window -> Preferences -> General -> Editors -> Text Editors -> Check.
1 Introduction to Java Brief history of Java Sample Java Program Compiling & Executing Reading: => Section 1.1.
Java Fundamentals 3 Input and Output statements. Standard Output Window Using System.out, we can output multiple lines of text to the standard output.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
Chapter 5 Loops.
Input/Output in Java. Output To output to the command line, we use either System.out.print () or System.out.println() or System.out.printf() Examples.
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) public static void main(String[]
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Logic Our programs will have to make decisions on what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if and if-else.
Chapter 4: Control Structures II
Chapter 5: Control Structures II
Introduction to Programming
Chapter 6. else-if & switch Copyright © 2012 Pearson Education, Inc.
Java Basics Hussein Suleman March 2007 UCT Department of Computer Science Computer Science 1015F.
CS110 Programming Language I Lab 4: Control Statements I Computer Science Department Spring 2014.
Chapter 3: Classes and Objects Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved Java’s String Class.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
Object Oriented Programming (OOP) LAB # 1 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal.
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC 110 – INTRO TO COMPUTING - PROGRAMMING Switch Statement.
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
CS0007: Introduction to Computer Programming The for Loop, Accumulator Variables, Seninel Values, and The Random Class.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC 110 – INTRO TO COMPUTING - PROGRAMMING For Loop.
Fundamentals 2 1. Programs and Data Most programs require the temporary storage of data. The data to be processed is stored in a temporary storage in.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Slides by Evan Gallagher
Slides by Evan Gallagher
CS 160 – Summer 16 Exam 1 Prep.
CSC111 Quick Revision.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 7 User-Defined Methods.
Chapter 6 More Conditionals and Loops
Maha AlSaif Maryam AlQattan
Chapter 5: Control Structures II
Repetition-Counter control Loop
Repetition.
Chapter 5: Control Structures II
SELECTION STATEMENTS (1)
INPUT STATEMENTS GC 201.
Control Statement Examples
An Introduction to Java – Part I, language basics
Introduction to Classes and Methods
SELECTION STATEMENTS (2)
Fundamental Error Handling
Fundamentals 2.
More on Classes and Objects
Module 4 Loops and Repetition 2/1/2019 CSE 1321 Module 4.
Chapter 2: Java Fundamentals
Module 3 Selection Structures 4/5/2019 CSE 1321 Module 3.
Chapter 2: Java Fundamentals
Repetition Statements
Random Numbers while loop
More on iterations using
Computer Science Club 1st November 2019.
Presentation transcript:

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

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

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

 Coding

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

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

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

// 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

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();

//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

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

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

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);

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 We can use either the letter it self or ASCII This condition for only capital letters. What about small

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

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………… We can use either the letter it self or ASCII

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

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

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

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

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;

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???) } } }

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

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

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

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

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