ENGR/CS 101 CS Session Lecture 3

Similar documents
ENGR/CS 101 CS Session Lecture 4

ENGR/CS 101 CS Session Lecture 9

ENGR/CS 101 CS Session Lecture 13

Chapter 1: A First Program Using C#

SKILL AREA 304: Review Programming Language Concept. Computer Programming (YPG)

ENGR/CS 101 CS Session Lecture 5

Syntactic Analysis. CS345H: Programming Languages. Lecture 3: Lexical Analysis. Outline. Lexical Analysis. What is a Token? Tokens

CS101 Introduction to Programming Languages and Compilers

Introduction to Lexical Analysis

Exercise 1.1 Hello world

C++ Support Classes (Data and Variables)

INFS 214: Introduction to Computing

printf( Please enter another number: ); scanf( %d, &num2);

Full file at

CS2900 Introductory Programming with Python and C++ Kevin Squire LtCol Joel Young Fall 2007

CS31 Discussion 1E. Jie(Jay) Wang Week3 Oct.12

Chapter 11 Introduction to Programming in C

Static Semantics. Lecture 15. (Notes by P. N. Hilfinger and R. Bodik) 2/29/08 Prof. Hilfinger, CS164 Lecture 15 1

Programmiersprachen (Programming Languages)

IT 374 C# and Applications/ IT695 C# Data Structures

Lab 1: First Steps in C++ - Eclipse

Fall 2017 CISC124 9/16/2017

Syntax Errors; Static Semantics

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

Fundamentals of Programming Session 4

COP4020 Programming Languages. Compilers and Interpreters Robert van Engelen & Chris Lacher

Chapter 11 Introduction to Programming in C

Visual C# Instructor s Manual Table of Contents

Structured Programming Using C++ Lecture 2 : Introduction to the C++ Language. Dr. Amal Khalifa. Lecture Contents:

Principles of computer programming. Profesor : doc. dr Marko Tanasković Assistent : doc. dr Marko Tanasković

Programming with Java

CS1500 Algorithms and Data Structures for Engineering, FALL Virgil Pavlu, Jose Annunziato,

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

COMP Primitive and Class Types. Yi Hong May 14, 2015

PYTHON- AN INNOVATION

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

Introduction to Programming Using Java (98-388)

COSC121: Computer Systems: Runtime Stack

Introduction to Programming

CS31 Discussion 1E. Jie(Jay) Wang Week5 Oct.27

Monty Python and the Holy Grail (1975) BBM 101. Introduction to Programming I. Lecture #03 Introduction to Python and Programming, Control Flow

CS558 Programming Languages

ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java

Data Types. (with Examples In Haskell) COMP 524: Programming Languages Srinivas Krishnan March 22, 2011

G Programming Languages Spring 2010 Lecture 6. Robert Grimm, New York University

1 Lexical Considerations

Chapter 1 Getting Started

COE318 Lecture Notes Week 3 (Week of Sept 17, 2012)

Basics of Java Programming

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

CSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

9/5/2018. Overview. The C Programming Language. Transitioning to C from Python. Why C? Hello, world! Programming in C

Chapter 11 Introduction to Programming in C

Chapter 11 Introduction to Programming in C

G52CPP C++ Programming Lecture 3. Dr Jason Atkin

Sixth lecture; classes, objects, reference operator.

6.096 Introduction to C++ January (IAP) 2009

Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah. Lecturer Department of Computer Science & IT University of Balochistan

CSE450. Translation of Programming Languages. Lecture 11: Semantic Analysis: Types & Type Checking

COMPILER DESIGN LECTURE NOTES

Chapter 3 Syntax, Errors, and Debugging. Fundamentals of Java

The C Programming Language. (with material from Dr. Bin Ren, William & Mary Computer Science)

Lecture 7. Log into Linux New documents posted to course webpage

Principles of Programming Languages. Lecture Outline

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

C Concepts - I/O. Lecture 19 COP 3014 Fall November 29, 2017

CSc Introduction to Computing

CS-201 Introduction to Programming with Java

A Pascal program. Input from the file is read to a buffer program buffer. program xyz(input, output) --- begin A := B + C * 2 end.

CS 3360 Design and Implementation of Programming Languages. Exam 1

Topic 1: Programming concepts

Lecture 12: Data Types (and Some Leftover ML)

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

CS 115 Lecture 8. Selection: the if statement. Neil Moore

Compiling and Interpreting Programming. Overview of Compilers and Interpreters

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

Lexical Considerations

Chapter 2: Data and Expressions

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.

Java Fall 2018 Margaret Reid-Miller

Scientific Computing

22c:111 Programming Language Concepts. Fall Types I

CS313D: ADVANCED PROGRAMMING LANGUAGE

Outline. Parts 1 to 3 introduce and sketch out the ideas of OOP. Part 5 deals with these ideas in closer detail.

Chapter 2: Programming Concepts

Chapter 11 Introduction to Programming in C

COMP1730/COMP6730 Programming for Scientists. Data: Values, types and expressions.

An Introduction to Python (TEJ3M & TEJ4M)

