CS_OOP Lecture #2: Computer Hardware/Software; Variables, C# Data Types, and IO.

Slides:



Advertisements
Similar presentations
Getting Started with C# 1 SWE 344 Internet Protocols & Client Server Programming.
Advertisements

Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
Constants and Data Types Constants Data Types Reading for this class: L&L,
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
Getting Started with C#.  Name: Ahmed Galib Reza   Lab: Ubiquitous Computing Lab  Nationality: Bangladeshi  Education:
COSC 120 Computer Programming
ECE122 L2: Program Development February 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 2 Program Development.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 1 Introduction.
String Escape Sequences
CS 0008 Day 2 1. Today Hardware and Software How computers store data How a program works Operators, types, input Print function Running the debugger.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Basic Elements of C++ Chapter 2.
1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 1 Introduction to Computers and Programming.
Chapter Introduction to Computers and Programming 1.
CSC 125 Introduction to C++ Programming Chapter 1 Introduction to Computers and Programming.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
1 Course Lectures Available on line:
1 Variables, Constants, and Data Types Primitive Data Types Variables, Initialization, and Assignment Constants Characters Strings Reading for this class:
CSCI 1100/1202 January 16, Why do we need variables? To store intermediate results in a long computation. To store a value that is used more than.
Elements of a C++ program 1. Review Algorithms describe how to solve a problem Structured English (pseudo-code) Programs form that can be translated into.
General Computer Science for Engineers CISC 106 Lecture 02 Dr. John Cavazos Computer and Information Sciences 09/03/2010.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 1: Introduction to Computers and Programming.
Input & Output: Console
The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character.
CSE 1301 Lecture 2 Data Types Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Introduction to Computer Systems and the Java Programming Language.
Chapter 2: Using Data.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
 JAVA Compilation and Interpretation  JAVA Platform Independence  Building First JAVA Program  Escapes Sequences  Display text with printf  Data.
Lecture #5 Introduction to C++
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs.
 Character set is a set of valid characters that a language can recognise.  A character represents any letter, digit or any other sign  Java uses the.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs -
Assignment An assignment statement changes the value of a variable The assignment operator is the = sign total = 55; Copyright © 2012 Pearson Education,
© 2004 Pearson Addison-Wesley. All rights reserved ComS 207: Programming I Instructor: Alexander Stoytchev
Computing with C# and the.NET Framework Chapter 2 C# Programming Basics ©2003, 2011 Art Gittleman.
C# C1 CSC 298 Elements of C# code (part 1). C# C2 Style for identifiers  Identifier: class, method, property (defined shortly) or variable names  class,
© 2011 Pearson Education, publishing as Addison-Wesley Monday  Class Requirements  Earn hawk points today with the Name Game  Network Login.
© 2006 Pearson Education Chapter 1: Computer Systems.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
Chapter 2 Input, Variables and Data Types. JAVA Input JAVA input is not straightforward and is different depending on the JAVA environment that you are.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types.
1.2 Primitive Data Types and Variables
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.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 1: Introduction to Computers and Programming.
Programming with Java. Chapter 1 Focuses on: –components of a computer –how those components interact –how computers store and manipulate information.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Lecture 2 Data Types Richard Gesick.
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Computing Fundamentals
Multiple variables can be created in one declaration
Java programming lecture one
Computing with C# and the .NET Framework
Escape Sequences What if we wanted to print the quote character?
IDENTIFIERS CSC 111.
Chapter 2: Java Fundamentals
Chapter 3 – Introduction to C# Programming
Focus of the Course Object-Oriented Software Development
Visual Programming COMP-315
Presentation transcript:

CS_OOP Lecture #2: Computer Hardware/Software; Variables, C# Data Types, and IO

2 Outline  Review  Computer hardware/software  Variables and C# data types  Input and output

3 Review r Different programming language levels m Machine code, assembly code, intermediate language, high level language m A compiler translates a program from a higher level language to a lower level language r C# compiler compiles a C# program to MSIL r Structure of a C# program m A program consists of one or more classes m A class consists of one or more methods m A method consists of one or more statements m White space and comments, identifiers, keywords, namespace r Our first C# programs

4 C# Program Structure (Console)  Program specifications (optional) //========================================================== // // File: HelloWorld.cs CS112 Demo // // Author: Zhong Shao // // Classes: HelloWorld // // This program prints the string "Hello World!” // //========================================================== r Library imports (optional) using System;  Class and namespace definitions class HelloWorld { static void Main(string[] args) { Console.WriteLine(“Hello World!”); }

5 C# Program Structure (Window)  Program specifications (optional) //========================================================== // // File: HelloWorld.cs CS112 Demo // // Author: Zhong Shao // // Classes: HelloWorld // // This program shows a message box. // //========================================================== r Library imports (optional) using System; using System.Windows.Forms; r Class and namespace definitions class HelloWorld { static void Main(string[] args) { MessageBox.Show(“Hello World!”); }

6 Outline  Review  Computer hardware/software  Variables and C# data types  Input and output

7 Outline  Admin. and review  Computer hardware/software  Variables and C# data types

8 Computer Environment: Hardware r Hardware m the physical, tangible parts of a computer m E.g., CPU, storage, keyboard, monitor Monitor Keyboard Main Memory Central Processing Unit CD ROM Hard Disk chip that executes program commands e.g., Intel Pentium IV Sun Sparc Transmeta primary storage area for programs and data also called RAM

9 Storing Information r Computers store all information digitally: m E.g. numbers, program instructions, text, audio, and video r Information is stored in binary numbers m A single binary digit (0 or 1) is called a bit m A single bit can represent two possible states, like a light bulb that is either on (1) or off (0) m Combinations of bits are used to store values H i, H e a t h e r

