1 / 22

Computer Programming with Java

Computer Programming with Java. Chapter 2 Primitive Types, Assignment, and Expressions. Contents . Variables Primitive Types Assignment Statements Constants Arithmetic Operators The String class Documentation Keyboard and Screen I/O. Variables. Definition

fmarion
Download Presentation

Computer Programming with Java

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. Computer Programming with Java Chapter 2 Primitive Types, Assignment, and Expressions

  2. Contents • Variables • Primitive Types • Assignment Statements • Constants • Arithmetic Operators • The String class • Documentation • Keyboard and Screen I/O Computer Programming with Java

  3. Variables • Definition • Variables in a program are used to store data such as numbers and letters. • Values arethe number or letter or other data item in a variable • A variable can have its value changed. • Identifiers are the variable names and must satisfy the spelling rules in Chapter 1. Computer Programming with Java

  4. public class EggBasket { public static void main(String[] args) { int numberOfBaskets, eggsPerBasket, totalEggs; System.out.println("Enter the number of eggs in each basket:"); eggsPerBasket = SavitchIn.readLineInt(); System.out.println("Enter the number of baskets:"); numberOfBaskets = SavitchIn.readLineInt(); totalEggs = numberOfBaskets * eggsPerBasket; System.out.println(eggsPerBasket + " eggs per basket."); System.out.println(numberOfBaskets + " baskets."); System.out.println("Total number of eggs is " + totalEggs); eggsPerBasket = eggsPerBasket - 2; totalEggs = numberOfBaskets * eggsPerBasket; … Display 2.1: A Simple Java Program Computer Programming with Java

  5. Primitive Types • Primitive Data Types • integers: byte, short, int, long • e.g.) 0, 1, -1, 2, -2 • floating point: float, double • e.g.) 9.9, 3.14159, -5.63 • others: char, boolean • e.g.) char • char symbol; • symbol= ‘A’; • e.g.) boolean • true or false Computer Programming with Java

  6. Computer Programming with Java

  7. Assignment Statements • Assignment operator • Variable = Expression or Values; • e.g.) • amount = 3.99; • score = numberOfCards + handicap; • Declaration and Initialization • Specialized assignment operators • +=, -=, /=, *=, %= • e.g.) • amount += 5; equals to amount = amount + 5; Computer Programming with Java

  8. Assignment Statements (1) • Assignment Compatibilities • Principle of Compatibility • you cannot put a value of one type in a variable of another type. • you can assign a value of any type on the following list to a variable of any type that appears further down the list • byte -> short -> int -> long -> float -> double • if you want to assign a value of type double to a variable of type int, you must change the type of the value using a type cast Computer Programming with Java

  9. Assignment Statements (2) • Type Casting • the changing of the type of a value from its normal type to some other type. • (Type_Name) Expression • e.g.) • double guess = 7.8; • int answer = (int) guess; (correct) • double distance = 9.0; • int points = distance; (incorrect) Computer Programming with Java

  10. Constants • Number constants • It is literal values like 2 or 3.7 and its value do not change in a Java program. • integer • e.g.) • 2, 3, 0, -3, 752, +12 (correct) • 1,000 (incorrect) • floating-point • e.g.) • 2.1, 3.14159, 865000000.0 • 8.65108: 865000000.0 or 8.65e8 Computer Programming with Java

  11. Constants (1) • Named Constants • a name be defined for constant • Type Variable = Constant; • e.g.) • double PI = 3.14159; Computer Programming with Java

  12. public class CircleCalculation { public static void main(String[] args) { double radius; //in inches double area; //in square inches System.out.println("Enter the radius of a circle in inches:"); radius = SavitchIn.readLineDouble(); area = 3.14159 * radius * radius; System.out.println("A circle of radius " + radius + " inches"); System.out.println("has an area of " + area + " square inches."); } } Display 2.9: Comments and Indenting Computer Programming with Java

  13. Arithmetic Operators • Basic Operators • +, -, *, / • it can used to combine variables and/or numbers • Arithmetic Expression Computer Programming with Java

  14. Arithmetic Operators (1) • Increment and Decrement Operators • it can be used to increase or decrease the value of a variable by one. • ++, -- • e.g.) • count = count + 1; count++; • count = count - 1; count--; Computer Programming with Java

  15. Arithmetic Operators (2) • Precedence Rules (pp. 140-143) • when the computer is deciding which of two operators to perform first and the order is not dictated by parentheses, then it does the operator of higher precedence before the operator of lower precedence. Some operators have equal precedence, in which the order of operations is determined by the left-to-right order of the operators. Computer Programming with Java

  16. Higher Precedence First: the unary operators: +, -, ++, --, ! Second: the binary arithmetic operators: *, /, % Third: the binary arithmetic operators: +, - Fourth: the boolean operators: <,>,<=,>= Fifth: the boolean operators: ==, != Sixth: the boolean operators: & Seventh: the boolean operators: | Eighth: the boolean operators: && Ninth: the boolean operators: || Lowest Precedence Display 3.13: Precedence Rules Computer Programming with Java

  17. The String Class • String class • it can be used to store and process strings of characters. • String methods • see text pp.55-59 • zero-based index • String tmp = “Java is fun.”; • char tmp_len = tmp.length(); Computer Programming with Java

  18. public class StringDemo { public static void main(String[] args) { String sentence = "Text processing is hard!"; int position; position = sentence.indexOf("hard"); System.out.println(sentence); System.out.println("012345678901234567890123"); System.out.println("The word \"hard\" starts at index " + position); sentence = sentence.substring(0, position) + "easy!"; System.out.println("The changed string is:"); System.out.println(sentence); } } Display 2.7: Using the String class Computer Programming with Java

  19. The String Class (1) • Escape characters • it escape from the usual meaning of a character. • \”: double quote, \’: single quote, \\: backslash, \n: new line, \r: carriage return, \t: tab • e.g.) The word “hard” starts at index 19 • System.out.println(“The word “hard” starts at index “+19); -> “The word” //incorrect • System.out.println(“The word \”hard\” starts at index “+19); //correct Computer Programming with Java

  20. Documentation • Documentation style • Truth on S/W development • A Project: Development (20~30%), Maintenance (70~80%) • if the program is not easy to read and understand, it will not be easy to maintain it. • Rule of thumb • Use meaningful names for variables • Self-documenting • thanks to a very clean style and very well-chosen variable names (and other names) Computer Programming with Java

  21. Documentation (1) • Comments • //, /*…*/: Things written into your program that help a person understand the program, but that are ignored by the compiler. • e.g.) • String sentence; //Spanish version • /*This program should only be • used…. • */ • Indenting Computer Programming with Java

  22. Keyboard and Screen I/O • I/O • input and output of program data • Screen Output • System.out.{println, print}() • e.g.) • System.out.println(“Enter the number of eggs ..:”); • System.out.println(“Lucky number = “+13+”Secret number = “+ number); Computer Programming with Java

More Related