Binary

import java.util.Scanner;
public class Binary {
   public static void main(String[] args)
   {
    int i = 0, carry = 0;
    int[] sum = new int[10];
    int b1 = 1;
    int b2 = 1;
    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("1 + 1 = ");
    while (i >= 0) {
        System.out.print(sum[i--]);
    }
    System.out.print("\n");
   }
}
Binary.main(null);

Primitive Examples

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


        double e = 2.7182818284590452353602874713527;
        System.out.println("Euler's number: " + e);


        boolean raining = true;
        System.out.println("Is it raining? " + raining);


        String message = "hellow wrld";
        char firstLetter = message.charAt(0);
        System.out.println("First letter : " + firstLetter);
    }
}
PrimitiveTypesExample.main(null);

Wrapper Class Examples

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

        
        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[] booleanArray = new boolean[]{true, false, true, false, true};
        boolean randomBoolean = booleanArray[(int) (Math.random() * booleanArray.length)];
        Boolean[] booleanArray = new Boolean[]{true, false, true, false, true};
        Boolean randomBoolean = booleanArray[(int) (Math.random() * booleanArray.length)];


        String charString = "hello world";
        char[] charArray = charString.toCharArray();
        String randomCharString = charString.substring((int) (Math.random() * charString.length()), (int) (Math.random() * charString.length() + 1));
        char randomChar = randomCharString.charAt(0);
    }
}
PrimitiveTypesExample.main(null);

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);

2019 FRQ Question 1

a.

public ArrayList<String> getDelimitersList(String[] tokens) 
{     
  ArrayList<String> d = new ArrayList<String>();    
  for (String str : tokens)    
  {       
    if (str.equals(openDel) || str.equals(closeDel))       
    {          d.add(str);       
    }    
}    
return d; 
} 

The above code is a Java method that takes an array of strings called “tokens” as input and returns an ArrayList of strings containing only the strings that are equal to either “openDel” or “closeDel”.

The method first initializes an empty ArrayList of strings called “d”. It then iterates through each string “str” in the “tokens” array using a for-each loop. For each “str” string, it checks whether it is equal to “openDel” or “closeDel” using the equals() method. If “str” is equal to either of these strings, it adds it to the “d” ArrayList using the add() method.

Finally, after iterating through all the strings in the “tokens” array, the method returns the “d” ArrayList containing only the strings that are equal to either “openDel” or “closeDel”.

Note that the values of “openDel” and “closeDel” are not defined in the code snippet provided, so their values are unknown.

b.

public boolean isBalanced(ArrayList < String > delimiters) {
  int openCount = 0;
  int closeCount = 0;
  for (String str: delimiters) {
    if (str.equals(openDel)) {
      openCount++;
    } else {
      closeCount++;
    }
    if (closeCount > openCount) {
      return false;
    }
  }
  if (openCount == closeCount) {
    return true;
  } else {
    return false;
  }
}

The code above is a Java method that takes an ArrayList of strings called “delimiters” as input and checks whether the input contains balanced delimiters. The method first initializes two integer variables, “openCount” and “closeCount”, to 0. It then iterates through each string “str” in the “delimiters” ArrayList using a for-each loop. For each “str” string, it checks whether it is equal to “openDel” using the equals() method. If “str” is equal to “openDel”, it increments the “openCount” variable. Otherwise, it increments the “closeCount” variable. At each iteration, the method checks whether the number of closing delimiters is greater than the number of opening delimiters. If this is the case, it means that the input is unbalanced, and the method immediately returns false. After iterating through all the strings in the “delimiters” ArrayList, the method checks whether the “openCount” variable is equal to the “closeCount” variable. If they are equal, it means that the input contains balanced delimiters, and the method returns true. Otherwise, it means that the input is unbalanced, and the method returns false. Note that the value of “openDel” is not defined in the code snippet provided, so its value is unknown. Additionally, this implementation assumes that all other strings in the “delimiters” ArrayList are closing delimiters, which may not always be the case.