Lecture 1 Introduction Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard...

36
Lecture 1 Introduction Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

Transcript of Lecture 1 Introduction Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard...

Page 1: Lecture 1 Introduction Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.

Lecture 1

Introduction

Figures from Lewis, “C# Software Solutions”, Addison Wesley

Richard Gesick

Page 2: Lecture 1 Introduction Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.

Agenda

• Overview of Programming Languages• Basics of C# and the IDE• Errors• Intro to Objects and Classes

1-2

Page 3: Lecture 1 Introduction Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.

Programming Languages

• A programming language specifies the words and symbols that we can use to write a program

• A programming language employs a set of rules that dictate how the words and symbols can be put together to form valid program statements

1-3

Page 4: Lecture 1 Introduction Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.

Programming Languages

• Machine language• Assembly language• High-level languages

1-4

Page 5: Lecture 1 Introduction Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.

Machine & Assembly Languages• Machine language

– Written in binary or hex – Written using CPU instruction set– Difficult to write, and not portable

• Assembly language– Written using mnemonics for instructions and

symbolic names for variables– Assembler converts code to machine language– Easier to write, but still not portable

1-5

Page 6: Lecture 1 Introduction Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.

High-Level Languages

• Examples: Fortran, Perl, COBOL, C++, Java• Highly symbolic• Portable among CPU architectures• Languages can be designed for specific uses:

– Perl: Internet applications– Fortran: scientific applications– COBOL: business applications

1-6

Page 7: Lecture 1 Introduction Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.

High-Level Languages

• Compiled– Compiler converts source code (instructions and

data) into machine language, then program is executed

• Interpreted– Interpreter converts instructions into machine

language at run time as instructions are executed – Usually executes more slowly than compiled

program

1-7

Page 8: Lecture 1 Introduction Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.

1-8

Page 9: Lecture 1 Introduction Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.

Programming Basics

• Programming is translating a problem into ordered steps consisting of operations a computer can perform:– Input– Calculations– Comparisons of values– Moving data– Output

• The order of execution of instructions is called flow of control

1-9

Page 10: Lecture 1 Introduction Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.

Four Types of Flow of Control• Sequential Processing

– Execute instructions in order• Method Call

– Jump to code in method, then return• Selection

– Choose code to execute based on data value• Looping or Iteration

– Repeat operations for multiple data values

1-10

Page 11: Lecture 1 Introduction Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.

Program Development

• The mechanics of developing a program include several activities– writing the program in a specific programming language

– translating the program into a form that the computer can execute

– investigating and fixing various types of errors that can occur

• Software tools can be used to help with all parts of this process

1-11

Page 12: Lecture 1 Introduction Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.

Problem Solving

• The purpose of writing a program is to solve a problem

• Solving a problem consists of multiple activities:– Understand the problem– Design a solution– Consider alternatives and refine the solution– Implement the solution– Test the solution

• These activities are not purely linear – they overlap and interact

1-12

Page 13: Lecture 1 Introduction Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.

Problem Solving

• The key to designing a solution is breaking it down into manageable pieces

• When writing software, we design separate pieces that are responsible for certain parts of the solution

• An object-oriented approach lends itself to this kind of solution decomposition

• We will dissect our solutions into pieces called objects and classes

1-13

Page 14: Lecture 1 Introduction Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.

Program Skeleton

1-14

Page 15: Lecture 1 Introduction Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.

1-15

Page 16: Lecture 1 Introduction Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.

Games Example

1-16

