Lecture 2 Fundamental Data Types
description
Transcript of Lecture 2 Fundamental Data Types
![Page 1: Lecture 2 Fundamental Data Types](https://reader034.fdocuments.us/reader034/viewer/2022051020/5681668f550346895dda63bd/html5/thumbnails/1.jpg)
Lecture 2
Fundamental Data Types
![Page 2: Lecture 2 Fundamental Data Types](https://reader034.fdocuments.us/reader034/viewer/2022051020/5681668f550346895dda63bd/html5/thumbnails/2.jpg)
Variable Declaration
![Page 3: Lecture 2 Fundamental Data Types](https://reader034.fdocuments.us/reader034/viewer/2022051020/5681668f550346895dda63bd/html5/thumbnails/3.jpg)
Rules for Identifiers
![Page 4: Lecture 2 Fundamental Data Types](https://reader034.fdocuments.us/reader034/viewer/2022051020/5681668f550346895dda63bd/html5/thumbnails/4.jpg)
Numeric Types
![Page 5: Lecture 2 Fundamental Data Types](https://reader034.fdocuments.us/reader034/viewer/2022051020/5681668f550346895dda63bd/html5/thumbnails/5.jpg)
All Numeric Types have Finite Ranges
0 1 1 1 1 1 1 00 0 0 0 0 0 0 1
0 1 1 1 1 1 1 1
0 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1
value is 128but the leading 1 indicates negative value
value is 127
![Page 6: Lecture 2 Fundamental Data Types](https://reader034.fdocuments.us/reader034/viewer/2022051020/5681668f550346895dda63bd/html5/thumbnails/6.jpg)
Floating Point Values are Approximate
![Page 7: Lecture 2 Fundamental Data Types](https://reader034.fdocuments.us/reader034/viewer/2022051020/5681668f550346895dda63bd/html5/thumbnails/7.jpg)
Numeric Operators
![Page 8: Lecture 2 Fundamental Data Types](https://reader034.fdocuments.us/reader034/viewer/2022051020/5681668f550346895dda63bd/html5/thumbnails/8.jpg)
Integer Division and Remainder
![Page 9: Lecture 2 Fundamental Data Types](https://reader034.fdocuments.us/reader034/viewer/2022051020/5681668f550346895dda63bd/html5/thumbnails/9.jpg)
Increment and Decrement Operators
Notice: In this course, we will NOT be using increment and decrement operators in regular assignment statements. (More on this later.)
![Page 10: Lecture 2 Fundamental Data Types](https://reader034.fdocuments.us/reader034/viewer/2022051020/5681668f550346895dda63bd/html5/thumbnails/10.jpg)
Assignment Statements
![Page 11: Lecture 2 Fundamental Data Types](https://reader034.fdocuments.us/reader034/viewer/2022051020/5681668f550346895dda63bd/html5/thumbnails/11.jpg)
When is "=" not Equal to "Equals"?In Java (and most other programming languages) the symbol = means assignment.
X = X + 1 "X is assigned the value X + 1
X = X + 1ALU Arithmetic Logic
UnitADD
LOAD
STORE
![Page 12: Lecture 2 Fundamental Data Types](https://reader034.fdocuments.us/reader034/viewer/2022051020/5681668f550346895dda63bd/html5/thumbnails/12.jpg)
Augmented Assignment Operators
![Page 13: Lecture 2 Fundamental Data Types](https://reader034.fdocuments.us/reader034/viewer/2022051020/5681668f550346895dda63bd/html5/thumbnails/13.jpg)
Declaring Constants
![Page 14: Lecture 2 Fundamental Data Types](https://reader034.fdocuments.us/reader034/viewer/2022051020/5681668f550346895dda63bd/html5/thumbnails/14.jpg)
Mathematical Expressions
![Page 15: Lecture 2 Fundamental Data Types](https://reader034.fdocuments.us/reader034/viewer/2022051020/5681668f550346895dda63bd/html5/thumbnails/15.jpg)
Ingredients
1 stick butter 1/2 cup sugar 1 egg 1 teaspoon vanilla extract 1/2 teaspoon salt 1 3/4 cups all-purpose flour
Directions
Mix the butter and sugar until light and fluffy. Beat in the egg, then vanilla and salt. Mix slowly while gradually adding the flour. Do not overmix. Shape into a 1-inch-thick disk, refrigerate for at least 2 hours. Heat oven to 350° F. Roll the dough ¼ inch thick, and cut the dough into shapes. Place 1 inch apart on prepared baking sheets, sprinkle with sugar. Bake until the edges just begin to brown, 12 to 15 minutes. Transfer to cooling racks.
Sugar Cookie Recipe
![Page 16: Lecture 2 Fundamental Data Types](https://reader034.fdocuments.us/reader034/viewer/2022051020/5681668f550346895dda63bd/html5/thumbnails/16.jpg)
Parameters
pi = 3.1415926 radius area
Actions
Read in the radius. Compute the Area using the formula:
Display Area.
Circle Area Algorithm
Computer Algorithms can be simpler than a recipe for sugar cookies. ;-)
![Page 17: Lecture 2 Fundamental Data Types](https://reader034.fdocuments.us/reader034/viewer/2022051020/5681668f550346895dda63bd/html5/thumbnails/17.jpg)
from Algorithm to Computer Program
![Page 18: Lecture 2 Fundamental Data Types](https://reader034.fdocuments.us/reader034/viewer/2022051020/5681668f550346895dda63bd/html5/thumbnails/18.jpg)
Completed Program
comments are ignoredby the computer
![Page 19: Lecture 2 Fundamental Data Types](https://reader034.fdocuments.us/reader034/viewer/2022051020/5681668f550346895dda63bd/html5/thumbnails/19.jpg)
Completed Program
![Page 20: Lecture 2 Fundamental Data Types](https://reader034.fdocuments.us/reader034/viewer/2022051020/5681668f550346895dda63bd/html5/thumbnails/20.jpg)
Getting Input from the UserWe can use the Scanner software package provided as part of java.util to get information typed on the keyboard. Some of the methods provided in Scanner are:
![Page 21: Lecture 2 Fundamental Data Types](https://reader034.fdocuments.us/reader034/viewer/2022051020/5681668f550346895dda63bd/html5/thumbnails/21.jpg)
User Input
![Page 22: Lecture 2 Fundamental Data Types](https://reader034.fdocuments.us/reader034/viewer/2022051020/5681668f550346895dda63bd/html5/thumbnails/22.jpg)
Interactive Version of Circle Area Calculator
![Page 23: Lecture 2 Fundamental Data Types](https://reader034.fdocuments.us/reader034/viewer/2022051020/5681668f550346895dda63bd/html5/thumbnails/23.jpg)
Convert Fahrenheit to Celsius
![Page 24: Lecture 2 Fundamental Data Types](https://reader034.fdocuments.us/reader034/viewer/2022051020/5681668f550346895dda63bd/html5/thumbnails/24.jpg)
Convert Seconds to Minutes and Seconds
Get total seconds from user.
Calculate number of whole minutes.
Calculate number of remaining seconds.
Display results.
![Page 25: Lecture 2 Fundamental Data Types](https://reader034.fdocuments.us/reader034/viewer/2022051020/5681668f550346895dda63bd/html5/thumbnails/25.jpg)
Convert Seconds to Minutes and Seconds
![Page 26: Lecture 2 Fundamental Data Types](https://reader034.fdocuments.us/reader034/viewer/2022051020/5681668f550346895dda63bd/html5/thumbnails/26.jpg)
Numeric Type Conversions
Java performs automatic type conversion in mathematical expressions. For example, when an integer and a floating-point value are multiplied, the integer is promoted to a floating-point value.
We can override the default type conversions by specifying the type of each parameter.
![Page 27: Lecture 2 Fundamental Data Types](https://reader034.fdocuments.us/reader034/viewer/2022051020/5681668f550346895dda63bd/html5/thumbnails/27.jpg)
9
2+i3𝜋
25
Oh great! Here come yourfriends, the odd couple.
For starters, they're a coupleof squares.
Why can't you keep it real?
I can't imagine whyyou don't like them.
As usual, you're being irrational.
Why do you insist on running on forever?
Conflicting Data Types
![Page 28: Lecture 2 Fundamental Data Types](https://reader034.fdocuments.us/reader034/viewer/2022051020/5681668f550346895dda63bd/html5/thumbnails/28.jpg)
ShowCurrentTime
![Page 29: Lecture 2 Fundamental Data Types](https://reader034.fdocuments.us/reader034/viewer/2022051020/5681668f550346895dda63bd/html5/thumbnails/29.jpg)
Using Type Casting
So why would we want to multiply and then divide by 100?
197.55 x 0.06 = 11.853
![Page 30: Lecture 2 Fundamental Data Types](https://reader034.fdocuments.us/reader034/viewer/2022051020/5681668f550346895dda63bd/html5/thumbnails/30.jpg)
A Sample Problem
![Page 31: Lecture 2 Fundamental Data Types](https://reader034.fdocuments.us/reader034/viewer/2022051020/5681668f550346895dda63bd/html5/thumbnails/31.jpg)
An Implemention
![Page 32: Lecture 2 Fundamental Data Types](https://reader034.fdocuments.us/reader034/viewer/2022051020/5681668f550346895dda63bd/html5/thumbnails/32.jpg)
Character Data Types and Operations
![Page 33: Lecture 2 Fundamental Data Types](https://reader034.fdocuments.us/reader034/viewer/2022051020/5681668f550346895dda63bd/html5/thumbnails/33.jpg)
Unicode and ASCII Code
no parent
![Page 34: Lecture 2 Fundamental Data Types](https://reader034.fdocuments.us/reader034/viewer/2022051020/5681668f550346895dda63bd/html5/thumbnails/34.jpg)
http://www.unicode.org/charts/
Samples of Unicode
![Page 35: Lecture 2 Fundamental Data Types](https://reader034.fdocuments.us/reader034/viewer/2022051020/5681668f550346895dda63bd/html5/thumbnails/35.jpg)
Escape Sequences for Special Characters
![Page 36: Lecture 2 Fundamental Data Types](https://reader034.fdocuments.us/reader034/viewer/2022051020/5681668f550346895dda63bd/html5/thumbnails/36.jpg)
Format Specifiers
![Page 37: Lecture 2 Fundamental Data Types](https://reader034.fdocuments.us/reader034/viewer/2022051020/5681668f550346895dda63bd/html5/thumbnails/37.jpg)
String Operations
![Page 38: Lecture 2 Fundamental Data Types](https://reader034.fdocuments.us/reader034/viewer/2022051020/5681668f550346895dda63bd/html5/thumbnails/38.jpg)
Types of Programming Errors
syntax errors - This type of error prevents the compilation of the program into an executable binary or into bytecode.
runtime errors - This type of error prevents a running program from completing its normal execution.
logic errors - With this type of error a program can complete its execution normally but the results obtained will be incorrect.
• easiest type of error to detect• relatively simple to correct
• can be data dependent• don't always occur (consider the divide-by-zero error)
• most difficult to detect (especially when they are intermittant)• amenable to unit testing and other SW engineering methods• can never be completely eliminated in large programs
![Page 39: Lecture 2 Fundamental Data Types](https://reader034.fdocuments.us/reader034/viewer/2022051020/5681668f550346895dda63bd/html5/thumbnails/39.jpg)
using Input and Message Dialogs
![Page 40: Lecture 2 Fundamental Data Types](https://reader034.fdocuments.us/reader034/viewer/2022051020/5681668f550346895dda63bd/html5/thumbnails/40.jpg)
The Math Library of Methods
![Page 41: Lecture 2 Fundamental Data Types](https://reader034.fdocuments.us/reader034/viewer/2022051020/5681668f550346895dda63bd/html5/thumbnails/41.jpg)
Algorithms are like RecipesThe Input-Process-Output Design PatternComments help make code understandibleAll Numeric Data Types have finite rangesValues can be converted from one data type to anotherFloats and Doubles approximate Real Numbersjava.util Scanner methods support user inputUnderstanding Assignment OperatorsIncrement and Decrement OperatorsIntroducing Processing.orgUnicode and ASCII CodeEscape SequencesProgramming ErrorsInput and Message Dialogs
Chapter 2 Summary