Lecture 3 Ch 3 Numerical Data Object Oriented

Lecture 3 Ch 3. Numerical Data Object Oriented Programming Iksan Bukhori, M. Phil iksan. bukhori@president. ac. id 2018

Variables What are they? Declaration Syntax: Ex: What’s Wrong? Iksan Bukhori Object Oriented Programming 1/2

Numerical Data Types Precision Memory Space Iksan Bukhori • int is commonly used to represent integer • long is commonly used to represent integer outside range of int • For real numbers, double is commonly more preferable Object Oriented Programming 1/3

Initialization and Assignment Operator Initialization Assignment IS NOT mathematical equality Iksan Bukhori Object Oriented Programming 1/4

Memory Diagram: Variable Initialization Iksan Bukhori Object Oriented Programming 1/5

Numerical Data Declaration Vs Object Declaration Iksan Bukhori Object Oriented Programming 1/6

Numerical Data Declaration Vs Object Declaration Primitive Data Types VS Reference Data Types Iksan Bukhori Object Oriented Programming 1/7

Arithmetic Expressions Iksan Bukhori Object Oriented Programming 1/8

Precedence Rules Iksan Bukhori Object Oriented Programming 1/9

Arithmetic Promotion Rules Implicit Typecasting Explicit Iksan Bukhori Object Oriented Programming 1/10

Explicit Typecasting Syntax: Ex: Iksan Bukhori Object Oriented Programming 1/11

Shorthand Assignments Iksan Bukhori Object Oriented Programming 1/12

Constants Good Practice: Naming Convention • All Caps • Words separated by underscores Iksan Bukhori Object Oriented Programming 1/13

Literal Constant Integer Long Float Double Iksan Bukhori Object Oriented Programming 1/14

Iksan Bukhori Object Oriented Programming 1/15

Displaying Numerical Values Operator Overloading TRY: How to obtain result 3? Iksan Bukhori Object Oriented Programming 1/16

Decimal. Format Class Decimal. Format Iksan Bukhori Format variable to have three decimal points Object Oriented Programming 1/17

Control Characters New line Tab Iksan Bukhori Object Oriented Programming 1/18

Getting Numerical Input (Scanner Class) Iksan Bukhori Object Oriented Programming 1/19

Example: Single line input Iksan Bukhori Object Oriented Programming 1/20

Example: Input Buffering Try to input all three integers at one before hitting ENTER Iksan Bukhori Object Oriented Programming 1/21

Example: Implicit Conversion Try to input integer instead of double Iksan Bukhori Object Oriented Programming 1/22

Bad Example: Implicit Conversion Iksan Bukhori Object Oriented Programming 1/23

Example: Numerical+String Try to input horse name with single and more words Iksan Bukhori Object Oriented Programming 1/24

Math Class Ex: Iksan Bukhori Object Oriented Programming 1/25

Methods of Math Class Iksan Bukhori Object Oriented Programming 1/26

Methods of Math Class Iksan Bukhori Object Oriented Programming 1/27

Homework 1. Write a program that accepts the unit weight of a bag of coffee in pounds and the number of bags sold and displays the total price of the sale, computed as total. Price = unit. Weight * number. Of. Units * 5. 99; total. Price. With. Tax = total. Price + total. Price * 0. 0725; where 5. 99 is the cost per pound and 0. 0725 is the sales tax. Display the result in the following manner: Iksan Bukhori Object Oriented Programming 1/28

Homework Iksan Bukhori Object Oriented Programming 1/29

Homework 3. Development Exercise. My. Java Coffee Outlet runs a catalog business. It sells only one type of coffee beans, harvested exclusively in the remote area of Irian Jaya. The company sells the coffee in 2 -lb bags only, and the price of a single 2 -lb bag is $5. 50. When a customer places an order, the company ships the order in boxes. The boxes come in three sizes: the large box holds 20 bags of 2 lb, the medium 10 bags, and the small 5 bags. The cost of a large box is $1. 80; a medium box, $1. 00; and a small box, $0. 60. The order is shipped in the least expensive manner. For example, the order of 52 bags will be shipped in four boxes: two large, one medium, and one small. The rule for packing is to fill the large and medium boxes completely; that is, the box is fully packed. Only the small boxes can have empty spaces. For example, to ship 52 bags, you could have used 3 large boxes, but that would leave third box not fully packed. Develop a program that computes the total cost of an order. Display the output in the following format: Iksan Bukhori Object Oriented Programming 1/30

Next Week • Defining Your Own Class (Part 1) Iksan Bukhori Object Oriented Programming 1/31
- Slides: 31