Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Introduction to Computing Using Java More about Primitive Types and Operators 2008-2009 2b Michael Fung, CS&E, The Chinese University of HK 1 Default Type of Floating-point Number Literals Floating-point Number literals are considered to be of type double by default. double double double float d1 d2 d3 f1 = = = = 3.14159; 3e8; -0.27e-5; 3.14159; // // // // ok ok ok not ok Adding a suffix F/ f to the number changes this default: float float 2008-2009 2b f2 = 3.14159F; f3 = -0.27e-5f; // ok // ok Michael Fung, CS&E, The Chinese University of HK 2 Type Casting We can also apply type casting. float f3 = (float) 3.14159; float f4 = (float) d1; // ok // ok In case of real to integer type casts, any fractional part is discarded, and results in the integral part (truncation): (int) 6.3 (int) 6.8 gives 6 gives 6 Syntax of type casting (explicit type conversion): ( <type_name> ) some_value 2008-2009 2b Michael Fung, CS&E, The Chinese University of HK 3 Type Casting Examples double UV_measurement = 6.754; int UV_level = (int) UV_measurement; short avg_height; avg_height = (short) ( (178.2 + 192.7 + 180.1) / 3 ); // type-casting has higher precedence than division! Benz peter = new Benz(); Advanced example Car michael = (Car) peter; // type-casting also works on class type objects // peter keeps a Benz object // we can "down-convert" it and consider it a Car 2008-2009 2b Michael Fung, CS&E, The Chinese University of HK 4 Type Casting Issues Integer-to-integer type casts – byte short int long Always ok – long int short byte May not… – Overflow may occur, causing errors. Real-to-real type casts – – – – float double double float Note that real numbers are often inexact. Always check the accuracy you needed. 2008-2009 2b Michael Fung, CS&E, The Chinese University of HK ? 5 Issue Concerning Real Numbers There are infinite real numbers in the world. Real numbers do not have absolute friends – There are always 第三者 in between – 1.4 1.43 1.442 1.445 1.5 Moreover, computers use 0 and 1 (binary digits) to represent numbers and there is always a limitation in representing decimal real numbers. You will find that 0.7 * 0.7 0.49 and (double) 3.123F gives 3.122999906539917 2008-2009 2b Michael Fung, CS&E, The Chinese University of HK 6 Self-study: More About Type class DataType { /* testing of primitive data types */ public static void main (String [ ] args) { byte int8 = -128; System.out.println(int8); int8 = (byte) 137; /* type casting with an overflow */ System.out.println(int8); boolean ok = (3 > 7); /* boolean expression */ System.out.println(ok); System.out.println("Hello \"World\" !!!"); double GPA = 3; System.out.println("GPA = " + GPA); System.out.println("(int)(5.23) = " + (int)(5.23)); System.out.println("Math.floor(5.23) = " + Math.floor(5.23)); System.out.println("Math.ceil(5.23) = " + Math.ceil(5.23)); System.out.println("Math.round(5.23) = " + Math.round(5.23)); System.out.println("(int)(-5.23) = " + (int)(-5.23)); System.out.println("Math.floor(-5.23) = " + Math.floor(-5.23)); System.out.println("Math.ceil(-5.23) = " + Math.ceil(-5.23)); System.out.println("Math.round(-5.23) = " + Math.round(-5.23)); } } 2008-2009 2b Michael Fung, CS&E, The Chinese University of HK 7 Self-study: More About Type -128 Look up Java API class Math -119 check these Math methods: false floor( ) Hello "World" !!! round( ) GPA = 3.0 (int)(5.23) = 5 ceil( ) Math.floor(5.23) = 5.0 What are their functions? Math.ceil(5.23) = 6.0 Do they act like a Math.round(5.23) = 5 mathematical function? (int)(-5.23) = -5 Math.floor(-5.23) = -6.0 Math.ceil(-5.23) = -5.0 Math.round(-5.23) = -5 2008-2009 2b Michael Fung, CS&E, The Chinese University of HK 8 The Math Class The class Math is provided with Java, offering various methods for simple mathematical operations. To use such methods, we have to send message to the Math class: double answer1; answer1 = Math.sqrt(49); A message usually generates a result (depending on the design and nature/type of the method): long answer2; answer2 = Math.round(Math.pow(2+3, 2)); 2008-2009 2b Michael Fung, CS&E, The Chinese University of HK 9 Methods in the Math Class PI is a class constant field pre- stored with many digits of . Notice that sin, cos, tan, etc. uses radian instead of degree. Math PI 3.14159… sqrt Don’t miss the pair of parenthesis in pow sending messages to the Math class! sin General form of a such a message: class_name.method_name(input); Math.function(input); 2008-2009 2b round … Michael Fung, CS&E, The Chinese University of HK 10 Operators Category of operators: – Arithmetic – Relational – Logical : + : < : ! – > & * / % -num <= >= == != | && || ^ – Conditional : bool_expression ? true_case : false_case – Short-hand : ++ -- += -= – Assignment : = (to store a value) 2008-2009 2b Michael Fung, CS&E, The Chinese University of HK *= … 11 Arithmetic Operators Addition/ Sum :a+b Subtraction/ Difference :a–b Multiplication/ Product :a*b Division/ Quotient :a/b Remainder (of integer division): a % b Negation/ Minus : –number 2008-2009 2b Michael Fung, CS&E, The Chinese University of HK 12 Real and Integer Division System.out.println(1.0 / 2.0); 0.5 System.out.println(1 / 2); 0 System.out.println(1.0 / 2); 0.5 2008-2009 2b Michael Fung, CS&E, The Chinese University of HK 13 Real and Integer Division System.out.println(1.0 / 2.0); 0.5 double / double System.out.println(1 / 2); 0 System.out.println(1.0 / 2); 0.5 2008-2009 2b Michael Fung, CS&E, The Chinese University of HK 14 Real and Integer Division System.out.println(1.0 / 2.0); 0.5 double / double System.out.println(1 / 2); 0 int / int int System.out.println(1.0 / 2); 0.5 2008-2009 2b Michael Fung, CS&E, The Chinese University of HK 15 Real and Integer Division System.out.println(1.0 / 2.0); 0.5 double / double System.out.println(1 / 2); 0 int / int int System.out.println(1.0 / 2); 0.5 2008-2009 2b double / int Michael Fung, CS&E, The Chinese University of HK 16 Real and Integer Division System.out.println(1.0 / 2.0); 0.5 double / double System.out.println(1 / 2); 0 int / int int System.out.println(1.0 / 2); 0.5 2008-2009 2b double / double Implicit Coercion Michael Fung, CS&E, The Chinese University of HK 17 Relational and Logical Operators (Examples will be covered in tutorial) Relational operators – to make comparisons – result is a boolean value (true or false) Logical/ Boolean operators – and, or, negate, exclusive-or, … – operate on boolean values – result is a boolean value 2008-2009 2b Michael Fung, CS&E, The Chinese University of HK 18 Short-hand Operators Increment operator (basic understanding) a++ ++a a = a + 1 Decrement operator (basic understanding) a–– --a a = a – 1 Short-hand assignment operators (lazy) a += b a = a + b a -= b a = a – b a *= b a = a * b ... 2008-2009 2b Michael Fung, CS&E, The Chinese University of HK 19 Summary Type casting usage and examples Floating-point number issues Java API Math class and methods Operators usage and examples – Conditional operator will be discussed later 2008-2009 2b Michael Fung, CS&E, The Chinese University of HK 20 End Note Readings and References – Section 2.1 – 2.5 Exercise – 1.3, 1.4, 1.16, 1.17, 1.18, – 2.2, 2.3, 2.4, 2.5, 2.6, 2.7 Programming Projects – 2.1 2008-2009 2b Michael Fung, CS&E, The Chinese University of HK 21