Data types Expressions Variables Assignment. COMP1400 Week 2

Similar documents
Datatypes, Variables, and Operations

Basics of Java Programming

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

Computational Expression

Ex: If you use a program to record sales, you will want to remember data:

Program Fundamentals

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

VARIABLES & ASSIGNMENTS

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. A Guide to this Instructor s Manual:

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

An Introduction to Processing

Full file at

Lecture 3 Tao Wang 1

Chapter 2: Using Data

CS Programming I: Primitives and Expressions

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

Lecture 2 Tao Wang 1

Full file at

Visual C# Instructor s Manual Table of Contents

5/3/2006. Today! HelloWorld in BlueJ. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont.

Midterms Save the Dates!

Basic Operations jgrasp debugger Writing Programs & Checkstyle

A First Program - Greeting.cpp

CS313D: ADVANCED PROGRAMMING LANGUAGE

Chapter 2 Elementary Programming

Programming Lecture 3

Topic 4 Expressions and variables

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

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

Getting started with Java

The C++ Language. Arizona State University 1

Declaration and Memory

Program Elements -- Introduction

LECTURE 3 C++ Basics Part 2

CMPT 125: Lecture 3 Data and Expressions

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

CS102: Variables and Expressions

Creating a C++ Program

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

ARG! Language Reference Manual

Chapter 2: Introduction to C++

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

Outline. Review of Last Week II. Review of Last Week. Computer Memory. Review Variables and Memory. February 7, Data Types

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

Chapter 2: Using Data

Key Differences Between Python and Java

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

StudyHub+ 1. StudyHub: AP Java. Semester One Final Review

1 Introduction Java, the beginning Java Virtual Machine A First Program BlueJ Raspberry Pi...

PROGRAMMING FUNDAMENTALS

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2

ESc101 : Fundamental of Computing

Mr. Monroe s Guide to Mastering Java Syntax

DATA TYPES AND EXPRESSIONS

BITG 1233: Introduction to C++

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Full file at C How to Program, 6/e Multiple Choice Test Bank

Hello, World and Variables

COMP 110 Introduction to Programming. What did we discuss?

C/C++ Programming for Engineers: Working with Integer Variables

Object-Oriented Programming

