Using Data Within A Program - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

Using Data Within A Program

Description:

Using Data Within A Program. Chapter 2. Dr. James Jiang ... System.out.println(459); Variables named memory locations. System.out. ... quotation marks ... – PowerPoint PPT presentation

Number of Views:37
Avg rating:3.0/5.0
Slides: 16
Provided by: Staf205
Category:

less

Transcript and Presenter's Notes

Title: Using Data Within A Program


1
Using Data Within A Program
  • Chapter 2
  • Dr. James Jiang

Data Within a Program
2
Variables and Constants
  • Literal constant
  • System.out.println(459)
  • Variables ? named memory locations
  • System.out.println(ovenTemperature)
  • Primitive data types
  • boolean float
  • byte int
  • char long
  • double short

3
Variable Declaration
  • Identify the type of data
  • Variable name begin with lowercase letter to
    distinguish variable from class
  • Initial assigned value (optional)
  • Semi-colon

4
Variable Declarations
  • int myAge 25
  • int yourAge 20
  • OR
  • int myAge 25, yourAge 20
  • int myAge, yourAge
  • double mySalary, yourSalary

5
int
  • Integers or whole numbers
  • -2,147,483,648 to 2,147,483,647
  • byte, short, long are variations of the integer
    type
  • When assigning values, dont type commas (only
    numbers and/or signs)

6
Printing Compound Phrases
  • System.out.print(The int is )
  • System.out.println(oneInt)
  • OR
  • System.out.println(The int is oneInt)

7
Arithmetic Operators
  • - /
  • Use modulus () only with integers
  • Use , -, , / with floating-point data and
    integers
  • Operator precedence - order of evaluation of
    mathematical expression

8
Boolean Data Type
  • Values true, false
  • boolean isItPayDay false
  • boolean areYouBroke true
  • Use is or are as part of variable name for
    identification purposes

9
Comparison Operators
  • lt Less than
  • gt Greater than
  • Equal to
  • lt Less than or equal to
  • gt Greater than or equal to
  • ! Not equal to

boolean overtime (hours gt 40) boolean
highTaxBracket (income gt 100000)
10
Floating-Point Data Types
  • Contain decimal positions
  • float - 7 significant digits of accuracy
  • double - 15 significant digits of accuracy
  • double is the default
  • To store as float
  • float pocketChange 4.87F

11
Numeric Type Conversion
  • Arithmetic operations with operands of unlike
    types
  • Java automatically (implicitly) converts operands
    to unifying type
  • double
  • float
  • long
  • int
  • short
  • byte

12
Numeric Type Conversion
  • Example
  • int hoursWorked 37
  • double payRate 6.73
  • grossPay hoursWorked payRate
  • grossPay must be a double

13
Overriding the Unifying Type
  • Use type casting
  • Place desired result type in parentheses
  • double bankBalance 189.66
  • float weeklyBudget (float) bankBalance / 4

14
char Data Type
  • Holds any single character
  • Place within single quotation marks
  • Store a string of characters using the String
    data structure
  • To store nonprinting characters using an escape
    sequence
  • char aBackspaceChar \b
  • char aLinefeedChar \n
  • Unicode - 16-bit character coding scheme

15
Exercise 2-13 (p.52)
  • //Name James Jiang
  • public class Interest
  • private double principle 1000, rate 0.05
  • private double futureAmount
  • private int month 6
  • public static void main (String args)
  • futureAmount principle (1 rate
    month / 12)
  • System.out.println (The future amount
    is futureAmount)
Write a Comment
User Comments (0)
About PowerShow.com