diff --git a/Conversions/HexaDecimalToBinary.java b/Conversions/HexaDecimalToBinary.java index e43bb1de..29608e1d 100644 --- a/Conversions/HexaDecimalToBinary.java +++ b/Conversions/HexaDecimalToBinary.java @@ -3,7 +3,7 @@ import java.util.*; import java.util.Scanner; import javax.swing.*; -public class HexaToBin { +public class HexaDecimalToBinary { private final int LONG_BITS = 8; @@ -28,10 +28,10 @@ public class HexaToBin { //Testing Numbers: String[] hexNums = {"1", "A1", "ef", "BA", "AA", "BB", "19", "01", "02", "03", "04"}; - Convert objConvert = new Convert(); + HexaDecimalToBinary objConvert = new HexaDecimalToBinary(); for (String num : hexNums) { objConvert.convert(num); } } -} \ No newline at end of file +} diff --git a/Conversions/OctalToDecimal.java b/Conversions/OctalToDecimal.java index 637b8f2e..2c909857 100644 --- a/Conversions/OctalToDecimal.java +++ b/Conversions/OctalToDecimal.java @@ -7,42 +7,41 @@ import java.util.Scanner; * */ public class OctalToDecimal { - + /** * Main method * - * @param args Command line arguments + * @param args + * Command line arguments */ public static void main(String args[]) { Scanner sc = new Scanner(System.in); - int o = sc.nextInt(); - System.out.println("Decimal equivalent: " + convertOctalToDecimal(o)); + System.out.print("Octal Input: "); + String inputOctal = sc.nextLine(); + int result = convertOctalToDecimal(inputOctal); + if (result != -1) + System.out.println("Result convertOctalToDecimal : " + result); sc.close(); } - + /** - * This method converts an octal number to - * a decimal number. + * This method converts an octal number to a decimal number. * - * @param o The octal number + * @param inputOctal + * The octal number * @return The decimal number */ - public static int convertOctalToDecimal(int o) { - System.out.print("Octal Input: "); - // Read the input from the console which we are expecting as an octal number: - Scanner s = new Scanner(System.in); - String inputHex = s.nextLine(); - try{ + public static int convertOctalToDecimal(String inputOctal) { + + try { // Actual conversion of Octal to Decimal: - Integer outputDecimal = Integer.parseInt(inputHex, 8); - System.out.println("Decimal Equivalent : " + outputDecimal); - } - catch(NumberFormatException ne){ - // Printing a warning message if the input is not a valid octal number: + Integer outputDecimal = Integer.parseInt(inputOctal, 8); + return outputDecimal; + } catch (NumberFormatException ne) { + // Printing a warning message if the input is not a valid octal + // number: System.out.println("Invalid Input, Expecting octal number 0-7"); - } - finally{ - s.close(); + return -1; } } } \ No newline at end of file diff --git a/Data Structures/Graphs/BFS.java b/Data Structures/Graphs/BFS.java index 4f3bd62d..d06a6606 100644 --- a/Data Structures/Graphs/BFS.java +++ b/Data Structures/Graphs/BFS.java @@ -6,7 +6,7 @@ import java.util.*; * @author Unknown * */ -public class bfs{ +public class BFS{ /** * The BFS implemented in code to use. diff --git a/Data Structures/Graphs/DFS.java b/Data Structures/Graphs/DFS.java index 8ceba166..747fcbed 100644 --- a/Data Structures/Graphs/DFS.java +++ b/Data Structures/Graphs/DFS.java @@ -7,7 +7,7 @@ import java.util.*; * */ -public class dfs{ +public class DFS{ /** * Implementation in code of a DFS diff --git a/Data Structures/Graphs/MatrixGraphs.java b/Data Structures/Graphs/MatrixGraphs.java index f064c012..5723bf38 100644 --- a/Data Structures/Graphs/MatrixGraphs.java +++ b/Data Structures/Graphs/MatrixGraphs.java @@ -120,7 +120,7 @@ class AdjacencyMatrixGraph { } /** - * this gives a list of verticies in the graph and their adjacencies + * this gives a list of vertices in the graph and their adjacencies * * @return returns a string describing this graph */ diff --git a/Data Structures/Graphs/PrimMST.java b/Data Structures/Graphs/PrimMST.java index 9d8c2d35..25695050 100644 --- a/Data Structures/Graphs/PrimMST.java +++ b/Data Structures/Graphs/PrimMST.java @@ -100,7 +100,7 @@ class PrimMST | / \ | (3)-------(4) 9 */ - MST t = new MST(); + PrimMST t = new PrimMST(); int graph[][] = new int[][] {{0, 2, 0, 6, 0}, {2, 0, 3, 8, 5}, {0, 3, 0, 0, 7}, diff --git a/Data Structures/Trees/AVLTree.java b/Data Structures/Trees/AVLTree.java index a3bde289..4bcf402d 100644 --- a/Data Structures/Trees/AVLTree.java +++ b/Data Structures/Trees/AVLTree.java @@ -1,4 +1,4 @@ -public class AVLtree { +public class AVLTree { private Node root; @@ -200,7 +200,7 @@ public class AVLtree { } public static void main(String[] args) { - AVLtree tree = new AVLtree(); + AVLTree tree = new AVLTree(); System.out.println("Inserting values 1 to 10"); for (int i = 1; i < 10; i++) diff --git a/Data Structures/Trees/LevelOrderTraversal.java b/Data Structures/Trees/LevelOrderTraversal.java index 845ab376..8cb304f1 100644 --- a/Data Structures/Trees/LevelOrderTraversal.java +++ b/Data Structures/Trees/LevelOrderTraversal.java @@ -9,12 +9,12 @@ class Node } } -class BinaryTree +public class LevelOrderTraversal { // Root of the Binary Tree Node root; - public BinaryTree() + public LevelOrderTraversal() { root = null; } @@ -65,7 +65,7 @@ class BinaryTree /* Driver program to test above functions */ public static void main(String args[]) { - BinaryTree tree = new BinaryTree(); + LevelOrderTraversal tree = new LevelOrderTraversal(); tree.root= new Node(1); tree.root.left= new Node(2); tree.root.right= new Node(3); diff --git a/Data Structures/Trees/LevelOrderTraversalQueue.java b/Data Structures/Trees/LevelOrderTraversalQueue.java index 4f263c95..ce7ad745 100644 --- a/Data Structures/Trees/LevelOrderTraversalQueue.java +++ b/Data Structures/Trees/LevelOrderTraversalQueue.java @@ -14,7 +14,7 @@ class Node { } /* Class to print Level Order Traversal */ -class BinaryTree { +public class LevelOrderTraversalQueue { Node root; @@ -49,7 +49,7 @@ class BinaryTree { { /* creating a binary tree and entering the nodes */ - BinaryTree tree_level = new BinaryTree(); + LevelOrderTraversalQueue tree_level = new LevelOrderTraversalQueue(); tree_level.root = new Node(1); tree_level.root.left = new Node(2); tree_level.root.right = new Node(3); diff --git a/Data Structures/Trees/PrintTopViewofTree.java b/Data Structures/Trees/PrintTopViewofTree.java index a429f8a6..27016ee2 100644 --- a/Data Structures/Trees/PrintTopViewofTree.java +++ b/Data Structures/Trees/PrintTopViewofTree.java @@ -78,7 +78,7 @@ class Tree } // Driver class to test above methods -public class Main +public class PrintTopViewofTree { public static void main(String[] args) { diff --git a/Data Structures/Trees/ValidBSTOrNot.java b/Data Structures/Trees/ValidBSTOrNot.java index 0f71a46a..4b2c08a1 100644 --- a/Data Structures/Trees/ValidBSTOrNot.java +++ b/Data Structures/Trees/ValidBSTOrNot.java @@ -10,7 +10,7 @@ class Node } } -public class BinaryTree +public class ValidBSTOrNot { //Root of the Binary Tree Node root; @@ -47,7 +47,7 @@ public class BinaryTree /* Driver program to test above functions */ public static void main(String args[]) { - BinaryTree tree = new BinaryTree(); + ValidBSTOrNot tree = new ValidBSTOrNot(); tree.root = new Node(4); tree.root.left = new Node(2); tree.root.right = new Node(5); diff --git a/Dynamic Programming/LevenshteinDistance.java b/Dynamic Programming/LevenshteinDistance.java index 8a5f7249..be0e7c43 100644 --- a/Dynamic Programming/LevenshteinDistance.java +++ b/Dynamic Programming/LevenshteinDistance.java @@ -6,7 +6,7 @@ * */ -public class Levenshtein_distance{ +public class LevenshteinDistance{ private static int minimum(int a, int b, int c){ if(a < b && a < c){ return a; diff --git a/Misc/heap_sort.java b/Misc/heap_sort.java index 59d362e5..991d689b 100644 --- a/Misc/heap_sort.java +++ b/Misc/heap_sort.java @@ -1,4 +1,4 @@ -public class HeapSort +public class heap_sort { public void sort(int arr[]) { @@ -64,7 +64,7 @@ public class HeapSort int arr[] = {12, 11, 13, 5, 6, 7}; int n = arr.length; - HeapSort ob = new HeapSort(); + heap_sort ob = new heap_sort(); ob.sort(arr); System.out.println("Sorted array is"); diff --git a/Others/BrianKernighanAlgorithm.java b/Others/BrianKernighanAlgorithm.java new file mode 100644 index 00000000..cb27eec1 --- /dev/null +++ b/Others/BrianKernighanAlgorithm.java @@ -0,0 +1,55 @@ +import java.util.Scanner; + +/** + * + * @author Nishita Aggarwal + * + * Brian Kernighan’s Algorithm + * + * algorithm to count the number of set bits in a given number + * + * Subtraction of 1 from a number toggles all the bits (from right to left) till the rightmost set bit(including the + * rightmost set bit). + * So if we subtract a number by 1 and do bitwise & with itself i.e. (n & (n-1)), we unset the rightmost set bit. + * + * If we do n & (n-1) in a loop and count the no of times loop executes we get the set bit count. + * + * + * Time Complexity: O(logn) + * + */ + + +public class BrianKernighanAlgorithm { + + /** + * @param num: number in which we count the set bits + * + * @return int: Number of set bits + * */ + static int countSetBits(int num) + { + int cnt = 0; + while(num != 0) + { + num = num & (num-1); + cnt++; + } + return cnt; + } + + + /** + * + * @param args : command line arguments + * + */ + public static void main(String args[]) + { + Scanner sc = new Scanner(System.in); + int num = sc.nextInt(); + int setBitCount = countSetBits(num); + System.out.println(setBitCount); + sc.close(); + } +} diff --git a/Others/Dijkshtra.java b/Others/Dijkshtra.java index 741c9bd3..05011dd4 100644 --- a/Others/Dijkshtra.java +++ b/Others/Dijkshtra.java @@ -9,7 +9,7 @@ import java.util.Arrays; import java.util.Scanner; import java.util.Stack; -public class Solution { +public class Dijkshtra { public static void main(String[] args) throws IOException { Scanner in =new Scanner(System.in); diff --git a/Others/FibToN.java b/Others/FibToN.java index e7b7a9a2..1d1efdc1 100644 --- a/Others/FibToN.java +++ b/Others/FibToN.java @@ -9,7 +9,7 @@ public class FibToN { // print fibonacci sequence less than N int first = 0, second = 1; //first fibo and second fibonacci are 0 and 1 respectively - + scn.close(); while(first <= N){ //print first fibo 0 then add second fibo into it while updating second as well diff --git a/Others/FindingPrimes.java b/Others/FindingPrimes.java deleted file mode 100644 index f7cc14d7..00000000 --- a/Others/FindingPrimes.java +++ /dev/null @@ -1,45 +0,0 @@ -/** - * The Sieve of Eratosthenes is an algorithm use to find prime numbers, - * up to a given value. - * Illustration: https://upload.wikimedia.org/wikipedia/commons/b/b9/Sieve_of_Eratosthenes_animation.gif - * (This illustration is also in the github repository) - * - * @author Unknown - * - */ -public class FindingPrimes{ - /** - * The Main method - * - * @param args Command line arguments - */ - public static void main(String args[]){ - SOE(20); //Example: Finds all the primes up to 20 - } - - /** - * The method implementing the Sieve of Eratosthenes - * - * @param n Number to perform SOE on - */ - public static void SOE(int n){ - boolean sieve[] = new boolean[n]; - - int check = (int)Math.round(Math.sqrt(n)); //No need to check for multiples past the square root of n - - sieve[0] = false; - sieve[1] = false; - for(int i = 2; i < n; i++) - sieve[i] = true; //Set every index to true except index 0 and 1 - - for(int i = 2; i< check; i++){ - if(sieve[i]==true) //If i is a prime - for(int j = i+i; j < n; j+=i) //Step through the array in increments of i(the multiples of the prime) - sieve[j] = false; //Set every multiple of i to false - } - for(int i = 0; i< n; i++){ - if(sieve[i]==true) - System.out.print(i+" "); //In this example it will print 2 3 5 7 11 13 17 19 - } - } -} \ No newline at end of file diff --git a/Others/FloydTriangle.java b/Others/FloydTriangle.java index 815dbd29..f8d479a8 100644 --- a/Others/FloydTriangle.java +++ b/Others/FloydTriangle.java @@ -6,7 +6,7 @@ class FloydTriangle { Scanner sc = new Scanner(System.in); System.out.println("Enter the number of rows which you want in your Floyd Triangle: "); int r = sc.nextInt(), n = 0; - + sc.close(); for(int i=0; i < r; i++) { for(int j=0; j <= i; j++) { System.out.print(++n + " "); diff --git a/Others/InsertDeleteInArray.java b/Others/InsertDeleteInArray.java index 12bbdc19..85702066 100644 --- a/Others/InsertDeleteInArray.java +++ b/Others/InsertDeleteInArray.java @@ -1,5 +1,5 @@ import java.util.*; -public class Array { +public class InsertDeleteInArray { public static void main(String[] args) { Scanner s = new Scanner(System.in); // Input statement diff --git a/Others/RootPrecision.java b/Others/RootPrecision.java index 0ae00de0..b792d692 100644 --- a/Others/RootPrecision.java +++ b/Others/RootPrecision.java @@ -4,7 +4,7 @@ import java.text.*; import java.math.*; import java.util.regex.*; -public class Solution { +public class RootPrecision { public static void main(String[] args) { //take input diff --git a/Others/StackPostfixNotation.java b/Others/StackPostfixNotation.java index c04b0ac6..be77eead 100644 --- a/Others/StackPostfixNotation.java +++ b/Others/StackPostfixNotation.java @@ -1,6 +1,6 @@ import java.util.*; -public class Postfix { +public class StackPostfixNotation { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String post = scanner.nextLine(); // Takes input with spaces in between eg. "1 21 +" diff --git a/Others/countwords.java b/Others/countwords.java index 065c9a10..30806e13 100644 --- a/Others/countwords.java +++ b/Others/countwords.java @@ -7,7 +7,7 @@ import java.util.Scanner; * @author Marcus * */ - class CountTheWords{ + public class countwords{ public static void main(String[] args){ Scanner input = new Scanner(System.in); diff --git a/Others/krishnamurthy.java b/Others/krishnamurthy.java index 66685bcc..52480eb0 100644 --- a/Others/krishnamurthy.java +++ b/Others/krishnamurthy.java @@ -1,30 +1,28 @@ import java.util.Scanner; -class krishnamurthy -{ - int fact(int n) - { - int i,p=1; - for(i=n;i>=1;i--) - p=p*i; - return p; - } - public static void main(String args[]) - { - Scanner sc=new Scanner(System.in); - int a,b,s=0; - System.out.print("Enter the number : "); - a=sc.nextInt(); - int n=a; - while(a>0) - { - b=a%10; - s=s+fact(b); - a=a/10; - } - if(s==n) - System.out.print(n+" is a krishnamurthy number"); - else - System.out.print(n+" is not a krishnamurthy number"); - } +class krishnamurthy { + static int fact(int n) { + int i, p = 1; + for (i = n; i >= 1; i--) + p = p * i; + return p; + } + + public static void main(String args[]) { + Scanner sc = new Scanner(System.in); + int a, b, s = 0; + System.out.print("Enter the number : "); + a = sc.nextInt(); + int n = a; + while (a > 0) { + b = a % 10; + s = s + fact(b); + a = a / 10; + } + if (s == n) + System.out.print(n + " is a krishnamurthy number"); + else + System.out.print(n + " is not a krishnamurthy number"); + sc.close(); + } }