Presentation is loading. Please wait.

Presentation is loading. Please wait.

Variables – the Key to Programming. ICS-3M1 - Mr. Martens - Variables Part 1 What is a Variable? A storage location that is given a “name” You can store.

Similar presentations


Presentation on theme: "Variables – the Key to Programming. ICS-3M1 - Mr. Martens - Variables Part 1 What is a Variable? A storage location that is given a “name” You can store."— Presentation transcript:

1 Variables – the Key to Programming

2 ICS-3M1 - Mr. Martens - Variables Part 1 What is a Variable? A storage location that is given a “name” You can store different things in it while the program is running The contents may “vary”, or change, while the program is running (hence the name – variable) age

3 ICS-3M1 - Mr. Martens - Variables Part 1 Variables in Java You can setup (or declare) different types of variables in Java, depending on the kind of information you want to store. Here are 4 main types of variables in Java TypeSizeUsed For int4 bytes+ or – integers double8 bytes+ or – values with decimals char2 bytesa single character / letter booleantrue or false

4 ICS-3M1 - Mr. Martens - Variables Part 1 Variables also called: “Primitive Data Types” These variables types – int, double, char, and boolean – along with 4 other types make up Java’s Primitive Data Types Recall Java is made up entirely of separate classes, including ones you write yourself The only part of Java that is NOT classes are the primitive data types They are called “primitive”, because they can only do 1 thing: store data

5 ICS-3M1 - Mr. Martens - Variables Part 1 Using Variables in a Program Here is a simple console application that determines the area of a rectangle Recall that for now, all code for a console application goes inside the main method:

6 ICS-3M1 - Mr. Martens - Variables Part 1 Running The Program After you save it, running it may look something like this: c.readInt() pauses the program to allow the user to enter a number. There is also a readDouble()

7 ICS-3M1 - Mr. Martens - Variables Part 1 Fun With Variables Often times, instead of REPLACING the value stored in a variable, a programmer wants to INCREASE or DECREASE the value that is already stored there For example, a game that keeps score – you want the program to increase a persons score when they earn points, or take away points if they do something wrong

8 ICS-3M1 - Mr. Martens - Variables Part 1 Fun With Variables The PRO way to increase or decrease the value of a variable is to use shortcuts: xInstead ofUseTo get 10x=x+5x+=515 20x=x-12x-=128 7x=x+1x++, or x+=18 6x=x-1x--, or x-=15 14x=x*1.13x*= ** **assume x is a double, not int

9 ICS-3M1 - Mr. Martens - Variables Part 1 Mixing it up with int and double This can be a problem. Since “int” can only hold whole numbers you have to be careful: Even though the answer is really 2.6, z can’t handle the “.6”, so it just becomes 2.

10 ICS-3M1 - Mr. Martens - Variables Part 1 Mixing it up with int and double But things can get even worse. This program won’t even run – you get an error: When you multiply an int with a double you get an answer that is a double “answer” does not have enough precision to store the real answer of 25.9

11 ICS-3M1 - Mr. Martens - Variables Part 1 You Must “cast” the variable If you put (int) in front of “d” you are converting it (called casting) to an integer This “dumbs it down” to a level that “answer” can understand Note the answer you get is now an integer that will simply ignore the.9 decimal part of the answer

12 ICS-3M1 - Mr. Martens - Variables Part 1 Casting the Other Way You may want to upgrade an “int” to a double sometimes too – when you need MORE precision to make a double calculation: In this example, “d” CAN handle a decimal answer, but the best x and y can do is send back an integer as an answer, so “d” has no choice but to make the answer 2.0

13 ICS-3M1 - Mr. Martens - Variables Part 1 Casting the Other Way If we CONVERT x and y to “double” values, then x/y WILL produce a decimal:

14 ICS-3M1 - Mr. Martens - Variables Part 1 Your Turn (Part 1): Temperature Converter Create a console application that converts a temperature from Fahrenheit to Celsius. First, we will PLAN the program using a flowchart Recall the new flowchart symbol -> the input/output symbol Remember, whenever you ASK for information from a user, or TELL the user something -> you flowchart a step like that using input/output

15 ICS-3M1 - Mr. Martens - Variables Part 1 Temperature Converter Flowchart Start Ask user for Fahrenheit Calculate Celsius C = 5.0/9.0*(F-32) Display Celsius answer for user Stop

16 ICS-3M1 - Mr. Martens - Variables Part 1 Your Turn (Part 2) Use a flowchart to map out the steps in a program that calculates the average on a report card, given the four marks earned in the different subjects. When you are finished, create the application in Java. Make sure the average is displayed with decimals.


Download ppt "Variables – the Key to Programming. ICS-3M1 - Mr. Martens - Variables Part 1 What is a Variable? A storage location that is given a “name” You can store."

Similar presentations


Ads by Google