Class 7.

Slides:



Advertisements
Similar presentations
Java Programming, 3e Concepts and Techniques Chapter 3 Section 63 – Manipulating Data Using Methods – Day 2.
Advertisements

Types and Arithmetic Operators
Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
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.
Arithmetic Operators MeaningOperator Addition Subtraction Multiplication Division Modulus + - * / % Type Binary Binary Binary Binary Binary.
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.
CIS 234: Using Data in Java Thanks to Dr. Ralph D. Westfall.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
Introduction to Primitives. Overview Today we will discuss: –The eight primitive types, especially int and double –Declaring the types of variables –Operations.
Program Elements We can now examine the core elements of programming (as implemented in Java) We focuse on: data types variable declaration and use, constants.
1 Data types, operations, and expressions Overview l Format of a Java Application l Primitive Data Types l Variable Declaration l Arithmetic Operations.
Data types and variables
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Chapter 2 Data Types, Declarations, and Displays
Starting Out with C++, 3 rd Edition 1 Chapter 2. Introduction to C++
1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral.
01- Intro-Java-part1 1 Introduction to Java, and DrJava Barb Ericson Georgia Institute of Technology June 2008.
Objectives You should be able to describe: Data Types
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Georgia Institute of Technology Introduction to Java, and DrJava Barb Ericson Georgia Institute of Technology Aug 2005.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
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.
BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA TYPES AND OPERATIONS.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Chapter 2 Variables.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
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)
1 Class 6. 2 Objectives Objectives Identify, declare, and use primitive data types Use the System class to create data streams Use the Scanner class of.
1 Chapter 2: Java Fundamentals cont’d Spring Lory Al Moakar.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
CS 106 Introduction to Computer Science I 09 / 10 / 2007 Instructor: Michael Eckmann.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
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.
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.
What will each of the following lines print? System.out.println("number" ); number645 System.out.println("number" + (6 + 4)+ 5); number105 System.out.println(6.
Review by Mr. Maasz, Summary of Chapter 2: Starting Out with Java.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Chapter 2 Variables.
Chapter 2 Basic Computation
BASIC ELEMENTS OF A COMPUTER PROGRAM
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Multiple variables can be created in one declaration
Type Conversion, Constants, and the String Object
Java Programming: From Problem Analysis to Program Design, 4e
Introduction to C++ Programming
Chapter 2: Basic Elements of Java
Introduction to Java, and DrJava part 1
Chapter 2 Variables.
Fundamentals 2.
Chapter 2: Java Fundamentals
The Data Element.
Introduction to Primitives
Introduction to Primitives
Primitive Types and Expressions
Unit 3: Variables in Java
The Data Element.
Chapter 2 Variables.
Presentation transcript:

Class 7

Objectives Identify, declare, and use primitive data types Use variables in programs to hold data in RAM Use assignment statements to store data with proper identifiers Use operators and parentheses correctly in numeric expressions

Introduction Data are collections of raw facts or figures A program performs operations on input data to output information Input data can come from a variety of sources The program itself Users of the program External files

Variables Variables are like storage locations in the computer’s memory. Storage for the value of the variable

Naming Rules of Variables First character must be one of the letters a-z, A-Z, or an underscore ( _ ) or a $ After first character use a-z, A-Z, 0-9, underscore ( _ ) or $ Any length Keep them meaningful

Variable Rules (con’t) Case sensitive ItemsOrder does not equal itemsorder Cannot declare two variables of the same name in a method Cannot give a variable the same name as a method

Storing Data Java is a strongly typed language Variables must be declared with a data type Variable locations can hold only that data type Java has two categories of data types Primitive data types hold single data items Integers, characters, floating point, and booleans are primitive types Reference data types hold a value that refers to the location of the data All Objects and arrays are reference types

Intrinsic Data Types JAVA has eight intrinsic data types, which form the basis for all other data types (i.e., classes): 1. boolean: - a 1 byte value that is assigned a value of either true or false (both are JAVA defined values). Boolean values have no direct conversion to integer values, and are initialized to false.

Intrinsic Data Types 2. byte: - a 1 byte integer, capable of representing numbers from numbers from -128 to +127. It is initialized to 0. 3. char: - a 2 byte integer that is normally used to represent character values using the Unicode system (the first 128 values of which correspond to ASCII values). It is initialized to \u0000.

Intrinsic Data Types 4. short: - a 2 byte integer, able to represent numbers between -32K and +32K. It is initialized to 0. 5. int: - a 4 byte integer, able to represent numbers between -2 billion and +2 billion.

Intrinsic Data Types 6. long: - an 8 byte integer, capable of representing numbers between -2 and +2 . It is initialized to 0. 7. float: - a 4 byte IEEE format real number, giving about 7 decimal digits of precision. It is initialized to 0.0f. 63 63

Intrinsic Data Types 8. double: - an 8 byte IEEE format real number, giving about 15 decimal digits of precision. It is initialized to 0.0d.

Declaring Variables General form: 1. dataType identifier; 2. dataType identifier, identifier, identifier; 3. dataType identifier = initialValue; 4. dataType identifier = new dataType(); int myAge; int myAge, yourAge, theirAges; int myAge = 24; Person me = new Person();

Key JAVA Operators ( ) Parenthesis (X+2)/3 Operator Description Example ( ) Parenthesis (X+2)/3

Key JAVA Operators Operator Description Example * Multiply X*2

Key JAVA Operators Operator Description Example / Divide X/12

Key JAVA Operators Operator Description Example % Modulus 7%3

Key JAVA Operators Operator Description Example + Add X+7

Key JAVA Operators Operator Description Example - Subtract X-6

Key JAVA Operators = Assignment operator Y=X+3 Operator Description Example = Assignment operator Y=X+3

Arithmetic Operators The order of operator precedence is a predetermined order that defines the sequence in which operators are evaluated in an expression Addition, subtraction, multiplication, and division can manipulate any numeric data type When Java performs math on mixed data types, the result is always the larger data type Casts allow programmers to force a conversion from one primitive type to another

Numeric Expressions Numeric expressions evaluate to a number Only numeric primitive data types may be used in a numeric expression A value and variable must be separated by an arithmetic operator Unless parentheses supercede, an expression is evaluated left to right with the following rules of precedence: Multiplication and/or division Integer division Modular division Addition and/or subtraction

Parentheses in Expressions Parentheses may be used to change the order of operations The part of the expression within the parentheses is evaluated first Parentheses can provide clarity in complex expressions Numeric and conditional expressions should be grouped with parentheses Parentheses can be nested Java evaluates the innermost expression first and then moves on to the outermost expression

How precedence works x = ( 3 + 8 ) / 3 * 2 + 5 % 3 x = ? 11 / 3 / 3 * 2 + 5 % 3 x = 3 * 2 + 5 % 3 x = 6 + 5 % 3 x = 6 + 2 x = 8

Assignment Statements General syntax: identifier = value; x = 5; dataType identifier = value; int x = 6; identifier = formula; x = 3 * y+5; identifier = differentIdentifier; x = y;

Using Variables in Output to the console instead of to a window System.out.println(“Hello World”); System.out.println(5); System.out.print(“my age is \n“ + age); System.out.println(“ “); String concatenation operator

Assignment Statements Exercise 1 The value in number is number _ Assignment Statements Exercise 1 int number; number = 5; System.out.println(“The value in number is ” + “number”) ; number ? 5

Assignment Statements Exercise 2 The value in number is 5 _ Assignment Statements Exercise 2 int number; number = 5; System.out.println(“The value in number is ” + number) ; number ? 5

Multiple Assignment Statements Tracing Code: exercise 3 int Checking; int Miles; long Days; Checking = -20; Miles = 4276; Days = 187000; Days ? 187000 Checking ? Miles ? -20 4276

System.out.println( “We have made a” + “ long trip of ” + Miles + “ miles.”); System.out.print(“Our checking account” + “ balance is ” + “Checking”); System.out.print(“\nExactly ” + Days + “ days ago Columbus” + “ stood on this spot.”); Days 187000 Checking -20 Miles 4276

Multiple Assignment Statements Tracing Code: Exercise 4 double RegWages, BasePay = 18.25; double RegHours = 40.0; double OTWages, OTPay = 27.78; double OTHours = 10; double TotalWages; RegWages ?.? OTWages ?.? TotalWages ?.? BasePay 18.25 OTPay 27.78 RegHours 40.0 OTHours 10

Basic usage of operators RegWages = BasePay * RegHours; OTWages = OTPay * OTHours; TotalWages = RegWages + OTWages; System.out.println( “Wages for this” + “ week are $ ” + TotalWages ); RegWages ?.? OTWages ?.? 730.0 277.8 TotalWages ?.? BasePay 18.25 OTPay 27.78 RegHours 40.0 OTHours 10 1007.8 PG 66 ~ Pgrm. 2-20

Write assignment statements A) Adds 2 to A and stores the result in B. B = A + 2; pg. 85 Problem #20a a) Adds 2 to A and stores the result in B b) Multiplies B by 4 and stores the reul in At

Write assignment statements B) Multiplies B times 4 and stores the result in A. A = B * 4;

Write assignment statements C) Divides A by 3.14 and stores the result in B. B = A / 3.14;

Write assignment statements D) Subtracts 8 from B and stores the result in A. A = B - 8;

Write assignment statements E) Stores the value 27 in A. A = 27;

What is this program’s output int Freeze = 32, Boil = 212; Freeze = 0; Boil = 100; System.out.print(Freeze + “\n “ + Boil + “\n”); 100 _

What is this program’s output int X = 0, Y = 2; X = Y * 4; System.out.println( “” + X + “\n “ +Y ); 8 2 _

System.out.print(“I am the incredible” + “ computing\nmachine ” + “\nand I will\namaze\n” + “you.”);

I am the incredible computing machine and I will amaze you. _

What is this program’s output System.out.println( “Be careful\n” + “This might/n be a trick ” + “question”); Be careful This might/n be a trick question _

What is this program’s output int A, X = 23; A = X % 2; System.out.println( X); System.out.println( A ); 23 1 _

Conclusion of Class 7