1 / 41

Lecture 2 Fundamental Data Types

Lecture 2 Fundamental Data Types. Variable Declaration. Rules for Identifiers. Numeric Types. All Numeric Types have Finite Ranges. 0. 1. 0. 0. 0. 0. 1. 1. 0. 0. 1. 0. 0. 0. 1. 0. 1. 1. 0. 0. 1. 0. 1. 1. 1. 1. 0. 0. 1. 0. 0. 0. 1. 1. 1. 0. 0. 1. 0. 1.

miracle
Download Presentation

Lecture 2 Fundamental Data Types

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. Lecture 2 Fundamental Data Types

  2. Variable Declaration

  3. Rules for Identifiers

  4. Numeric Types

  5. All Numeric Types have Finite Ranges 0 1 0 0 0 0 1 1 0 0 1 0 0 0 1 0 1 1 0 0 1 0 1 1 1 1 0 0 1 0 0 0 1 1 1 0 0 1 0 1 1 0 1 0 1 0 1 1 value is 127 value is 128 but the leading 1 indicates negative value

  6. Floating Point Values are Approximate

  7. Numeric Operators

  8. Integer Division and Remainder

  9. Increment and Decrement Operators Notice: In this course, we will NOT be using increment and decriment operators in regular assignment statements. (More on this later.)

  10. Assignment Statements

  11. When is "=" not Equal to "Equals"? In Java (and most other programming languages) the symbol = means assignment. X = X + 1 "X is assigned the value X + 1 LOAD X = X + 1 Arithmetic Logic Unit ADD ALU STORE

  12. Augmented Assignment Operators

  13. Declaring Constants

  14. Mathematical Expressions

  15. Sugar Cookie Recipe Ingredients 1 stick butter 1/2 cup sugar 1 egg 1 teaspoon vanilla extract 1/2 teaspoon salt 1 3/4 cups all-purpose flour Directions Mix the butter and sugar until light and fluffy. Beat in the egg, then vanilla and salt. Mix slowly while gradually adding the flour. Do not overmix. Shape into a 1-inch-thick disk, refrigerate for at least 2 hours. Heat oven to 350° F. Roll the dough ¼ inch thick, and cut the dough into shapes. Place 1 inch apart on prepared baking sheets, sprinkle with sugar. Bake until the edges just begin to brown, 12 to 15 minutes. Transfer to cooling racks.

  16. Circle Area Algorithm Parameters pi = 3.1415926 radius area Actions Read in the radius. Compute the Area using the formula: Display Area. Computer Algorithms can be simpler than a recipe for sugar cookies. ;-)

  17. from Algorithm to Computer Program

  18. Completed Program comments are ignored by the computer

  19. Completed Program

  20. Getting Input from the User We can use the Scanner software package provided as part of java.util to get information typed on the keyboard. Some of the methods provided in Scanner are:

  21. User Input

  22. Interactive Version of Circle Area Calculator

  23. Convert Fahrenheit to Celsius

  24. Convert Seconds to Minutes and Seconds Get total seconds from user. Calculate number of whole minutes. Calculate number of remaining seconds. Display results.

  25. Convert Seconds to Minutes and Seconds

  26. Numeric Type Conversions Java performs automatic type conversion in mathematical expressions. For example, when an integer and a floating-point value are multiplied, the integer is promoted to a floating-point value. We can override the default type conversions by specifying the type of each parameter.

  27. Conflicting Data Types Oh great! Here come your friends, the odd couple. For starters, they're a couple of squares. Why can't you keep it real? 9 25 I can't imagine why you don't like them. As usual, you're being irrational. Why do you insist on running on forever? 2+i3

  28. ShowCurrentTime

  29. Using Type Casting So why would we want to multiply and then divide by 100? 197.55 x 0.06 = 11.853

  30. A Sample Problem

  31. An Implemention

  32. Character Data Types and Operations

  33. Unicode and ASCII Code no parent

  34. Samples of Unicode http://www.unicode.org/charts/

  35. Escape Sequences for Special Characters

  36. Format Specifiers

  37. String Operations

  38. Types of Programming Errors syntax errors - This type of error prevents the compilation of the program into an executable binary or into bytecode. runtime errors - This type of error prevents a running program from completing its normal execution. logic errors - With this type of error a program can complete its execution normally but the results obtained will be incorrect. • easiest type of error to detect • relatively simple to correct • can be data dependent • don't always occur (consider the divide-by-zero error) • most difficult to detect (especially when they are intermittant) • amenable to unit testing and other SW engineering methods • can never be completely eliminated in large programs

  39. using Input and Message Dialogs

  40. The Math Library of Methods

  41. Chapter 2 Summary Algorithms are like Recipes The Input-Process-Output Design Pattern Comments help make code understandible All Numeric Data Types have finite ranges Values can be converted from one data type to another Floats and Doubles approximate Real Numbers java.util Scanner methods support user input Understanding Assignment Operators Increment and Decrement Operators Introducing Processing.org Unicode and ASCII Code Escape Sequences Programming Errors Input and Message Dialogs

More Related