1 + 1 = 10 because in binary 10 or one-zero is 2.

import java.util.Scanner;
public class BinaryLong {
   public static void main(String[] args)
   {

    // Two added values to equal binary
    int a = 1;
    int b = 1;

    //Two variables
    int i = 0, carry = 0;

    //This is to hold the output binary number
    int[] sum = new int[10];

    int b1 = a;
    int b2 = b;

    //while the ints are not 0, code uses mod
    while (b1 != 0 || b2 != 0) 
    {
        sum[i++] = (int)((b1 % 10 + b2 % 10 + carry) % 2);
        carry = (int)((b1 % 10 + b2 % 10 + carry) / 2);
        b1 = b1 / 10;
        b2 = b2 / 10;
    }
    if (carry != 0) {
        sum[i++] = carry;
    }
    --i;
    System.out.print(a + " + " + b + " = ");
    while (i >= 0) {
        System.out.print(sum[i--]);
    }
    System.out.print("\n");
   }
}
BinaryLong.main(null);
1 + 1 = 10
import java.lang.Math;

public class BinaryShort {
 
    public static void main(String[] args) {

        int a = 0b1;
        int b = 0b1;

        int sum = a + b;
        System.out.println(Integer.toBinaryString(sum));

    }
}
BinaryShort.main(null);

Primitive Examples

import java.util.Random;

public class PrimitiveTypesExample {
    public static void main(String[] args) {
        // int example
        int[] numbers = {1, 2, 3, 4, 5};
        int randomNumber = new Random().nextInt(numbers.length);
        System.out.println("Random number from array: " + numbers[randomNumber]);

        // double example
        double pi = 3.141592653589793238;
        System.out.println("Pi: " + pi);

        // boolean example
        boolean isJavaFun = true;
        System.out.println("Is Java fun? " + isJavaFun);

        // char example
        String message = "Hello, world!";
        char firstLetter = message.charAt(0);
        System.out.println("First letter of message: " + firstLetter);
    }
}
PrimitiveTypesExample.main(null);
Random number from array: 2
Pi: 3.141592653589793
Is Java fun? true
First letter of message: H

Wrapper Class Examples

import java.util.Arrays;

public class PrimitiveTypesExample {
    public static void main(String[] args) {
        // int example with wrapper class
        Integer[] numbers = {1, 2, 3, 4, 5};
        int sum = Arrays.stream(numbers).mapToInt(Integer::intValue).sum();
        System.out.println("Sum of numbers: " + sum);

        // double example with wrapper class
        Double[] temperatures = {25.6, 28.9, 30.1, 23.4, 21.0};
        double average = Arrays.stream(temperatures).mapToDouble(Double::doubleValue).average().getAsDouble();
        System.out.println("Average temperature: " + average);

        // boolean example with wrapper class
        Boolean[] choices = {true, false, false, true};
        long countTrue = Arrays.stream(choices).filter(Boolean::booleanValue).count();
        System.out.println("Number of true choices: " + countTrue);

        // char example with wrapper class
        Character[] letters = {'H', 'e', 'l', 'l', 'o', ',', ' ', 'w', 'o', 'r', 'l', 'd', '!'};
        String message = Arrays.stream(letters).map(Object::toString).reduce("", String::concat);
        System.out.println("Message: " + message);
    }
}
PrimitiveTypesExample.main(null);
Sum of numbers: 15
Average temperature: 25.8
Number of true choices: 2
Message: Hello, world!

Methods and Control Structures

Methods

In Java, a method is a set of code that performs a specific task. Methods enable code reusability and modularity. A method can receive parameters as input and return a value as output.

Control Structures

Control structures are programming statements that manage the program's execution flow. In Java, conditional statements, loops, and switch statements are the common control structures.

Methods and Control Structures in methodDataTypes

methodDataTypes is a Java project consisting of various classes that exemplify the use of methods and control structures. For instance, the DiverseArrays and Matrix classes demonstrate the use of control structures like loops and conditional statements to perform operations on arrays and matrices. The Number and Random classes demonstrate the use of control structures for performing operations on numbers, and the DoNothingByValue and IntByReference classes illustrate how passing parameters to methods works. Lastly, the Menu class demonstrates the use of control structures to control the flow of execution of the program.

import java.util.Scanner;

public class Menu {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int choice = 0;
        
        Runnable menuRunnable = () -> {
            System.out.println("Menu:");
            System.out.println("1. Option 1");
            System.out.println("2. Option 2");
            System.out.println("3. Option 3");
            System.out.println("4. Exit");
            System.out.print("Enter your choice: ");
        };
        
        while (choice != 4) {
            try {
                menuRunnable.run();
                choice = scanner.nextInt();
                scanner.nextLine();
                
                switch (choice) {
                    case 1:
                        System.out.println("Option 1 selected");
                        break;
                    case 2:
                        System.out.println("Option 2 selected");
                        break;
                    case 3:
                        System.out.println("Option 3 selected");
                        break;
                    case 4:
                        System.out.println("Goodbye!");
                        break;
                    default:
                        System.out.println("Invalid choice!");
                        break;
                }
            } catch (Exception e) {
                System.out.println("Invalid input. Please try again.");
                scanner.nextLine();
            }
        }
        
        scanner.close();
    }
}
Menu.main(null);
Menu:
1. Option 1
2. Option 2
3. Option 3
4. Exit
Enter your choice: Option 1 selected
Menu:
1. Option 1
2. Option 2
3. Option 3
4. Exit
Enter your choice: Goodbye!

2015 FRQ Question 1

a.

public static int arraySum(int[] arr) {
    int sum = 0;
  
    for (int n: arr)
      sum += n;
  
    return sum;
  }

The code defines a method named arraySum that takes an array of integers as input and returns the sum of all the integers in the array. It starts by initializing a variable sum to zero, and then loops through the elements in the array using a for-each loop. In each iteration, it adds the current element to the sum variable. Finally, it returns the sum variable which holds the total sum of all elements in the array.

b.

public static int[] rowSums(int[][] arr2D) {
    int[] sums = new int[arr2D.length];
  
    for (int i = 0; i < sums.length; i++) {
      sums[i] = arraySum(arr2D[i]);
    }
  
    return sums;
  }

The code defines a static method called rowSums that takes a two-dimensional integer array arr2D as an input and returns a one-dimensional integer array. The method initializes a new integer array sums with the same length as the number of rows in the input array.

The method then iterates through each row in the input array using a for loop. For each row, it calls another method called arraySum to calculate the sum of all the elements in the row. The result is stored in the corresponding index of the sums array.

Finally, the method returns the sums array containing the sum of each row in the input array.

In summary, the rowSums method calculates the sum of each row in a two-dimensional integer array and returns the result in a new one-dimensional integer array.

c.

public static boolean isDiverse(int[][] arr2D) {
    int[] sums = rowSums(arr2D);
  
    for (int i = 0; i < sums.length; i++)
      for (int j = i + 1; j < sums.length; j++)
        if (sums[i] == sums[j])
          return false;
  
    return true;
}

The isDiverse method is a static method that takes a two-dimensional integer array as input and returns a boolean value. It uses the rowSums method to calculate the sum of each row of the input array and stores the resulting sums in a new array called sums.

The method then compares each sum in the sums array with all other sums in the array to determine whether any two rows have the same sum. If it finds any such duplicate sums, it immediately returns false to indicate that the input array is not diverse. Otherwise, if it reaches the end of the loops without finding any duplicates, it returns true to indicate that the input array is diverse.

In essence, the isDiverse method checks whether a two-dimensional array is diverse, meaning that none of its rows has the same sum as any other row, by calculating row sums and comparing them to one another.