Lecture Java methods: Object-oriented programming and data structures (2nd AP edition): Chapter 6 - Maria Litvin, Gary Litvin

32 30 0
Lecture Java methods: Object-oriented programming and data structures (2nd AP edition): Chapter 6 - Maria Litvin, Gary Litvin

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Chapter 6 - Data types, variables, and arithmetic. In this chapter, the learning objectives are: Discuss primitive data types; learn how to declare fields and local variables; learn about arithmetic operators, compound assignment operators, and increment/decrement operators; discuss common mistakes in arithmetic.

Java Methods Object-Oriented Programming and Data Structures 2nd AP edition with GridWorld Maria Litvin ● Gary Litvin int chapter = 6; Data Types, Variables, and Arithmetic Copyright © 2011 by Maria Litvin, Gary Litvin, and Skylight Publishing All rights reserved Objectives: • Discuss primitive data types • Learn how to declare fields and local variables • Learn about arithmetic operators, compound assignment operators, and increment / decrement operators • Discuss common mistakes in arithmetic 6­2 Variables • A variable is a “named container” that holds a value count • q = 100 - q; means:    Read the current value of q Subtract it from 100 Move the result back into q mov ax,q mov bx,100 sub bx,ax mov q,bx 6­3 Variables (cont’d) • Variables can be of different data types: int, char, double, boolean, etc • Variables can hold objects; then the type is the class of the object • The programmer gives names to variables • Names of variables usually start with a lowercase letter 6­4 Variables (cont’d) • A variable must be declared before it can be used: int Type count; double x, y; JButton go; Name(s) Bug bob; String firstName; 6­5 Variables (cont’d) • The assignment operator = sets the variable’s value: count = 5; x = 0; go = new JButton("Go"); firstName = args[0]; 6­6 Variables (cont’d) • A variable can be initialized in its declaration: int count = 5; JButton go = new JButton("Go"); String firstName = args[0]; 6­7 Variables: Scope • Each variable has a scope — the area in the source code where it is “visible.” • If you use a variable outside its scope, the compiler reports a syntax error • Variables can have the same name when their scopes not overlap { int k = ; } for (int k = ) { } 6­8 Fields • Fields are declared outside all constructors and methods • Fields are usually grouped together, either at the top or at the bottom of the class • The scope of a field is the whole class 6­9 Fields (cont’d) Scope public class SomeClass { Fields } Construct ors and methods Or: Scope public class SomeClass { Construct ors and methods Fields } 6­10 Literal Constants new line tab 'A', '+', '\n', '\t' char -99, 2010, int 0.75, -12.3, 8., double “coin.gif", "1776", "y", "\n" String 6­18 Symbolic Constants • Symbolic constants are initialized final variables: private final int sideLength = 8; private static final int BUFFER_SIZE = 1024; public static final int PIXELS_PER_INCH = 6; 6­19 Why Symbolic Constants? • Easy to change the value throughout the program, if necessary • Easy to change into a variable • More readable, self-documenting code • Additional data type checking by the compiler 6­20 Arithmetic • Operators: +, -, /, * , % • The precedence of operators and parentheses is the same as in algebra • m % n means the remainder when m is divided by n (for example, 17 % is 2; % is 2) • % has the same rank as / and * • Same-rank binary operators are performed in order from left to right 6­21 Arithmetic (cont’d) • The type of the result is determined by the types of the operands, not their values; this rule applies to all intermediate results in expressions • If one operand is an int and another is a double, the result is a double; if both operands are ints, the result is an int 6­22 Arithmetic (cont’d) • Caution: if a and b are ints, then a / b is truncated to an int… 17 / gives 3 / gives • …even if you assign the result to a double: double ratio = / 3; The double type of the result doesn’t help: ratio still gets the value 0.0 6­23 Arithmetic (cont’d) • To get the correct double result, use double constants or the cast operator: double ratio = 2.0 / 3; double ratio = / 3.0; int m = , n = ; double factor = (double)m / (double)n; double factor = (double)m / n; Casts double r2 = n / 2.0; 6­24 Arithmetic (cont’d) • A cast to int can be useful: Returns a double int ptsOnDie = (int)(Math.random() * 6) + 1; int miles = (int)(km * 1.61 + 0.5); Converts kilometers to miles, rounded to the nearest integer 6­25 Arithmetic (cont’d) • Caution: the range for ints is from -231 to 231-1 (about -2·109 to 2·109) • Overflow is not detected by the Java compiler or interpreter: n n n n n n n = = = = = = = 10 11 12 13 14 10^n 10^n 10^n 10^n 10^n 10^n 10^n = = = = = = = 100000000 1000000000 1410065408 1215752192 -727379968 1316134912 276447232 n! n! n! n! n! n! n! = 40320 = 362880 = 3628800 = 39916800 = 479001600 = 1932053504 = 1278945280 6­26 Arithmetic (cont’d) • Compound assignment operators: • Increment and decrement operators: a = a + b; a += b; a = a + 1; a++; a = a - b; a -= b; a = a - 1; a ; a = a * b; a *= b; a = a / b; a /= b; a = a % b; a %= b; Do not use these in larger expressions 6­27 From Numbers to Strings • The easiest way to convert x into a string is to concatenate x with an empty string: String s = x + ""; 'A' 123 -1 3.14 Math.PI Empty string "A" "123" "-1" "0.1" "3.14" "3.141592653589793" • The same rules apply to System.out.print(x) 6­28 From Objects to Strings • The toString method is called: public class Fraction { private int num, denom; public String toString () { return num + "/" + denom; } } Fraction f = new Fraction (2, 3); System.out println (f) ; Output: 2/3 is called automatically f.toString() 6­29 Review: • What is a variable? • What is the type of a variable that holds an object? • What is meant by the scope of a variable? • What is the scope of a field? • What is the scope of a local variable? 6­30 Review (cont’d): • Is it OK to give the same name to variables in different methods? • Is it OK to give the same name to a field and to a local variable of the same class? • What is the range for ints? • When is a cast to double used? 6­31 Review (cont’d): • Given double dF = 68.0; double dC = / * (dF - 32); what is the value of dC? • When is a cast to int used? • Should compound assignment operators be avoided? 6­32 ... 100000000 1000000000 1410 065 408 1215752192 -7 27379 968 13 161 34912 2 764 47232 n! n! n! n! n! n! n! = 40320 = 362 880 = 362 8800 = 399 168 00 = 47900 160 0 = 1932053504 = 1278945280 6? ? 26 Arithmetic (cont’d)... field radius remains 0.0 6? ?15 Primitive Data Types • • • • int double char boolean • • • • byte short long float Used in Java Methods 6? ? 16 Strings • String is not a primitive data type • Strings work... rounded to the nearest integer 6? ?25 Arithmetic (cont’d) • Caution: the range for ints is from -2 31 to 23 1-1 (about -2 ·109 to 2·109) • Overflow is not detected by the Java compiler or interpreter:

Ngày đăng: 04/11/2020, 23:14

Mục lục

    Local Variables (cont’d)

    From Numbers to Strings

    From Objects to Strings

Tài liệu cùng người dùng

Tài liệu liên quan