Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 10. Review (Char) Character is one of primitive data types of variable (such as integer and double) –Character variable contains one character.

Similar presentations


Presentation on theme: "Lecture 10. Review (Char) Character is one of primitive data types of variable (such as integer and double) –Character variable contains one character."— Presentation transcript:

1 Lecture 10

2 Review (Char) Character is one of primitive data types of variable (such as integer and double) –Character variable contains one character as its value: Alphabet, Digit, Symbol Declaration of char variable example char ch = ‘b’; Single quotation mark

3 Examples of char variable char grade = ‘B’; char plus = ‘+’; char num5 = ‘5’; System.out.println( “My grade is “ + grade ); System.out.println( “My grade is “ + grade + plus); System.out.println( num5 + plus + num5 + “ is 10“ ); On the screen, you will see My grade is B My grade is B+ 5+5 is 10

4 Strings String is a class which contains a collection of characters. The string “O’Bryant” could be thought of as a sequence of 8-elements ('O', '’', ‘B', 'r', ‘y', ‘a‘, ‘n’, ‘t’)

5 String name = new String(“Chonho”); String name = “Chonho”; Simplify! Since String is a class Create object! (declare object) Declaration (String class) name Chonho

6 Examples of String String name = “Chonho”; String job = “Student at UMB”; String id = “4816828”; String email = “chonho@gmail.com” System.out.println( name + “ information” ); System.out.println( “Who? “ + job + “, ID: “ + id ); System.out.println( “Please email at “ + email ); On the screen, you will see Chonho information Who? Student at UMB, ID: 4816828 Please email at chonho@gmail.com

7 Today’s topic Boolean data type –boolean Flow of control –Repetition statement (lecture ) –Conditional statement Relational operators IF statement

8 Boolean Boolean is one of primitive data types of variable (such as integer, double, and char) Boolean variable contains one truth value as its value –true or false flag true boolean flag = true; type name value

9 9 Equality and Relational operator Similar to arithmetic operators Equality operator == equal to != not equal to Relational operator < less than > greater than <= less than or equal to >= greater than or equal to

10 Boolean expression is –a condition that evaluates either true or false –It contains equality or relational operators –It returns boolean results example int num = 3; boolean flag; flag = num == 3; boolean expressions/conditions Equality operator Assignment operator true Boolean expression Ask like “is the value of num 3 ?”

11 Examples boolean a; a = (15 / 3 == 5); System.out.println(“a contains a value “ + a); a = (10 != 2 * 5); System.out.println(“a contains a value “ + a); double num = 2.5; a = (10.5 >= num); System.out.println(“a contains a value “ + a); true false true

12 12 if Statement The if statement has the following syntax: if ( condition ) { statement; } if is a Java key word The condition must be a boolean expression. It must evaluate to either true or false. If the condition is true, the statement is executed. If it is false, the statement is skipped.

13 int MAX = 10; int num = 5; if ( num < MAX ) System.out.println(“Is this line executed?”); System.out.println(“Done!!!”); Boolean expression (condition) true or false? Print as Is this line executed? Done!!! Example 1

14 int MAX = 10; int num = 3; if ( MAX-1 != Math.pow(num,2) ) System.out.println(“Is this line executed?”); System.out.println(“Done!!!”); More complex Boolean expression Print as Done!!! 9 != 3 2 Example 2

15 Today’s Exercise Upgrade MyCalculator –Let’s upgrade MyCalculator –Ask an user to enter two double numbers –Ask an user to select which operator (+, -, *, /) he/she wants to use –Print out the results

16 public class MyCalculator { public static void main(String[] argv) { JFrame frame = new JFrame(“My Calculator"); IOConsole console = new IOConsole(); frame.getContentPane().add(BorderLayout.CENTER, console); frame.setSize(500, 300); frame.setVisible(true); console.println( “This calculator performs following calculations” ); console.println( “1. addition, 2. subtraction”); next page

17 double num1, num2, result; int op; for( int i=0; i<10; i++) { num1 = console.readDouble( “Enter num1: “ ); num2 = console.readDouble( “Enter num2: “ ); op = console.readInt( “Which operator you want to use? “ ); if( op == 1 ) result = num1 + num2; if( op == 2 ) result = num1 – num2; console.println( “The result is “ + result + “.”); console.println( “” ); }


Download ppt "Lecture 10. Review (Char) Character is one of primitive data types of variable (such as integer and double) –Character variable contains one character."

Similar presentations


Ads by Google