Numbers and Arrays. Widening and narrowing Numeric types are arranged in a continuum: double float long int short byte, char You can easily assign a narrower.

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

Chapter 9 – One-Dimensional Numeric Arrays. Array u Data structure u Grouping of like-type data u Indicated with brackets containing positive integer.
Numeric Types & Ranges. ASCII Integral Type Numerical Inaccuracies Representational error – Round-off error – Caused by coding a real number as a finite.
STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
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.
18-May-15 Numbers. 2 Bits and bytes A bit is a single two-valued quantity: yes or no, true or false, on or off, high or low, good or bad One bit can distinguish.
Arrays. A problem with simple variables One variable holds one value –The value may change over time, but at any given time, a variable holds a single.
18-Jun-15 Arrays. 2 A problem with simple variables One variable holds one value The value may change over time, but at any given time, a variable holds.
CS102 Data Types in Java CS 102 Java’s Central Casting.
22-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
26-Jun-15 Arrays. 2 A problem with simple variables One variable holds one value The value may change over time, but at any given time, a variable holds.
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
PHYS 2020 Making Choices; Arrays. Arrays  An array is very much like a matrix.  In the C language, an array is a collection of variables, all of the.
Arrays. A problem with simple variables One variable holds one value –The value may change over time, but at any given time, a variable holds a single.
Chapter 9: Arrays and Strings
Chapter 8 Arrays and Strings
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
Representation and Conversion of Numeric Types 4 We have seen multiple data types that C provides for numbers: int and double 4 What differences are there.
Lecture 2: Topics Bits and Bytes Primitive Types Casting Strings Boolean expressions.
Arrays. A problem with simple variables One variable holds one value –The value may change over time, but at any given time, a variable holds a single.
Expressions, Data Conversion, and Input
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Chapter 2: Introducing Data Types and Operators.  Know Java’s primitive types  Use literals  Initialize variables  Know the scope rules of variables.
 Value, Variable and Data Type  Type Conversion  Arithmetic Expression Evaluation  Scope of variable.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Java 2 More Java Then Starbucks ;-). Important Terms Primitive Data – Basic, built-in values (characters & numbers) Data Type – Used to talk about values.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Lec 6 Data types. Variable: Its data object that is defined and named by the programmer explicitly in a program. Data Types: It’s a class of Dos together.
BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA TYPES AND OPERATIONS.
CISC105 – General Computer Science Class 9 – 07/03/2006.
Mathematical Calculations in Java Mrs. G. Chapman.
C++ Programming, Namiq Sultan1 Chapter 3 Expressions and Interactivity Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin.
Mathematical Calculations in Java Mrs. C. Furman.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
Operators & Identifiers The Data Elements. Arithmetic Operators exponentiation multiplication division ( real ) division ( integer quotient ) division.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
CHAPTER 6 ARRAYS IN C++ 2 nd Semester King Saud University College of Applied studies and Community Service CSC 1101 By: Fatimah Alakeel Edited.
1 Chapter 12 Arrays. 2 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating.
Data Types and Conversions, Input from the Keyboard CS303E: Elements of Computers and Programming.
Doing math In java.
© 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.
Chapter 3 The New Math. C++ Data Types simple integral charshort intlong bool floating float double Long double enum address pointer reference structured.
LESSON 5 – Assignment Statements JAVA PROGRAMMING.
A data type in a programming language is a set of data with values having predefined characteristics.data The language usually specifies:  the range.
CSCI 1100/1202 January 18, Arithmetic Expressions An expression is a combination of operators and operands Arithmetic expressions compute numeric.
Expressions Version Topics Arithmetic expressions Conversions Operator precedence String class 2.
Introduction to programming in java Lecture 21 Arrays – Part 1.
CSE 1301 Lecture 12 Arrays Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
27-Jun-16 Arrays. 2 Multiple values An array lets you associate one name with a fixed (but possibly large) number of values Arrays are like Python’s lists,
Numbers Mar 27, 2013.
Expressions.
2.0 FUNDAMENTALS OF JAVA PROGRAMMING LANGUAGE
Object Oriented Programming
Multiple variables can be created in one declaration
Type Conversion, Constants, and the String Object
Fundamental Data Types
Conversions of the type of the value of an expression
Type Conversion, Constants, and the String Object
Increment and Decrement
Arithmetic Expressions & Data Conversions
Fundamental Data Types
Numbers 27-Apr-19.
Java’s Central Casting
Type Conversion It is a procedure of converting one data type values into another data type In C programming language we are having two types of type conversion.
Numbers 6-May-19.
Chapter 2: Java Fundamentals cont’d
Chapter 3 The New Math.
Arithmetic Expressions & Data Conversions
Presentation transcript:

Numbers and Arrays

Widening and narrowing Numeric types are arranged in a continuum: double float long int short byte, char You can easily assign a narrower type to a wider type: double wide; int narrow; wide = narrow; But if you want to assign a wider type to a narrow type, you have to cast it: narrow = (int)wide; You must use a cast in both directions for byte and char Wider Narrower

Mixed types If you mix numeric types, the narrower type is automatically promoted to the wider type –int narrow = 5; double wide; double anotherWide = wide + narrow; Integer division is when you divide one integer type by another; the fractional part is discarded –Example: narrow = 19 / 5; // result is 3

Math methods Converting a double to an int just discards the fractional part: (int)17.93 is 17 (int) –17.93 is -17 double Math.floor(double) –Given a double, returns (as a double) the largest integral value not greater than the argument Math.floor(17.93) returns 17.0 Math.floor(-17.93) returns –18.0 double Math.ceil(double) –Given a double, returns (as a double) the smallest integral value not smaller than the argument Math.ceil(17.93) returns 18.0 Math.ceil(-17.93) returns –17.0

A problem with simple variables One variable holds one value –The value may change over time, but at any given time, a variable holds a single value If you want to keep track of many values, you need many variables All of these variables need to have names What if you need to keep track of hundreds or thousands of values?

Multiple values An array lets you associate one name with a fixed (but possibly large) number of values All values must have the same type The values are distinguished by a numerical index between 0 and array length minus 1 In this example, the name myArray refers to the entire array Subscripted names, such as myArray[4], refer to a single element of the array myArray

Indexing into arrays To reference a single array element, use array- name [ index ] Indexed elements can be used just like simple variables –You can access their values –You can modify their values An array index is sometimes called a subscript

Using array elements Examples: x = myArray[1]; // sets x to 3 myArray[4] = 97; // replaces 11 with 97 m = 5; y = myArray[m]; // sets y to 13 w = myArray[m + 1] // sets w to 17 z = myArray[myArray[3]]; // sets z to myArray

Array values An array may hold any type of value All values in an array must be the same type –For example, you can have: an array of int an array of String an array of Person an array of arrays of String an array of Object

The End