Primatives

// 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]);
}
1
4
6
5
3
// 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);
7.301043248071254
3.9008856742247797
9.461633641089941
6.668201382560991
0.853542092221744
true
//bool

boolean tof = true;

if (tof) {
    System.out.println("bool is true");
}
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);
hello

Wrapper Class

//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]);
}
2
5
7
4
3
// 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);
7.5698481641159185
2.086576518913615
8.956948380868376
7.035930903235372
2.0862771279196535
false
//Boolean

Boolean b1 = Boolean.TRUE;
Boolean b2 = Boolean.FALSE;
Boolean b3 = true;

System.out.println(b1);
System.out.println(b2);
System.out.println(b3);
true
false
true
//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);
hello

Methods and Control Structures

Methods take in an input and output a value. They can be called from other parts of the code.

Control Structures allow users to specify what actions occur in what conditions. Some examples inclue for loops and if else statements.

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.

Math.random

  • Math.random() * 10, random numbers between 0 and 10 (not includng 10)
  • returns double
  • if you want it in int add (int) to the front of Math.random()

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
  • 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.