1 / 16

Variables – the Key to Programming

Variables – the Key to Programming. age. 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). 29. 47. 16.

nicolez
Download Presentation

Variables – the Key to Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Variables – the Key to Programming

  2. age 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) 29 47 16 ICS-3M1 - Mr. Martens - Variables Part 1

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

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

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

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

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

  8. Fun With Variables • The PRO way to increase or decrease the value of a variable is to use shortcuts: **assume x is a double, not int ICS-3M1 - Mr. Martens - Variables Part 1

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

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

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

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

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

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

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

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

More Related