## 2. Number and String Conversion Functions

### Converting Strings to Numbers

Because we cannot cast from strings to numbers, we must use these functions:

```public class MyProgram {
public static void main(String[] args) {
int a = Integer.parseInt("1"); //converts "1" to 1
double b = Double.parseDouble("3.14"); //converts "3.14" to 3.14
//There is also:
//Byte.parseByte
//Short.parseShort
//Long.parseLong
//Float.parseFloat
//Boolean.parseBoolean

//we will learn the charAt method for getting individual char types
}
}
```

Note that these functions will throw errors if the string cannot be parsed. For example, Integer.parseInt("Hello") will throw a NumberFormatException .

### Converting non-decimal Strings to Numbers

The parseInt and similar functions also can take two inputs ("arguments","parameters"). The second input is the base or "radix". For example, we can use this to parse numbers in binary, octal, hex, or any other base:

```public class MyProgram {
public static void main(String[] args) {
//These are all 42:
System.out.println(Integer.parseInt("101010", 2));
System.out.println(Integer.parseInt("52", 8));
System.out.println(Integer.parseInt("2A", 16));
System.out.println(Integer.parseInt("33", 13));
}
}
```

### Converting from Numbers to Strings in a certain base:

We will learn later that you can concatenate strings with numbers, so that "x"+123 becomes "x123", and ""+123 becomes "123". However, there are also toString functions that take a number and an optional base ("radix"):

```public class MyProgram {
public static void main(String[] args) {
System.out.println(Integer.toString(42, 2));  //101010
System.out.println(Integer.toString(42, 8));  //52
System.out.println(Integer.toString(42, 13)); //33
System.out.println(Integer.toString(42, 16)); //2A

String a = Double.toString(42.0);    //42.0
String b = Float.toString(42.0f);    //42.0
System.out.println(a);
System.out.println(b);

System.out.println(Integer.toString(6*9, 13)); //6*9 is 42 in base 13
}
}
```

## 3. Random Functions

### Generating a random double from 0 to 1

The Math.random() function returns a number from 0 to 1:

```public class MyProgram {
public static void main(String[] args) {
double x = Math.random();
System.out.println(x);
}
}
```

### Generating a random double from 0 to n

Multiply Math.random() by n to get a number between 0 to n:

```public class MyProgram {
public static void main(String[] args) {
int n = 10;
double x = Math.random()*n;
System.out.println(x);
}
}
```

### Generating a random integer from 0 to n-1

Cast the previous example to an int for an integer. Note that because casts truncate, the number will be from 0 to n-1 (n is excluded)

```public class MyProgram {
public static void main(String[] args) {
int n = 10;
int x = (int)(Math.random()*n);
System.out.println(x);
}
}
```

Advanced users should also note there is a nextInt function on the Random class.

### Generating a random integer from a to b

To get a set of numbers from a to b (inclusive), we need to first multiply a random number by the range (b-a+1). Then we need to add a to it and cast the result to an int:

```public class MyProgram {
public static void main(String[] args) {
int a = 5;
int b = 10;
int x = (int)(Math.random()*(b-a+1)+a);
System.out.println(x);
}
}
```

### SecureRandom

Advanced programmers should note that the numbers generated by Math.random() and the Random class are not cryptographically secure. Use SecureRandom if you are using random numbers for encryption, hashing, etc.

## 4. Arrays Functions

### Arrays.toString

Sometimes it is convenient to print an array. It is not enough to do System.out.println(theArray). Instead, use Arrays.toString:

```import java.util.Arrays; //Needed to use the Arrays functions
public class MyProgram {
public static void main(String[] args) {
String[] food = {"apple","banana","carrot"};
System.out.println(Arrays.toString(food));

//prints [apple, banana, carrot]
}
}
```

### Arrays.sort

Although we have covered sorting algorithms, in most cases you should not write your own sort. Instead, the built in Arrays.sort (and later on Collections.sort) automatically pick an efficient algorithm for the input:

import java.util.Arrays; //Needed to use the Arrays functions public class MyProgram { public static void main(String[] args) { int[] x = {4,3,5,2,6,1}; System.out.println(Arrays.toString(x)); Arrays.sort(x); System.out.println(Arrays.toString(x)); //Outputs: //[4, 3, 5, 2, 6, 1] //[1, 2, 3, 4, 5, 6] } }

Note that Strings are sorted according to their natural ordering. This means that lower case letters are sorted to be after upper case letters.

### Arrays.copyOf

Due to the way arrays work, if you have int[] x = {1,2,3}, you cannot copy it with int[] y = x. Instead, you need to copy the internal values, and we will learn more about this when we learn about Classes and References. Also, there are many Arrays.copyOf and copyOfRange functions. We will show one example here:

import java.util.Arrays; //Needed to use the Arrays functions public class MyProgram { public static void main(String[] args) { int[] x = {4,3,5,2,6,1}; System.out.println(Arrays.toString(x)); int[] y = Arrays.copyOf(x, x.length); System.out.println(Arrays.toString(y)); } }

## 1. Math Functions

### How to use functions

Functions are blocks of code that can be written once and run from various parts of your program. In fact, System.out.println() was a function. You can identify that something is a function by the fact that it uses parenthesis in Java. To use a function, type the function name, and supply inputs:

```public class MyProgram {
public static void main(String[] args) {
//Run the println function with "Hello World" as an input
System.out.println("Hello World");

Math.abs(-5); //Runs the Math.abs function with the input -5
//Notice that this does not output anything
}
}
```

### Using functions that return values

Many functions return a value, which means that the function can be used as part of an expression. Just like 1+2 is the int 3, Math.abs(-3) performs absolute value and evaluates to the int 3. Thus, you can use Math.abs() as part of a more complex expression:

```public class MyProgram {
public static void main(String[] args) {
int x = -5;
int positiveTimesTen = Math.abs(x)*10;
System.out.println(positiveTimesTen); //prints 50

System.out.println(Math.abs(-42));    //prints 42
}
}
```

### Common Math functions

`Math.abs` is absolute value

`Math.ceil` rounds to the more positive integer

`Math.floor` rounds to the more negative integer

`Math.round` rounds to the closest integer

`Math.sqrt` takes one number and returns the square root

`Math.pow` takes two doubles, and returns the first to the power of the second

`Math.log10` takes one double, and returns the logarithm of that number in base 10

`Math.log` takes one double, and returns the logarithm of that number in base e

### Trig functions

`Math.sin` returns the sine of the double argument. The argument should be in radians, not degrees.

`Math.cos` returns the cosine of the double argument. The argument should be in radians, not degrees.

`Math.tan` returns the tangent of the double argument. The argument should be in radians, not degrees.

`Math.asin` returns the arc sine (inverse function) of the double argument. The return value is between -PI/2 and PI/2 radians.

`Math.acos` returns the arc cosine (inverse function) of the double argument. The return value is between 0 and PI radians.

`Math.atan` returns the arc tangent (inverse function) of the double argument. The return value is between -PI/2 and PI/2 radians.

`Math.atan2` returns the arc tangent (inverse function) of the two arguments, y and x. This will return an angle that spans all of 2PI, which is useful for getting objects to point at each other

`Math.toDegrees` converts an angle in radians (the argument) to degrees (the return value)

`Math.toRadians` converts an angle in degrees (the argument) to radians (the return value)

### Math constants

The doubles Math.PI and Math.E can be used as approximate values of pi and e.