Binary Addition

Takes two binary inputs, adds them, and displays the sum in binary.

import java.util.Scanner;

public class binaryAddition {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in); // scanner object

        System.out.println("Enter binary input: ");
        String input1 = sc.nextLine(); //takes input 1
        System.out.println(input1);

        System.out.println("Enter another binary input: ");
        String input2 = sc.nextLine(); //takes input 2
        System.out.println(input2);

        int sum = Integer.parseInt(input1, 2) + Integer.parseInt(input2, 2); // takes parsedInt sum of inputs

        System.out.println();
        System.out.println("Your output is: ");
        System.out.println(Integer.toBinaryString(sum)); // parse integer sum back to binary

    }
}


binaryAddition.main(null);
Enter binary input: 
10
Enter another binary input: 
01

Your output is: 
11
import java.lang.Integer;

public class binaryAdditionAlgo {

    public static int binToDec(Integer input) { // convert binary to decimal
        Integer inputLen = input.toString().length(); // length of input 1
        Integer dec = 0;
        String tmp;

        for (int i = 0; i < inputLen; i++) { // will iterate for as many digits in binary input
            tmp = input.toString().substring(inputLen-1-i,inputLen-i); // starts iterating from the last digit of binary moving backwards
            dec += Integer.parseInt(tmp) * (int) Math.pow(2,i); // adds up each iteration of the current binary digit * 2^i
        }

        return dec;
    }

    public static int decToBin(Integer input) {
        Integer mod = 0;
        String output = "";
        while (input > 0) {
            mod = input % 2;
            input = (int) Math.floor(input/2);
            output = output.concat(mod.toString()); // concat each modulo to final string
            
        }
        output = new StringBuilder(output).reverse().toString(); // reverse string
        return Integer.parseInt(output); // return string parsed back to int
    }

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in); // scanner object

        System.out.println("Enter binary input: ");
        Integer dec1 = binToDec(sc.nextInt());
        System.out.println("Input as decimal: " + dec1);
        

        System.out.println("Enter another binary input: ");
        Integer dec2 = binToDec(sc.nextInt());
        System.out.println("input as decimal: " + dec2);

        System.out.println();

        Integer sum = decToBin(dec1+dec2);
        System.out.println("Your output is: " + sum);

    }
}


binaryAdditionAlgo.main(null);
69 in binary is: 1000101
Enter binary input: 
Input as decimal: 2
Enter another binary input: 
input as decimal: 1

Your output is: 11

Data Types

A brief overview of int, double, boolean, and char data types.

public class wrapperClasses {

    public static void main(String args[]) {

        // integer array --------------------
        int[] intArr = new int[3];
        
        for (int i = 0; i < intArr.length; i++) {
            intArr[i] = (int) Math.floor(Math.random() * 1000);
            System.out.println("Int " + i + ": " + intArr[i]);
        }
        System.out.println();

        // double array --------------------
        double[] doubleArr = new double[3];
        
        for (int i = 0; i < doubleArr.length; i++) {
            doubleArr[i] = (Math.random() * 1000);
            System.out.println("Double " + i + ": " + doubleArr[i]);
        }
        System.out.println();

        // boolean example --------------------
        boolean bool1 = true;
        boolean bool2 = false;
        if(bool1) {
            System.out.println("Bool 1 is true");
        };
        if(bool2) {
            System.out.println("Bool 2 is true");
        };
        System.out.println();

        // char example --------------------
        char c1 = 'a';
        Character ch1 = Character.valueOf(c1);  // .valueOf converts primitive "char" into wrapper class "Character"
        System.out.print("Character 1: " + ch1);

        
    }
}

wrapperClasses.main(null);
Int 0: 231
Int 1: 998
Int 2: 687

Double 0: 182.25046098320286
Double 1: 148.91749221450078
Double 2: 111.39723548772373

Bool 1 is true

Character 1: a

Methods and Control Structures

  • Methods are blocks of code which can be called upon and reusued at any point in a program
  • Control Strucutres are the building blocks of computer programs, being commands which take decisions and follow a set of instructions

Math.random()

  • The Math.random method with give a randomized double type value between 0 and 1 with no parameters set
  • You can change the paramters of Math.random to customize the range of random numbers in which it will generate

Do nothing by value

  • Do nothing by value is the principle of changing a local variable value, which will only temporarily remain in the current class/method, and ultimately, the change to this value is never set outside of the local scope, and therefore does nothing.

Int by reference

  • Int by reference is the principle of changing an integer value, and then referencing it into other classes to actually set your change in a non-local setting. This is the opposite of "do nothing by value", which does nothing for changing variables non-locally.