Programming in Java

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

𝑓(𝑥)

PROGRAMMING IN JAVA
The Math library (and more on numbers)
Primitive data types in Java
Data Type Size Description Examples of values
(1 byte = 8 bits)
byte 1 byte Whole numbers from -128 to 127 ‘A’, ‘b’, ‘1’ , ‘%’, ‘\n’
short 2 bytes Whole numbers from -32,768 to 32,767 -32,768, -10, 0, 1,
2,022, 32,767
int 4 bytes Whole numbers from -2,147,483,648 to -1,882,745, -10, 0, 1,
2,147,483,647 2,022, 32,768
long 8 bytes Whole numbers from - -1,882,745, -10, 0, 1,
9,223,372,036,854,775,808 to 2,022, 2,147,483,648
9,223,372,036,854,775,807
float 4 bytes Fractional numbers of up to 6 to 7 decimal digits 3.14159
double 8 bytes Fractional numbers of up to 15 decimal digits 3.1415926535 8979

boolean 1 bit A boolean has one of two possible values: true, false

char 2 bytes Stores a single Unicode character/letter or ASCII ‘a’, ‘\u0061’


values
Some
methods
in the
Math
library
Calling a library method
Type conversion:
Methods to convert Strings to numbers
Type
conversion:
Some explicit
and implicit
number type
conversions
Math.random: A practical example

Exercise
Write a program to generate a random whole number between 0
and N-1, with N being an integer obtained as an input argument
through the command line (i.e., the terminal window, or console).
Math.random: A practical example
Exercise 1 (5 points)

Exercise 2 (10 points)


Exercise 3 (10 points)

Exercise 4 (2 points)

Exercise 5 (3 points)

Exercise 6 (5 points)
Can we store irrational numbers on a computer? Why?
Exercise 7 (3 points)

Exercise 8 (7 points)
Exercise 9 (5 points)
Exercise 10 (1 point)

Exercise 11 (4 points)
Exercise 12 (5 points)

Write a program that takes two double values, 𝒙 and 𝒚, as input


arguments, and prints out the distance of (𝒙, 𝒚) to the origin of
coordinates.

Exercise 13 (5 points)
Can we store complex numbers on a computer?

You might also like