Lecture 1 Review of 13011321 CSE 1322 4262018






























- Slides: 30
Lecture 1 Review of 1301/1321 CSE 1322 4/26/2018 1 -1
Common data types byte, short, int, long, double, float, char, C#: bool, string /String Java: boolean, String 4/26/2018 1 -2
To declare a variable <type> <name>; Example: int my. Num; You can also initialize it when you declare it: int my. Num = 42; double tax. Rate=. 07; float interest. Rate=. 065 f; 4/26/2018 1 -3
C# Console I/O Console. Write(X); Console. Write. Line(X); string A = Console. Read. Line(); • Note that Read. Line() returns a string, so if you want to get something else from the user (and int for example), you would have to convert it. int my. Num = Int 32. Parse(Console. Read. Line()); • This takes in input from the user (as a string) and then passes this string to the Parse method of the Int 32 class; this Parse method returns an int, so this is a valid assignment since now the left and the right side of the assignment (=) operator are of the same type. 4/26/2018 1 -4
Java Console I/O System. out. print(x); System. out. println(x); String word=scan. next. Line(); int num=scan. next. Int(); Recall that with using scanner, if you mix the use of next. Line() and next. Int(), etc. , you may have to flush the stream. 4/26/2018 1 -5
Data validity • For 1322, you may assume the user will give valid input, as far as the data type. • You will have to ensure the data is in the proper range. • Later in the course we will explore exception handling. 4/26/2018 1 -6
Pseudocode – Console input NAME = “” PRINT “What is your name? : ” READ user_input NAME← user_input PRINT Hello +NAME 10/2/2020 7
C# User interaction Console. Write("What is your name? "); string name = Console. Read. Line(); Console. Write. Line("Hello " + name); Notice in the above that the + operator acts as string concatenation. 4/26/2018 1 -8
Java User interaction System. out. print(“What is your name? “); String name= scan. next. Line(); System. out. println(“Hello “+name): Notice in the above that the + operator acts as string concatenation. 4/26/2018 1 -9
Pseudocode - IF Statement Problem Statement: Acquire the user age from the console and use if else statements to make a decision -----------------------------------------------------AGE ← 0 PRINT “How old are you? : ” READ user_input AGE ← user_input IF (AGE < 18) THEN PRINT “can’t vote or drink” ELSE IF (AGE <21) PRINT “can vote but can’t drink” ELSE PRINT “You can vote and drink, but don't vote shortly after drinking!” ENDIF 10/2/2020 10
C# Conditionals Console. Write("How old are you? "); int age = Int 32. Parse(Console. Read. Line()); if (age < 18) Console. Write. Line("You cannot drink or vote. "); else if (age < 21) Console. Write. Line("You can vote but not drink. "); else Console. Write. Line("You can vote and drink, but don't vote shortly after drinking!"); 4/26/2018 1 -11
Java Conditionals System. out. print("How old are you? "); int age = scan. next. Int(); if (age < 18){ System. out. println("You cannot drink or vote. "); } else if (age < 21){ System. out. println("You can vote but not drink. "); } else { System. out. println("You can vote and drink, but don't vote shortly after drinking!"); } 4/26/2018 1 -12
• • Notice in the previous examples that we don't have to use curly-brackets to define the block in the if-else because we only want to control one line of code if you wanted to do more than one statement, use { } to mark the body of the if-else statement blocks. { } 4/26/2018 1 -13
Declaring Methods You declare a method in java and C# using the following pattern: <return type> <name> (<parameter(s)>) { <body of method> } 4/26/2018 1 -14
Method Returns The return type of a method indicates the type of value that the method sends back to the calling location. The return statement halts execution within the method and (optionally) returns a value A method that does not return a value has a void return type A return statement specifies the value that will be returned return expression; Its expression must conform to the return type 4/26/2018 1 -15
Pseudocode – void method menu METHOD Print. Menu BEGIN PRINT "1. Display result" PRINT "2. Add a number" PRINT “ 3. Delete a number" PRINT "4. QUIT" END Print. Menu 10/2/2020 16
Method examples 1 a menu C# void Print. Menu() { Console. Write. Line("1. Display result"); Console. Write. Line("2. Add a number"); Console. Write. Line("3. Delete a number"); Console. Write. Line("4. QUIT"); } 4/26/2018 1 -17
Method examples 1 a menu java void Print. Menu() { System. out. println("1. Display result"); System. out. println("2. Add a number"); System. out. println("3. Delete a number"); System. out. println("4. QUIT"); } 4/26/2018 1 -18
Pseudocode –method menu with input and return METHOD Get. Choice() BEGIN choice ← 0 DO PRINT “Enter your choice (1 -4) : “ READ user input choice ← user input WHILE choice is less than 1 or choice is greater than 4 ENDDO return choice; END Get. Choice 10/2/2020 19
Method examples 2 choice java int Get. Choice() { int choice; do { System. out. print("Enter your choice (1 -4) : "); choice = scan. next. Int(); } while ((choice < 1) || (choice > 4)); return choice; } 4/26/2018 1 -20
Method examples 2 choice C# int Get. Choice() { int choice; do { Console. Write("Enter your choice (1 -4) : "); choice = Int 32. Parse(Console. Read. Line()); } while ((choice < 1) || (choice > 4)); return choice; } 4/26/2018 1 -21
Pseudocode –for loop FOR month = 1 to 12 block of statements ENDFOR 10/2/2020 22
for loops java for(<initialize>; <conditional>; <post-activity>) { <body/work> } int sum=0; for ( int i=0; i < 100; i += 2) { sum+= i; } System. out. println(“the sum is “+sum); 4/26/2018 1 -23
for loops C# for(<initialize>; <conditional>; <post-activity>) { <body/work> } int sum=0; for ( int i=0; i < 100; i += 2) { sum+= i; } Console. Write. Line(“the sum is “+sum); 4/26/2018 1 -24
Pseudocode –do while loop REPEAT block of statements UNTIL condition DO block of statements WHILE condition ENDDO 10/2/2020 25
C# do while loop do { <body/work> } while (<conditional>); int i = 10; int total=0; do { total+=i; i--; } while (i > 0); Console. Write(“Total “+total); 4/26/2018 1 -26
Java do while loop do { <body/work> } while (<conditional>); int i = 10; int total=0; do { total+=i; i--; } while (i > 0); System. out. print(“Total “+total); 4/26/2018 1 -27
Pseudocode – while loop WHILE condition = true block of statements ENDWHILE 10/2/2020 28
Java while loop while (<conditional>) { <body/work> } int i = 0; while (i != 0) { i -= 1; } System. out. println(“the result is “+i); 4/26/2018 1 -29
C# while loop while (<conditional>) { <body/work> } int i = 0; while (i != 0) { i -= 1; } Console. Write(“result is “+i); 4/26/2018 1 -30