using System;namespace CastleAttack{ static class Program { /// <summary> /// The main entry point for the application. /// </summary> static void Main(string[] args) { using (CastleAttack game = new CastleAttack( )) { game.Run(); } } }}

Page 17: Lecture 1 Introduction Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.

Identifiers

• Words in programs• Categories:

– We make up– Others make up– Reserved in the language

1-17

Page 18: Lecture 1 Introduction Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.

C# Identifiers

• Letter followed by zero or more letters and digits

• Case sensitive

1-18

Page 19: Lecture 1 Introduction Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.

1-19

Page 20: Lecture 1 Introduction Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.

1-20

Page 21: Lecture 1 Introduction Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.

White Space

• Spaces, blank lines, and tabs are called white space

• White space is used to separate words and symbols in a program

• Extra white space is ignored

• A valid C# program can be formatted many ways

• Programs should be formatted to enhance readability, using consistent indentation

1-21

Page 22: Lecture 1 Introduction Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.

Comments• Comments in a program are called inline

documentation

• They should be included to explain the purpose of the program and describe processing steps

• They do not affect how a program works

• C# comments can take two forms:

1-22

// this comment runs to the end of the line

/* this comment runs to the terminating symbol, even across line breaks */

Page 23: Lecture 1 Introduction Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.

Syntax and Semantics• The syntax rules of a language define how we can put

together symbols, reserved words, and identifiers to make a valid program

• The semantics of a program statement define what that statement means (its purpose or role in a program)

• A program that is syntactically correct is not necessarily logically (semantically) correct

• A program will always do what we tell it to do, not what we meant to tell it to do

1-23

Page 24: Lecture 1 Introduction Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.

Errors

• A program can have three types of errors• The compiler will find syntax errors and other basic

problems (compile-time errors)– If compile-time errors exist, an executable version of the

program is not created• A problem can occur during program execution, such

as trying to divide by zero, which causes a program to terminate abnormally (run-time errors)

• A program may run, but produce incorrect results, perhaps using an incorrect formula (logical errors)

1-24

Page 25: Lecture 1 Introduction Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.

Program Errors

• Compiler errors– Found by the compiler. – Usually caused by incorrect syntax or spelling

• Run-time errors– Reported by the system– Usually caused by incorrect use of prewritten classes or

invalid data

• Logic errors– Found by testing the program– Incorrect program design or incorrect execution of the

design1-25

Page 26: Lecture 1 Introduction Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.

Expect Errors

1-26

Page 27: Lecture 1 Introduction Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.

.NET

1-27

Page 28: Lecture 1 Introduction Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.

Program Structure

• In the C# programming language:– A program is made up of one or more classes– A class contains one or more methods– A method contains program statements

• These terms will be explored in detail throughout the course

• An application always contains a method called Main

1-28

Page 29: Lecture 1 Introduction Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.

Object-oriented Programming (OOP)

• Class – tool for encapsulating data and operations

(methods) into one package– defines a template or model for creating and

manipulating objects• Objects

– data created using the class and its methods– an object is an instance of the class– creating an object is instantiation

1-29

Page 30: Lecture 1 Introduction Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.

Objects

• An object has:– state - descriptive characteristics– behaviors - what it can do (or what can be done to it)

• The state of a bank account includes its current balance

• The behaviors associated with a bank account include the ability to make deposits and withdrawals

• Note that the behavior of an object might change its state

1-30

Page 31: Lecture 1 Introduction Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.

Classes• An object is defined by a class• A class is the blueprint of an object• The class uses methods to define the behaviors of

the object• The class that contains the main method of a C#

program represents the entire program• A class represents a concept, and an object

represents the embodiment of that concept• Multiple objects can be created from the same class

1-31

Page 32: Lecture 1 Introduction Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.

1-32

Page 33: Lecture 1 Introduction Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.

Objects and Classes

Bank Account

A class(the concept)

John’s Bank AccountBalance: $5,257

An object(the realization)

Bill’s Bank AccountBalance: $1,245,069

Mary’s Bank AccountBalance: $16,833

Multiple objectsfrom the same class

Page 34: Lecture 1 Introduction Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.

Class and Object Example

1-34

• How would you describe or define these pictures?

• What are the differences?

• What are the similarities?

• Are you describing a single entity or an entire group of entities?

Page 35: Lecture 1 Introduction Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.

What might the classes be? Objects?

Page 36: Lecture 1 Introduction Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.

Summary

• So what did you learn?• Name three important concepts from the first

two lectures.• What would I put on a test?