Lecture 2 - Kennesaw State University

Lecture 2 - Kennesaw State University

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 ; Example: int myNum; You can also initialize it when you declare it: int myNum = 42; double taxRate=.07; float interestRate=.065f; 4/26/2018 1-3

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

1-4 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 1-5

Pseudocode Console input Ps NAME = PRINT What is your name? READ user_input NAME user_input PRINT Hello +NAME 02/04/2020 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 1-7 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 1-8 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 1-9 From now on Well use generic PRINT() instead of: Console.WriteLine() System.out.println() Well use generic READ() instead of: Scanner scan = new Scanner (System.in); scan.nextLine(); Console.ReadLine()

4/26/2018 10 Pseudocode - IF Statement Ps 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 cant vote or drink ELSE IF (AGE <21) PRINT can vote but cant drink ELSE PRINT You can vote and drink, but dont vote shortly after drinking! ENDIF 02/04/2020 11

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 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 want to execute 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: () { } 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 Ps METHOD PrintMenu BEGIN PRINT "1. Display result" PRINT "2. Add a number" PRINT 3. Delete a number" PRINT "4. QUIT" END PrintMenu

02/04/2020 16 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

1-17 Pseudocode method menu with input and return Ps 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 02/04/2020 18 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 1-19 Pseudocode for loop Ps

FOR month = 1 to 12 block of statements ENDFOR 02/04/2020 20 for loops - Java and C# for(; ; ) { }

int sum = 0; for (int i=0; i < 100; i += 2) { sum+= i; } PRINT ("The sum is "+sum); 4/26/2018 1-21 Pseudocode do while loop Ps

REPEAT block of statements UNTIL condition DO block of statements WHILE condition ENDDO 02/04/2020 22 do while loop

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

Pseudocode while loop Ps WHILE condition = true block of statements ENDWHILE 02/04/2020 24 while loop while ()

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

Recently Viewed Presentations

  • Warp Processors - ics.uci.edu

    Warp Processors - ics.uci.edu

    Digital Design - Datapath Components Chapter 4 - Datapath Components
  • Title of Game 2-D Game Design

    Title of Game 2-D Game Design

    Outbreak Our prototype: Defend the center Control nodes - Show direction - Earn cash each round Build Babo soldiers - Equip with guns (right + left hand) - Upgrade damage - Increase ammo Fight the zombies - Theme of each...
  • Introduction to Biochemistry

    Introduction to Biochemistry

    Carbohydrates * Dr Seemal Jelani Introduction to Biochemistry*
  • Close Reading: - Midland Independent School District

    Close Reading: - Midland Independent School District

    CLOSE READING FINDING THE ... Concrete? ANALYZING DICTION Which words are… formal? informal? colloquial? slang? figurative? figures of speech? metaphors? ANALYZING SYNTAX Is it the usual subject-verb-object order, or is it inverted? Inverted syntax "Usual order, the words are not...
  • Exercise and Endothelium - تندرستی و علوم ورزشی

    Exercise and Endothelium - تندرستی و علوم ورزشی

    Blockage of endothelium-derived NO did not alter the exercise-induced hyperemia in skeletal muscle of cat or rabbit. Vascular Control in Skeletal Muscle at Rest and During Exercise In human, interabrachial infusion of NO inhibitors produced a decrease of forearm blood...
  • Office of Legal Services - Alamo Colleges District

    Office of Legal Services - Alamo Colleges District

    Education records may be released to anyone without consent from the student if all personally identifiable information has been removed. "Personally Identifiable Information" includes any information that is linked or "linkable" to a student. Please consult with Legal Services before...
  • 5 Things to Teach Your Kids About Money

    5 Things to Teach Your Kids About Money

    Money doesn't grow on trees, but it does grow in the bank. Saving is one of the best things you can do with your money. How: Have your kids set goals. Saving is easier when you have a goal in...
  • VCaaS R1 Commit - Catalyst Avaya

    VCaaS R1 Commit - Catalyst Avaya

    Just obtain your SSO log in, Access Avaya Universities IP Office Selling class and pass the assessment…You are ready to sell your first IP Office system! As you can see from the example, we boast a 25 - 35% partner...