Transcript File

Numeric Variable Basics
Ch 4 Show 1
1
Put this into BlueJ
• public static void main(String[] args) {
long = 1000000;
System.out.println(n * n);
}
This is called integer overflow! This is BAD 
2
Real Number Variables
•Real numbers have decimals. Example: 2.555
•Chop off decimal values after limit without warning
•Usually use double because of calculation loss.
Type
Accuracy
Space
Needs
4 bytes
float
7 decimals
double
15 decimals 8 bytes
Value Ranges
+3.40282347E+38
7 decimal places accuracy
+1.79769313486231570E+308
3
Integer Data Types
• Integer data: No decimal values stored
• Most common type is int
Type
Value Ranges
int
Space
Needs
4 bytes
short
long
2 bytes
8 bytes
-32768 to +32767
byte
1 byte
-128 to 127
-2,147,483,648
+2,147483,647
to
-9,223,372,036,854,775,808
To
9,223,372,036,854,775,807
4
Constants
Variables can change values, however constant variable
can not.
Example:
What to create a variable to store the HST % amount:
public final double HST_AMOUNT = 0.13;
5
Java ShortHands
sum = sum + 1;
sum ++;
sum = sum - 1;
sum --;
count = count + x;
count += x;
count = count * x;
count *= x;
6
Purse Class
Purse()
addNickels(int)
addDimes(int)
addQuarters(int)
getTotal()
final double NICKEL_VALUE = 0.05
final double DIME_VALUE =0.1
final double QUARTER_VALUE = 0.25
double totalValue
7
Task 7
public void addNickels(int count) {
}
8
Task 7
•
•
Code the Purse class that we have developed the
UML for together. Code a driver class that adds
several coins to a purse and printouts the purses
total value.
Modify your code so that now you can compute the
average of the Purse. Do this by adding
appropriate variables to your Purse class, add a
new method called getAverage(), and test your
new class by adding several coins to the purse,
then outputting its total and its average.
9
10