Introduction to Computers and Programming

Similar documents
Motivation. Each distinct type needs its own I/O library INTEGER FLOAT CHARACTER BOOLEAN. Using pure numbers, we can write nonsense:

Aerospace Software Engineering

Enumerated Types. Mr. Dave Clausen La Cañada High School

Arrays (Lists) # or, = ("first string", "2nd string", 123);

Lecture 3. The syntax for accessing a struct member is

PASCAL. PASCAL, like BASIC, is a computer language. However, PASCAL, unlike BASIC, is a Blocked Structured Language (BASIC is known as unstructured).

Topic 5: Enumerated Types and Switch Statements

Object-Oriented Principles and Practice / C++

Introduction to Computers and Programming

Private Swimming Lessons

Ada LOOP statement allows for repetition of a sequence of statements Three forms of a LOOP statement FOR loop_specification LOOP...

Lecture 12: Data Types (and Some Leftover ML)

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

Introduction. Primitive Data Types: Integer. Primitive Data Types. ICOM 4036 Programming Languages

MULTIMEDIA AND WEB TECHNOLOGY

CSCE 314 Programming Languages

Daily Math Week 10 ( ) Mon. October 21, 2013 Tues. October 22, 2013 Wed. October 23, 2013 Thurs. October 24, 2013 Fri.

Lecture Overview. [Scott, chapter 7] [Sebesta, chapter 6]

INFORMATION TECHNOLOGY SPREADSHEETS. Part 1

Daily Math Week 8 ( ) Mon. October 7, 2013 Tues. October 8, 2013 Wed. October 9, 2013 Thurs. October 10, 2013 Fri.

Chapter 7:: Data Types. Mid-Term Test. Mid-Term Test (cont.) Administrative Notes

Scheduling. Scheduling Tasks At Creation Time CHAPTER

SUN Sun Certified Associate for the Java Platform.

AIMMS Function Reference - Date Time Related Identifiers

Enumerated Types. CSE 114, Computer Science 1 Stony Brook University

Maintaining the Central Management System Database

Darshan Institute of Engineering & Technology for Diploma Studies Unit 5

EECS402 Lecture 24. Something New (Sort Of) Intro To Function Pointers

22c:111 Programming Language Concepts. Fall Types I

BUP2 5/2 12/4/07 12:49 AM Page 1. Introduction

Chapter 6 part 1. Data Types. (updated based on 11th edition) ISBN

For each of the following variables named x, specify whether they are static, stack-dynamic, or heapdynamic:

Using Variables to Write Pattern Rules

Publication Name: The Sacramento Bee Date info completed: March 2007

Francesco Nidito. Programmazione Avanzata AA 2007/08

CSCI312 Principles of Programming Languages!

BUT2 7day 11/4/07 11:07 PM Page 1. Introduction

2014 FALL MAILING SEASON Update for the Mailing Industry. August 18, 2014

TYPES, VALUES AND DECLARATIONS

Chapter 3. Ch 1 Introduction to Computers and Java. Selections

MyOwnDeliveries. a Magento module. User manual

Introduction to Classes

Lecture 13. Math 22 Summer 2017 July 17, 2017

Introduction Primitive Data Types Character String Types User-Defined Ordinal Types Array Types. Record Types. Pointer and Reference Types

BLM2031 Structured Programming. Zeyneb KURT

Part No. P CallPilot. Programming Record

Lecture 12. Data Types and Strings

STA 313: Topics in Statistics

Nimsoft Monitor. reboot Guide. v1.4 series

Structures and Pointers

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Additional Function Manual

Chapter 6. Data Types

} Evaluate the following expressions: 1. int x = 5 / 2 + 2; 2. int x = / 2; 3. int x = 5 / ; 4. double x = 5 / 2.

Assignment: 2. CS 135 Winter 2018 Graham, Nijjar

Organization of Programming Languages CS3200 / 5200N. Lecture 06

G Programming Languages - Fall 2012

Arrays III and Enumerated Types

) $$%*%"3%*# 2# "%$& # $&# # "$) %) "$$%,*$"# 2$$%$" 3# 2)!# # "$6!# # # "$*# *$"*%*&"+ 2 # # 2"# # )

Complex data structures. Cedric Saule

