Early seed award

  • Write a sample binary addition 1 + 1 = 10
  • Have Java Code cell on screen at start of lecture
  • Don't tell anyone
int first = 1010;
int second = 1000;
String firstString = String.valueOf(first);
String secondString = String.valueOf(second);
int carryover = 0;
String result = "";
for (int i = firstString.length() - 1; i >= 0; i--) {
    String firstChar = String.valueOf(firstString.charAt(i));
    String secondChar = String.valueOf(secondString.charAt(i));
    int numOfOnes = 0;
    if (firstChar.equals("1")) {
        numOfOnes++;
    }
    if (secondChar.equals("1")) {
        numOfOnes++;
    }
    if (carryover == 1) {
        numOfOnes++;
    }
    if (numOfOnes % 2 == 0) {
        result = "0" + result;
    } else {
        result = "1" + result;
    }
    if (numOfOnes >= 2) {
        carryover = 1;
    }
}
if (carryover == 1) {
    result = "1" + result;
}
System.out.println(result);
10010

Start with some small code exercises

  • Write a Jupyter notebook code example on the following primitive types with a code example (4 to 5 lines), preference would be using array and methods like substring and random as applicable: int, double, boolean, char.

int

Random rand = new Random();
int first = rand.nextInt();
int second = rand.nextInt();
int result = rand.nextInt();
System.out.println(result);
-138056936

double

Random rand = new Random();
double first = rand.nextDouble();
double second = rand.nextDouble();
double third = rand.nextDouble();
System.out.println(first + second - third);
0.8047577767924244

boolean

boolean[] array = new boolean[5];
for (int i = 0; i < 5; i++) {
    boolean isEven = i % 2 == 0;
    array[i] = isEven;
    System.out.println(array[i]);
}
true
false
true
false
true

char

String start = "start";
for (int i = 0; i<start.length(); i++) {
    if(String.valueOf(start.charAt(i)).equals("t")) {
        char letterBefore = start.charAt(i - 1);
        System.out.println(letterBefore);
    }
}
s
r
  • Now convert each of the examples to corresponding Wrapper classes, using arrays.
  • Expression of these in Class or PBL forms is an option. But the review must be easy for me to see work.

int

Random rand = new Random();
Integer first = rand.nextInt();
Integer second = rand.nextInt();
Integer result = rand.nextInt();
System.out.println(result);
-562278610

double

Random rand = new Random();
Double first = rand.nextDouble();
Double second = rand.nextDouble();
Double third = rand.nextDouble();
System.out.println(first + second - third);
-0.7301652696781806

boolean

ArrayList<Boolean> array = new ArrayList<Boolean>();
for (int i = 0; i < 5; i++) {
    Boolean isEven = i % 2 == 0;
    array.add(isEven);
    System.out.println(array.get(i));
}
true
false
true
false
true

char

String start = "start";
for (int i = 0; i<start.length(); i++) {
    if(String.valueOf(start.charAt(i)).equals("t")) {
        Character letterBefore = start.charAt(i - 1);
        System.out.println(letterBefore);
    }
}
s
r