Week 25 Hacks
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);
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);
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);