Chapter 2 Basic Computation

Slides:



Advertisements
Similar presentations
Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
Advertisements

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.
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.
Introduction to Primitives. Overview Today we will discuss: –The eight primitive types, especially int and double –Declaring the types of variables –Operations.
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
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.
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.
Mathematical Calculations in Java Mrs. G. Chapman.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Primitive Data Types. Identifiers What word does it sound like?
Chapter 2 Variables.
Mathematical Calculations in Java Mrs. C. Furman.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
COMP Primitive and Class Types Yi Hong May 14, 2015.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Data Tonga Institute of Higher Education. Variables Programs need to remember values.  Example: A program that keeps track of sales needs to remember.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
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.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
CompSci 230 S Programming Techniques
Chapter 2 Variables.
Building Java Programs
Introduction to Computer Science / Procedural – 67130
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Multiple variables can be created in one declaration
Variables and Primative Types
Variables, Expressions, and IO
Computer Science 3 Hobart College
Chapter 3 Assignment Statement
Variables, Printing and if-statements
Type Conversion, Constants, and the String Object
Java Programming: From Problem Analysis to Program Design, 4e
Chapter 2 Basic Computation
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
IDENTIFIERS CSC 111.
Building Java Programs Chapter 2
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Introduction to C++ Programming
Chapter 2 Edited by JJ Shepherd
Chapter 2: Basic Elements of Java
Building Java Programs
Chapter 2 Variables.
Building Java Programs
Building Java Programs Chapter 2
Chapter 2 Programming Basics.
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Introduction to Primitives
Introduction to Primitives
Building Java Programs
Primitive Types and Expressions
Building Java Programs
Unit 3: Variables in Java
Building Java Programs Chapter 2
Building Java Programs
Chapter 2 Variables.
Just Enough Java 17-May-19.
Building Java Programs
Building Java Programs
Building Java Programs
Presentation transcript:

Chapter 2 Basic Computation © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission

Screen Output System.out = In Java the screen is represented by the object: System.out The screen object contains the following methods for displaying output: println() – prints what is inside the parentheses, with a line break at the end print() – prints what is inside the parentheses, with no line break Java Program Physical Hardware System.out =

Screen Output Source code Program output System.out.println("Hello"); System.out.println("World"); Hello World Hello World Line-Break Line-Break System.out.println("Hello"); System.out.println("World"); Note: Number of lines of source code have no relationship to the number of lines of output!

Screen Output Source code Program output System.out.println("Hello"); System.out.println("World"); Hello World Line-Break Line-Break Hello World \n System.out.println("Hello\nWorld"); \n Note: The "\n" character in Java represents the "Enter" key

Screen Output Source code Program output Hello World System.out.println("Hello"); System.out.println("World"); \n \n Adds line break at the end of the line System.out.print("Hello\n"); System.out.print("World\n"); but, WE have added line breaks in our output Does NOT add line break at the end of the line Note: The "\n" character in Java represents the "Enter" key

Screen Output Source code Program output Hello \n World System.out.println("Hello"); System.out.println("World"); \n \n System.out.print("Hello\n"); System.out.print("World\n"); System.out.print("Hello\n"); System.out.println("World"); System.out.println("Hello\nWorld");

Screen Output Source code Program output System.out.print("Hello"); System.out.println("World"); HelloWorld \n Does NOT add line break at the end of the line System.out.print("Hello"); Does NOT add line break at the end of the line Note: Number of lines of source code have no relationship to the number of lines of output!

Screen Output Source code Program output Add space here System.out.print("Hello"); System.out.println("World"); HelloWorld \n Add space here System.out.print("Hello "); System.out.println("World"); Hello World \n System.out.print("Hello"); System.out.println(" World"); or add space here Note: Number of lines of source code have no relationship to the number of lines of output!

Variables Memory locations that a program can use to store data Variables must be declared before they can be used – variable declaration To declare a variable, the programmer must: Choose a name to represent the memory location – variable name State the type of data that will be stored in the variable – data type After declaration, to use the variable, you only need to type it's name – not it's data type You cannot re-declare a variable, or change it's data type later

Variable Declaration Examples data type variable name int n1; int n1; ... double n1; int n1; ... int n1; ... System.out.print(int n1); Variable Redeclaration States that n1 should hold integers, but later states it should hold something else Variable Redeclaration States that n1 should hold integers, but later tries to declare n1 again Variable Redeclaration Just use the variable name; no need to try to specify it's data type again

Assignment Statements Assignment Operator Assignment statements use the assignment operator to store a value into a variable It works by taking the "stuff" on the right side of the operator, and places it into the variable on the left side of the operator ; Variable Stuff to store in variable Note: A single variable name ALWAYS appears on the left side of an assignment operator

