Presentation is loading. Please wait.

Presentation is loading. Please wait.

M105 - Week 2 Chapter 3 Numerical Data 1 Prepared by: M105 Team - AOU - SAB.

Similar presentations


Presentation on theme: "M105 - Week 2 Chapter 3 Numerical Data 1 Prepared by: M105 Team - AOU - SAB."— Presentation transcript:

1 M105 - Week 2 Chapter 3 Numerical Data 1 Prepared by: M105 Team - AOU - SAB

2 Objectives Select proper types for numerical data. Write arithmetic expressions in Java. Evaluate arithmetic expressions using the precedence rules. Evaluate implicit and explicit type casting. Declaring Constants in Java. Write mathematical expressions, using methods in the Math class. Convert input string values to numerical data Perform input and output by using System.in and System.out – using Scanner Class The non-numerical primitive data types Boolean &char 2 Prepared by: M105 Team - AOU - SAB

3 Variables - Manipulating Numbers In Java, to add two numbers x and y, we write x + y But before the actual addition of the two numbers takes place, we must declare their data type. If x and y are integers, we write int x, y; or int x; int y; 3 Prepared by: M105 Team - AOU - SAB

4 Variables - Declarations When the declaration is made, memory space is allocated to store the values of x and y. x and y are called variables. A variable has three properties: –A memory location to store the value, –The type of data stored in the memory location, and –The name used to refer to the memory location. Sample variable declarations: int x; int v, w, y; 4 Prepared by: M105 Team - AOU - SAB

5 Variables - Identifiers A Java identifier is a set of sequence of letters, digits, underscores, and dollar sign with the first one is being a letter. the –Uppercase and lowercase letters are distinguished. –Spaces are not allowed in an identifier We use an identifier to name class, object, method, variable, constant and others The Java Standard naming convention is using lowercase letter for the first letter. If the identifier consists of a multiple words, the first letter from every word will be capitalized except the first word –Ex. myFullName, studentList, Prepared by: M105 Team - AOU - SAB 5

6 Variables - Numerical Data Types There are six numerical data types: byte, short, int, long, float, and double. The syntax is ; Sample variable declarations: int i, j, k; float numberOne, numberTwo; long bigInteger; double bigNumber; At the time a variable is declared, it also can be initialized. For example, we may initialize the integer variables count and height to 10 and 34 as int count = 10, height = 34; 6 Prepared by: M105 Team - AOU - SAB

7 Variables - Data Type Precisions The six data types differ in the precision of values they can store in memory. They are considered as keywords Prepared by: M105 Team - AOU - SAB 7

8 Variables - Assignment Statements We assign a value to a variable using an assignment statements. The syntax is = ; Examples: sum = firstNumber + secondNumber; avg = (one + two + three) / 3.0; Before using a variable, first we must declare and assign a value to it. Numerical data type is called primitive data type Prepared by: M105 Team - AOU - SAB 8

9 Arithmetic Expressions - Arithmetic Operators This is an arithmetic expression 32 + 78 Because it consists of arithmetic operators and operands, designates numerical computation. The table summarizes the arithmetic binary operators available in Java. Prepared by: M105 Team - AOU - SAB 9

10 Arithmetic Expressions - Arithmetic Operators How does the expression x + 3 * y get evaluated? Answer: x is added to 3*y. We determine the order of evaluation by following the precedence rules. A higher precedence operator is evaluated before the lower one. If two operators are the same precedence, then they are evaluated left to right for most operators. You need to write the expression as (x + 3) * y if you want to multiply y to the sum of x and 3. Prepared by: M105 Team - AOU - SAB 10

11 Arithmetic Expressions - Precedence Rules If you want to alter the precedence rules, then use parentheses to dictate the order of evaluation. Prepared by: M105 Team - AOU - SAB 11

12 (2 * (3 + (6 / (1 + 2)) –1)) The work out for the expression: The order of evaluation and the evaluation step by step are shown. On each line the part that will be evaluated next is shaded Prepared by: M105 Team - AOU - SAB 12

13 Constants There are two types of data values – Variables that can change over the time – Constants that can not change Java convention uses all capitalized letters for constants. final double PI = 3.14159; final int MONTH_IN_YEAR = 12; final short FARADAY_CONSTANT = 23060 Prepared by: M105 Team - AOU - SAB 13 These are constants, also called named constant. The reserved word final is used to declare constants. These are called literal constant.

14 Type Casting When an arithmetic expression consists of variables and constants of the same data type, then the result of the expression will be that data type also. When the data types of variables and constants in an arithmetic expression are of different data types, then a casting conversion will take place. The expression is called a mixed expression A casting conversion, or type casting, is a process that converts a value of one data type to another data type. Two types of casting conversions in Java are implicit and explicit. Prepared by: M105 Team - AOU - SAB 14

15 Implicit Type Casting - Promotion An implicit conversion called numeric promotion is applied to the operands of an arithmetic operator. The promotion rules ensure that the data type of the expression will be the same as the data type of an operand whose type has the highest precision. Prepared by: M105 Team - AOU - SAB 15

16 Implicit Type Casting The assignment conversion is implicit conversion Consider the following expression: double x = 3 + 5; The result of 3 + 5 is of type int. However, since the variable x is double, the value 8 (type int) is promoted to 8.0 (type double) before being assigned to x. Notice that it is a promotion. Demotion is not allowed. int x = 3.5; Prepared by: M105 Team - AOU - SAB 16 A higher precision value cannot be assigned to a lower precision variable.

