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(); Note that ReadLine() 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 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); 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 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

7 Pseudocode – Console input
NAME = “” PRINT “What is your name?: ” READ user_input NAME← user_input PRINT Hello +NAME 4/3/2019

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 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 4/3/2019

11 C# Conditionals Console.Write("How old are you? "); int age = Int32.Parse(Console.ReadLine()); if (age < 18)   Console.WriteLine("You cannot drink or vote."); else if (age < 21)   Console.WriteLine("You can vote but not drink."); else   Console.WriteLine("You can vote and drink, but don't vote shortly after drinking!"); 4/26/2018

12 Java Conditionals System.out.print("How old are you? "); int age = scan.nextInt(); 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

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 wanted to do 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 4/3/2019

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

18 Method examples 1 a menu java
void PrintMenu() {   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

19 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 4/3/2019

20 Method examples 2 choice java
int GetChoice() {   int choice;   do {     System.out.print("Enter your choice (1-4) : ");     choice = scan.nextInt();   } while ((choice < 1) || (choice > 4));   return choice; } 4/26/2018

21 Method examples 2 choice C#
int GetChoice() {   int choice;   do {     Console.Write("Enter your choice (1-4) : ");     choice = Int32.Parse(Console.ReadLine());   } while ((choice < 1) || (choice > 4));   return choice; } 4/26/2018

22 Pseudocode –for loop FOR month = 1 to 12 block of statements ENDFOR
4/3/2019

23 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

24 for loops C# for(<initialize>; <conditional>; <post-activity>) {   <body/work> } int sum=0; for ( int i=0; i < 100; i += 2) {   sum+= i; } Console.WriteLine(“the sum is “+sum); 4/26/2018

25 Pseudocode –do while loop
REPEAT block of statements UNTIL condition DO WHILE condition ENDDO 4/3/2019

26 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

27 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

28 Pseudocode – while loop
WHILE condition = true block of statements ENDWHILE 4/3/2019

29 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

30 C# while loop while (<conditional>) { <body/work> }
int i = 0; while (i != 0) {    i -= 1; } Console.Write(“result is “+i); 4/26/2018


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

Similar presentations


Ads by Google