Data Types Hacks
- Primatives
- Wrapper Class
- Methods and Control Structures
- Math.random
- DoNothingByValue
- IntByReference
- Menu
// int
int[] intArray = new int[5];
for (int i=0; i<intArray.length; i++) {
intArray[i] = (int) (Math.random() *10);
System.out.println(intArray[i]);
}
// double
double[] doubleArray = new double[5];
for (int i=0; i<intArray.length; i++) {
doubleArray[i] = (Math.random() *10);
System.out.println(doubleArray[i]);
}
double d1 = 1.23;
double d2 = 1.23;
System.out.println(d1==d2);
//bool
boolean tof = true;
if (tof) {
System.out.println("bool is true");
}
//char
char letter = 'h';
char letter2 = 'e';
char letter3 = 'l';
char letter4 = 'o';
String word = "" + letter + letter2 + letter3 + letter3 + letter4;
System.out.println(word);
//Integer
Integer[] intArray = new Integer[5];
for (int i = 0; i < intArray.length; i++) {
intArray[i] = (int) (Math.random() * 10);
System.out.println(intArray[i]);
}
// double
double[] doubleArray = new double[5];
for (int i=0; i<intArray.length; i++) {
doubleArray[i] = (Math.random() *10);
System.out.println(doubleArray[i]);
}
Double d3 = 1.23;
Double d4 = 1.23;
System.out.println(d3==d4);
//Boolean
Boolean b1 = Boolean.TRUE;
Boolean b2 = Boolean.FALSE;
Boolean b3 = true;
System.out.println(b1);
System.out.println(b2);
System.out.println(b3);
//Character
Character letter = 'h';
Character letter2 = 'e';
Character letter3 = 'l';
Character letter4 = 'o';
String word = new StringBuilder().append(letter).append(letter2).append(letter3).append(letter3).append(letter4).toString();
System.out.println(word);
Diverse Arrays and Matrix include both methods and control strucutures.
For example, matrix includes the method String toString() and multiple for loops.
There are multiple data type examples in their code. Uses int and String.
DoNothingByValue
- Essentially this code shows the differenc between passing by value and by reference
- the main method calls multiple methods with different paramteter types
- integer array "arr" defined
- inter value "val" defined
- "word" defined as first 5 char of input string
- all elements of arr set to 0
- print arr elememnts through for loop
- changeIt essentially shows that passing parameters by value does not modify the original values of the variables. The method creates a new integer array of length 5, sets val to 0, and sets word to the first 5 characters of the input string. Then, it initializes all elements of the arr array to 0 and prints the array elements and word using a for loop.
- changeIt2 has the same purpose as changeIt. It creates a new integer array of length 5, sets value to 0, and sets name to the first 5 characters of the input string. Then, it initializes all elements of the nums array to 0 and prints the array elements and name using a for loop.
- changeIt3 and 4 both demonstrate that it is possible to change values by returning a new value. changeIt3 creates a new string that is the first 5 characters of the input string and initializes all elements of the arr array to 0. Then, it prints the array elements and the new string while changeIt4 creates a new integer array of length 5, sets the integer value to 0, and sets the string value to the first 5 characters of the input string. Then, it initializes all elements of the integer array to 0 and prints the array elements and the string.
IntByReference
- the method swapToLowHighOrder() basically swaps the value fiels of two objects if this.value > i.value
- the static swapper() method takes two integer arguments n0 and n1. This method creates two IntByReference objects, a and b, with n0 and n1 as their values, respectively. It then calls the swapToLowHighOrder() method on a, passing in b as an argument. Finally, it prints the values of a and b before and after the swap.
- main() calls sweeper 3 times with different values
Menu
- Try is literally used to catch errors in a block of code
- If there are errors in the try block, catch can catch the error and output an applicable error message to help debugging
- Runnable is used to store a class-method to be run when a title is selected from a menu. It will be executed when the correspinding MenuRow object is selected.