1.2 Built-in Types of Data Introduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright © 2008 · December.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 3: Flow Control I: For Loops.
This is Java Jeopardy.
1 Fundamental Data types Overview l Primitive Data Types l Variable declaration l Arithmetical Operations l Expressions l Assignment statement l Increment.
1 Fundamental Data Types. 2 Outline  Primitive Data Types  Variable declaration  Numbers and Constants  Arithmetic Operators  Arithmetic Operator.
Java Intro. Strings “This is a string.” –Enclosed in double quotes “This is “ + “another “ + “string” –+ concatenates strings “This is “ “ string”
Data types and variables
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
CS101: Introduction to Computer Science Slides adapted from Sedgewick and Wayne Copyright © Your First Program.
Exercises A-Declare a variable of double type with initial value=0.0; B- Declare a constant of String type with initial value=“Good” C- Declare a variable.
The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character.
Chapter 3: Data Types and Operators JavaScript - Introductory.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
Java Data Types. Primitive Data Types Java has 8 primitive data types: – char: used to store a single character eg. G – boolean: used to store true or.
CSE 131 Computer Science 1 Module 1: (basics of Java)
JAVA 0. HAFTA Algorithms FOURTH EDITION Robert Sedgewick and Kevin Wayne Princeton University.
Mathematical Calculations in Java Mrs. G. Chapman.
An Introduction to Java – Part 1 Dylan Boltz. What is Java?  An object-oriented programming language  Developed and released by Sun in 1995  Designed.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
CS177 Week2: Recitation Primitive data types and Strings with code examples.
Controlling Program Flow. Data Types and Variable Declarations Controlling Program Flow.
Recap of Functions. Functions in Java f x y z f (x, y, z) Input Output Algorithm Allows you to clearly separate the tasks in a program. Enables reuse.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
1 CS 177 Week 3 Recitation Slides Basic Math Operations, Booleans, and Character Operations.
CS101: Introduction to Computer Science Slides adapted from Sedgewick and Wayne Copyright © Your First Java.
Mathematical Calculations in Java Mrs. C. Furman.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
1 More data types Character and String –Non-numeric variables –Examples: char orange; String something; –orange and something are variable names –Note.
“Great leaders are never satisfied with current levels of performance. They are restlessly driven by possibilities and potential achievements.” – Donna.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Boolean expressions, part 1: Compare operators. Compare operators Compare operators compare 2 numerical values and return a Boolean (logical) value A.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
Object Oriented Programming Lecture 2: BallWorld.
Chapter INTRODUCTION Data Types and Arithmetic Calculations.
CSE 110: Programming Language I Matin Saad Abdullah UB 1222.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Primitive data types Lecture 03. Review of Last Lecture Write a program that prints the multiplication table of 5. class MultiplicationTable { public.
Foundations of Programming: Java
CompSci 230 S Programming Techniques
Multiple variables can be created in one declaration
User input We’ve seen how to use the standard output buffer
Type Conversion, Constants, and the String Object
Chapter 2.
Escape Sequences What if we wanted to print the quote character?
IDENTIFIERS CSC 111.
CS 177 Week 3 Recitation Slides
OPERATORS (2) CSC 111.
Introduction to Java Programming
An Introduction to Java – Part I, language basics
Java so far Week 7.
Building Java Programs Chapter 2
COM-152: Computer Programming Types, Variables, Operators Part 1 of 2
Building Java Programs
Chapter 3 Selections Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Building Java Programs
In this class, we will cover:
Introduction to Computer Science I.
Building Java Programs Chapter 2
Building Java Programs
COMPUTING.
Presentation transcript:

1.2 Built-in Types of Data Introduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright © 2008 · December 25, :26 tt

2 Built-in Data Types Data type. A set of values and operations defined on those values. add, subtract, multiply, divide e23 floating point numbers double add, subtract, multiply, divide integers int and, or, not true false truth values boolean sequences of characters characters set of valuesoperationsliteral valuestype compare 'A' char String concatenate "Hello World" "CS is fun"

3 Basic Definitions Variable. A name that refers to a value. Assignment statement. Associates a value with a variable.

4 Trace Trace. Table of variable values after each statement.

Text

6 String data type. Useful for program input and output.

7 Subdivisions of a Ruler % java Ruler "1 2 1" " " "1" public class Ruler { public static void main(String[] args) { String ruler1 = "1"; String ruler2 = ruler1 + " 2 " + ruler1; String ruler3 = ruler2 + " 3 " + ruler2; String ruler4 = ruler3 + " 4 " + ruler3; System.out.println(ruler4); } string concatenation

Integers

9 int data type. Useful for expressing algorithms.

10 public class IntOps { public static void main(String[] args) { int a = Integer.parseInt(args[0]); int b = Integer.parseInt(args[1]); int sum = a + b; int prod = a * b; int quot = a / b; int rem = a % b; System.out.println(a + " + " + b + " = " + sum); System.out.println(a + " * " + b + " = " + prod); System.out.println(a + " / " + b + " = " + quot); System.out.println(a + " % " + b + " = " + rem); } Integer Operations 1234 = 12* command-line arguments Java automatically converts a, b, and rem to type String % javac IntOps.java % java IntOps = * 99 = / 99 = % 99 = 46

Floating-Point Numbers

12 Floating-Point Numbers double data type. Useful in scientific applications.

13 Math Library

14 Quadratic Equation public class Quadratic { public static void main(String[] args) { // parse coefficients from command-line double b = Double.parseDouble(args[0]); double c = Double.parseDouble(args[1]); // calculate roots double discriminant = b*b - 4.0*c; double d = Math.sqrt(discriminant); double root1 = (-b + d) / 2.0; double root2 = (-b - d) / 2.0; // print them out System.out.println(root1); System.out.println(root2); } Ex. Solve quadratic equation x 2 + bx + c = 0.

15 % java Quadratic – % java Quadratic –1.0 – % java Quadratic NaN % java Quadratic 1.0 hello java.lang.NumberFormatException: hello % java Quadratic 1.0 java.lang.ArrayIndexOutOfBoundsException Testing Testing. Some valid and invalid inputs. command-line arguments not a number golden ratio x 2 – 3x + 2 x 2 – x - 1 x 2 + x + 1

Booleans

17 Booleans boolean data type. Useful to control logic and flow of a program.

18 Comparisons Comparisons. Take operands of one type and produce an operand of type boolean.

19 Leap Year Q. Is a given year a leap year? A. Yes if either (i) divisible by 400 or (ii) divisible by 4 but not 100. public class LeapYear { public static void main(String[] args) { int year = Integer.parseInt(args[0]); boolean isLeapYear; // divisible by 4 but not 100 isLeapYear = (year % 4 == 0) && (year % 100 != 0); // or divisible by 400 isLeapYear = isLeapYear || (year % 400 == 0); System.out.println(isLeapYear); } % java LeapYear 2004 true % java LeapYear 1900 false % java LeapYear 2000 true

Type Conversion

21 Type Conversion Type conversion. Convert from one type of data to another. n Automatic: no loss of precision; or with strings. n Explicit: cast; or method.

22 Random Integer Ex. Generate a pseudo-random number between 0 and N-1. public class RandomInt { public static void main(String[] args) { int N = Integer.parseInt(args[0]); double r = Math.random(); int n = (int) (r * N); System.out.println("random integer is " + n); } String to int (method) double to int (cast) int to double (automatic) int to String (automatic) % java RandomInt 6 random integer is 3 % java RandomInt 6 random integer is 0 % java RandomInt random integer is 3184 double between 0.0 and 1.0

23 Summary A data type is a set of values and operations on those values. String text processing. double, int mathematical calculation. boolean decision making. Be aware. n Declare type of values. n Convert between types when necessary. n In 1996, Ariane 5 rocket exploded after takeoff because of bad type conversion. 1.3

Extra Slides

25 Initializing Variables Q. What happens if I forget to initialize the variable a or b ? n Java compiler does not allow this. n Caveat: in other languages, variable initialized to arbitrary value. Q. What is default value for Registrar's room assignment variables?

26 Initializing Variables Q. What happens if I forget to initialize the variable a or b ? n Java compiler does not allow this. n Caveat: in other languages, variable initialized to arbitrary value. Q. What is default value for Registrar's room assignment variables? A. 61 Nassau Street. Nassau Presbyterian Church