Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Variables in Java Part 2

2 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

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

4 ICS-3M1 - Mr. Martens - Variables Part 2 More Modulus Examples xyx / yx % yx * (x % y) 107 3 612 214 651 848 3312 1 3 30 3 1 10 0 6 36 5 1 21 65 0 0 10 4 336 2 9 297

5 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;

6 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

7 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

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

9 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

10 ICS-3M1 - Mr. Martens - Variables Part 2 Here is the Entire Program Import the NumberFormat class Declare some variables with numbers Use the format method to format Create 3 NumberFormat objects

11 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:

12 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

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

14 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

15 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

16 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 (3 rd, 4 th, 21 st ) to your final total

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

18 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

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


Download ppt "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."

Similar presentations


Ads by Google