19
Variables in Java Part 2

Variables in Java Part 2. ICS-3M1 - Mr. Martens - Variables Part 2 Recall the “int” Data Types When you divide one integer by another – you always get

Embed Size (px)

Citation preview

Page 1: Variables in Java Part 2. ICS-3M1 - Mr. Martens - Variables Part 2 Recall the “int” Data Types When you divide one integer by another – you always get

Variables in Java Part 2

Page 2: Variables in Java Part 2. ICS-3M1 - Mr. Martens - Variables Part 2 Recall the “int” Data Types When you divide one integer by another – you always get

ICS-3M1 - Mr. Martens - Variables 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

Page 3: Variables in Java Part 2. ICS-3M1 - Mr. Martens - Variables Part 2 Recall the “int” Data Types When you divide one integer by another – you always get

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

Page 4: Variables in Java Part 2. ICS-3M1 - Mr. Martens - Variables Part 2 Recall the “int” Data Types When you divide one integer by another – you always get

ICS-3M1 - Mr. Martens - Variables Part 2

More Modulus Examples

x y x / y x % y x * (x % y)

10 7

10 3

6 12

21 4

65 1

84 8

33 12

1 3 30

3 1 10

0 6 36

5 1 21

65 0 0

10 4 336

2 9 297

Page 5: Variables in Java Part 2. ICS-3M1 - Mr. Martens - Variables Part 2 Recall the “int” Data Types When you divide one integer by another – you always get

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;

Page 6: Variables in Java Part 2. ICS-3M1 - Mr. Martens - Variables Part 2 Recall the “int” Data Types When you divide one integer by another – you always get

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

Page 7: Variables in Java Part 2. ICS-3M1 - Mr. Martens - Variables Part 2 Recall the “int” Data Types When you divide one integer by another – you always get

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

Page 8: Variables in Java Part 2. ICS-3M1 - Mr. Martens - Variables Part 2 Recall the “int” Data Types When you divide one integer by another – you always get

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.

Page 9: Variables in Java Part 2. ICS-3M1 - Mr. Martens - Variables Part 2 Recall the “int” Data Types When you divide one integer by another – you always get

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

Page 10: Variables in Java Part 2. ICS-3M1 - Mr. Martens - Variables Part 2 Recall the “int” Data Types When you divide one integer by another – you always get

ICS-3M1 - Mr. Martens - Variables Part 2

Here is the Entire ProgramImport the NumberFormat class

Declare some variables with numbers

Use the format method to format

Create 3 NumberFormat objects

Page 11: Variables in Java Part 2. ICS-3M1 - Mr. Martens - Variables Part 2 Recall the “int” Data Types When you divide one integer by another – you always get

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:

Page 12: Variables in Java Part 2. ICS-3M1 - Mr. Martens - Variables Part 2 Recall the “int” Data Types When you divide one integer by another – you always get

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

Page 13: Variables in Java Part 2. ICS-3M1 - Mr. Martens - Variables Part 2 Recall the “int” Data Types When you divide one integer by another – you always get

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 ..

Page 14: Variables in Java Part 2. ICS-3M1 - Mr. Martens - Variables Part 2 Recall the “int” Data Types When you divide one integer by another – you always get

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

Page 15: Variables in Java Part 2. ICS-3M1 - Mr. Martens - Variables Part 2 Recall the “int” Data Types When you divide one integer by another – you always get

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

Page 16: Variables in Java Part 2. ICS-3M1 - Mr. Martens - Variables Part 2 Recall the “int” Data Types When you divide one integer by another – you always get

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

Page 17: Variables in Java Part 2. ICS-3M1 - Mr. Martens - Variables Part 2 Recall the “int” Data Types When you divide one integer by another – you always get

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??????????

Page 18: Variables in Java Part 2. ICS-3M1 - Mr. Martens - Variables Part 2 Recall the “int” Data Types When you divide one integer by another – you always get

ICS-3M1 - Mr. Martens - Variables Part 2

A Good Magician Never Reveals His Secrets Lucky for

you, I’m not a good magician.

Here is a flowchart that breaks down my process to get your birthday

START

Show Rules to User

Subtract 165 from number

Birth Month=number / 100

Birth Day=number % 100

Tell user their birthday

STOP

Ask for number from user

Ex – Your

birthday is 4/17

Page 19: Variables in Java Part 2. ICS-3M1 - Mr. Martens - Variables Part 2 Recall the “int” Data Types When you divide one integer by another – you always get

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.