06.09.2021 Views

Java with BlueJ, 2016a

Java with BlueJ, 2016a

Java with BlueJ, 2016a

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

28 CHAPTER 2. BASICS<br />

The output from the above is:<br />

Default Decimal Data Type<br />

When a numeric literal (such as 100.25) appears in a program and has a<br />

decimal point, the data type used is double; wesaythatdouble is the default<br />

data type for values <strong>with</strong> a decimal point. If the programmer wanted<br />

to use a float value then the suffix f would be used, as in 100.25f.<br />

To a non-programmer literals such as 100.0 and 100 may seem the same,<br />

but a <strong>Java</strong> programmer knows the first is represented internally as a double<br />

and the second is represented as an int.<br />

We say that double and int are the default numeric data types. We focus<br />

on these numeric data types for the rest of this text.<br />

Exercises<br />

8. Modify the program in Listing 2.4 to calculate and display fuel consumption<br />

as the number of kilometres travelled per litre of fuel.<br />

9. Write a program that converts a value in centimetres to an equivalent<br />

value in inches. Use the conversion: one inch equals 2.54 centimetres.<br />

Use variables of type double.<br />

10. Write a program that converts a value in dollars to an equivalent value<br />

in euros. Use the conversion: one euro equals 1.5 dollars. Use variables<br />

of type double.<br />

11. Modify Listing 2.5 to run some different calculations, such as:<br />

System.out.println(100.5-100.00);<br />

System.out.println(100.33-100.00);

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!