25
Chapter 3 Fundamental Data Types of Java Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E. Reingold

Chapter 3 Fundamental Data Types of Java Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas,

Embed Size (px)

Citation preview

Page 1: Chapter 3 Fundamental Data Types of Java Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas,

Chapter 3Fundamental Data Types of Java

Lecture Slides to Accompany

An Introduction to Computer Science Using Java (2nd Edition)

by

S.N. Kamin, D. Mickunas, E. Reingold

Page 2: Chapter 3 Fundamental Data Types of Java Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas,

Chapter Preview

In this chapter we will:• discuss four important data types

– integers– real numbers– strings– characters

• describe the process of developing and debugging a Java program

Page 3: Chapter 3 Fundamental Data Types of Java Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas,

Integers

• Numbers without fractional parts

3, 47, -12• Variables can be used to store integers using

an assignment statementint daysInWeek;

daysInWeek = 7;

• In general integer variables may be used any place an integer literal can be

Page 4: Chapter 3 Fundamental Data Types of Java Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas,

Reading Integers

import CSLib.*;

Inputbox in;

int i;

in = newInputBox();

in.setPrompt(“Enter an integer: “);

i = in.readInt();

Page 5: Chapter 3 Fundamental Data Types of Java Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas,

Integer Arithmetic Operations

Symbol Operation Example

+ Addition 45 + 5 = 50

- Subtraction 657 – 57 = 600

* Multiplication 7000 * 3 = 2100

/ Division 10 / 3 = 3

% Remainder 10 % 3 = 1

Page 6: Chapter 3 Fundamental Data Types of Java Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas,

Precedence Rules

1. Evaluate all subexpressions in parentheses2. Evaluate nested parentheses from the

inside out3. In the absence of parentheses or within

parenthesesa. Evaluate *, /, or % before + or –b. Evaluate sequences of *, /, and % operators

from left to rightc. Evaluate sequences of + and – operators from

left to right

Page 7: Chapter 3 Fundamental Data Types of Java Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas,

Precedence Examples

• Example 16 + 37 % 8 / 5 is the same as6 + ((37 % 8) / 5) = 6 + ( 5 / 5) = 7

• Example 26 + 37 % (8 / 5) =6 + 37 % 1 =6 + 0 = 6

Page 8: Chapter 3 Fundamental Data Types of Java Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas,

Additional Integer Operators

• Self-assignmenttemperature = temperature + 10;

• Increment cent++; equivalent to cent = cent + 1;

• Decrement cent--; equivalent to cent = cent - 1;

Page 9: Chapter 3 Fundamental Data Types of Java Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas,

Initializers

• May be used to give variables initial valuesint x = 5;int y = 6;

• Can be written more conciselyint x = 5, y = 6;

• Can use expressions on the right hand sideint x = 5, y = x + 1;

Page 10: Chapter 3 Fundamental Data Types of Java Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas,

Symbolic Constants

• Useful when you want a variable whose value never changes

• Use the modifier final in its declaration

• Examplefinal int US_Population = 278058881;

Page 11: Chapter 3 Fundamental Data Types of Java Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas,

Real Numbers

• Numbers with fractional parts

3.14159, 7.12, 9.0, 0.5e001, -16.3e+002• Declared using the type double

double pricePerPound = 3.99;

taxRate = 0.05;

shippingCost = 5.55;

• The initialization part of the declaration is optional

Page 12: Chapter 3 Fundamental Data Types of Java Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas,

Real Arithmetic Operations

Symbol Operation Example

+ Addition 4.50e01 + 5.30e00 = 5.03e01

- Subtraction 6.57e02 – 5.7oe01 = 6.00e02

* Multiplication 7e02 * 3.0e00 =

2.1e04

/ Division 9.6e01 / 2e01 =

4.8e00

Page 13: Chapter 3 Fundamental Data Types of Java Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas,

Reading Real Numbers

import CSLib.*;

Inputbox in;

double temp;

