Week 2 - Wednesday CS 121.

Slides:



Advertisements
Similar presentations
 Variables  What are they?  Declaring and initializing variables  Common uses for variables  Variables you get “for free” in Processing ▪ Aka: Built-in.
Advertisements

CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type.
COMP3221: Microprocessors and Embedded Systems--Lecture 1 1 COMP3221: Microprocessors and Embedded Systems Lecture 3: Number Systems (I)
Week 2: Primitive Data Types 1.  Programming in Java  Everything goes inside a class  The main() method is the starting point for executing instructions.
Introduction to Programming with Java, for Beginners Primitive Types Expressions Statements Variables Strings.
10-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
CIS 234: Using Data in Java Thanks to Dr. Ralph D. Westfall.
Introduction to Primitives. Overview Today we will discuss: –The eight primitive types, especially int and double –Declaring the types of variables –Operations.
Constants Variables change, constants don't final = ; final double PI = ; … area = radius * radius * PI; see Liang, p. 32 for full code.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Fundamental data types Horstmann Chapter 4. Constants Variables change, constants don't final = ; final double PI = ; … areaOfCircle = radius *
01- Intro-Java-part1 1 Introduction to Java, and DrJava Barb Ericson Georgia Institute of Technology June 2008.
Input & Output: Console
Week 2 - Monday.  What did we talk about last time?  Software development  Lab 1.
Georgia Institute of Technology Introduction to Java, and DrJava Barb Ericson Georgia Institute of Technology Aug 2005.
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.
First Program.  The type of all data used in a C program must be specified  A data type is a description of the data being represented ▪ That is, a.
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.
5 BASIC CONCEPTS OF ANY PROGRAMMING LANGUAGE Let’s get started …
Week 2 - Wednesday.  What did we talk about last time?  C compilation model  Lab 1.
Variables and Functions. Open your Encoder program Let’s begin by opening the “Labyrinth Auto Straight” code. Save this file as Labyrinth with variables.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
Data Tonga Institute of Higher Education. Variables Programs need to remember values.  Example: A program that keeps track of sales needs to remember.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
© A+ Computer Science - A reference variable stores the memory address of an object. Monster fred = new Monster(); Monster sally.
Basic Data Types อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา Chapter 4.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Week 3 - Monday.  What did we talk about last time?  Video games  Lab 2.
Choosing Data Types Database Administration Fundamentals
Programming and Data Structure
EGR 2261 Unit 10 Two-dimensional Arrays
Week 3 - Monday CS 113.
Number Representation
Week 3 - Friday CS222.
Console Output, Variables, Literals, and Introduction to Type
Chapter 6: Data Types Lectures # 10.
Documentation Need to have documentation in all programs
Chapter 3 Data Storage.
Multiple variables can be created in one declaration
Variables and Primative Types
Computer Programming Methodology Introduction to Java
Invitation to Computer Science, Java Version, Third Edition
Primitive Types Vs. Reference Types, Strings, Enumerations
Chapter 2.
Strings, Line-by-line I/O, Functions, Call-by-Reference, Call-by-Value
Chapter 2 Bits, Data Types & Operations Integer Representation
Lesson 2: Building Blocks of Programming
Introduction to Java, and DrJava part 1
Numbers.
Introduction to Primitive Data types
Coding Concepts (Data- Types)
Storing Negative Integers
Chapter 3 DataStorage Foundations of Computer Science ã Cengage Learning.
Introduction to Java, and DrJava
Chapter 2: Introduction to C++.
Introduction to Primitives
Introduction to Primitives
Introduction to Java, and DrJava
Introduction to Java, and DrJava part 1
In your notebook… Purpose: What types of data can we store in C
Variables and Constants
Introduction to Primitive Data types
Week 7 - Monday CS 121.
Week 1 - Friday COMP 1600.
Section 6 Primitive Data Types
Presentation transcript:

Week 2 - Wednesday CS 121

Last time What did we talk about last time? Software development Lab 1

Questions?

Data Representation

Data What if you want to write a Java program that can… Edit music files Play a DVD Organize your photo album Each of these tasks manipulates a lot of data MP3’s, DVD’s, and jpegs are complicated kinds of data Java must build these kinds of data out of much simpler kinds of data

Binary Hardware You have heard people (me, probably) talking about all the 1’s and 0’s inside of a computer What does that all really mean? Using semiconductor physics, we can make a tiny little piece of a microchip be in one of two states, say, OFF and ON, like a switch If we say that OFF is 0 and ON is 1, then, by using a lot of these switches, we can represent a lot of 1’s and 0’s

Binary Representation What do we do with those 1’s and 0’s? To begin with, we represent numbers How many of you have heard of base 10? How many of you have heard of base 2? What’s the definition of a number system with a given base?

Base 10 (decimal) numbers Our normal number system is base 10 This means that our digits are: 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9 Base 10 means that you need 2 digits to represent ten, namely 1 and 0 Each place in the number as you move left corresponds to an increase by a factor of 10

3,482,931 Base 10 Example Ten thousands Hundreds Millions Ones Hundred Tens

