Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 1 Review of 1301/1321 CSE 1322 4/26/2018.

Similar presentations


Presentation on theme: "Lecture 1 Review of 1301/1321 CSE 1322 4/26/2018."— Presentation transcript:

1 Lecture 1 Review of 1301/1321 CSE 1322 4/26/2018

2 Common data types byte, short, int, long, double, float, char,
C#: bool, string / String Java: boolean, String 4/26/2018

3 To declare a variable <type> <name>; Example: int myNum; You can also initialize it when you declare it: int myNum = 42; double taxRate=.07; float interestRate=.065f; 4/26/2018

4 C# Console I/O Console.Write(X); Console.WriteLine(X); string A = Console.ReadLine(); ReadLine() returns a string, so if you want to get something else from the user (and int for example), you have to convert it.  int myNum = Int32.Parse(Console.ReadLine()); This takes in input from the user (as a string) and then passes this string to the Parse method of the Int32 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

5 Java Console I/O System.out.print(x); System.out.println(x); Scanner scan = new Scanner (System.in); String word=scan.nextLine(); int num=scan.nextInt(); Recall that with using scanner, if you mix the use of nextLine() and nextInt(), etc., you may have to flush the stream. 4/26/2018

6 Pseudocode Console input
NAME = “” PRINT “What is your name? ” READ user_input NAME← user_input PRINT Hello +NAME 7/19/2019

7 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

8 C# User interaction Console.Write("What is your name? "); string name = Console.ReadLine(); Console.WriteLine("Hello " + name); Notice in the above that the + operator acts as string concatenation. 4/26/2018

9 Java User interaction System.out.print(“What is your name? “);
String name= scan.nextLine(); System.out.println(“Hello “+name); Notice in the above that the + operator acts as string concatenation. 4/26/2018

10 From now on… We’ll use generic PRINT() instead of:
Console.WriteLine() System.out.println() We’ll use generic READ() instead of: Scanner scan = new Scanner (System.in); scan.nextLine(); Console.ReadLine() 4/26/2018

11 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 7/19/2019

12 Conditionals – C# and Java
PRINT("How old are you? "); int age = READ(); if (age < 18)   PRINT("You cannot drink or vote."); else if (age < 21)   PRINT("You can vote but not drink."); else   PRINT("You can vote and drink, but don't vote shortly after drinking!"); 4/26/2018

13 { } 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 want to execute more than one statement, use { } to mark the body of the if-else statement blocks. 4/26/2018

14 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

15 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

16 Pseudocode – void method menu
METHOD PrintMenu BEGIN   PRINT "1. Display result"   PRINT "2. Add a number"   PRINT “3. Delete a number"   PRINT "4. QUIT" END PrintMenu 7/19/2019

17 Method examples 1 a menu – Java/C#
void PrintMenu() { PRINT("1. Display result"); PRINT("2. Add a number"); PRINT("3. Delete a number"); PRINT("4. QUIT"); } 4/26/2018

18 Pseudocode –method menu with input and return
METHOD GetChoice() 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 GetChoice 7/19/2019

19 Method examples 2 choice – Java and C#
int GetChoice() { int choice; do { PRINT("Enter your choice (1-4) : "); choice = READ(); } while ((choice < 1) || (choice > 4)); return choice; } 4/26/2018

20 Ps Pseudocode – for loop
FOR month = 1 to 12 block of statements ENDFOR 7/19/2019

21 for loops - Java and C# int sum = 0; for (int i=0; i < 100; i += 2)
for(<initialize>; <conditional>; <post-activity>) {   <body/work> } int sum = 0; for (int i=0; i < 100; i += 2) { sum+= i; } PRINT ("The sum is "+sum); 4/26/2018

22 Pseudocode –do while loop
REPEAT block of statements UNTIL condition DO WHILE condition ENDDO 7/19/2019

23 do while loop do { <body/work> } while (<conditional>); int i = 10; int total=0; do { total+=i; i--; } while (i > 0); PRINT("Total" + total); 4/26/2018

24 Pseudocode – while loop
WHILE condition = true block of statements ENDWHILE 7/19/2019

25 while loop while (<conditional>) {   <body/work> } int i = 0; while (i != 0) {   i -= 1; PRINT("the result is "+i); 4/26/2018


Download ppt "Lecture 1 Review of 1301/1321 CSE 1322 4/26/2018."

Similar presentations


Ads by Google