Chapter 3. Numerical Data. Objectives. Understand n umerical data type . Write arithmetic expressions in Java. Evaluate arithmetic expressions using the precedence rules. Describe how the memory allocation works for objects and primitive data values. Learn to use standard classes
Data Types supported by Java:
IEEE 754 float-point single precision layout
Space in the literal
string“ //err! string can not across multilines
// from two string literals
cf: we use octal literal 036 to represent int number 0x1e(= 30) but use ‘\36’ or ‘\036’ to represent char ‘\u001e’
in character and string literals.
Notes:Unlike C, in java
^= %= <<= >>= >>>=
x + y
int x, y;
int x;int y;
int x;int v, w, y;
int i, j, k;// default = 0 or no value
float numberOne, numberTwo;
The six data types differ in the precision of values they can store in memory.
<variable> = <expression> ;
sum = firstNumber + secondNumber;avg = (one + two + three) / 3.0;
This is an integer division where the fractional part is truncated.
x + 3 * y
get evaluated? Answer: x is added to 3*y.
int a = 57; b = 16 ;
c = a%b;
x * y
The answer is float.
A higher precision value cannot be assigned to a lower precision variable.Implicit Type Casting
double x = 3 + 5;
int x = 3.5;
These are constants, also called named constant.
These are called literal constant.Constants
final double PI = 3.14159;final int MONTH_IN_YEAR = 12;final short FARADAY_CONSTANT = 23060;
A. The variable is allocated in memory.
B. The reference to the new object is assigned to customer.
C. The reference to another object overwrites the reference in customer.Assigning Objects
customer = new Customer( );
customer = new Customer( );
State of Memory
//code fragment to input radius and output
//area and circumference
double radius, area, circumference;
StringradiusStr = JOptionPane.showInputDialog(
null, "Enter radius: " );
radius = Double.parseDouble(radiusStr);
//compute area and circumference
area = PI * radius * radius;
circumference = 2.0 * PI * radius;
"Given Radius: " + radius + "\n" +
"Area: " + area + "\n" +
"Circumference: " + circumference);
output = “test” + 1 + 2;
output = 1 + 2 + “test”;
System.out.print( “Hello, Dr. Caffeine.” );
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();
import static java.lang.Math.*;
// then all “Math” name can be skipped!
alphaRad = toRadians(alpha);
betaRad = toRadians(beta);
Write a loan calculator program that computes both monthly and total payments for a given loan amount, annual interest rate, and loan period.
We will develop this program in four steps:
Program source file is too big to list here. From now on, we ask
you to view the source files using your Java IDE.
-> month_rate = log(1+year_rate) /log 12 -1.
instead of year_rate /12.
Source File: Ch3LoanCalculator.java
Purchase Price: $ 34.80
Amount Tendered: $ 40.00
Your change is : $ 5.20
5 one-dollar bill(s)
Thank you for your business. Com back soon.