17 Explicit Type Casting We can make an explicit type cast by prefixing the operand with the data type using the following syntax: ( ) Example (float) x / 3 (int) (x / y * 3.0) Prepared by: M105 Team - AOU - SAB 17 Type cast the result of the expression x / y * 3.0 to int. Type case x to float and then divide it by 3.

18 The Math class The Math class in the java.lang package contains class methods for commonly used mathematical functions. Some Math Class Methods Prepared by: M105 Team - AOU - SAB 18 MethodDescription exp(a) Natural number e raised to the power of a. log(a) Natural logarithm (base e) of a. floor(a) The largest whole number less than or equal to a. max(a,b) The larger of a and b. pow(a,b) The number a raised to the power of b. sqrt(a) The square root of a. sin(a) The sine of a. (Note: all trigonometric functions are computed in radians)

19 Computing using The Math class Prepared by: M105 Team - AOU - SAB 19 alphaRad = Math.toRadians(alpha); betaRad = Math.toRadians(beta); height =(distance * Math.sin(alphaRad)*Math.sin(betaRad)) / Math.sqrt( Math.sin(alphaRad + betaRad) * Math.sin(alphaRad - betaRad) );

20 Type Conversion- Getting Numerical Input Values Suppose we want to input an age. Will this work? No. String value cannot be assigned directly to an int variable. Wrapper classes are used to perform necessary type conversions, such as converting a String object to a numerical value. Prepared by: M105 Team - AOU - SAB 20 int age; age=JOptionPane.showInputDialog(null, “Enter your age”); int age; String inputStr; inputStr=JOptionPane.showInputDialog(null, “Enter your age”); age = Integer.parseInt(inputStr);

21 Other Conversion Methods Prepared by: M105 Team - AOU - SAB 21

22 Sample Code Fragment Prepared by: M105 Team - AOU - SAB 22 //code fragment to input radius and output //area and circumference double radius, area, circumference; final double PI = 3.14159; radiusStr = JOptionPane.showInputDialog( null, "Enter radius: " ); radius = Double.parseDouble(radiusStr); //compute area and circumference area = PI * radius * radius; circumference = 2.0 * PI * radius; JOptionPane.showMessageDialog(null, "Given Radius: " + radius + "\n" + "Area: " + area + "\n" + "Circumference: " + circumference);

23 Prepared by: M105 Team - AOU - SAB A simple Java program using the standard output window A sample standard output window for displaying multiple lines of text. The exact style of standard output window depends on the Java tool you use. We use the print method to output a value to the standard output window. The print method will continue printing from the end of the currently displayed output. We use println instead of print to skip a line. 23

24 Prepared by: M105 Team - AOU - SAB A simple Java program using the standard output window We use println instead of print to skip a line. int x = 123, y = x + x; System.out.println( "Hello, Dr. Caffeine.“ ); System.out.print( " x = “ ); System.out.println( x ); System.out.print( " x + x = “ ); System.out.println( y ); System.out.println( " THE END“ ); 24

25 Prepared by: M105 Team - AOU - SAB A simple Java program using the standard output window We use println instead of print to skip a line. int x = 123, y = x + x; System.out.println( "Hello, Dr. Caffeine.“ ); System.out.print( " x = “ ); System.out.println( x ); System.out.print( " x + x = “ ); System.out.println( y ); System.out.println( " THE END“ ); 25

26 Standard Input The technique of using System.in to input data is called standard input. We can only input a single byte using System.in directly. To input primitive data values, we use the Scanner class (from Java 5.0). Prepared by: M105 Team - AOU - SAB 26 System.out.println(“enter number: ”); Scanner myScanner =new Scanner (System.in); int num = myScanner.nextInt();

27 Common Scanner Methods: To use Scanner class, we have to import java.util.*; MethodExample nextByte( )byte b = scanner.nextByte( ); nextDouble( )double d = scanner.nextDouble( ); nextFloat( )float f = scanner.nextFloat( ); nextInt( )int i = scanner.nextInt( ); nextLong( )long l = scanner.nextLong( ); nextShort( )short s = scanner.nextShort( ); next() String str = scanner.next(); Prepared by: M105 Team - AOU - SAB 27

28 The non-numerical Primitive Data Type In addition to the six numerical data types, there are two non-numerical primitive data types The data type Boolean is used to represent two logical values true and false Boolean ok = true ; The data type char is used to represent a single character (letter, digit, punctuation marks, and others). It is designated by single quotes. char letter = ‘A’; Prepared by: M105 Team - AOU - SAB 28

29 Shorthand Assignment Operators In writing program, we need to increment and decrement the values of the variables. sum = sum +5; We can rewrite this statement without repeating the same variable on the right- hand side using the shorthand assignment operators as: sum += 5; The five shorthand assignment operators in Java are: +=a+=ba=a+b -=a-=ba=a-b *=a*=ba=a*b /=a/=ba=a/b %=a%=ba=a%b Prepared by: M105 Team - AOU - SAB 29

30 Cascade the Assignment Operator We can assign a value to multiple variables by cascade the assignment operator x = 1; y = 1; We can write it as: x = y = 1; The assignment operator is an operator and its precedence order is lower than any other operators and it is evaluated right to left Prepared by: M105 Team - AOU - SAB 30


Download ppt "M105 - Week 2 Chapter 3 Numerical Data 1 Prepared by: M105 Team - AOU - SAB."

Similar presentations


Ads by Google