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:
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 47 29 16
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
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
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:
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()
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
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*=1.1315.82 ** **assume x is a double, not int
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.
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
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
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
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:
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
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
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.