©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3 - 1 Chapter 3: Numerical Data Manipulating Numbers Variables.

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.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 2 1 Chapter 2 Primitive.
CS 180 Recitation 9 / {06, 07} / Reminders Assignment 1 was due last night. Assignment 2 is available & due in 1 week. 10:00pm, Wednesday, September.
©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[]
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: Numeric Data *Variables *Numeric data.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3 Numerical Data.
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.
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.
Chapter 3b Standard Input and Output Sample Development.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: IO *Standard Output *Formatting Decimal.
What is a variable?  A variable holds data in memory so the program may use that data, or store results.  Variables have a data type. int, boolean, char,
Expressions, Data Conversion, and Input
Fundamental concepts in Java. Lesson plan Variable declaration, assign statement & practice Design document & practice.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3 Numerical Data Animated Version.
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)
Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.
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,
©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.
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.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
C++ Programming, Namiq Sultan1 Chapter 3 Expressions and Interactivity Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin.
FUNDAMENTALS 2 CHAPTER 2. OPERATORS  Operators are special symbols used for:  mathematical functions  assignment statements  logical comparisons 
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.
Lecture 5: Expressions and Interactivity Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
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.
CSCI 1100/1202 January 18, Arithmetic Expressions An expression is a combination of operators and operands Arithmetic expressions compute numeric.
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.
M105 - Week 2 Chapter 3 Numerical Data 1 Prepared by: M105 Team - AOU - SAB.
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.
Numeric Data Types There are six numeric data types: byte, short, int, long, float, and double.
Chapter 2 Basic Computation
Elementary Programming
Intro to OOP with Java, C. Thomas Wu Numerical Data
2.5 Another Java Application: Adding Integers
Primitive and Reference Data Values
Type Conversion, Constants, and the String Object
Java Programming: From Problem Analysis to Program Design, 4e
Chapter 2 Basic Computation
Primitive and Reference Data Values
Pseudocode and Flowcharts
Lecture 3 Expressions Richard Gesick.
Chapter 2: Basic Elements of Java
Chapter 3 Numerical Data
Fundamentals 2.
Chapter 2: Java Fundamentals
CS2011 Introduction to Programming I Elementary Programming
Data Types and Expressions
Data Types and Expressions
Data Types and Expressions
Presentation transcript:

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 3: Numerical Data Manipulating Numbers Variables 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; We assign a value to a variable using an assignment statements. The syntax is = ; Examples: sum = firstNumber + secondNumber; avg = (one + two + three) / 3.0;

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Data Type Precisions The six data types differ in the precision of values they can store in memory. Primitive data types vs reference data types

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Arithmetic Operators The following table summarizes the arithmetic operators available in Java. This is an integer division where the fractional part is truncated.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Precedence Rules

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Type Casting If x is a float and y is an int, what will be the data type of the following expression? x * y The answer is float. The data types of the operands in mixed expressions are converted based on the promotion rules. 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. Explicit type cast by prefixing the operand with the data type using the following syntax: ( ) Example (float) x / 3 (int) (x / y * 3.0) Type case x to float and then divide it by 3. Type cast the result of the expression x / y * 3.0 to int.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Constants We can change the value of a variable. If we want the value to remain the same, we use a constant. final double PI = ; These are constants, also called named constant. The reserved word final is used to declare constants. These are called literal constant. Conversion Methods

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Sample Code Fragment //code fragment to input radius and output //area and circumference double radius, area, circumference; 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);

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Overloaded Operator and DecimalFormat Overloaded Operator + The plus operator + can mean two different operations, depending on the context. + is an addition if both are numbers. If either one of them is a String, the it is a concatenation. Evaluation goes from left to right. The DecimalFormat Class Sample Code Demo

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Standard Output and Input The showMessageDialog method is intended for displaying short one-line messages, not for a general- purpose output mechanism. Using System.out, we can output multiple lines of text to the standard output window. System.out.print( “Hello, Dr. Caffeine.” ); Standard Input Scanner scanner; scanner = Scanner.create(System.in); int num = scanner.nextInt();

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 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(); Common Scanner Methods:

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Some Math Class Methods 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) Table 3.8 page 113 in the textbook contains a list of class methods defined in the Math class.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Random Class and The GregorianCalendar Class Math.random()  [0.0,1.0) Use a GregorianCalendar object to manipulate calendar information GregorianCalendar today, independenceDay; today = new GregorianCalendar(); independenceDay = new GregorianCalendar(1776, 6, 4); //month 6 means July; 0 means January

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Retrieving Calendar Information This table shows the class constants for retrieving different pieces of calendar information from Date.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Problem Statement Problem statement: Write a loan calculator program that computes both monthly and total payments for a given loan amount, annual interest rate, and loan period. –Given in the text book. Will be left for self study A Vending Machine Change Program requirements –The user enters an amount between 1 cent and 99 cents. –The program determines a combination of coins equal to that amount. –For example, 55 cents can be two quarters and one nickel.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Vending Machine (cont’d) sample dialog Enter a whole number from 1 to 99. The machine will determine a combination of coins cents in coins: 3 quarters 1 dime 0 nickels 2 pennies Tasks, Program Diagram