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

Slides:



Advertisements
Similar presentations
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 3 Numerical Data.
Advertisements

L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 2 1 Chapter 2 Primitive.
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 3 Console Output Keyboard Input Numerical.
Primitive Data Types and Operations. Introducing Programming with an Example public class ComputeArea { /** Main method */ public static void main(String[]
CMT Programming Software Applications
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: Numeric Data *Variables *Numeric data.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3 Numerical Data.
1 Fundamental Data Types. 2 Outline  Primitive Data Types  Variable declaration  Numbers and Constants  Arithmetic Operators  Arithmetic Operator.
Chapter 2: Java Fundamentals Input and Output statements.
Numerical Data Recitation – 01/30/2009
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 3 Console Output Keyboard Input Numerical.
Program Elements We can now examine the core elements of programming (as implemented in Java) We focuse on: data types variable declaration and use, constants.
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
Chapter 3 Numerical Data. Topics Variables Numeric data types Assignment Expressions.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
بسم الله الرحمن الرحيم CPCS203: Programming II. ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display., Modifications by Dr.
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Primitive Data Types and Operations Identifiers, Variables, and Constants Primitive Data Types Byte, short, int, long, float, double, char, boolean Casting.
Simple Programs from Chapter 2 Putting the Building Blocks All Together (corresponds with Chapter 2)
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
1 Do you have a CS account? Primitive types –“ building blocks ” for more complicated types Java is strongly typed –All variables in a Java program must.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 3 Numerical Data.
Chapter 2 Elementary Programming
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3 Numerical Data Animated Version.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 3: Numerical Data Manipulating Numbers Variables.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Java Programming: From Problem Analysis to Program Design, 5e Chapter 2 Basic Elements of Java.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 3 Console Output Keyboard Input Numerical.
FUNDAMENTALS 2 CHAPTER 2. OPERATORS  Operators are special symbols used for:  mathematical functions  assignment statements  logical comparisons 
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
Numeric Data Types There are six numeric data types: byte, short, int, long, float, and double. Sample variable declarations: int i, j, k; float numberOne,
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Chapter 3 Numerical Data. Objectives After you have read and studied this chapter, you should be able to Select proper types for numerical data. Write.
Strings and I/O. Lotsa String Stuff… There are close to 50 methods defined in the String class. We will introduce several of them here: charAt, substring,
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Elementary Programming.
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
Fundamentals 2 1. Programs and Data Most programs require the temporary storage of data. The data to be processed is stored in a temporary storage in.
Data and Expressions. Let's explore some other fundamental programming concepts Chapter 2 focuses on: Character Strings Primitive Data The Declaration.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Elementary Programming
Intro to OOP with Java, C. Thomas Wu Numerical Data
Primitive and Reference Data Values
Type Conversion, Constants, and the String Object
Java Programming: From Problem Analysis to Program Design, 4e
IDENTIFIERS CSC 111.
Primitive and Reference Data Values
Pseudocode and Flowcharts
Chapter 2: Basic Elements of Java
Chapter 3 Numerical Data
Fundamentals 2.
Chapter 2: Java Fundamentals
Chapter 2: Java Fundamentals
Chapter 2: Java Fundamentals
CS2011 Introduction to Programming I Elementary Programming
Primitive Types and Expressions
Chapter 2 Primitive Data Types and Operations
Presentation transcript:

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

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

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

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

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

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

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

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

Arithmetic Expressions - Arithmetic Operators This is an arithmetic expression 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

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

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

(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

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 = ; final int MONTH_IN_YEAR = 12; final short FARADAY_CONSTANT = 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.

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

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

Implicit Type Casting The assignment conversion is implicit conversion Consider the following expression: double x = 3 + 5; The result of 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.

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.

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)

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) );

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);

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

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 = ; 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);

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

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

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

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();

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

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

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

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