Primitive Data Types int is a primitive data type A primitive data type is one that stores only a single piece of data. TypeStorageDescription int 4 bytes+ve.

Slides:



Advertisements
Similar presentations
© 2007 Lawrenceville Press Slide 1 Assignment Statement An assignment statement gives a value to a variable. Assignment can take several forms: x = 5;
Advertisements

Types and Arithmetic Operators
Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
Slide 1 Chapter 4 Assignment Statement An assignment statement gives a value to a variable. Assignment can take several forms: x = 5; a literal (5) is.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
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.
CS 106 Introduction to Computer Science I 09 / 13 / 2006 Instructor: Michael Eckmann.
Chapter 2: Using Data.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: Numeric Data *Variables *Numeric data.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
Lecture 4 Types & Expressions COMP1681 / SE15 Introduction to Programming.
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
Chapter 3 Numerical Data. Topics Variables Numeric data types Assignment Expressions.
© 2006 Pearson Addison-Wesley. All rights reserved2-1 Console Input Using the Scanner Class Starting with version 5.0, Java includes a class for doing.
Basic Input/Output and Variables Ethan Cerami New York
Week 2 - Friday.  What did we talk about last time?  Data representation  Binary numbers  Types  int  boolean  double  char  String.
Intro to Java Programming  A computer follows the instruction precisely and exactly.  Anything has to be declared and defined before it can be used.
Expressions, Data Conversion, and Input
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
 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)
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Agenda  Commenting  Inputting Data from Keyboard (scanf)  Arithmetic Operators  ( ) * / + - %  Order of Operations  Mixing Different Numeric Data.
Chapter 4 Variables and Constants. Goals and Objectives 1. Understand simple data types (int, boolean, double). 2. Declare and initialize variables using.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Chapter 2: Using Data.
OOP with Java, David J. Barnes Adding Sequential Behavior1 Adding Behavior Previous class definitions have passively maintained attributes. Active behavior.
BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA TYPES AND OPERATIONS.
Variables and Expressions CMSC 201 Chang (rev )
Introduction to C Programming Chapter 2 : Data Input, Processing and Output.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Java Programming: From Problem Analysis to Program Design, 5e Chapter 2 Basic Elements of Java.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
FUNDAMENTALS 2 CHAPTER 2. OPERATORS  Operators are special symbols used for:  mathematical functions  assignment statements  logical comparisons 
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
© 2005 Lawrenceville Press Slide 1 Chapter 4 Assignment Statement An assignment statement gives a value to a variable. Assignment can take several forms:
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Java – Variables and Constants By: Dan Lunney. Declaring Variables All variables must be declared before they can be used A declaration takes the form:
1 Review for exam 1 CS 101 Aaron Bloomfield. 2 Today’s lecture An overview of the “review” sections of chapters 1-3 Stop me if you want me to go over.
CS 106 Introduction to Computer Science I 09 / 10 / 2007 Instructor: Michael Eckmann.
© 2007 Lawrenceville Press Slide 1 Chapter 4 Review Assignment Statement An assignment statement gives a value to a variable. Assignment can take several.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Variables and Constants Chapter 4 Review. Declaring Variables A variable is a name for a value stored in memory. Variables and constants are used in programs.
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
1 2. Program Construction in Java. 01 Java basics.
Fundamentals 2 1. Programs and Data Most programs require the temporary storage of data. The data to be processed is stored in a temporary storage in.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
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.
CompSci 230 S Programming Techniques
Lecture 2 D&D Chapter 2 & Intro to Eclipse IDE Date.
Chapter 2 Basic Computation
Chapter 4 Assignment Statement
Chapter 2 More on Math More on Input
2.5 Another Java Application: Adding Integers
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Multiple variables can be created in one declaration
Chapter 3 Assignment Statement
Java Programming: From Problem Analysis to Program Design, 4e
INPUT STATEMENTS GC 201.
Chapter 2 Edited by JJ Shepherd
Chapter 2: Basic Elements of Java
Fundamentals 2.
A+ Computer Science INPUT.
Primitive Types and Expressions
Presentation transcript:

Primitive Data Types int is a primitive data type A primitive data type is one that stores only a single piece of data. TypeStorageDescription int 4 bytes+ve or -ve integer double 8 bytes +ve or -ve number with optional decimal point char 2 bytesa single character boolean 1 bit true or false

You Do Review: Distance - part 1 of 2 on P 79 of your text.

