17 views

Uploaded by api-307093335

- bpj lesson 8
- bpj 26 lesson and exercise
- bpj lesson 25 and project
- bpj lesson 24 and projects
- bpj lesson 4
- bpj lesson 22
- bpj lesson 23
- bpj lesson 21
- bpj lesson 20
- bpj lesson 19 and exercise
- bpj lesson 18
- bpj lesson 14
- bpj lesson 13
- bpj lesson 12
- bpj lesson 11
- bpj lesson 10
- bpj lesson 9
- bpj lesson 7
- bpj lesson 17
- project 15 - overdrawn at the bank

You are on page 1of 4

So far we have looked mostly at simple cases in which all the numbers involved in a

calculation were either

integers or

. Here, we will see what happens when we

these types in calculations.

Here is an important principle to remember: Java

normally store information in a

variable if in doing so it would

information. Consider the following two examples:

1. An example of when we would

information:

double d = 29.78;

int i = d; //

since i is an integer and it would have to chop-off

// the .78 and store just 29 in i.thus, it would

information.

There is a way to make the above code work. We can

compilation and

therefore result in 29.78 being stored in as follows (actually, just 29 is stored

since can only hold integers):

int i = (int)d; //(int)

lose information:

int j = 105;

double d = j; //legal, because no information is lost in storing 105 in the

// double variable d.

In a math operation involving

, the result is given in terms of the

of those two typesas in the following example:

int i = 4;

double d = 3;

double ans = i/d; //ans will be 1.33333333333333the result is double precision

20 + 5 * 6.0 returns a

. The 6.0 might look like an integer to us, but

because its written with a decimal point, it is considered to be a floating point

numbera

.

What does 3 + 5.0/2 + 5 * 2 3 return?

What does 3.0 + 5/2 + 5 * 2 3 return?

What does (int)(3.0 + 4)/(1 + 4.0) * 2 3 return?

Consider the following two examples that are very similarbut have different

answers:

1.25

1.0

Constants:

cannot be

changed

final

String int

j+= x;

to j = j + x;

implied cast

j+= x;

j

int

MixedResults

Tester

Tester

main

Problem

Problem

Problem

Problem

Problem

1:

2:

3:

4:

5:

1.0

39.13333333333333

-1345.39999999999

-1337

-1313

Exercise on Lesson 5

E

NUM_STUDENTS

main

double f4 = 22;

j

d

i

j

double

- bpj lesson 8Uploaded byapi-307093335
- bpj 26 lesson and exerciseUploaded byapi-307093335
- bpj lesson 25 and projectUploaded byapi-307093335
- bpj lesson 24 and projectsUploaded byapi-307093335
- bpj lesson 4Uploaded byapi-307093335
- bpj lesson 22Uploaded byapi-307093335
- bpj lesson 23Uploaded byapi-307093335
- bpj lesson 21Uploaded byapi-307093335
- bpj lesson 20Uploaded byapi-307093335
- bpj lesson 19 and exerciseUploaded byapi-307093335
- bpj lesson 18Uploaded byapi-307093335
- bpj lesson 14Uploaded byapi-307093335
- bpj lesson 13Uploaded byapi-307093335
- bpj lesson 12Uploaded byapi-307093335
- bpj lesson 11Uploaded byapi-307093335
- bpj lesson 10Uploaded byapi-307093335
- bpj lesson 9Uploaded byapi-307093335
- bpj lesson 7Uploaded byapi-307093335
- bpj lesson 17Uploaded byapi-307093335
- project 15 - overdrawn at the bankUploaded byapi-307093335
- bpj lesson 16Uploaded byapi-307093335
- bpj lesson 6Uploaded byapi-307093335
- bpj lesson 3Uploaded byapi-307093335
- bpj lesson 2Uploaded byapi-307093335
- bpj lesson 1Uploaded byapi-307093335