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

Slides:



Advertisements
Similar presentations
Building Java Programs Chapter 5 Program Logic and Indefinite Loops Copyright (c) Pearson All rights reserved.
Advertisements

Reading and Writing to the Console Svetlin Nakov Telerik Corporation
Simple Programs from Chapter 2 Putting the Building Blocks All Together (corresponds with Chapter 2)
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Building Java Programs Program Logic and Indefinite Loops.
CS-1010 Dr. Mark L. Hornick 1 Selection and Iteration and conditional expressions.
Expressions and Order of Operations Operators – There are the standard operators: add, subtract, divide, multiply – Note that * means multiply? (No times.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Lecture 2 Review of 1301 and C# Richard Gesick. Common data types int, long, double, float, char, bool, string.
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
CS0007: Introduction to Computer Programming The for Loop, Accumulator Variables, Seninel Values, and The Random Class.
1 BUILDING JAVA PROGRAMS CHAPTER 5 PROGRAM LOGIC AND INDEFINITE LOOPS.
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
Building Java Programs Chapter 4 Lecture 4-1: Scanner ; cumulative algorithms reading: 3.3 – 3.4, 4.2.
CPSC 233 Tutorial January 21 st /22 nd, Linux Commands.
Topics introduced today (these topics would be covered in more detail in later classes) – Primitive Data types Variables Methods “for” loop “if-else” statement.
CompSci 230 S Programming Techniques
C# Basic Syntax, Visual Studio, Console Input / Output
User-Written Functions
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Pseudocode Key Revision Points.
Chapter 2 Basic Computation
Using the Console.
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
COMP 170 – Introduction to Object Oriented Programming
Repetition (While-Loop) version]
Variables, Expressions, and IO
Review If you want to display a floating-point number in a particular format use The DecimalFormat Class printf A loop is… a control structure that causes.
Building Java Programs
Variables, Printing and if-statements
Java Programming: From Problem Analysis to Program Design, 4e
Chapter 2 Basic Computation
Topic 11 Scanner object, conditional execution
Building Java Programs
Control Statement Examples
Building Java Programs
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Building Java Programs
Selection (if-then-else)
Variables, Loops, Decision Statements, etc
An Introduction to Java – Part I, language basics
Chapter 2: Basic Elements of Java
Building Java Programs
Building Java Programs
Module 4 Loops and Repetition 2/1/2019 CSE 1321 Module 4.
Building Java Programs
Building Java Programs
Building Java Programs
Module 4 Loops and Repetition 4/7/2019 CSE 1321 Module 4.
CSE Module 1 A Programming Primer
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Unit 3: Variables in Java
Indefinite loop variations
Building Java Programs
Building Java Programs
Chapter 4 Lecture 4-1: Scanner; if/else reading: 3.3 – 3.4, 4.1, 4.5
LCC 6310 Computation as an Expressive Medium
CSE Module 1 A Programming Primer
Module 3 Selection Structures 6/25/2019 CSE 1321 Module 3.
Building Java Programs
Lecture 1 Review of 1301/1321 CSE /26/2018.
Presentation transcript:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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