10 Binary Bit Combinations 1 bit 01 2 bits bits bits Each additional bit doubles the number of possible combinations

11 Information is Stored in Memory Large values are stored in consecutive memory locations Each memory cell stores a set number of bits (usually 8 bits, or one byte) Each memory cell has a numeric address, which uniquely identifies it

12 Computer Environment: Software r Operating System m E.g., Linux, Mac OS X, Windows 2000, Windows XP m manages resources such as CPU, memory, and disk m controls all machine activities r Application programs m generic term for any other kind of software m compiler, word processors, missile control systems, games

13 Operating System r What does an OS do? m hides low level details of bare machine m arbitrates competing resource demands r Useful attributes m multi-user m multi-tasking Operating System User Program CPU Disk Network

14 File System r Hierarchical (directories and files) r Filename: sequence of directory names ending with a file name WINDOWS C: Documents and Settings … zs9 My Documents ……

15 Some Useful Commands r File system  mkdir as0 // creates a directory named as0  cd as0 // changes current directory to as0  cd.. // changes current directory one level up  dir // list the files of current directory  del // delete the file m Note 1: you can always do the above using Windows GUI m Note 2: you can even access the directory remotely by typing \\plucky.cs.yale.edu\zs9$\MyDocs in the Address field of your browser (replace zs9 with your net id) r Editing  notepad // edit a file using notepad Note: notepad insists on adding.txt after the file name. If you do not want the.txt suffix, choose “All Files” as “Save as type”  scite // edit file using SciTE, a code editor

16 Outline  Review  Computer hardware/software  Variables and C# data types  Input and output

17 Variables r A variable is a typed name for a location in memory r A variable must be declared, specifying the variable's name and the type of information that will be held in it int numberOfStudents; … int average, max; data type variable name int total; … Which ones are valid variable names? myBigVar VAR1 99bottles namespace It’s-all-over numberOfStudents: average: max: total:

18 Assignment r An assignment statement changes the value of a variable  The assignment operator is the = sign total = 55; r You can only assign a value to a variable that is consistent with the variable's declared type (more later)  You can declare and assign initial value to a variable at the same time, e.g., int total = 55; r The value on the right is stored in the variable on the left  The value that was in total is overwritten int total; …

19 Example static void Main(string[] args) { int total; total = 15; System.Console.Write(“total = “); System.Console.WriteLine(total); total = ; System.Console.Write(“total = “); System.Console.WriteLine(total); }

20 Constants r A constant is similar to a variable except that it holds one value for its entire existence r The compiler will issue an error if you try to change a constant  In C#, we use the constant modifier to declare a constant constant int numberOfStudents = 42; r Why constants? m give names to otherwise unclear literal values m facilitate changes to the code m prevent inadvertent errors

21 C# Data Types r There are 15 data types in C# r Eight of them represent integers:  byte, sbyte, short, ushort, int, uint, long,ulong r Two of them represent floating point numbers  float, double r One of them represents decimals:  decimal r One of them represents boolean values:  bool r One of them represents characters: m char r One of them represents strings: m string r One of them represents objects: m object

22 Numeric Data Types r The difference between the various numeric types is their size, and therefore the values they can store: Range , ,147,483,648 – 2,147,483,647 0 – 4,294,967,  to 9  – 1.8   1.0  ;  7.9  with significant digits  1.5  ;  3.4  with 7 significant digits  5.0  ;  1.7  with significant digits Question: you need a variable to represent world population. Which type do you use? Type byte sbyte short ushort int uint long ulong decimal float double Storage 8 bits 16 bits 32 bits 64 bits 128 bits 32 bits 64 bits

23 Examples of Numeric Variables int x = 1; short y = 10; float pi = 3.14f; // f denotes float float f3 = 7E-02f; // 0.07 double d1 = 7E-100; // use m to denote a decimal decimal microsoftStockPrice = 28.38m; Example: TestNumeric.cs

24 Boolean  A bool value represents a true or false condition r A boolean can also be used to represent any two states, such as a light bulb being on or off  The reserved words true and false are the only valid values for a boolean type bool doAgain = true;

25 Characters  A char is a single character from the a character set r A character set is an ordered list of characters; each character is given a unique number r C# uses the Unicode character set, a superset of ASCII m Uses sixteen bits per character, allowing for 65,536 unique characters m It is an international character set, containing symbols and characters from many languages m Code chart can be found at: r Character literals are represented in a program by delimiting with single quotes, e.g., 'a‘ 'X‘ '7' '$‘ ',‘ char response = ‘Y’;

26 Common Escape Sequences

27 string  A string represents a sequence of characters, e.g., string message = “Hello World”;  Strings can be created with verbatim string literals by starting e.g., string a2  Question: how to represent this string: The double quotation mark is “  Question: how to represent this string: \\plucky.cs.yale.edu\zs9$\MyDocs

28 Outline  Review  Computer hardware/software  Variables and C# data types  Input and output

29 Data Input r Console.ReadLine() m Used to get a value from the user input  Example string myString = Console.ReadLine(); r Convert from string to the correct data type m Int32.Parse() Used to convert a string argument to an integer Allows math to be preformed once the string is converted Example: string myString = “1023”; int myInt = Int32.Parse( myString ); m Double.Parse() m Single.Parse()

30 Output  Console.WriteLine(variableName) will print the variable  You can use the values of some variables at some positions of a string: System.Console.WriteLine(“{0} {1}.”, iAmVar0, iAmVar1);  You can control the output format by using the format specifiers: float price = 2.5f; System.Console.WriteLine(“Price = {0:C}.”, price); Price = $2.50.  For a complete list of format specifiers, see Example: TaxAndTotal.cs