Datatypes, Variables, and Operations

Similar documents
Chapter 2 Elementary Programming

CEN 414 Java Programming

Chapter 2 Elementary Programming. Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.

Motivations. Chapter 2: Elementary Programming 8/24/18. Introducing Programming with an Example. Trace a Program Execution. Trace a Program Execution

Chapter 2 ELEMENTARY PROGRAMMING

Chapter 2 Primitive Data Types and Operations. Objectives

Chapter 2 Elementary Programming

Motivations 9/14/2010. Introducing Programming with an Example. Chapter 2 Elementary Programming. Objectives

Chapter 2 Elementary Programming

Chapter 2 Primitive Data Types and Operations

Chapter 2. Elementary Programming

Chapter 2 Primitive Data Types and Operations

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

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. Chapter 1 Introduction to Computers, Programs, and Java

JAVA Programming Concepts

Computational Expression

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

A very simple program. Week 2: variables & expressions. Declaring variables. Assignments: examples. Initialising variables. Assignments: pattern

Data types Expressions Variables Assignment. COMP1400 Week 2

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

Lecture Notes. System.out.println( Circle radius: + radius + area: + area); radius radius area area value

Computer System and programming in C

These are reserved words of the C language. For example int, float, if, else, for, while etc.

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

Basics of Java Programming

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

Lesson 02 Data Types and Statements. MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

Identifiers and Variables

Object-Oriented Programming

Fundamentals of Programming

Zheng-Liang Lu Java Programming 45 / 79

Information Science 1

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

Chapter 2 Working with Data Types and Operators

Fundamental of Programming (C)

Visual C# Instructor s Manual Table of Contents

BASIC ELEMENTS OF A COMPUTER PROGRAM

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

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

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

Professor: Sana Odeh Lecture 3 Python 3.1 Variables, Primitive Data Types & arithmetic operators

Full file at

Gaddis: Starting Out with Java: From Control Structures through Objects, 6/e

Basic Operations jgrasp debugger Writing Programs & Checkstyle

Values, Variables, Types & Arithmetic Expressions. Agenda

Advanced Computer Programming

CMPT 125: Lecture 3 Data and Expressions

Starting Out with Java: From Control Structures through Data Structures 3e (Gaddis and Muganda) Chapter 2 Java Fundamentals

Elementary Programming. CSE 114, Computer Science 1 Stony Brook University

The C++ Language. Arizona State University 1

Fall 2017 CISC124 9/16/2017

Information Science 1

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

