43
1 Chapter 3 Fundamental Data Types Number Types (3.1) Assignment (3.2) Constants (3.3) Arithmetic and Mathematical Functions (3.4) Calling Static methods (3.5) Quality Tips 3.1, 3.2 Advanced Topics 3.1, 3.3 Common Error 3.1, 3.2

Chapter 3

Embed Size (px)

DESCRIPTION

Chapter 3. Fundamental Data Types Number Types (3.1) Assignment (3.2) Constants (3.3) Arithmetic and Mathematical Functions (3.4) Calling Static methods (3.5) Quality Tips 3.1, 3.2 Advanced Topics 3.1, 3.3 Common Error 3.1, 3.2. Java primitive types - examples. int - PowerPoint PPT Presentation

Citation preview

Page 1: Chapter 3

1

Chapter 3

Fundamental Data TypesNumber Types (3.1)

Assignment (3.2)Constants (3.3)

Arithmetic and Mathematical Functions (3.4)Calling Static methods (3.5)

Quality Tips 3.1, 3.2Advanced Topics 3.1, 3.3Common Error 3.1, 3.2

Page 2: Chapter 3

2

Java primitive types - examples

int 3 4 -700 32767 -21211 0

double3.456 -3456.789 0.6 0.0

char'd' 'a' '*'

booleantrue false

Page 3: Chapter 3

3

Numbers

Integers•int type denotes integers.

• Integers can have a range of -2,147,483,648 to 2,147,483,647. • Remember, an int is represented using 32 bits.

• If you need to handle integral values beyond that range, use long (64 bits).

Page 4: Chapter 3

4

Numbers

Floating-point numbers•double type denotes floating point numbers.

• go to about 10300. • Remember, an double is represented using 64

bits.

•double numbers suffer from precision problems. They store only about 15 significant digits.

Page 5: Chapter 3

5

Precision Problem:

double originalPrice = 3E14;(3*1014)300,000,000,000,000.00

double discountedPrice = originalPrice – 0.05;299,999,999,999,999.95

double discount = originalPrice - discountedPrice;

System.out.println(discount); //Should print 0.05, prints 0.0625

Page 6: Chapter 3

6

Arithmetic Operations

addition + subtraction - multiplication * division / remainder % (integer operation)

• 7 % 3 = 1

• 1 % 3 = 1

• 8 % 4 = 0

Page 7: Chapter 3

7

Rules of Arithmetic:

2 + 3 = 5 2 + 3.0 = 5.0 2.0 + 3 = 5.0 2.0 + 3.0 = 5.0 7.0 / 2.0 = 3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Chapter 3

12

Rules of Arithmetic:

2 + 3 = 5 2 + 3.0 = 5.0 2.0 + 3 = 5.0 2.0 + 3.0 = 5.0 7.0 / 2.0 = 3.5 7 / 2 = 3 3 / 4 = 0 3.0 / 4 = 0.75

5 % 2 + 6 = 7 1 + 2 * 3 / 5 = 2 6 / 7 * 4 = 0 6.0 / 2.0 * 5 = 15.0 1 - 8 / 3 = -1 4 + 4 / 4 % 4 * 4 =

Page 13: Chapter 3

13

Rules of Arithmetic:

2 + 3 = 5 2 + 3.0 = 5.0 2.0 + 3 = 5.0 2.0 + 3.0 = 5.0 7.0 / 2.0 = 3.5 7 / 2 = 3 3 / 4 = 0 3.0 / 4 = 0.75

5 % 2 + 6 = 7 1 + 2 * 3 / 5 = 2 6 / 7 * 4 = 0 6.0 / 2.0 * 5 = 15.0 1 - 8 / 3 = -1 4 + 4 / 4 % 4 * 4 = 8

Page 14: Chapter 3

14

Assignment

public Circle()

{diameter = 30;

xPosition = 20;

yPosition = 6;

color = "magenta";

isVisible = false;

} The = operator is called the assignment operator.

Page 15: Chapter 3

15

Assignment

On the left side of the assignment operator you must have a variable name.

On the right side of the assignment operator you may have a single value or an expression.•diameter = 30;

•balance = balance + howMuch;

•circumference = 3.14 * diameter;

Page 16: Chapter 3

16

Assignment

• Arithmetic operations can be combined with the assignment operator.•balance += howMuch;•balance -= howMuch;

• Operations combined with assignment+=

-=

*=

/=

%=

Page 17: Chapter 3

17

Increment/Decrement Operators

int count = 0;

count++; //count is now 1;

count += 3; //count is now 4;

count--; //count is now 3;

Page 18: Chapter 3

18

Operator Order of Precedence

++ -- * / % + - = *= /= %= += -=

Page 19: Chapter 3

19

Constants

A final variable is a constant. Once its value is set, it cannot be changed.

final variables are named with all capital letters.

Do not use magic numbers in your programs. If you have a need to do this, rethink….should this be a final variable?

Page 20: Chapter 3

20

Circle class

Let's add•findCircumference()•findArea()

Page 21: Chapter 3

21

Examplepublic class Circle{ // Constructor goes here

// Returns the circumference of the Circlepublic double findCircumference(){ //code goes here

}

// Returns the area of the Circlepublic double findArea(){ //code goes here

}

Page 22: Chapter 3

22

Examplepublic class Circle{ // Other code goes here

// Returns the circumference of the Circlepublic double findCircumference(){ return 3.14 * diameter; //magic number

}

// Returns the area of the Circlepublic double findArea(){

