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

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

Basics of Java Programming

Datatypes, Variables, and Operations

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

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

CCBC Math 081 Order of Operations Section 1.7. Step 2: Exponents and Roots Simplify any numbers being raised to a power and any numbers under the

Program Fundamentals

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

The Order of Operations. Unit 1 Lesson 3

Data types Expressions Variables Assignment. COMP1400 Week 2

Full file at

Declaration and Memory

ECE 122 Engineering Problem Solving with Java

CS 106 Introduction to Computer Science I

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

Chapter 2: Using Data

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

Object-Oriented Programming

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

MODULE 02: BASIC COMPUTATION IN JAVA

Reserved Words and Identifiers

BASIC ELEMENTS OF A COMPUTER PROGRAM

Lecture Set 4: More About Methods and More About Operators

Expressions and Data Types CSC 121 Spring 2017 Howard Rosenthal

Summer Assignment Glossary

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

Lesson 3: Basic Programming Concepts

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

An Introduction to Processing

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

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

CS 112 Introduction to Programming

Admin. CS 112 Introduction to Programming. Recap: Java Static Methods. Recap: Decomposition Example. Recap: Static Method Example

DATA TYPES AND EXPRESSIONS

REVIEW. The C++ Programming Language. CS 151 Review #2

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

COMP 110 Introduction to Programming. What did we discuss?

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

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

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

Computational Expression

Lecture Set 4: More About Methods and More About Operators

Chapter 2 Elementary Programming

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

Is the statement sufficient? If both x and y are odd, is xy odd? 1) xy 2 < 0. Odds & Evens. Positives & Negatives. Answer: Yes, xy is odd

Basic Operations jgrasp debugger Writing Programs & Checkstyle

CS313D: ADVANCED PROGRAMMING LANGUAGE

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

AP Computer Science A

Accuplacer Arithmetic Study Guide

Lecture 3 Tao Wang 1

Objects and Types. COMS W1007 Introduction to Computer Science. Christopher Conway 29 May 2003

A Java program contains at least one class definition.

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

Computer System and programming in C

Introduction to Programming EC-105. Lecture 2

The C++ Language. Arizona State University 1

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

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

Lecture 1. Types, Expressions, & Variables

age = 23 age = age + 1 data types Integers Floating-point numbers Strings Booleans loosely typed age = In my 20s

CMPT 125: Lecture 3 Data and Expressions

Accelerating Information Technology Innovation

Outline. Data and Operations. Data Types. Integral Types

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

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

Lesson 02 Working with Data Types. MIT 31043: VISUAL PROGRAMMING By: S. Sabraz Nawaz Senior Lecturer in MIT

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

ITP 342 Mobile App Dev. Code

1-6 Order of Operations

Chapter 2: Introduction to C++

Expressions and Casting. Data Manipulation. Simple Program 11/5/2013

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

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

CEN 414 Java Programming

Introduction to TURING

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

Student Success Center Arithmetic Study Guide for the ACCUPLACER (CPT)

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

Expressions and Casting

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

Chapter 2: Using Data

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

Java Notes. 10th ICSE. Saravanan Ganesh

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

Arithmetic Expressions in C

Fundamentals. Fundamentals. Fundamentals. We build up instructions from three types of materials

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

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

Values and Variables 1 / 30

LESSON 2 VARIABLES, OPERATORS, EXPRESSIONS, AND USER INPUT

Values, Variables, Types & Arithmetic Expressions. Agenda

Building Java Programs. Chapter 2: Primitive Data and Definite Loops

Chapter 2: Data and Expressions

CIS133J. Working with Numbers in Java

Let's Look Back. We talked about how to create a form in HTML. Forms are one way to interact with users

JAVA Programming Fundamentals

Fundamentals of Programming CS-110. Lecture 2

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

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

Transcription:

Data

Variables Programs need to remember values. Ex: If you use a program to record sales, you will want to remember data: A loaf of bread was sold to Sione Latu on 14/02/19 for T$1.00. Customer Name: Sione Latu Date Sold: 14/02/19 Item Sold: Loaf of Bread Item Cost: T$1.00 Variables Places in the computer s memory where we can store information. Values and Objects are stored in variables. Numbers Characters Strings Dates