Primitive Data Types (cont’d) double is a sometimes called a floating point number Don’t choose a bigger data type than you need (e.g. a double where an int will do). Conserve memory. Choose the appropriate size also signals the type of value which is expected.

Abstract Data Types Variables can also be declared using abstract data types. A class is an abstract data type. A variable declared with a class is an object.

Abstract Data Types - Instantiation = new ( ); Circle spot = new Circle(4); //spot has radius 4 We have new variable spot with radius of 4. Access members of a class using the object name, then a dot, then the member name. Suppose Circle has at getRadius() method. System.out.println(“Radius of spot: “ + spot.getRadius()); Radius of spot: 4

Data from the Outside So far we’ve only used data that comes from inside our program. –i.e. data that the program already knows about when it’s compiled and before it begins to run. Useful programs need to be able to get/read data from outside themselves.

Data from the Outside (cont’d) We can get data from the keyboard (typed in by the user). We can also read data from files. –that’s for later. We can also read data from a device or sensor (think robotics or environmental monitoring.)

Getting Data from the User Values can be read from an input stream. An input stream is a sequence of characters received from an input device, such as a keyboard. We can use the included Scanner class to read from the input stream in Java.

The Scanner Class To use the Scanner class you have to declare a Scanner object in your program. –Q:What do we call it when we declare an object? –A:instantiation.

The Scanner Class Instantiating a scanner object: Scanner input = new Scanner(System.in); We’ve just declared a Scanner object and called it input. This won’t work unless we also add an import statement at the top of our program outside the class.

The Scanner Class Here’s the import statement: import java.util.Scanner; This tells the compiler that we want to use the Scanner package.

The Scanner Class We can use the Scanner object that we just declared and a method called nextInt() to get an integer value from the user: someVariable = input.nextInt(); Assuming someVariable has already been declared then after this line executes someVariable will have whatever integer the user types on the keyboard. What datatype do you think someVariable will have to have?

The Scanner Class The Scanner class also has methods that will accept decimal numbers - nextDouble() - and character strings - nextLine()

You Do Implement RectangleArea2 as laid out on P82 of your text. What happens if you enter a decimal number instead of an integer when prompted? Now, do Review: Distance - part 2 of 2 on p 82 of your text.

UserInput Here are the general steps for getting values from the keyboard: –IMPORT the Scanner class –DECLARE some variables to store stuff –Declare a Scanner object –PROMPT the user to enter the data –use nextInt(), nextDouble(), nextLine() with the Scanner object to get the data and store it in the variable.

Numeric Expressions Arithmetic operators: –Addition + –Subtraction - –Multiplication * –Division / –Modulus Division %

Integer Division If the values being divided are both integers, “/” returns only the integer portion of the division. E.g. 20/7 ==> 2 (with remainder 6)

You Do: Create an application called AvgDistance that prompts the user for 3 distances and displays the average. Users should be able to enter decimal numbers. You will need to use: nextDouble()

Modulus Division Modulus division returns the remainder of an integer division E.g. 20 % 7 ==> 6 Integer and modulus division are useful for retrieving individual digits in a number E.g 27 % 10 ==> 7 27 / 10 ==> 2

You Do. Create a program called ParseInteger that prompts the user for a 3 digit integer and returns each individual digit. You will need to use modulus Division for this. Brainstorm with a partner/colleague in class. Hint: You will need to use both integer and modulus division to make this work

Order of Operations What does * evaluate to? It depends on the order in which you do the operations. Java uses the same rules of precedence as mathematics. Multiplication and division precede addition and subtraction. Operators of equal precedence are evaluated from left to right.

Order of Operations (cont’d) Change the order of operations by using round parentheses “ ( ) ” (6 + 4) * (2 – 1) evaluates to 10 It is good practice to include parentheses whenever there is the possibility of ambiguity.

Type Casting Typecasting is a way of making a variable of one data type act like a variable of another data type for a single operation. Most useful when we want to divide data that we collect as integers without truncating. Remember truncation?

Type Casting (cont’d) int litres; int kilometres; int fuelEfficiency; //should be expressed in litres/km Car mazda = new Car(); // assume we have a class called Car litres = mazda.getGasPurchases(); kilometres = mazda.getMileage(); //assume 42 litres and 475 km fuelEfficiency = litres/kilometres; //this was integer div so the result was 0 //instead fuelEfficiency = (double)litres/kilometres;