1 / 22

Data

Data. Tonga Institute of Higher Education. Variables. Programs need to remember values. Example: A program that keeps track of sales needs to remember data: A loaf of bread was sold to Sione Latu on 14/02/04 for T$1.00. Customer Name: Sione Latu Date Sold: 02/14/04

sfunk
Download Presentation

Data

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. Data Tonga Institute of Higher Education

  2. Variables • Programs need to remember values. • Example: A program that keeps track of sales needs to remember data: • A loaf of bread was sold to Sione Latu on 14/02/04 for T$1.00. • Customer Name: Sione Latu • Date Sold: 02/14/04 • Item Sold: Loaf of Bread • Item Cost: T$1.00 • Variable – Place in the computer’s memory where information is stored.

  3. Value Types vs. Reference Types • A variable stores a value or object. • Value Types only store values • int only stores: • 1 • 2 • 5 • boolean only stores: • True • False • Reference Types only store references to an object • Object - A self-contained entity that contains data and procedures to manipulate the data. • A reference is a location in memory • The location is the starting point of an object

  4. Primitive Data Types • Primitives - The basic types supported by the programming language. • Use lowercase for primitive data types in code

  5. Primitive Data Type Selection Practice • How to choose the right primitive variable type. • Byte, short, int and long store whole numbers. • Float and double store fractions. Double is more accurate, so if accuracy is important, use double. • If you pick something that is too big, you’re wasting memory space. Wasting memory space slows things down. • If you pick something that is too small, then you’re going to crash. • If you need a character, use char. • If you need a true/false value, use boolean. • What is a good data type for: • Someone’s age? • A customer’s identification number for a video rental store in Nuku’alofa? • A very large number with decimals? • The price of an item?

  6. Using Primitive Variables • 2 Steps to using variables • Declare the variable • Initialize the variable

  7. Declaring Primitive Variables • Declare the variable – Tell the computer to reserve a space in memory for the variable. • You need to tell the computer 2 things: • Name of the variable • Type of the variable (What kind of variable you have) • Primitive types • byte • short • int • long • float • double • char • boolean Name Type

  8. Declaring Primitive Variables Guidelines • Use a name that is easy to remember. • Do not use x, y, z • Variable names must start with a letter, underscore or dollar sign. • Begin variables with a lowercase character. Then, use a capital letter for each word. • Examples • firstName • customerID

  9. Initializing Primitive Variables • Initialize the variable – Assign an initial value to a variable. • Char values must be enclosed in single quotes. • String values must be enclosed in double quotes. • Boolean value should be True or False Initial Value

  10. Declaring and Initializing Primitive Variables in 1 line • You can declare and initialize a variable in 1 line.

  11. Demonstration Declaring and Initializing Variables

  12. Converting Data TypesSmaller to Larger • It is possible to automatically move a value from a smaller data type to larger data type • Example Maximum Short Value Maximum Long Value Is 9.2 x 1015

  13. Converting Data TypesLarger to Smaller • If a value from a larger data type is moved into a smaller data type, the value may be too big for the new data type • This will cause an error • Using this: • Results in: • Do not do this! Maximum Short Value is 32767

  14. Converting Data Types • Force conversion of data types by casting it. • To cast, use this format: (<Desired data type>)<Variable name> • If the Cast function isn’t able to convert the value, you may not get the answer you expect. int is bigger than a byte Forcing a cast from a larger data type to a smaller data type is not recommended! Without this, we would get an error But the final value Is -24, which is strange!

  15. Demonstration Converting Data Types

  16. Arithmetic Operators • Declare and Initialize x, y and z • Get values from x and y • Adds x and y together • Assigns the sum of x an y to z

  17. String Concatenation • Addition • You can add strings. Adding strings is called concatenation. • This converts all non strings into strings. Make sure you add your numbers before converting them to strings.

  18. Demonstration String Concatenation

  19. Division • Tricky because result may not be an integer. In this case the number gets cut off. NOT rounded! • Even having a result of float doesn’t work. Because Java has rules for dealing with data types. The result of a division between two integers is always an integer. So the float just added a .0 to it. • To get around this, we need to divide two floats!

  20. Demonstration Division

  21. Multiplication and Modulus • Multiplication • Use asterisk (*) instead of x • Modulus • The remainder of a division • Can be used to determine whether a number is divisible by another number • Can also determine if a number is even or odd

  22. Order of Operations • When you have a lot of operations, they are performed in a certain order. • Parentheses () • Multiplication or Division operations from left to right • Addition or Subtraction operations from left to right. • Examples: • 3 + 6 + 9 / 3 = 3 + 6 + 3 = 12 • (3 + 6 + 9) / 3 = 18 / 3 = 6

More Related