What Can You Store? x 1 1 x 1 3 y x 3 3 LITERALS int x; LITERALS (simple, unchanging values) x 1 1 x = 1; CALCULATED ANSWERS x calculate: 1+2 answer is 3 1 3 x = 1 + 2; VALUE FROM ANOTHER VARIABLE y x int y; y = x; 3 3 retrieve value from x store into y

What Can You Store? x 3 4 x 4 39 retrieve value of x value of x is 3 int x = 3; Scanner kbd = new Scanner(System.in); CALCULATIONS INVOLVING VARIABLES retrieve value of x value of x is 3 calculate 3 + 1 answer is 4 store 4 back into x x 3 4 x = x + 1; VALUES RETRIEVED FROM A METHOD retrieve value from keyboard assume user enters 39 x 4 39 x = kbd.nextInt(); Note: Assume the keyboard has been properly declared and initialized in all future examples!

What Can't You Do? Use an undeclared variable anywhere in your program int x; Use an undeclared variable anywhere in your program z = 1; Error: z cannot be resolved to a variable Declare all variables before attempting to use them int z; z = 1; Use an uninitialized variable int z; x = z + 2; Error: The local variable z may not have been initialized A variable must have a value before you attempt to READ it's value int z = 3; x = z + 2;

Printing Variable Values Source code Program output int x = 5; System.out.println("x"); x System.out.println(x); 5 System.out.print("x = "); System.out.println(x); x = 5 System.out.println("x = " + x); x = 5 Note: Literal data inside double quotes (") is called a String

print/println ONLY prints Strings expression HOW TO "Calculate" int x = 5; System.out.println("x = " + x); retrieve the value of x System.out.println("x = " + 5); convert int 5 to a String System.out.println("x = " + "5"); Strings must be concatenated System.out.println("x = 5"); print String to the screen: x = 5 Note: Concatenate means to join together

"+" for Addition int int add System.out.println(1 + 2); int int add System.out.println(1 + 2); calculate 1 + 2 System.out.println(3); int 3 must be converted to String System.out.println("3"); Print String to the screen: 3 Note: + performs addition if there is a number on both sides of the operator

"+" for Concatenation String String concatenation System.out.println("1" + "2"); String String concatenation System.out.println("1" + "2"); concatenate the Strings System.out.println("12"); Print String to the screen: 12 Note: + performs concatenation if there is a String involved

"+" With Mixed Types System.out.println(1 + 2 + " is the answer"); Prints: 3 is the answer System.out.println(1 + 2 + " is the answer"); + is applied left to right System.out.println((1 + 2) + " is the answer"); add 1 + 2 System.out.println(3 + " is the answer"); convert int 3 to String System.out.println("3" + " is the answer"); concatenate the Strings System.out.println("3 is the answer "); Print String to the screen: 3 is the answer Note: + performs addition ONLY if there is a number on both sides of the operator

What Does This Print? System.out.println("The answer is " + 1 + 2); Prints: The answer is 12 System.out.println("The answer is " + 1 + 2); + is applied left to right System.out.println(("The answer is " + 1) + 2); convert int 1 to String System.out.println(("The answer is " + "1") + 2); concatenate the Strings System.out.println("The answer is 1" + 2); convert int 2 to String System.out.println("The answer is 1" + "2"); concatenate the Strings System.out.println("The answer is 12"); Print String to the screen: The answer is 12

Variable Names Note: Variable names SHOULD start with a lowercase letter Programmers are free to choose whatever names they like for their variables The only rules for variable naming are as follows: NO KEYWORDS Variable names cannot be the same as a Java defined keyword int class; int cLass; START WITH A LETTER OR UNDERSCORE Variable names must start with an alphabetic letter or the underscore (_) int 3pay; int pay3; USE ONLY LETTERS, NUMBERS, OR UNDERSCORES After the 1st character, variable names can only contain letters, numbers and underscore (_) characters int pay rate; int pay_rate; int payRate;

Primitive Data Types byte short int long float double char boolean CHARACTERS REAL WHOLE TRUE / FALSE NUMERIC TYPES Note: Primitives hold a single value / Objects CAN hold multiple values

Whole Number Data Types Space Required For Storage Range * int 4 bytes - 2,147,483,648 to +2,147,483,647 short 2 bytes - 32,768 to +32,767 byte 1 byte - 128 to +127 - 9,223,372,036,854,775,808 to long 8 bytes +9,223,372,036,854,775,808 Note: Whole numbers are stored in 2's compliment notation * int is the default whole number data type

Real Number Data Types ± 1.79769313486231570 x 10 to * double 8 bytes Space Required For Storage Range ± 1.79769313486231570 x 10 +308 to * double 8 bytes ± 4.94065645841246544 x 10 - 324 ± 3.40282347 x 10 +38 to float 4 bytes ± 1.40239846 x 10 - 45 Note: These variable values are stored in floating point notation * double is the default real number data type

Other Data Types char 2 bytes Unicode values from 0 to 65,535 boolean Space Required For Storage Range char 2 bytes Unicode values from 0 to 65,535 boolean 1 byte The values: false or true

Data Type Literals Single character (char) literals are surrounded by single quotes ( ' ) Multiple character ( String ) literals are surrounded by double quotes ( " ) Numbers are not surrounded by any special characters char letter = 'A'; char letter = "A"; String name1 = 'Andy'; String name2 = 'A'; String name = "Andy"; int num1 = "39"; int num2 = '39'; int num = 39;

Test Your Knowledge Source code Program output int x = 5; System.out.println( x ); 5 System.out.println( "x" ); x System.out.println( 'x' ); x x = 1 + 2; System.out.println( x ); 3 x = "1 + 2"; Type mismatch: cannot convert from String to int System.out.println( '1' + '2' ); 99

Why Not Concatenation? char 2 bytes Unicode values from 0 to 65,535 System.out.println( '1' + '2' ); 99 char char Note: + performs concatenation if there is a String involved char 2 bytes Unicode values from 0 to 65,535 Note: char values are actually stored in memory as numbers (a Unicode value)

Type Compatibilities Is 1 equivalent to "1" ? Is 1 equivalent to '1' ? Strings are objects ints are primitives int String Although, both are primitives, and both are numbers, the character '1' is not stored in memory as the number 1 Is 1 equivalent to '1' ? int char The Unicode value for the character '1' is the number 49 Is 49 equivalent to '1' ?

asciitable.com The ASCII table is a smaller subset of the larger Unicode table (and easier to read for our purposes)

'1' is equivalent to the number 49 System.out.println( '1' + '2' ); substitute values System.out.println( 49 + 50 ); add 49 + 50 System.out.println( 99 ); convert to String System.out.println( "99" ); Prints: 99

We have already established that '1' + '2' equals the number 99 System.out.println( '1' + '2' ); Prints: 99 char ch = '1' + '2'; System.out.println( ch ); Prints: c We have already established that '1' + '2' equals the number 99 The number 99 is equivalent to the character 'c'

char ch = '1' + '2'; System.out.println( ch ); int num = '1' + '2'; System.out.println( num ); Prints: c Prints: 99 ch and num are both holding the same value in memory (which is 99) BUT… character, you expect to see a character If you ask Java to print a integer, you expect to see an integer If you ask Java to print an

Thinking Outside the Box! Example: Print the string "ABCD" to the screen Obvious answer Not so obvious answer System.out.println( "ABCD" ); char ch = 'A'; System.out.print( ch ); ch = ch + 1; System.out.print( ch ); ch = ch + 1; System.out.print( ch ); ch = ch + 1; System.out.println( ch );

char ch = 'A'; System.out.print( ch ); ch = ch + 1; System.out.println( ch ); store 65 into ch print 'A' ch = 65 + 1; print 'B' ch = 66 + 1; print 'C' ch = 67 + 1; print 'D'; ch = 65 ch = 66 ch = 67 ch = 68 Prints: ABCD \n

Review of Data Types Is 5 equivalent to 5.0 ? int 5 Primitive: 2's compliment 8.6 double Primitive: Floating point 'x' Primitive: Unicode char String "x" Object (containing multiple Unicode chars) Is 5 equivalent to 5.0 ? 2's compliment Floating point

Automatic Type Conversion Java will automatically convert from a small data type to a larger data type byte short int long float double Automatic Conversion No Automatic Conversion

Automatic Type Conversion double x; x = 3.2867; double x; x = 5.0; double x; x = 5; Auto type convert: double = int Type match: double = double Type match: double = double int 5 double 5.0 x = 5.0;

When Auto Type Conversion Doesn't Work int y; y = 5; int y; y = 5.0; Type match: int = int Type Mismatch: int = double int 5 double 5.0 Note: Auto conversion does not work when going from a large type to a smaller type

(desiredType) valueToConvert Type Casting Type casting allows us to change primitive data type into another primitive type Syntax: (desiredType) valueToConvert int y; y = (int)5.0; int 5 double 5.0

Double-to-Int Type Casting When converting from floating point data types to whole number data types, the decimal places are simply truncated – NOT rounded. int y; y = 5.1; int y; y = (int)5.1; int y; y = (int)5.9; Type Mismatch: int = double Type match: int = ( int  double ) Type match: int = ( int  double ) y y 5 5

Type Compatibilities Source code Program output int x = 'B'; char y = 65; System.out.println( x ); 66 System.out.println( y ); A y = 'A'; System.out.println( y ); A System.out.println( (int)y ); 65 System.out.println( (char)x ); B

Java Math Operators + - * / % num + num a * b a . b + - * / % add subtract multiply divide modulus num + num returns the remainder from a division a * b concatenate a . b String + num num + String String + String a ( b )