Category Archives: Java

Add 2 Binary Strings using Java

class Solution { public String addBinary(String a, String b) { int c=Integer.parseInt(a, 2) + Integer.parseInt(b, 2) ; return Integer.toBinaryString(c); } } Advertisements

Posted in Java, LeetCode Solutions, Uncategorized | Leave a comment

Calculate Length of Hypotenuse of Right Angle Triangle

package test5; public class RightTriangle { public static void main(String[] args) { double c; double a=4,b=3; c=Math.sqrt(a*a+b*b); System.out.println(“Length of the hypotenuse is “+c); } }

Posted in Java | Leave a comment

Encrypt/Decrypt a password

package test5; import java.util.Base64; /** * Encrypt/Decrypt a Password * @author asharda * */ public class PasswordTest { /** * @param args */ public static void main(String[] args) { String password=”Hi this is a password”; byte []b=Base64.getEncoder().encode(password.getBytes()); System.out.println(new String(Base64.getDecoder().decode(b))); } … Continue reading

Posted in Java, Uncategorized | Leave a comment

Simple Binary Search using Java

/** * Simple Binary Search * @author asharda * */ public class BinSearch { public static void main(String[] args) { BinSearch bin=new BinSearch(); int arr[]= {2,4,6,8,10,12,14,16}; boolean b=bin.binSearch(arr,0,arr.length-1,16); System.out.println(“Number found “+b); } public boolean binSearch(int arr[],int low,int high,int val) { … Continue reading

Posted in Data Structures, Java, Java 8, Uncategorized | Leave a comment

Rotate Array

/**Program to rotate array * @author asharda * */ public class RotateArray { /** * @param args */ public static void main(String[] args) { RotateArray r =new RotateArray(); int arr[]= {1,2,3,4,5,6,7}; int b[]=r.rotateArray(arr,2); for(int i:b) { System.out.println(i); } } /** … Continue reading

Posted in Data Structures, Java, Java 8 | Leave a comment

Find minimum in ArrayList using Stream API

import java.util.ArrayList; import java.util.Optional; import java.util.stream.Stream; import org.testng.annotations.Test; public class StreamMin { /** * Find min of arraylist using stream */ @Test public void testStreams() { ArrayList<Integer> list=new ArrayList<Integer>(); list.add(10); list.add(20); Stream<Integer> stream=list.stream(); Optional<Integer>min=stream.min(Integer::compare); System.out.println(“Minimum element seen is “+min); } … Continue reading

Posted in Data Structures, Java, Java 8, Uncategorized | Tagged | Leave a comment

Find Unique Chars in a word

package test5; import java.util.ArrayList; import org.testng.Assert; import org.testng.annotations.Test; /** * Code to validate if String contains unique characters * @author asharda * */ public class UniqueChars { @Test public void testUnique() { UniqueChars w=new UniqueChars(); Assert.assertTrue(w.isUnique(“word”), “Chars are not unique”); … Continue reading

Posted in Java, Java 8, Uncategorized | Leave a comment