Ada A Crash Course. Peter Chapin Vermont Technical College. Generated: July 27,

Programming Languages

Programming Language Concepts: Lecture 19

Semantic Analysis. How to Ensure Type-Safety. What Are Types? Static vs. Dynamic Typing. Type Checking. Last time: CS412/CS413

Chapter 6. Data Types ISBN

Problem Solving. Problem Solving Concept for Computer Science

GEOMETRY (TENTATIVE SCHEDULE) WEEK 1. Abbreviations: LT = Learning Target, Std. = Standard GR = Geometry Review Worksheet (Mixed Math Review)

Chapter. Solving Problems that Require Decisions. Objectives: At the end of the chapter students should be able to:

Chapter 2: Objects and Primitive Data

C++: Const Function Overloading Constructors and Destructors Enumerations Assertions

Blood Glucose Monitoring System. Copyright 2017 Ascensia Diabetes Care Holdings AG. All rights reserved.

Fundamentals of Programming Languages

Types. Chapter Six Modern Programming Languages, 2nd ed. 1

Structuring the Data. Structuring the Data

Total Market Demand Wed Jul 26 Thu Jul 27 Fri Jul 28 Sat Jul 29 Sun Jul 30 Mon Jul 31 Tue Aug 01

Primitive Types. Four integer types: Two floating-point types: One character type: One boolean type: byte short int (most common) long

SOUTH DAKOTA BOARD OF REGENTS. Board Work ******************************************************************************

ComS 207: Programming I Homework 7 Out: Wed. Oct 11, 2006 Due: Fri. Oct 20, 2006 (Online submission *ONLY*) Student Name: Recitation Section:

Math 202 Test Problem Solving, Sets, and Whole Numbers 19 September, 2008

Lecture #23: Conversion and Type Inference

January and February

Blood Glucose Monitoring System. Copyright 2018 Ascensia Diabetes Care Holdings AG

PRINT MODULAR RATECARD

Conversion vs. Subtyping. Lecture #23: Conversion and Type Inference. Integer Conversions. Conversions: Implicit vs. Explicit. Object x = "Hello";

Systems Optimization Spring 2003 Mid Term Exam

PROGRAMMAZIONE I A.A. 2017/2018

SOUTH DAKOTA BOARD OF REGENTS. Board Work ******************************************************************************

Armstrong State University Engineering Studies MATLAB Marina Switch-Case Statements Primer

Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types.

Objectives. Describe ways to create constants const readonly enum

Questionnaire for Client

CS 485 Advanced Object Oriented Design. Enum. Spring 2017

Digital Design Using VHDL Using Xilinx s Tool for Synthesis and ModelSim for Verification Part II

CS 11 Ocaml track: lecture 2

View a Students Schedule Through Student Services Trigger:

Ada for the C++ or Java Developer

ACT! Calendar to Excel

MATH 1131Q - Calculus 1.

61A Lecture 9. Friday, September 20

Transcription:

Introduction to Computers and Programming Prof. I. K. Lundqvist Lecture 8 Reading: FK pp. 115-151 Sept 17 2003 Type A set of values A set of primitive operations Grouped into classes based on the similarity of values and primitive operations Elementary types Composite

Type Classification Elementary : Values are logically indivisible Composite : Values composed from components Elementary Scalar Discrete Access Real Scalar Ordered relational operators are defined Each value of a discrete type has a position number Scalar Discrete Real Integer Enumeration Fixed Point Floating Point

Attributes of Scalar S First denotes the lower bound of the range of S. The value of this attribute is of the type of S. S Last denotes the upper bound of the range of S S Range is equivalent to the range S First.. S Last Operations on Scalar S Min returns lower of two elements S Max returns higher of two elements S Value accepts a string and returns the value in the type S Image converts the value into a string S Pred and S Succ behavior depends on the scalar type S Pred (Integer) : returns (Integer -1) S Succ (Integer) : returns (Integer + 1)