Base 2 (binary) numbers The binary number system is base 2 This means that its digits are: 0 and 1 Base 2 means that you need 2 digits to represent two, namely 1 and 0 Each place in the number as you move left corresponds to an increase by a factor of 2 instead of 10

11111100001 Base 2 Example Sixty fours 256’s Sixteens Fours 1024’s Ones 512’s 128’s Thirty twos Eights Twos

So, what’s the value? 11111100001 = 1∙210 + 1∙29 + 1∙28 + 1∙27 + 1∙26 + + 1∙25 + 0∙24 + 0∙23 + 0∙22 + 0∙21 + 1∙20 = 1024 + 512 + 256 + 128 + 64 + 32 + 1 = 2017

The good news You don’t actually have to worry about doing binary conversions when you are coding Java You should know this information because it explains how Java is different from math In math, you can talk about an arbitrarily large number In Java, each number is stored with a specific number of binary digits There are limits on how big (or small) a given number can be

Built-in Types of Data

Built-in types We are going to focus on five basic types of data in Java These are: int For whole numbers double For rational numbers boolean For true or false values char For single characters String For words String is a little different from the rest, but we’ll get into that later

The int Type

The int type As I just said, the int type is used to store integers (positive and negative whole numbers and zero) Examples: 54 -893992 Inside the computer, an int takes up 4 bytes of space, which is 32 bits (1’s and 0’s)

Range of the int type With 32 bits, an int can hold integers from about -2 billion up to 2 billion Positive numbers are represented just like we’ve seen Negative numbers need an extra trick that we aren’t going to go into The actual maximum value is: 2147483647

Overflow and underflow Let’s say you add 100 to the maximum int value 2147483647 You do not get 2147483747 Instead, it becomes a very negative number: -2147483549 This phenomenon is called overflow The opposite thing happens if you have a very negative number and you subtract a number that makes it too negative This phenomenon is called underflow

Literals Each type has a number of literals associated with it A literal is a concrete value within a given type Literals for the int type are numbers exactly like what you would expect: 115 -9837461 2

Variables It is also possible to create variables for each data type Think of a variable as a “box” that you can put values into The name of a variable is an identifier We can declare a variable of type int with identifier i using the following line of code: int i;

Storage for an int int i; This line of code creates a box named i that is designed only to hold ints That is, it is 32 bits in size What’s inside this box? i

Assignment into an int int i; i By default, the declaration of an int puts the literal value 0 inside the box Remember, you must declare a variable before using it i

Changing the value of a variable Java variables are not like variables in math which have a fixed (but unknown) value Instead, a Java variable can be changed by a line of code We use the assignment operator (=) to change the value of a variable as follows: i = 5;

Changing the value of a variable This line of code stores 5 into i Think of the = operator as an arrow pointing left Let’s see this in Eclipse i 5 5

The double Type

The double type You will use the int type very often Sometimes, however, you need to represent numbers with a fractional part The double type is well suited to this purpose Declaration of a double variable is just like an int variable: double x;

Storage for a double double x; This line of code creates a box named x that is designed only to hold doubles It has a different size from an int x

Assignment for a double x = 3.14159; This line of code stores 3.14159 into x Remember that the = operator is like an arrow pointing left Let’s see this in Eclipse x 3.14159 3.14159

The boolean Type

The boolean type Numbers are great But, sometimes you only need to keep track of whether or not something is true or false This is what the boolean type is for You will understand it better when we cover conditionals in a couple of weeks Declaration of a boolean variable is like so: boolean value;

Storage for a boolean boolean value; This line of code creates a box named value that is designed only to hold booleans It cannot be used to store numbers value

Assignment for a boolean value = false; This line of code stores false into value Remember that the = operator is like an arrow pointing left value false false

The char Type

The char type Sometimes you need to deal with characters This is what the char type is for The char type only allows you to store a single character like '$' or 'q' Declaration of a char variable is like so: char c;

Storage for a char char c; This line of code creates a box named c that is designed only to hold chars It is used to store characters from most of the different scripts in the world c

Assignment for a char c = 'a'; c This line of code stores the letter 'a' into into c We must use the single quotes so that Java knows we are talking about the character 'a' and not a variable named a Try this on your own in Eclipse c 'a' 'a'

The String Type

The String type The String type is different from the other types in several ways The important thing for you to focus on now is that it can hold a large number of chars, not just a single value A String literal is what we used in the Hello, World program String word;

Storage for a String String word; This line of code creates a box named word that is designed only to hold Strings It is used to store text of any length from most of the different scripts in the world word

Assignment for a String word = "Mad flavor"; This line of code stores the String "Mad flavor" into word We must use the double quotes so that Java knows we are talking about the text "Mad flavor" word "Mad flavor" "Mad flavor"

Recap

Summary of types Type Kind of values Sample Literals int double Integers -5 900031 double Floating-point Numbers 3.14 -0.6 6.02e23 boolean Boolean values true false char Single characters 'A' 'Z' '&' String Sequences of characters "If you dis Dr. Dre" "10 Sesquipedalians"

Quiz

Upcoming

Next time… Getting input from the command line Basic math operations Lab 2

Reminders Keep reading Chapter 3 of the textbook