import java.util.Scanner;
public class BinaryAddition {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the first binary number: ");
String binary1 = input.nextLine();
System.out.println(binary1);
System.out.print("Enter the second binary number: ");
String binary2 = input.nextLine();
System.out.println(binary2);
int decimal1 = Integer.parseInt(binary1, 2);
int decimal2 = Integer.parseInt(binary2, 2);
int sum = decimal1 + decimal2;
String binarySum = Integer.toBinaryString(sum);
System.out.println("The sum of " + binary1 + " and " + binary2 + " is " + binarySum + " in binary and " + sum + " in decimal");
}
}
BinaryAddition.main(null);
public class IntByValue {
public static void changeInt(int n) {
System.out.println("In changeInt method");
System.out.println("\tBefore n += 10: n = " + n); // prints 5
n = n += 10;
System.out.println("\tAfter n += 10: n = " + n); // prints 10
}
public static void main(String[] args) {
int n = 5;
System.out.println("Main method before changeInt(n): n = " + n); // prints 5
changeInt(n);
System.out.println("Main method after changeInt(n): n = " + n); // still prints 5
}
}
IntByValue.main(null);
public class IntegerByValueOrReference {
public static void changeInteger(Integer n) {
System.out.println("In changeInteger method");
System.out.println("\tBefore change: n = " +
n + // prints 5
" hash code = " +
n.hashCode());
n += 10; // behind the scenes, this is: n = new Integer(n+10)
System.out.println("\tAfter change: n = " +
n + // prints 15
" hash code = " +
n.hashCode());
}
public static void main(String[] args) {
Integer n = 5;
System.out.println("Main method before changeInteger(n): n = " +
n + // prints 5
" hash code = " +
n.hashCode());
changeInteger(n);
System.out.println("Main method after changeInteger(n): n = " +
n + // now prints 15
" hash code = " +
n.hashCode());
}
}
IntegerByValueOrReference.main(null);
import java.util.concurrent.atomic.AtomicInteger;
public class PassByReference {
public static void changeAtomicInteger(AtomicInteger n) {
System.out.println("In changeAtomicInteger method");
System.out.println("\tBefore change: n = " +
n + // prints 5
" hash code = " +
n.hashCode());
n.set(n.get() + 10); // at this point, we are clearly working with reference data type
System.out.println("\tAfter change: n = " +
n + // prints 15
" hash code = " +
n.hashCode());
}
public static void main(String[] args) {
AtomicInteger n = new AtomicInteger(5); // unlike conventional wrapper class, this requires new
System.out.println("Main method before changeAtomicInteger(n): n = " +
n + // prints 5
" hash code = " +
n.hashCode());
changeAtomicInteger(n);
System.out.println("Main method after changeAtomicInteger(n): n = " +
n + // now prints 15
" hash code = " +
n.hashCode());
}
}
PassByReference.main(null);
public class IntByReference {
private int value;
public IntByReference(Integer value) {
this.value = value;
}
public String toString() {
return (String.format("%d", this.value));
}
public void swapToLowHighOrder(IntByReference i) {
if (this.value > i.value) {
int tmp = this.value;
this.value = i.value;
i.value = tmp;
}
}
public static void swapper(int n0, int n1) {
IntByReference a = new IntByReference(n0);
IntByReference b = new IntByReference(n1);
System.out.println("Before: " + a + " " + b);
a.swapToLowHighOrder(b); // conditionally build swap method to change values of a, b
System.out.println("After: " + a + " " + b);
System.out.println();
}
public static void main(String[] ags) {
IntByReference.swapper(21, 16);
IntByReference.swapper(16, 21);
IntByReference.swapper(16, -1);
}
}
IntByReference.main(null);
int[] numbers = {123, 456, 789};
for (int i = 0; i < numbers.length; i++) {
String str = String.valueOf(numbers[i]);
String subStr = str.substring(1, 2);
int num = Integer.parseInt(subStr);
System.out.println(num);
}
Integer[] numbers = {123, 456, 789};
for (int i = 0; i < numbers.length; i++) {
String str = numbers[i].toString();
String subStr = str.substring(1, 2);
int num = Integer.parseInt(subStr);
System.out.println(num);
}
int[] intArray = {1, 2, 3, 4, 5};
Integer[] integerArray = new Integer[intArray.length];
for (int i = 0; i < intArray.length; i++) {
integerArray[i] = Integer.valueOf(intArray[i]);
}
double[] values = new double[5];
for (int i = 0; i < values.length; i++) {
values[i] = Math.random();
System.out.println(values[i]);
}
Double[] values = new Double[5];
for (int i = 0; i < values.length; i++) {
values[i] = Math.random();
System.out.println(values[i]);
}
boolean[] boolArray = {true, false, true};
for (int i = 0; i < boolArray.length; i++) {
char c = boolArray[i] ? 'T' : 'F';
System.out.println(c);
}
Boolean[] boolArray = {true, false, true};
for (int i = 0; i < boolArray.length; i++) {
char c = boolArray[i] ? 'T' : 'F';
System.out.println(c);
}
public static String toString(Double[] array) {
StringBuilder sb = new StringBuilder();
sb.append("[");
for (int i = 0; i < array.length; i++) {
sb.append(array[i].toString());
if (i < array.length - 1) {
sb.append(", ");
}
}
sb.append("]");
return sb.toString();
}