1 class Lecture2 { 2 3 "Elementray Programming" / References 8 [1] Ch. 2 in YDL 9 [2] Ch. 2 and 3 in Sharan 10 [3] Ch.

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

Program Fundamentals

An Introduction to Processing

3. Java - Language Constructs I

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

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

Chapter 1 Introduction to java

COMP 202 Java in one week

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

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

Getting started with Java

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

Data Types, Literals, Operators

Chapter 2: Using Data

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

Work relative to other classes

Java Identifiers, Data Types & Variables

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

Simple Java Reference

COMP 110 Introduction to Programming. What did we discuss?

Data Types and the while Statement

3. Simple Types, Variables, and Constants

ARG! Language Reference Manual

Java Fall 2018 Margaret Reid-Miller

CS313D: ADVANCED PROGRAMMING LANGUAGE

COMP-202 Unit 2: Java Basics. CONTENTS: Using Expressions and Variables Types Strings Methods

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

ENGR 101 Engineering Design Workshop

Tester vs. Controller. Elementary Programming. Learning Outcomes. Compile Time vs. Run Time

1st and 3rd September 2015

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

Elementary Programming

4. If the following Java statements are executed, what will be displayed?

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

Define a method vs. calling a method. Chapter Goals. Contents 1/21/13

Full file at

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

VARIABLES AND TYPES CITS1001

Entry Point of Execution: the main Method. Elementary Programming. Learning Outcomes. Development Process

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

Full file at

Java Identifiers. Java Language Essentials. Java Keywords. Java Applications have Class. Slide Set 2: Java Essentials. Copyright 2012 R.M.

CHAPTER 3 BASIC INSTRUCTION OF C++

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

Full file at

COSC 243. Data Representation 3. Lecture 3 - Data Representation 3 1. COSC 243 (Computer Architecture)

Transcription:

Datatypes, Variables, and Operations 1

Primitive Type Classification 2

Numerical Data Types Name Range Storage Size byte 2 7 to 2 7 1 (-128 to 127) 8-bit signed short 2 15 to 2 15 1 (-32768 to 32767) 16-bit signed int 2 31 to 2 31 1 (-2147483648 to 2147483647) 32-bit signed long 2 63 to 2 63 1 64-bit signed (i.e., -9223372036854775808 to 9223372036854775807) float Negative range: 32-bit IEEE 754-3.4028235E+38 to -1.4E-45 Positive range: 1.4E-45 to 3.4028235E+38 double Negative range: 64-bit IEEE 754-1.7976931348623157E+308 to -4.9E-324 Positive range: 4.9E-324 to 1.7976931348623157E+308 3

int x; Declaring Variables // Declare x to be an // integer variable; double radius; // Declare radius to // be a double variable; char a; // Declare a to be a // character variable; 4

Identifiers An identifier is a sequence of characters that consist of letters, digits, underscores (_), and dollar signs ($). An identifier must start with a letter, an underscore (_), or a dollar sign ($). It cannot start with a digit. An identifier cannot be a reserved word. (See Appendix A, Java Keywords, for a list of reserved words). An identifier can be of any length. 5

Assignment Statements x = 1; // Assign 1 to x; radius = 1.0; // Assign 1.0 to radius; a = 'A'; // Assign 'A' to a; 6

Declaring and Initializing in One Step int x = 1; double d = 1.4; 7

Named Constants final datatype CONSTANTNAME = VALUE; final double PI = 3.14159; final int SIZE = 3; 8

Naming Conventions Choose meaningful and descriptive names. Variables and method names: Use lowercase. If the name consists of several words, concatenate all in one, use lowercase for the first word, and capitalize the first letter of each subsequent word in the name. For example, the variables radius and area, and the method computearea. 9

Naming Conventions, cont. Class names: Capitalize the first letter of each word in the name. For example, the class name ComputeArea. Constants: Capitalize all letters in constants, and use underscores to connect words. For example, the constant PI and MAX_VALUE 10

Numeric Operators Name Meaning Example Result + Addition 34 + 1 35 - Subtraction 34.0 0.1 33.9 * Multiplication 300 * 30 9000 / Division 1.0 / 2.0 0.5 % Remainder 20 % 3 2 11

Integer Division 5 / 2 yields an integer 2. 5.0 / 2 yields a double value 2.5 5 % 2 yields 1 (the remainder of the division) 12

Remainder Operator Remainder is very useful in programming. For example, an even number % 2 is always 0 and an odd number % 2 is always 1. So you can use this property to determine whether a number is even or odd. Suppose today is Saturday and you and your friends are going to meet in 10 days. What day is in 10 days? You can find that day is Tuesday using the following expression: Saturday is the 6 th day in a week (6 + 10) % 7 is 2 After 10 days A week has 7 days The 2 nd day in a week is Tuesday 13

Floating Point Approximation Calculations involving floating-point numbers are approximated because these numbers are not stored with complete accuracy. For example, System.out.println(1.0-0.1-0.1-0.1-0.1-0.1); displays 0.5000000000000001, not 0.5, and System.out.println(1.0-0.9); displays 0.09999999999999998, not 0.1. Integers are stored precisely. Therefore, calculations with integers yield a precise integer result. 14

Floating Point (cont d) double x1 = 0.3; double x2 = 0.1 + 0.1 + 0.1; System.out.println(x1 == x2); // evaluates to false Why? 1/10 = 1/16 + 1/32 + 1/256 + 1/512 + 1/4096 + 1/8192 + 15

Floating Point cont d double epsilon = 1e-15; double x1 = 0.3; double x2 = 0.1 + 0.1 + 0.1; System.out.println(Math.abs(x1 x2) < epsilon) // evaluates to true 16

Exponent Operations System.out.println(Math.pow(2, 3)); // Displays 8.0 System.out.println(Math.pow(4, 0.5)); // Displays 2.0 System.out.println(Math.pow(2.5, 2)); // Displays 6.25 System.out.println(Math.pow(2.5, -2)); // Displays 0.16 17

Number Literals A literal is a constant value that appears directly in the program. int i = 34; long x = 1000000; double d = 5.0; int ssn = 342_44_9922; byte b = 1000; // compile error float f = 5.0f; long l = 500000000000; // error long l = 500000000000L; long l = 500_000_000_000L; // _ seperator 18

Scientific Notation 1.23456e+2 is the same as 1.23456e2 and is equivalent to 123.456 1.23456e-2 is equivalent to 0.0123456. E (or e) represents an exponent and it can be either in lowercase or uppercase. 19

Shortcut Assignment Operators Operator Example Equivalent += i += 8 i = i + 8 -= f -= 8.0 f = f - 8.0 *= i *= 8 i = i * 8 /= i /= 8 i = i / 8 %= i %= 8 i = i % 8 20