Variables in Java, Part 2
Download
Report
Transcript Variables in Java, Part 2
Variables in Java Part 2
Recall the “int” Data Types
When you divide one integer by another –
you always get an integer as an answer
The answer always rounds DOWN (it ignores
the remainder)
int x=27;
int y=7;
int answer=x/y;
The answer is
3 and 6/7
- but it is
calculated as 3
ICS-3M1 - Mr. Martens - Variables Part 2
Sometimes You Want the Remainder
On the last slide, you saw that 27/7 = 3-R6
There are 2 parts to the answer the “3” and
the remainder of “6”
Most languages have the ability to extract
only the remainder part – this is called the
“modulus” – and in Java the “%” is used.
So ... 27/7=3 ... and 27%7=6
ICS-3M1 - Mr. Martens - Variables Part 2
More Modulus Examples
x
y
x/y
x%y
x * (x % y)
10
7
1
3
30
10
3
3
1
10
6
12
0
6
36
21
4
5
1
21
65
1
65
0
0
84
8
10
4
336
33
12
2
9
297
ICS-3M1 - Mr. Martens - Variables Part 2
Using Constants in Java
A constant is a value that never changes
during the life of a program (opposite of
variable)
In Java, we use the keyword “final” in front of
a declaration to change it from a variable into
a constant:
final double PI = 3.14;
final double PST = 0.08;
ICS-3M1 - Mr. Martens - Variables Part 2
A Constant Example
It is also a
customary
practice to
CAPITALIZE
constants so
that they
stand out
from the
variables
ICS-3M1 - Mr. Martens - Variables Part 2
Formatting Numbers
To format numbers you have to “import” a
part of Java that deals with formatting
To get this one
going, we need
some numbers to
format
ICS-3M1 - Mr. Martens - Variables Part 2
Number Format, Continued
Next, we will declare 3 NumberFormat
“objects”, each with a slightly different job
The 3 objects have been named according to
the kind of format they will perform
This isn’t mandatory, but it is just good
programming practice.
ICS-3M1 - Mr. Martens - Variables Part 2
The Rest of the Program
Each NumberFormat object has a “format”
method that actually does the formatting:
Here is the output of
the program
ICS-3M1 - Mr. Martens - Variables Part 2
Here is the Entire Program
Import the NumberFormat class
Declare some variables with numbers
Create 3
NumberFormat
objects
Use the format method to format
ICS-3M1 - Mr. Martens - Variables Part 2
The 3 Types of Programming Errors
Syntax error. This type of error is detected
when you try to compile your program.
It is a typing error made by the programmer:
ICS-3M1 - Mr. Martens - Variables Part 2
The 3 Types of Programming Errors
Logic Error.
These are harder
to catch.
There is nothing
wrong with the
way you TYPED
your code so it is
not detected by
the compiler.
But, when you
run the program,
you seem to get
unexpected
results
ICS-3M1 - Mr. Martens - Variables Part 2
The 3 Types of Programming Errors
Runtime Error. The code is typed correctly,
but then something happens during the
running of the program that causes it to crash
Nothing seems wrong
with this program
when you run it ..
ICS-3M1 - Mr. Martens - Variables Part 2
The 3 Types of Programming Errors
But, we could cause the program to crash
while its running if we do some ‘funny stuff’:
Java doesn’t like it when we try to divide by 0
ICS-3M1 - Mr. Martens - Variables Part 2
Runtime Errors are called “Exceptions”
Like everything else in Java, Exceptions are
also grouped into different “objects”
Java contains a whole collection of the
various types of errors / exceptions that can
occur during the running of a program.
When a runtime error occurs, Java will stop
the program and report to you what exception
has happened
ICS-3M1 - Mr. Martens - Variables Part 2
Case Study – The Birthday Puzzle
Write down the month you were born in (as a
number) i.e. Jan=1, Feb = 2, etc
Multiply that number by 5
Add another 6 to your total
Multiply the new number by 4
Now add another 9 (almost done)
Next, multiply the new number by 5
Lastly add your birth DATE (3rd, 4th, 21st) to
your final total
ICS-3M1 - Mr. Martens - Variables Part 2
Now – tell me your number
Give me the number you ended up with and I
can tell when your birthday is...
Who dares to challenge the great, psychic,
Mr. Martens?!?!?!?
Well??????????
ICS-3M1 - Mr. Martens - Variables Part 2
A Good Magician Never Reveals His
START
Secrets
Lucky for
you, I’m not a
good
magician.
Here is a
flowchart that
breaks down
my process
to get your
birthday
Show Rules to User
Ask for number from user
Subtract 165 from number
Birth Month=number / 100
Birth Day=number % 100
Ex –
Your
birthday
is 4/17
Tell user their birthday
STOP
ICS-3M1 - Mr. Martens - Variables Part 2
Now, Write the Java Program
Based on the flowchart on the previous slide,
write the program
Be sure to include several print statements at
the beginning to explain to the user how they
are to calculate the number they are to
“punch in” to the program
Use comments -> // within your code to
explain what the code is doing.
ICS-3M1 - Mr. Martens - Variables Part 2