Skip to content

Fixed 8XCorrectness_Bugs + 10XBad_Practice_Bugs + 14XDodgy_Code_Bugs #1325

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 28, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Conversions/DecimalToBinary.java
Original file line number Diff line number Diff line change
@@ -27,7 +27,7 @@ public static void main(String args[]) {
public static void conventionalConversion() {
int n, b = 0, c = 0, d;
Scanner input = new Scanner(System.in);
System.out.printf("Conventional conversion.\n\tEnter the decimal number: ");
System.out.printf("Conventional conversion.%n Enter the decimal number: ");
n = input.nextInt();
while (n != 0) {
d = n % 2;
@@ -46,7 +46,7 @@ public static void conventionalConversion() {
public static void bitwiseConversion() {
int n, b = 0, c = 0, d;
Scanner input = new Scanner(System.in);
System.out.printf("Bitwise conversion.\n\tEnter the decimal number: ");
System.out.printf("Bitwise conversion.%n Enter the decimal number: ");
n = input.nextInt();
while (n != 0) {
d = (n & 1);
8 changes: 4 additions & 4 deletions Conversions/OctalToHexadecimal.java
Original file line number Diff line number Diff line change
@@ -15,7 +15,7 @@ public class OctalToHexadecimal {
* @param s The Octal Number
* @return The Decimal number
*/
public static int OctToDec(String s) {
public static int octToDec(String s) {
int i = 0;
for (int j = 0; j < s.length(); j++) {
char num = s.charAt(j);
@@ -32,7 +32,7 @@ public static int OctToDec(String s) {
* @param d The Decimal Number
* @return The Hexadecimal number
*/
public static String DecimalToHex(int d) {
public static String decimalToHex(int d) {
String digits = "0123456789ABCDEF";
if (d <= 0)
return "0";
@@ -54,10 +54,10 @@ public static void main(String args[]) {
String oct = input.next();

// Pass the octal number to function and get converted deciaml form
int decimal = OctToDec(oct);
int decimal = octToDec(oct);

// Pass the decimla number to function and get converted Hex form of the number
String hex = DecimalToHex(decimal);
String hex = decimalToHex(decimal);
System.out.println("The Hexadecimal equivalant is: " + hex);
input.close();
}
4 changes: 2 additions & 2 deletions DataStructures/DynamicArray/DynamicArray.java
Original file line number Diff line number Diff line change
@@ -41,7 +41,7 @@ public void add(final E element) {
}

public void put(final int index, E element) {
Objects.checkIndex(index, this.size);
// Objects.checkIndex(index, this.size);

this.elements[index] = element;
}
@@ -79,7 +79,7 @@ private void fastRemove(final Object[] elements, final int index) {
}

private E getElement(final int index) {
Objects.checkIndex(index, this.size);
// Objects.checkIndex(index, this.size);
return (E) this.elements[index];
}

2 changes: 1 addition & 1 deletion DataStructures/Graphs/BellmanFord.java
Original file line number Diff line number Diff line change
@@ -23,7 +23,7 @@ class Edge
* @param v End vertex
* @param c Weight
*/
Edge(int a,int b,int c)
public Edge(int a,int b,int c)
{
u=a;
v=b;
3 changes: 1 addition & 2 deletions DataStructures/Graphs/MatrixGraphs.java
Original file line number Diff line number Diff line change
@@ -127,8 +127,7 @@ public boolean removeEdge(int from, int to) {
* @return returns a string describing this graph
*/
public String toString() {
String s = new String();
s = " ";
String s = " ";
for (int i = 0; i < this.numberOfVertices(); i++) {
s = s + String.valueOf(i) + " ";
}
18 changes: 16 additions & 2 deletions DataStructures/Heaps/HeapElement.java
Original file line number Diff line number Diff line change
@@ -117,7 +117,21 @@ public String toString() {
* @return true if the keys on both elements are identical and the additional info objects
* are identical.
*/
public boolean equals(HeapElement otherHeapElement) {
return (this.key == otherHeapElement.key) && (this.additionalInfo.equals(otherHeapElement.additionalInfo));
@Override
public boolean equals(Object o) {
if (o != null) {
if (!(o instanceof HeapElement)) return false;
HeapElement otherHeapElement = (HeapElement) o;
return (this.key == otherHeapElement.key) && (this.additionalInfo.equals(otherHeapElement.additionalInfo));
}
return false;
}

@Override
public int hashCode() {
int result = 0;
result = 31*result + (int) key;
result = 31*result + (additionalInfo != null ? additionalInfo.hashCode() : 0);
return result;
}
}
8 changes: 4 additions & 4 deletions DataStructures/Heaps/MaxHeap.java
Original file line number Diff line number Diff line change
@@ -49,9 +49,9 @@ private void swap(int index1, int index2) {
// Toggle an element up to its right place as long as its key is lower than its parent's
private void toggleUp(int elementIndex) {
double key = maxHeap.get(elementIndex - 1).getKey();
while (getElementKey((int) Math.floor(elementIndex / 2)) < key) {
swap(elementIndex, (int) Math.floor(elementIndex / 2));
elementIndex = (int) Math.floor(elementIndex / 2);
while (getElementKey((int) Math.floor(elementIndex / 2.0)) < key) {
swap(elementIndex, (int) Math.floor(elementIndex / 2.0));
elementIndex = (int) Math.floor(elementIndex / 2.0);
}
}

@@ -101,7 +101,7 @@ public void deleteElement(int elementIndex) {
maxHeap.set(elementIndex - 1, getElement(maxHeap.size()));
maxHeap.remove(maxHeap.size());
// Shall the new element be moved up...
if (getElementKey(elementIndex) > getElementKey((int) Math.floor(elementIndex / 2))) toggleUp(elementIndex);
if (getElementKey(elementIndex) > getElementKey((int) Math.floor(elementIndex / 2.0))) toggleUp(elementIndex);
// ... or down ?
else if (((2 * elementIndex <= maxHeap.size()) && (getElementKey(elementIndex) < getElementKey(elementIndex * 2))) ||
((2 * elementIndex < maxHeap.size()) && (getElementKey(elementIndex) < getElementKey(elementIndex * 2))))
8 changes: 4 additions & 4 deletions DataStructures/Heaps/MinHeap.java
Original file line number Diff line number Diff line change
@@ -44,9 +44,9 @@ private void swap(int index1, int index2) {
// Toggle an element up to its right place as long as its key is lower than its parent's
private void toggleUp(int elementIndex) {
double key = minHeap.get(elementIndex - 1).getKey();
while (getElementKey((int) Math.floor(elementIndex / 2)) > key) {
swap(elementIndex, (int) Math.floor(elementIndex / 2));
elementIndex = (int) Math.floor(elementIndex / 2);
while (getElementKey((int) Math.floor(elementIndex / 2.0)) > key) {
swap(elementIndex, (int) Math.floor(elementIndex / 2.0));
elementIndex = (int) Math.floor(elementIndex / 2.0);
}
}

@@ -96,7 +96,7 @@ public void deleteElement(int elementIndex) {
minHeap.set(elementIndex - 1, getElement(minHeap.size()));
minHeap.remove(minHeap.size());
// Shall the new element be moved up...
if (getElementKey(elementIndex) < getElementKey((int) Math.floor(elementIndex / 2))) toggleUp(elementIndex);
if (getElementKey(elementIndex) < getElementKey((int)Math.floor(elementIndex / 2.0))) toggleUp(elementIndex);
// ... or down ?
else if (((2 * elementIndex <= minHeap.size()) && (getElementKey(elementIndex) > getElementKey(elementIndex * 2))) ||
((2 * elementIndex < minHeap.size()) && (getElementKey(elementIndex) > getElementKey(elementIndex * 2))))
2 changes: 1 addition & 1 deletion DataStructures/Lists/CircleLinkedList.java
Original file line number Diff line number Diff line change
@@ -14,7 +14,7 @@ private Node(E value, Node<E> next) {
//For better O.O design this should be private allows for better black box design
private int size;
//this will point to dummy node;
private Node<E> head;
private Node<E> head = null;

//constructer for class.. here we will make a dummy node for circly linked list implementation with reduced error catching as our list will never be empty;
public CircleLinkedList() {
12 changes: 9 additions & 3 deletions DataStructures/Lists/DoublyLinkedList.java
Original file line number Diff line number Diff line change
@@ -86,9 +86,12 @@ public void insertTail(int x) {
public Link deleteHead() {
Link temp = head;
head = head.next; // oldHead <--> 2ndElement(head)
head.previous = null; // oldHead --> 2ndElement(head) nothing pointing at old head so will be removed
if (head == null)

if (head == null) {
tail = null;
} else {
head.previous = null; // oldHead --> 2ndElement(head) nothing pointing at old head so will be removed
}
return temp;
}

@@ -100,10 +103,13 @@ public Link deleteHead() {
public Link deleteTail() {
Link temp = tail;
tail = tail.previous; // 2ndLast(tail) <--> oldTail --> null
tail.next = null; // 2ndLast(tail) --> null

if (tail == null) {
head = null;
} else{
tail.next = null; // 2ndLast(tail) --> null
}

return temp;
}

4 changes: 2 additions & 2 deletions DataStructures/Stacks/NodeStack.java
Original file line number Diff line number Diff line change
@@ -74,7 +74,7 @@ public void push(Item item) {
} else {
newNs.setPrevious(NodeStack.head);
NodeStack.head.setNext(newNs);
NodeStack.head = newNs;
NodeStack.head.setHead(newNs);
}

NodeStack.setSize(NodeStack.getSize() + 1);
@@ -89,7 +89,7 @@ public Item pop() {

Item item = (Item) NodeStack.head.getData();

NodeStack.head = NodeStack.head.getPrevious();
NodeStack.head.setHead(NodeStack.head.getPrevious());
NodeStack.head.setNext(null);

NodeStack.setSize(NodeStack.getSize() - 1);
4 changes: 2 additions & 2 deletions DataStructures/Trees/LevelOrderTraversal.java
Original file line number Diff line number Diff line change
@@ -15,8 +15,8 @@ public Node(int item) {
// Root of the Binary Tree
Node root;

public LevelOrderTraversal() {
root = null;
public LevelOrderTraversal( Node root) {
this.root = root;
}

/* function to print level order traversal of tree*/
4 changes: 1 addition & 3 deletions DataStructures/Trees/LevelOrderTraversalQueue.java
Original file line number Diff line number Diff line change
@@ -19,11 +19,9 @@ public Node(int item) {
}
}

Node root;

/* Given a binary tree. Print its nodes in level order
using array for implementing queue */
void printLevelOrder() {
void printLevelOrder(Node root) {
Queue<Node> queue = new LinkedList<Node>();
queue.add(root);
while (!queue.isEmpty()) {
3 changes: 1 addition & 2 deletions DataStructures/Trees/ValidBSTOrNot.java
Original file line number Diff line number Diff line change
@@ -13,14 +13,13 @@ public Node(int item) {
}

//Root of the Binary Tree
Node root;

/* can give min and max value according to your code or
can write a function to find min and max value of tree. */

/* returns true if given search tree is binary
search tree (efficient version) */
boolean isBST() {
boolean isBST(Node root) {
return isBSTUtil(root, Integer.MIN_VALUE,
Integer.MAX_VALUE);
}
2 changes: 1 addition & 1 deletion DynamicProgramming/LongestIncreasingSubsequence.java
Original file line number Diff line number Diff line change
@@ -22,7 +22,7 @@ public static void main(String[] args) {

private static int upperBound(int[] ar, int l, int r, int key) {
while (l < r - 1) {
int m = (l + r) / 2;
int m = (l + r) >>> 1;
if (ar[m] >= key)
r = m;
else
2 changes: 1 addition & 1 deletion DynamicProgramming/MatrixChainMultiplication.java
Original file line number Diff line number Diff line change
@@ -25,7 +25,7 @@ public static void main(String[] args) {
count++;
}
for (Matrix m : mArray) {
System.out.format("A(%d) = %2d x %2d\n", m.count(), m.col(), m.row());
System.out.format("A(%d) = %2d x %2d%n", m.count(), m.col(), m.row());
}

size = mArray.size();
2 changes: 1 addition & 1 deletion Maths/GCD.java
Original file line number Diff line number Diff line change
@@ -52,6 +52,6 @@ public static void main(String[] args) {

// call gcd function (input array)
System.out.println(gcd(myIntArray)); // => 4
System.out.printf("gcd(40,24)=%d gcd(24,40)=%d\n", gcd(40, 24), gcd(24, 40)); // => 8
System.out.printf("gcd(40,24)=%d gcd(24,40)=%d%n", gcd(40, 24), gcd(24, 40)); // => 8
}
}
Loading