1) Write a method that returns a random day of the week
```java
String [] days = { "Monday", "Tuesday", "Wednesday", "Thursday",
"Friday", "Saturday", "Sunday" };
int random = (int)(Math.random()*days.length);
System.out.println(days[random]);
```
2) Write a method that accepts an integer as a parameter and returns a String saying if the parameter is odd or even
```java
String oddOrEven(int i)
{
return (i%2 == 0)?"Even":"Odd";
}
```
3) 1, 2, 4, 8, 16 … are all powers of 2. Print out the first 30 numbers in this sequence.
```java
for(int i =0; i<30; i++)
{
System.out.println(Math.pow(2,i));
}
```
4) 1, 2, 4, 8, 16 … are all powers of 2. Write a method that checks if a number is a power of 2 and returns true or false
```java
boolean powerOfTwo(int i)
{
//using a bitwise AND...
return ((i & (i-1)) == 0) ? true : false;
}
```
5) 1, 8, 27, 81, 243 … are all powers of 3. Write a method that checks if a number is a power of 3 and returns true or false
```java
boolean powerOfThree(double d)
{
while(d >= 3)
{
d = d/3;
}
return d == 1;
}
```
6) The first few numbers of the Fibonacci sequence are 1, 1, 2, 3, 5, 8, 13, 21, … Note that the next number in the sequence is the sum of the last two numbers in the sequence. Use a for loop to print out the first 100 fibonacci numbers
```java
System.out.print("1, 1, ");
int fibp = 1;
int fibq = 1;
int fib;
for(int i = 1; i <29; i++)
{
fib = fibp + fibq;
fibp = fibq;
fibq = fib;
System.out.print(fib + ", ");
}
```
7) Take a number n. If n is even, divide it by 2 to get n / 2. If n is odd, multiply it by 3 and add 1 to obtain 3n + 1. Repeat the process indefinitely. The Collatz Conjecture states that you will always reach 1. Write a method that accepts a number n as input and outputs all the numbers in the sequence, terminating when it reaches 1.
```java
void collatz(int i)
{
while (i != 1)
{
if (i%2 == 0)
{
i = i / 2;
}
else
{
i = 3*i +1;
}
System.out.print(i + ", ");
}
System.out.println("");
}
```