Computer Programming, I. Laboratory Manual. Experiment #2. Elementary Programming

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Computer Components. Software{ User Programs. Operating System. Hardware

Java Foundations: Introduction to Program Design & Data Structures, 4e John Lewis, Peter DePasquale, Joseph Chase Test Bank: Chapter 2

I Internal Examination Sept Class: - BCA I Subject: - Principles of Programming Lang. (BCA 104) MM: 40 Set: A Time: 1 ½ Hrs.

Computational Physics Operating systems

What we will do today Explain and look at examples of. Programs that examine data. Data types. Topic 4. variables. expressions. assignment statements

Procedures, Parameters, Values and Variables. Steven R. Bagley

Chapter 2: Overview of C++

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

MODULE 02: BASIC COMPUTATION IN JAVA

Values, Variables, Types & Arithmetic Expressions. Agenda

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

Data and Expressions. Outline. Data and Expressions 12/18/2010. Let's explore some other fundamental programming concepts. Chapter 2 focuses on:

Reserved Words and Identifiers

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

Java Notes. 10th ICSE. Saravanan Ganesh

I. Variables and Data Type week 3

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Chapter 2. C++ Basics

SECTION II: LANGUAGE BASICS

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Basics of Java Programming variables, assignment, and input

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

Identifiers and Variables

CHAPTER 3 BASIC INSTRUCTION OF C++

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

Class 2: Variables and Memory. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

Arithmetic and IO. 25 August 2017

3. Java - Language Constructs I

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

Introduction. For instance, if we need to store three whole numbers we can create two integer (int) variables called N1, N2 and tot. tot.

CSc Introduction to Computing

Building Java Programs

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

MATVEC: MATRIX-VECTOR COMPUTATION LANGUAGE REFERENCE MANUAL. John C. Murphy jcm2105 Programming Languages and Translators Professor Stephen Edwards

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Lecture 2: Variables and Operators. AITI Nigeria Summer 2012 University of Lagos.

Transcription:

Data types Expressions Variables Assignment COMP1400 Week 2

Data types Data come in different types. The type of a piece of data describes: What the data means. What we can do with it.

Primitive types Java defines a selection of primitive data types. They are called primitive because they are the basic building blocks we can use to construct more complex types.

Binary Numbers All data in a computer are stored as binary numbers, e.g. 11012 = 1 x 2 3 + 1 x 2 2 + 0 x 2 1 + 1 x 2 0 = 8+4+0+1 = 13

Computer Memory long int and double int and float short int byte 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 1 byte = 8 bits

Primitive types Type Keyword Meaning Examples integer int whole numbers 0, 1, 100, -3322 floating point number float fractional numbers 0.0f, 1.5f, -32.7f, 3.14159f

Extended types Type Keyword Meaning Examples long integer long whole numbers 0L, 9223372036854775807L doubleprecision floating point number double fractional numbers 0.000000000000000 00000000000000000 00000000000000001! 10000000000000000 00000000000000000 00000000000000000

Primitive types Type Keyword Meaning Examples boolean boolean true or false true false character char characters! (letters, numerals, symbols) 'a' 'A' '1' ' '

Representing numbers There are two types for representing numbers. Use int when you know you are deal with whole numbers (e.g. when counting things) Use double when you have to deal with fractions.

Representing numbers Numbers have internal limitations on their precision. An int can only represent values between -2,147,483,648 and 2,147,483,647. A double can represent larger values but significant digits are lost. 10000000000000000.0 + 1 = 10000000000000000.0

Memory can encode anything characters, strings, database records, music, images, video, weather models, etc numbers can be addresses to other locations in memory instructions in computer program

Expressions An expression is a formula for computing a value. Examples: 1 + 1 1.0 / 3 (1.5 * 3) + 9-2

Integer expressions There are five basic operators you can use to make integer expressions: Symbol Purpose Example + addition 3 + 4 = 7 - subtraction 3-4 = -1 * multiplication 3 * 4 = 12 / integer division 14 / 3 = 4 % remainder 14 % 3 = 2

Float expressions The operators have a slightly different meaning when applied to doubles and floats: Symbol Purpose Example + addition 3.5 + 4.0 = 7.5 - subtraction 3.5-4.0 = -0.5 * multiplication 3.0 * 4.0 = 12.0 / division 14.0 / 3.0 = 4.66666 % remainder 7.5 % 3 = 1.5

Order of operations When computing an expression the operators *, / and % take precedence over + and -. Otherwise order is left-to-right. 1 + 4 * 3 = 13 2-5 / 3 = 1 2 + 4 % 3 = 3 5-2 + 3 = 6

Order of operations Use parentheses ( and ) to vary the order: So: (1 + 4) * 3 = 15 (2-5) / 3 = -1 (2 + 4) % 3 = 0 5 - (2 + 3) = 0

Code Pad BlueJ has a Code Pad tool to allow you to test snippets of code.

Code Pad Expression Result (Type)

Variables When we compute a value we usually want to store it somewhere so we can use it later. A variable is a named container for storing a single piece of data of a specified type. Name Type Value count int 0 age double 37.5

Variables The value of a variable can change. When a new value is put into a variable, the old one is forgotten. The type of a variable cannot change. Name Type Value count int 17 age double 38.0

Variable declaration Before we use a variable we must declare it, specifying its name and type. We can optionally specify an initial value. Otherwise it is set to zero (for ints and floats). int count; // initially 0 double age = 37.5; type name initial value semicolon

Assignment Once we have declared a variable we can change its value as often as we like, using an assignment statement: count = 3; // count is now 3 count = 4 + 5; // count is now 9 count = count + 1; // count is now 10

Assignment The format of an assignment statement: count = 4 + 5; variable equals expression! semicolon The value of the expression on the right is the new value for the variable. It must have the same type as the variable.

Semicolons Notice that any single statement in Java has to end with a semicolon (;). This is the way Java knows you have finished one statement and are starting the next.

Variable naming A variable name can be any sequence of letters and numbers or the _ (underscore) character. Good naming: 1. Is meaningful. 2. Has a consistent style.

Variable naming The recommend style is bumpycaps style (aka camelcase ). Start with a lowercase letter. String words together with an uppercase letter starting each word: age, numberofchildren, taxfilenumber, oldlength

Good Style Good style makes code readable by providing meaningful names: age = 37.5; numchildren = 2; disttotarget = 50;

Bad Style Bad style makes code unreadable by providing obscure names or mismatched styles: a = 37.5; n_c = 2; Dt = 50;

Variables and expressions Once a variable has been declared, it can be used in expressions to compute new values: int count = 7; int size = 3; int spaceneeded = count * size; // spaceneeded is now 21

Variables and expressions A variable can even be used in an expression that is assigned to itself: int count = 7; count = count * 2; // count is now 14

Variables and expressions This kind of operation is so common that there are shorthands: count += 2; // count = count + 2 count -= 2; // count = count - 2

Variables and expressions count *= 2; // count = count * 2 count /= 2; // count = count - 2 count++; // count = count + 1 count--; // count = count - 1