A subtype type. Subtypes is a subrange of a larger Subtypes of the same larger type are not distinct types. A subtype and the larger type are also not distinct types. Thus subtypes of the same thing are assignment-compatible. The benefit of subtypes is that range checks avoid some nonsense. Subtype Example Two useful sub-types of the integers are built into Ada: subtype POSITIVE is INTEGER range 1..INTEGER'LAST; subtype NATURAL is INTEGER range 0..INTEGER'LAST; Subtypes are appropriate whenever there are ranges of allowed values. min_on_bus : constant := 0; max_on_bus : constant := 80; type no_on_buses is range min_on_bus.. max_on_bus; max_seated : constant no_on_buses := 50; subtype seated_on_buses is no_on_buses range min_on_bus.. max_seated; subtype standing_on_buses is range min_on_bus.. (max_on_bus - max_seated);

Subtypes subtype Natural is Integer range 0..Integer Last; subtype Positive is Integer range 1..Integer Last; subtype NonNegativeFloat is Float range 0.0.. Float Last; subtype SmallInt is Integer range -50..50; subtype CapitalLetter is Character range A.. Z ; X, Y, Z : SmallInt; NextChar : CapitalLetter; Hours_Worked : NonNegFloat; X := 25; Y := 26; Z := X + Y; Operations on Discrete S Pos(Arg) returns the position number of the argument S Val(Arg) a value of the type of S whose position number equals the value of S

CQ The outputs are exactly the same There will be no outputs The outputs are different I don t know Enumeration A data type whose values are a collection of allowed words type Class is (Freshman, Sophomore, Junior, Senior); type days is (Mon, Tue, Wed, Thu, Fri, Sat, Sun); type colours is (white, red, yellow, green, blue, pink, black); type traffic_colours is (green, yellow, red); type suits is (clubs, diamonds, hearts, spades);

Enumeration Enumeration types have the following benefits: readable programs avoid arbitrary mapping to numbers e.g. better to use "Wed" than 3 for a day of the week they work well as selectors in case statements Example: mix_colours.adb Attributes of Enumerated type Days is (Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday); Today : Days; --current day of the week Tomorrow : Days; --day after Today Today := Friday; Tomorrow := Saturday; Days First Days Last Days Pos(Monday) Days Val(0) Days Pred(Wednesday) Days Pred(Today) Days Succ(Tuesday) Days Succ(Today) is Monday is Sunday is 0 is Monday is Tuesday is Thursday is Wednesday is Saturday You must ensure the result is legal. A CONSTRAINT_ERROR will occur at run-time otherwise. For example, days'succ(sun) is illegal.

Derived age := -20; height := age - class_size; shoe_size := 2 * no_on_bus; help program values reflect the real world. Derived Integer New data types can be derived from INTEGER: type ages is new INTEGER range 0.. 110; age : ages; voting_age : constant ages := 18; type heights is range 0.. 230; height : heights; min_enrolment : constant := 6; max_enrolment : constant := 200; type class_sizes is range 0..max_enrolment; class_size : class_sizes;

Type conversion Ada has strong typing: different types cannot be mixed Explicit type conversion is permitted: type length is digits 5 range 0.0.. 1.0E10; type area is digits 5 range 0.0.. 1.0E20; function area_rectangle (L,H : length) return area is begin return area(l) * area(h); end; Benefits of derived types Nonsense rejected by compiler height := age - class_size; "Out of range" rejected by compiler age := -20; Out of range run time error class_size := class_size + 100; Enforce distinct nature of different objects Robust, elegant, effective programs

I/O Libraries Each distinct type needs its own I/O library. General form: package type_io is new TEXT_IO.basetype_io (typename); package int_io is new TEXT_IO.INTEGER_IO (INTEGER); type ages is new INTEGER range 0.. 110; package ages_io is new TEXT_IO.INTEGER_IO (ages); type measurement is digits 10; package measurement_io is new TEXT_IO.FLOAT_IO (measurement); type suits is (clubs, diamonds, hearts, spades); package suits_io is new TEXT_IO.ENUMERATION_IO (suits); type colours is (white, red, yellow, green, brown, blue, pink, black); package colours_io is new TEXT_IO.ENUMERATION_IO (colours); Input/Output Operations type Days is (Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday); package Day_IO is new Ada.Text_IO.Enumeration_IO(Enum=>Days); if this_day in weekend_days then put( Holliday! ); end if; Day_IO.Get(Item => Today); Day_IO.Put(Item => Today, Width => 10);

Example subtypes[1..3].adb