double radius = diamter/2; return 3.14 * radius * radius; //magic number

}

}

Page 23: Chapter 3

23

Examplepublic class Circle{

// Other code goes here

// Returns the circumference of the Circlepublic double findCircumference(){ return PI * diameter; //magic number replaced

}

// Returns the area of the Circlepublic double findArea(){ double radius = diamter/2; return PI * radius * radius; //magic number replaced

} private static final double PI = 3.1415926;

//private instance fields declared here}

Page 24: Chapter 3

24

Other useful constants

TAX_RATE DIME_VALUE QUARTER_VALUE PENNIES_PER_DOLLAR HOURS_IN_DAY MINUTES_IN_HOUR MAX_SEATS

Page 25: Chapter 3

25

the Math class

Java provides certain math finctions for us. The Math class contains methods that can be very useful (page 95 of your text).

Page 26: Chapter 3

26

Useful methods in Math classMethod Returns

Math.sqrt(x) square root of x (>=0)

Math.pow(x,y) xy

Math.abs(x) absolute value of x

Math.max(x,y) The larger of x and y

Math.min(x,y) The smaller of x and y

Math.ceil(x) Smallest integer >= x

Math.floor(x) Largest integerr <= x

Page 27: Chapter 3

27

All methods in the Math class

Our first look at the API !

Page 28: Chapter 3

28

Static methods

The Math class contains static methods. This means that the method does not

operate on a particular object. You do not instantiate an object of the Math class in order to use the methods of the Math class.

You invoke the square root method by•Math.sqrt(4)

Page 29: Chapter 3

29

The quadratic formula

2 4

2

b b acx

a

Page 30: Chapter 3

30

The quadratic formula

2 4

2

b b acx

a

Write assignment statements that will return the two roots, x1 and x2.

Page 31: Chapter 3

31

Answers

x1 = (-b + Math.sqrt(b*b – 4*a*c))/(2*a);

x2 = (-b - Math.sqrt(b*b – 4*a*c))/(2*a);

Page 32: Chapter 3

32

A better way…

double root = Math.sqrt(b * b – 4 * a * c);

x1 = (-b + root) / (2*a);

x2 = (-b - root) / (2*a);

Why is this way "better"?

Page 33: Chapter 3

33

The Math class

Java API – look at field summary. How does this change our findCircumference and findArea methods in out Circle class?

Page 34: Chapter 3

34

Examplepublic class Circle{

// Other code goes here

// Returns the circumference of the Circlepublic double findCircumference(){ return PI * diameter; //magic number replaced

}

// Returns the area of the Circlepublic double findArea(){ double radius = diamter/2; return PI * radius * radius; //magic number replaced

} private static final double PI = 3.1415926;

//private instance fields declared here}

Page 35: Chapter 3

35

Examplepublic class Circle{

// Other code goes here

// Returns the circumference of the Circlepublic double findCircumference(){ return Math.PI * diameter; //final variable replaced

}

// Returns the area of the Circlepublic double findArea(){ double radius = diamter/2; return Math.PI * radius * radius; //final variable replaced

}

//private instance fields declared here}

Page 36: Chapter 3

36

Our BankAccount Class

Let's add a method computeInterest().•computeInterest will increase our current

balance by 2.5%.

Let's add a method chargeServiceFee().•chargeServiceFee()will deduct $9.95 from our

balance.

Page 37: Chapter 3

37

Reminders about naming conventions A variable name must be a legal identifier and not a

keyword (double, int, new, etc.) or null.

By Convention :  Variable names begin with a lowercase letter, and class names begin with an uppercase letter. If a variable name consists of more than one word, the words are joined together, and each word after the first begins with an uppercase letter, like this: isVisible. The underscore character (_) is used to separate words in constants(final variables are named with all caps).

Page 38: Chapter 3

38

Reminders about object variables and object references

Circle sun;• defines an object variable or type Circle.

new Circle(); • constructs a Circle object.

Circle otherSun = new Circle();• constructs a Circle object and assigns it to the

object variable otherSun.

Page 39: Chapter 3

39

Reminders aboutmethods and parameters

Circle sun(30,10,40,"yellow");• 30, 10, 40, "yellow" are construction parameters (provide information for

the construction of the object). b1.deposit(100);

• 100 is a value passed to the method deposit. deposit (double howMuch);

• howMuch is a parameter of the method deposit. In a method definition, each parameter has a type and a name. The

parameters are separated by commas. In a method call, the values passed must be consistent with the

parameters listed in the method header. A method definition specifies the access modifier, the return type,

the method name, the parameters, and the statements for performing the task.

Page 40: Chapter 3

40

Reminders about errors A compile-time error

• a syntax error is a violation of the rules of the language• missing semicolon• misspelled identifier• missing { or }

A runtime error • division by 0;• NullPointerException (didn't initialize an object field)

A logic error (an error of intent?)• program executes but performs a task that was not

intended by the programmer.

Page 41: Chapter 3

41

Reminders about types

Primitive types• int

• double

• char

• boolean

Everything else is a reference

Page 42: Chapter 3

42

Number variables versesObject variables

Number variables hold values:

diameter = 30;

anotherNumber = diameter

diameter = 40;

Changing diameter DOES NOT affect the value of anotherNumber.

30 30

diameter anotherNumber

Page 43: Chapter 3

43

Number variables versesObject variables

Object variables hold references:

BankAccount b1 = new BalkAccount(1000);

BankAccount b2 = b1

b1.deposit(200);

Changing b1 DOES affect the b2.

balance = 1000b1

b2