1 / 25

Introduction to Computing Concepts

Introduction to Computing Concepts. Note Set 7 . Overview. Variables Data Types Basic Arithmetic Expressions Arithmetic. Memory in a computer. Computer uses a few levels of memory to hold data that it is using in processing. . RAM. 3366. 3368. 3370. 3372. 3374. 3376. Data.

fagan
Télécharger la présentation

Introduction to Computing Concepts

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. Introduction to Computing Concepts Note Set 7

  2. Overview • Variables • Data Types • Basic Arithmetic Expressions • Arithmetic

  3. Memory in a computer • Computer uses a few levels of memory to hold data that it is using in processing. RAM 3366 3368 3370 3372 3374 3376

  4. Data • Collection of raw facts or figures • Perform some processing on data in order to obtain information • Example Data: Test Grades • Example Information: Class Average, Standard Deviation, etc. • Data (and info.) usually has an intrinsic Data Type • Numerical Data (test grades)? String Data (names)? 3.1415927 Bob, Sam, Jane Computer Science 99

  5. Data in Java • Need some way to store data/info while program is running/processing • For this, we use variables • Allow storage a piece of data • Will become “larger” or “smarter” as the semester progresses • Each has a particular data type

  6. Data Types • To efficiently use the computer’s memory, each variable has a data type • Behind the scenes, tells the compiler and JVM how to use memory to store a pieces of data • E.g. A number is stored differently than an integer • Java is a Strongly Typed Language • The languages enforces rules as to what you can do with the data in variables • E.g. Won’t necessarily let you do this? “Bob” + 27 Doesn’t necessarily make sense to add a string and number…

  7. 2 Categories of Data Types • Primitive Data Types • Holds a single data item such as integer, character, or true/false value • Reference Data Types • Data type whose value is actually a memory address

  8. Primitive Data Types

  9. Declaring Variables • declaration statement • Line of code that identifies, or declares, the type and names the identifier or variable. General Form: SomeDataTypeSomeIdentifierName; float price; int grade; long atomsInBody; double Average;

  10. Declaring Variables – Identifier Names • Rules for names of Identifiers • must start with letter, underscore or dollar sign ($) • subsequent characters can be letters, underscores, dollar signs, or digits (0 – 9) • Any Length (but be reasonable) • Name should be meaningful (no variables named x, y, z) • Remember – Java is Case Sensitive • Can’t declare 2 variables with the same name in the same method • Can’t have a variable that is a reserved word or the same name as a method General Form: SomeDataTypeSomeIdentifierName; Where have we seen these rules before?

  11. In Code public class TestVariables { public static void main (String [] args) { int grade1 = 100; int grade2, grade3; float average; //Other stuff here } } Notice that you can declare the variable and provide an initial value all in one statement.

  12. check point • Declare a variable to hold your GPA • Declare a variable to hold your age (in years)

  13. Assignment Statements • Used to store a new value in a variable • uses the assignment operator (=) userAge = 21; boolean flag newUserAge = userAge; sum = grade1 + grade2 + grade; float average = sum / 3.0;

  14. Assignment Statements int grade1 = 98; int grade2 = 100; int sum = grade1 + grade2; • = is right associative • Means always stores what’s on the right side in the variable on the left side • Will evaluate expression on right first (step 1), then perform the assignment Step 2 Stores 198 in sum Step 1 Evaluates to 198

  15. Variables in Output • Can use a variable in an output statement • Can concatenate a string literal and variable with plus sign userAge = 21; System.out.println(userAge); System.out.println(“Age is “ + userAge);

  16. Important Arithmetic Operators in Java

  17. Important Arithmetic Operators in Java Might Be New Math Ideas

  18. check point • What is the result of? • 3 / 2 • 2 / 3 • 5 % 4 • 4 % 5

  19. Arithmetic Operators • The order of operator precedence is a predetermined order that defines the sequence in which operators are evaluated in an expression • Addition, subtraction, multiplication, and division can manipulate any numeric data type • When Java performs math on mixed data types, the result is always the larger data type • Casts allow programmers to force a conversion from one primitive type to another

  20. Numeric Expressions • Any Expression that can be evaluated to a number • Can include operators, literal values, variables, method calls • Only primitive data types may participate in numeric expressions • Any method calls we use in a numeric expression will return a primitive data type. • A literal value and a variable (or 2 variables) must be separated by an arithmetic operator

  21. What if multiple operators in 1 expression? • Follow the order of Precedence • Unless parentheses dictate otherwise, evaluate expressions in the following order • Multiplication and/or Division • Integer Division • Modular Division • Addition and/or subtraction • When multiple operations of the same kind are present, Java performs the left to right 18 / 3 – 2 + 4 * 2

  22. Let’s Evaluate 18 / 3 – 2 + 4 * 2 6 – 2 + 4 * 2 6 – 2 + 8 4 + 8 12

  23. Parentheses in Expressions • Change the order of operations • when found in an expression, the part inside the parentheses is evaluated first. Then the rest of the expression is evalutated • If they are nested, then the inner-most set of parentheses is evaluated first

  24. Let’s Evaluate 18 / (3 – 2) + 4 * 2 18 / 1 + 4 * 2 18 + 4 * 2 18 + 8 26

  25. check point – Your Turn! 18 / 3 * 2 + (3 * (2 + 5))

More Related