in = newInputBox();

in.setPrompt(“Enter a real number: “);

temp = in.readDouble();

Page 14: Chapter 3 Fundamental Data Types of Java Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas,
Page 15: Chapter 3 Fundamental Data Types of Java Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas,

Strings

• String is a class defined in the java.lang package

• Unlike other Java classes String has literals and a defined operation

• ExamplesString prompt = “Enter an integer:”;

String t1 = “To be “,

t2 = “or not to be”;

out.print(t1 + t2);

Out.print(“Mass is “ + x * 2.2 + “ Kg”);

Page 16: Chapter 3 Fundamental Data Types of Java Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas,

String Method Examples

OutputBox out = new OutputBox();String s1 = “Here is a test string”;

out.println(s1.indexOf(“s”)); // prints 6out.println(s1.indexOf(“x”)); // prints -1

out.println(s1.length()); // prints 22

out.println(s1.substring(8,14)); // prints ‘a test’

Page 17: Chapter 3 Fundamental Data Types of Java Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas,
Page 18: Chapter 3 Fundamental Data Types of Java Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas,

Reading Strings

import CSLib.*;

Inputbox in;

String input;

in = newInputBox();

in.setPrompt(“Enter a real number: “);

input = in.readString();

Page 19: Chapter 3 Fundamental Data Types of Java Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas,

Characters

• Any key you type on the keyboard generates a character which may or may not be displayed on the screen (e.g. nonprinting characters)

• Characters are a primitive type in Java and are not equivalent to strings

• Exampleschar vitamin = ‘’A’, chromosome = ‘’y’, middleInitial = ‘’N’;

Page 20: Chapter 3 Fundamental Data Types of Java Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas,

Important Literal Characters

‘A’, … ,‘Z’ Uppercase letters

‘a’, … ,‘z’ Lowercase letters

‘0’, … , ‘9’ Digits

‘.’,’,’,’!’,’”’,etc. Punctuation Marks

‘ ’ Blank

‘\n’ New line

‘\t’ Tab

‘\\’ Backslash

‘\’’ Single Right Quote

Page 21: Chapter 3 Fundamental Data Types of Java Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas,

Common Debugging Problems

• Misleading compiler error messages– Syntax errors indicated on one-line may actually

reflect an error made on an earlier line

• Capitalization errors– Java is case sensitive, identifier names must use

the same capitalization rules each time

• Logic Errors– Program appears to run correctly, but on closer

inspection the wrong output is displayed

Page 22: Chapter 3 Fundamental Data Types of Java Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas,

Limitations of Numeric Variables

• Unlike the integers mathematics the type int is not infinitely large and it is possible to compute a value incorrectly because the value is too large to be stored in an int variable storage location

• Unlike the real numbers in mathematics the type double is not dense, it is not always possible to test double expressions for equality and obtain a correct result due to rounding errors in representations

Page 23: Chapter 3 Fundamental Data Types of Java Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas,

Mixing Numeric Data Types

• Java will automatically convert int expressions to double values without loss of information

int i = 5;

double x = i + 10.5;• To convert double expressions to int requires a

typecasting operation and truncation will occuri = (int) (10.3 * x)

• To round-up instead of truncating add 0.5i = (int) (10.3 * x + 0.5)

Page 24: Chapter 3 Fundamental Data Types of Java Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas,

Mixed Mode Operations and Strings

• It is important to remember that “13” and 13 are not the same

• Examplesout.println(“4” + “5”) // prints 45

out.println(“4” + 5) // prints 45

out.println(4 + 5) // prints 9

Page 25: Chapter 3 Fundamental Data Types of Java Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas,

Characters as Integers

• It is legal to assign a char to an int variable

int i = ‘a’; // assigns 97 to i• It is legal to assign an int to an char variable

char c = 97; // assigns ‘a’ to c• It is possible to perform arithmetic on char

variables

char ch = ‘a’;

ch = ch + 1; // assigns ‘b’ to ch