Chapter 2. Lexical Elements & Operators

Introduction To Java. Chapter 1. Origins of the Java Language. Origins of the Java Language. Objects and Methods. Origins of the Java Language

Outline. Introduction to Programming (in C++) Introduction. First program in C++ Programming examples

Final CSE 131B Spring 2004

Introduction to Programming (Java) 2/12

Full file at

Compiling Regular Expressions COMP360

CMPT 125: Lecture 3 Data and Expressions

CS 106A, Lecture 9 Problem-Solving with Strings

Transcription:

ENGR/CS 101 CS Session Lecture 3 Log into Windows/ACENET (reboot if in Linux) Start Microsoft Visual Studio 2010 Windows button -> All Programs -> 02 Programming -> Microsoft Visual Studio 2010 -> Microsoft Visual Studio 2010 Choose C# as default environment, click Start Visual Studio button Wait for a long time... (next time should be faster) Lecture 3 ENGR/CS 101 Computer Science Session 1

Registration Session The registration session has been moved to next class, Wednesday, November 2 (instead November 7 in the syllabus) Students should try to pre-register for courses. Guidebooks are available in EECS office. Students will meet in groups based on major: CS students - KC-267 with Dr. Hwang CoE students - KC-137 with Dr. Mitchell EE students - KC-136 with Dr. Howe Lecture 3 ENGR/CS 101 Computer Science Session 2

Outline Program specification Program design Programming languages Using MS Visual Studio C# programming language Types and variables Assignment and expressions Input and output Lecture 3 ENGR/CS 101 Computer Science Session 3

Program Specification Work our way up to a GUI program to apply the Vigenere cipher to a text file. Today's console program will do the following: Ask the user for an uppercase key letter (to represent shift A-> key) and an uppercase letter to encipher with this shift Output the corresponding ciphertext letter Example run (user input in bold): Enter an uppercase key letter: I Enter an uppercase letter to encipher: G The corresponding ciphertext letter is: O Lecture 3 ENGR/CS 101 Computer Science Session 4

Program Design How will the program accomplish the specifications? Identify the data being used shift key, plaintext letter, ciphertext letter Write the steps of an algorithm 1. Get the shift key letter from the user 2. Get the plaintext letter from the user 3. Compute the ciphertext letter 4. Output the ciphertext letter to the screen Lecture 3 ENGR/CS 101 Computer Science Session 5

Programming Languages Syntax: What are the legal "sentences" in the language? Semantics: What do the "sentences" mean? Compilers and interpreters enforce syntax. Semantics determine whether the computation is correct. A program is not "working" if it gives the wrong results! Lecture 3 ENGR/CS 101 Computer Science Session 6

Compiling vs. Interpreting Some languages are compiled with a program called a compiler. Source code file is translated into a machine code file. Examples: C/C++, Java, Pascal, COBOL, Fortran Other languages are interpreted. An interpreter is a program that receives programming language statements and executes them directly. Examples: (original) BASIC, LISP, Prolog, LOGO Lecture 3 ENGR/CS 101 Computer Science Session 7

Source Code to Running Program EDITOR -> programming language source file -> COMPILER -> object file (+ libraries) -> LINKER -> executable file -> LOADER -> running program Sometimes programs are run individually; sometimes all work together in an Integrated Development Environment (IDE) Lecture 3 ENGR/CS 101 Computer Science Session 8

Microsoft Visual Studio Microsoft Visual Studio is an IDE for developing applications for Windows in multiple programming languages, including C#. Free Express versions of each individual compiler are available. Link to the download page on the CS session webpage. EECS students can get the full version through UE's MS Alliance program (see Jeff Cron). Lecture 3 ENGR/CS 101 Computer Science Session 9

Creating a Console Project All C# code belongs to a project. Start with New -> Project Each project produces a particular kind of application. We will be creating a console application. After selecting the console application template, set the Name box to "cs101console". Make sure the Location is on your network drive, then click OK. A large text window and a Solution Explorer panel will appear. Lecture 3 ENGR/CS 101 Computer Science Session 10

New Project Dialog Make sure this is on your network drive! Lecture 3 ENGR/CS 101 Computer Science Session 11

C# Programs The C# IDE tries to be helpful by creating the parts of code that all C# program have. This includes: using statements that cause (pre-defined) method names in libraries like System to become known namespace and class definition names based on the project name given a stub for the Main( ) method. The main program code goes in this stub. It is the code that is executed first when a program is run. Lecture 3 ENGR/CS 101 Computer Science Session 12

MS VS Project Window Project code goes here! Lecture 3 ENGR/CS 101 Computer Science Session 13

C# Programming Language Developed by Microsoft for.net framework Syntax similar to C++ and Java Semantics similar to Java Object-oriented - won't cover in this class Built-in support to make GUIs (Graphical User Interfaces) - will look at this next class Lecture 3 ENGR/CS 101 Computer Science Session 14