Value Types vs. Reference Types Value Types only store a value Integer only stores: 1 2 5 Boolean only stores: True False Reference Types store a reference to an object A reference is a location in memory The location is the starting point of an object

Primitive Data Types Primitives - The basic types supported by the programming language. Use lowercase for primitive data types in code Type Size (bits) Value Range byte 8-128 to 127 short 16-32,768 to 32767 int 32-2,147,483,648 to 2,147,483,647 long 64-9.2 x 10 15 to 9.2 x 10 15 float 32-3.4 x 10 38 to 3.4 x 10 38 double 64-1.8 x 10 308 to 1.8 x 10 308 char 16 0 to 65,535 boolean 1 True or False

Primitive Data Type Selection Practice How to choose the right primitive variable type. Byte, Short, Int and Long store whole numbers. Float and Double store fractions. Double is more accurate, so if accuracy is important, use double. If you pick something that is too big, you re wasting memory space. Wasting memory space slows things down. If you pick something that is too small, then you re going to crash. If you need a character, use Char. If you need a true/false value, use Boolean. What is a good data type for: Someone s age? A customer s identification number for a video rental store in Nuku alofa? A very large number with decimals? The price of an item?

Using Primitive Variables 2 Steps to using variables 1. Declare the variable 2. Initialize the variable

Declaring Primitive Variables 1 Declare the variable Tell the computer to reserve a space in memory for the variable. You need to tell the computer 2 things: 1. Name of the variable 2. Type of the variable (What kind of variable you have) Primitive types byte short int long float double char boolean Name Type

Declaring Primitive Variables 2 Use a name that is easy to remember. Do not use x, y, z Variable names must start with a letter, underscore or dollar sign. Begin variables with a lowercase character. Then, use a capital letter for each word. Examples firstname customerid

Initializing Primitive Variables Initialize the variable Assign an initial value to a variable. Char values must be enclosed in single quotes. String values must be enclosed in double quotes. Boolean value should be True or False Initial Value

Declaring and Initializing Primitive Variables in 1 line You can declare and initialize a variable in 1 line.

Converting Variable Values Smaller to Larger It is possible to move a value from a variable with a smaller type to a variable with a larger type Example Maximum Short Value Maximum Long Value Is 9.2 x 10 15

Converting Variable Values Larger to Smaller If a value from a variable with a larger type is moved into a variable with smaller type, it may be too big for the smaller type This will cause an error to occur when you run it Maximum Using this: Short Value is 32767 Results in: Do not do this!

Converting Variable Types You can convert variable types by casting it. To cast, use this format: (<desired data type>)variable int is bigger than a byte Casting from a datatype to a smaller datatype is not recommended! Without this, we Would get an error If the Cast function isn t able to convert the value, you will get an error.

Arithmetic Operators Operator Meaning Example + Addition 11 + 22 - Subtraction 22 11 * Multiplication 5 * 6 / Division 21 / 3 % Modulus 12 percent 2 Declare and Initialize x, y and z Get values from x and y Adds x and y together Assigns the sum of x an y to z

Arithmetic Operator Details Addition You can add strings. Adding strings is called concatenation. This converts all non strings into strings. Make sure you add your numbers before converting them to strings. You can t add other objects.

Arithmetic Operator Details Division Tricky because result may not be an integer. In this case the number gets cut off. NOT rounding Even having a result of float doesn t work. Because Java has rules for dealing with data types. The result of a division between two integers is always an integer. So the float just added a.0 to it. To get around this, we need to diving two floats!

Arithmetic Operator Details Subtraction You can t subtract strings or other objects Multiplication User asterisk (*) instead of x Modulus The remainder of a division Can be used to determine whether a number is divisible by another number Can also determine if a number is even or odd

Order of Operations When you have a lot of operations, they are performed in a certain order. Operations in Parentheses () Exponentiation operations from left to right Multiplication or Division operations from left to right Addition or Subtraction operations from left to right Please Excuse My Dear Aunt Sally. Examples: 3 + 6 + 9 / 3 = 3 + 6 + 3 = 12 (3 + 6 + 9) / 3 = 18 / 3 = 6 (5 + 3) / 2 ^ 2 = 8 / 2 ^ 2 = 8 / 4 = 2