CSE 1321 Modules 1-5 Review Spring 2019

Slides:



Advertisements
Similar presentations
2.1 Program Construction In Java
Advertisements

AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
INSTRUCTOR: SHIH-SHINH HUANG Windows Programming Using Java Chapter4: Control Statements Part I.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
Program Design and Development
Classes, methods, and conditional statements We’re past the basics. These are the roots.
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
Week #2 Java Programming. Enable Line Numbering in Eclipse Open Eclipse, then go to: Window -> Preferences -> General -> Editors -> Text Editors -> Check.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 4.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
CIS Computer Programming Logic
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Computer Science Selection Structures.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
Input, Output, and Processing
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 3.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
F27SA1 Software Development 1 3. Java Programming 2 Greg Michaelson.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) public static void main(String[]
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
CHAPTER 4 GC 101 Data types. DATA TYPES  For all data, assign a name (identifier) and a data type  Data type tells compiler:  How much memory to allocate.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
FUNDAMENTALS 2 CHAPTER 2. OPERATORS  Operators are special symbols used for:  mathematical functions  assignment statements  logical comparisons 
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
Lecture 7: Menus and getting input. switch Multiple-selection Statement switch Useful when a variable or expression is tested for all the values it can.
© 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
CompSci 230 S Programming Techniques
ALGORITHMS AND FLOWCHARTS
CSC111 Quick Revision.
Topics Designing a Program Input, Processing, and Output
Chapter 4 Assignment Statement
REPETITION CONTROL STRUCTURE
Control Statements: Part 2
The Selection Structure
User input We’ve seen how to use the standard output buffer
Chapter 4 – Control Structures Part 1
Lecture 4B More Repetition Richard Gesick
IDENTIFIERS CSC 111.
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Coding Concepts (Sub- Programs)
MSIS 655 Advanced Business Applications Programming
Introduction to Primitive Data types
Module 4 Loops and Repetition 2/1/2019 CSE 1321 Module 4.
Module 3 Selection Structures 2/19/2019 CSE 1321 Module 3.
elementary programming
Topics Designing a Program Input, Processing, and Output
Java Programming Loops
Topics Designing a Program Input, Processing, and Output
Loops CGS3416 Spring 2019 Lecture 7.
Module 3 Selection Structures 6/25/2019 CSE 1321 Module 3.
CSE 1321 Modules 6-8 Review Spring 2019
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Lesson 3. Controlling program flow. Loops. Methods. Arrays.
Introduction to Primitive Data types
Presentation transcript:

CSE 1321 Modules 1-5 Review Spring 2019 Introduce yourself and welcome the students

Module 1 Algorithms and Abstraction Algorithms are: A set of logical steps to accomplish a specific task A set of direction/instructions Abstraction is: LOGICAL GROUPING of concepts or objects Hide the details (in functions or methods) We like to think at a high level of abstraction (no details) Too many details overwhelm us!

Algorithm in a Flowchart for Sorting Sample flowchart for a sorting algorithm. This flowchart illustrates the conditional constructs, loops, and other elements of control flow that comprise an algorithm for sorting, from smallest to largest, an arbitrary list of numbers (the algorithm is known as “bubble sort”). In this type of diagram, arrows symbolize the flow of logic (control flow), rounded rectangles mark the start and end points, slanted parallelograms indicate I/O (e.g., a user-provided list), rectangles indicate specific subroutines or procedures (blocks of statements), and diamonds denote conditional constructs (branch points). Note that this sorting algorithm involves a pair of nested loops over the list size (blue and orange), meaning that the calculation cost will go as the square of the input size (here, an N-element list); this cost can be halved by adjusting the inner loop conditional to be “”, as the largest i elements will have already reached their final positions Image and Text from https://www.researchgate.net/figure/Sample-flowchart-for-a-sorting-algorithm-This-flowchart-illustrates-the-conditional_fig7_303337342

Ps Pseudocode Skeleton BEGIN MAIN END MAIN Note: every time you BEGIN something, you must END it! Write them as pairs!

C# Skeleton using System; class MainClass { public static void Main (string[] args) } Note: The opening “{“ means BEGIN and the closing “}” means END

Java Skeleton class MainClass { public static void main (String[] args) { } Note: Capitalization matters!

Python Skeleton def main(): pass if __name__ == '__main__': main()

Pseudocode Variables & Input BEGIN MAIN CREATE inputNum PRINT(“Please enter a number: “) inputNum ← READ from user input END MAIN

C# Variables & Input using System; class MainClass { Console.Write("Please enter a number: "); int inputNum = Convert.ToInt16(Console.ReadLine()); }

Java Variables & Input import java.util.Scanner; public class TestApp { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Please enter a number: "); int inputNum = input.nextInt(); } }

Python Variables & Input def main(): inputNum = int(input("Please enter a number: ")) if __name__ == '__main__': main()

Escape Sequences Printing and escape sequence prints a special character in an output string. Common escape sequences: \b backspace. (e.g “B\bsecz\bret” prints what?) \t tab \n newline \r carriage return \” double quote \’ single quote \\ backslash

Data Types 8 simple data types: 4 subsets of integers (int, long, short, byte) 2 subsets of floating point numbers (float and double) Character Boolean Complex Data Types: String Classes Arrays

CONSTANTS and Literals CONSTANTS are values that cannot change while the program is running. E.g.: MAX_WEIGHT = 600 ELEVATOR_LIMIT = 10 * MAX_WEIGHT Literals are the values that are entered into the code: E.g. : ELEVATOR_LIMIT = 6000

Order of Operations

Shortcut Operators ++ increment by 1 -- decrement by 1 += Add value on the right of the symbol to the variable on the left and store the result in the variable on the left. -= Subtract value on the right of the symbol from the variable on the left and store the result in the variable on the left. *= Multiply the variable on the left by the value on the right of the symbol and store the result in the variable /= Divide the variable on the left by the value on the right of the symbol and store the result in the variable %= Divide the variable on the left by the value on the right and store the resulting remainder in the variable.

Relational and Logical Operators Used to create a true or false expression: Relational: > greater than < less than == equals (note the double equals) ! not != not equal >= greater than or equal <= less than or equal Logical: Logical NOT (only works on one operand) Logical AND (requires two operands) Logical OR (requires two operands) Exclusive OR

IF Statements Ps Builds on the Pseudocode from Slide 8 BEGIN MAIN CREATE inputNum PRINT(“Please enter a number: “) inputNum ← READ from user input IF (inputNum % 2 == 0) PRINTLINE(“That number is even.”) END MAIN

Ps IF-ELSE Statements Builds on the Pseudocode from Slide 18 BEGIN MAIN CREATE inputNum PRINT(“Please enter a number: “) inputNum ← READ from user input IF (inputNum % 2 == 0) PRINTLINE(“That number is even.”) ELSE PRINTLINE(“That number is odd.”) END IF END MAIN

IF-ELSE-IF Statements Ps Builds on the Pseudocode from Slide 19 BEGIN MAIN CREATE inputNum PRINT(“Please enter a number: “) inputNum ← READ from user input IF (inputNum % 2 == 0) PRINTLINE(“That number is even.”) ELSE IF (inputNum % 2 != 0) PRINTLINE(“That number is odd.”) ELSE PRINTLINE(“You did not enter a number!”) END IF END MAIN

SWITCH/CASE Statements Different approach to the same problem (determining if a number is even or odd) BEGIN MAIN CREATE inputNum PRINT(“Please enter a number: “) inputNum ← READ from user input SWITCH(inputNum % 2) CASE 0: PRINTLINE(“That number is even.”) BREAK CASE 1: PRINTLINE(“That number is odd.”) CASE default: (“You did not enter a number!”) END SWITCH END MAIN Ps

In-Class Exercise 1 2 3 4 5 6 7 8 9 CLOSE OPEN Write a IF…ELSE…IF or SWITCH/CASE solution to the problem below: You have been tasked with writing the code to program an elevator for a skyscraper that is being built in Downtown Atlanta. Due to a new elevator design, there are no individual floor buttons, passengers simply press buttons to enter their desired floor (like inputting a PIN). In addition, there is no 13th floor and the top floor is 100. 1 2 3 4 5 6 7 8 9 CLOSE OPEN

In-Class Exercise Solution BEGIN MAIN CREATE inputFloor PRINT(“Please enter a floor using keypad: “) inputFloor ← READ from user input SWITCH(inputFloor) CASE 0: PRINTLINE(“Returning to ground floor.”) BREAK CASE 1: PRINTLINE(“Moving to 1st floor.”) CASE 2: PRINTLINE(“Moving to 2nd floor.”) BREAK //skipped floors 3-99 CASE 100: PRINTLINE(“Moving to 100th floor.”) DEFAULT: PRINTLINE(“Staying put, that floor does not exist”) END SWITCH END MAIN

WHILE Loops – Repetition and Validation Builds on the Pseudocode from Slide 20 BEGIN MAIN CREATE inputNum = 1 WHILE (inputNum > 0) PRINT(“Please enter a number: “) inputNum ← READ from user input IF (inputNum % 2 == 0) PRINTLINE(“That number is even.”) ELSE IF (inputNum % 2 != 0) PRINTLINE(“That number is odd.”) ELSE PRINTLINE(“You did not enter a number!”) END IF END WHILE END MAIN

DO…WHILE Loops - Repetition and Validation Builds on the Pseudocode from Slide 20 BEGIN MAIN CREATE inputNum DO PRINT(“Please enter a number: “) inputNum ← READ from user input BEGIN WHILE IF (inputNum % 2 == 0) PRINTLINE(“That number is even.”) ELSE IF (inputNum % 2 != 0) PRINTLINE(“That number is odd.”) ELSE PRINTLINE(“You did not enter a number!”) END IF END WHILE WHILE (inputNum > 0) END MAIN

Ps FOR Loops - Repetition Builds on the Pseudocode from Slide 20 BEGIN MAIN CREATE inputNum PRINT(“Please enter a number: “) inputNum ← READ from user input FOR i is 0 to inputNum by 1 BEGIN FOR IF (i % 2 == 0) PRINTLINE(i + “ is even.”) ELSE IF (inputNum % 2 != 0) PRINTLINE(i + “ is odd.”) ELSE PRINTLINE(“Not a number!”) END IF END FOR END MAIN

In-Class Exercise Next Stop: 1 2 3 4 5 6 7 8 9 CLOSE OPEN Write a WHILE or DO…WHILE solution to the problem below: You have been asked to update the elevator code to keep track of which floor the elevator is going to by incorporating a display which shows the next floor to be visited (only one floor can be entered at a time), the variable to display the next stop is called NextStop. Remember, there is no 13th floor and the top floor is 100. Next Stop: 1 2 3 4 5 6 7 8 9 CLOSE OPEN

In-Class Exercise Solution BEGIN MAIN CREATE NextStop = 0 DO PRINT(“Please enter a floor using keypad ‘0 to shut elevator down’: “) NextStop ← READ from user input SWITCH(NextStop) CASE 1: PRINTLINE(“Moving to 1st floor.”) NextStop = 1 BREAK //skipped floors 2-99 (13th is covered by default) CASE 100: PRINTLINE(“Moving to 100th floor.”) NextStop = 100 BREAK DEFAULT : PRINTLINE(“Staying put, that floor does not exist!”) END SWITCH WHILE (NextStop > 0) END MAIN

Methods Provides ability to reuse code more easily Can perform calculations and/or return a value to method call Must send in the required number of parameters as well as matching parameter data types Method header is the method declaration (usually the top line of the method) Method signature is the combination of the method name and the parameter list.

Methods Parameters are passed by reference or by value to a method By reference means that the method is given a pointer to the parameter’s memory space and any changes made are permanent By value means that the method is given a copy of the parameter’s value and the original memory space is left untouched. Methods either have a return type or must be void Return types can be of any data type, there must be a RETURN statement if a return type is declared in the header Void means that no data is returned from the method to the calling code

In-Class Exercise Next Stops: 1 2 3 4 5 6 7 8 9 CLOSE OPEN Write METHODs for the problem below: You have been asked to update the elevator code to write a method that determines which floor the elevator should stop at next based on the elevator’s current floor and the desired stops that have been entered (up to two can be stored at a time now in separate variables). This method should read in the next two floors, the current floor and return a single value for the next floor to move the elevator to. You must also write a method that checks the weight of the current passengers against the ELEVATOR_LIMIT to make sure the elevator can move. This method should return a Boolean value and print a message that it cannot move to being overloaded. Remember, there is no 13th floor and the top floor is 100. Also, convert the main program into a method for ease of use. Next Stops: 1 2 3 4 5 6 7 8 9 CLOSE OPEN

In-Class Exercise Solution METHOD BOOLEAN OVERLIMIT(parameters: PassengersWeightTotal) BEGIN METHOD IF (PassengersWeightTotal >= ELEVATOR_LIMIT) PRINTLINE(“Unable to move, elevator weight limit exceeded, please exit elevator.”) RETURN TRUE ELSE RETURN FALSE END IF END METHOD METHOD INTEGER NextFloorToVisit(parameters: Floor1, Floor2, curFloor) CREATE NextFloor = curFloor IF (|curFloor – Floor1| < |curFloor – Floor2|) NextFloor = Floor1 NextFloor = Floor2 RETURN NextFloor

In-Class Exercise Solution METHOD VOID goToFloor(parameters: NextStop) BEGIN METHOD SWITCH(NextStop) CASE 0: PRINTLINE(“Returning to ground floor.”) NextStop = 0, currentFloor = 0 BREAK CASE 1: PRINTLINE(“Moving to 1st floor.”) NextStop = 1, currentFloor = 1 BREAK //skipped floors 2-98 (13 is covered by the DEFAULT statement) CASE 99: PRINTLINE(“Moving to 99th floor.”) NextStop = 99, currentFloor = 99 CASE 100: PRINTLINE(“Moving to 100th floor.”) NextStop = 100, currentFloor = 100 DEFAULT: PRINTLINE(“Staying put, that floor does not exist!”) NextStop = currentFloor, currentFloor = currentFloor END SWITCH END METHOD

In-Class Exercise Solution Concluded BEGIN MAIN CREATE inputFloor1, inputFloor2 = 0, NextStop = 0, currentFloor = 0 DO PRINT(“Please enter a floor using keypad ‘0 to shut elevator down‘: “) inputFloor1 ← READ from user PRINT(“Please enter a floor using keypad ‘0 to shut elevator down’: “) inputFloor2 ← READ from user NextStop ← NextFloorToVisit(inputFloor1, inputFloor2, currentFloor) IF (! OVERLIMIT) goToFloor(NextStop) END If WHILE (inputNum != 0) END DO…WHILE END MAIN

End of Review Questions?