Types and Variables A variable is a named memory location that holds a value. All memory is in bits. A variable has a type that determines how the bits are interpreted into a value. Numbers are in binary. Characters are mapped to binary numbers. E.g., ASCII or Unicode. C# types include int for integers (e.g., 5, -25, 0) char for characters (e.g. 'A', 'b', '7', '%') string (e.g. "Hello!") Lecture 3 ENGR/CS 101 Computer Science Session 15

Types and Variables Variables are declared by giving type and name Syntax is: <type> <var1>, <var2>,..., <varn>; Examples: char shiftkey, // key letter plainletter, // user input cipherletter;// result int shiftnumber, // # of shift places index; // of cipher letter // marks the beginning of a comment to the end of the line Lecture 3 ENGR/CS 101 Computer Science Session 16

Assignment and Expressions Assignment means to store a value into a variable. Syntax is: <var> = <expression>; The expression is evaluated and the result is stored in the variable. An expression can be: A literal. E.g., 12 or 'A' A variable. E.g., shiftnumber A function call. (More on this later.) An expression of one or more literals, variables, or function calls. Lecture 3 ENGR/CS 101 Computer Science Session 17

Assignment and Expressions Examples: shiftkey = 'I'; plainletter = shiftkey; shiftnumber = shiftkey 'A'; shiftkey = char.parse(system.console.readline()); Lecture 3 ENGR/CS 101 Computer Science Session 18

Computing the Cipher Letter Assume that variable shiftkey holds the key letter and variable plainletter holds the letter to be enciphered. Since the alphabetic characters have sequential mapping (i.e., 'A' is first, followed by 'B', etc.), the number of places to shift is the key letter minus 'A'. In C# code, this is: shiftnumber = shiftkey 'A'; Lecture 3 ENGR/CS 101 Computer Science Session 19

Computing the Cipher Letter To find the cipher letter, we determine the index of plaintext letter (i.e., where in the alphabet it is when we start counting at 0) using a similar method, then add the shift number. This will be the index of the ciphertext letter, except that the number may be greater than 26. To make it circular, we compute the modulus with respect to 26. In code, this is: index = (plainletter 'A' + shiftnumber) % 26; The modulus operator symbol is % Lecture 3 ENGR/CS 101 Computer Science Session 20

Computing the Cipher Letter Now we add this new index back to 'A' to find the ciphertext letter. However, C# is strict about types and is unhappy that we are trying to add a number to a character, so we have to tell the compiler to treat 'A' as a number, then treat the result as a character by casting. The code becomes: cipherletter = (char) ((int) 'A' + index); Lecture 3 ENGR/CS 101 Computer Science Session 21

Output C# output is done by calling a built-in function that takes a string as an argument. There are two forms: System.Console.Write ( ) - displays string to screen System.Console.WriteLine ( ) - displays string to screen followed by a newline character Strings can created by concatenating the items to be displayed using +. Lecture 3 ENGR/CS 101 Computer Science Session 22

Output Examples: System.Console.Write ("Enter an uppercase key letter: "); System.Console.WriteLine ("The corresponding ciphertext letter is: " + cipherletter); Lecture 3 ENGR/CS 101 Computer Science Session 23

Input C# input is done by calling a built-in function that has no arguments and returns a string that contains what the user typed in. System.Console.Read( ) Since all input is in the form of a string, it must be converted to the appropriate type before assignment. Each type has a Parse function for this purpose. E.g. shiftkey = char.parse(system.console.read()); Lecture 3 ENGR/CS 101 Computer Science Session 24

Review: Program Design Identify the data being used - what are their types? shift key (char), plaintext letter (char), ciphertext letter (char), shift number (int), index (int) Write the steps of an algorithm 1. Get the shift key letter from the user 2. Get the plaintext letter from the user 3. Compute the ciphertext letter - more details - Compute shift number - Compute index of ciphertext letter - Compute the ciphertext letter 4. Output the ciphertext letter to the screen Lecture 3 ENGR/CS 101 Computer Science Session 25

Putting the Code Together // This code goes in the place indicated on Slide 12 // Variable declarations char shiftkey, // key letter plainletter, // user input cipherletter;// result int shiftnumber, // # of shift places index; // of cipher letter // Get the key letter and a letter to encipher from user System.Console.Write("Enter an uppercase key letter: "); shiftkey = char.parse(system.console.readline()); System.Console.Write ("Enter an uppercase letter to encipher: "); plainletter = char.parse(system.console.readline()); Lecture 3 ENGR/CS 101 Computer Science Session 26

Putting the Code Together // Compute the corresponding ciphertext letter shiftnumber = shiftkey 'A'; index = (plainletter 'A' + shiftnumber) % 26; cipherletter = (char)((int)'a' + index); // Display the result System.Console.WriteLine ("The corresponding ciphertext letter is: " + cipherletter); Lecture 3 ENGR/CS 101 Computer Science Session 27

Building a Program To build (i.e., compile) the program do Build -> Build Solution If there are no syntax errors, great! If there are syntax errors, they will be listed at the bottom of the screen. Correct and build again. Lecture 3 ENGR/CS 101 Computer Science Session 28

Running a Console Program To run the program, do Debug -> Start Without Debugging. This will start the console window and run the program. Lecture 3 ENGR/CS 101 Computer Science Session 29