Fixed Error:(6, 8) java: class algorithm is public, should be declared in a file named algorithm.java. Inside file PrimeFactorization, the name of public class was wrong.

This commit is contained in:
Maria Lungeanu 2020-05-25 00:21:28 +03:00
parent 63e5ce4c8f
commit a5f42e293b
25 changed files with 293 additions and 238 deletions

View File

@ -27,7 +27,7 @@ class DecimalToBinary {
public static void conventionalConversion() { public static void conventionalConversion() {
int n, b = 0, c = 0, d; int n, b = 0, c = 0, d;
Scanner input = new Scanner(System.in); 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(); n = input.nextInt();
while (n != 0) { while (n != 0) {
d = n % 2; d = n % 2;
@ -46,7 +46,7 @@ class DecimalToBinary {
public static void bitwiseConversion() { public static void bitwiseConversion() {
int n, b = 0, c = 0, d; int n, b = 0, c = 0, d;
Scanner input = new Scanner(System.in); 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(); n = input.nextInt();
while (n != 0) { while (n != 0) {
d = (n & 1); d = (n & 1);

View File

@ -15,7 +15,7 @@ public class OctalToHexadecimal {
* @param s The Octal Number * @param s The Octal Number
* @return The Decimal number * @return The Decimal number
*/ */
public static int OctToDec(String s) { public static int octToDec(String s) {
int i = 0; int i = 0;
for (int j = 0; j < s.length(); j++) { for (int j = 0; j < s.length(); j++) {
char num = s.charAt(j); char num = s.charAt(j);
@ -32,7 +32,7 @@ public class OctalToHexadecimal {
* @param d The Decimal Number * @param d The Decimal Number
* @return The Hexadecimal number * @return The Hexadecimal number
*/ */
public static String DecimalToHex(int d) { public static String decimalToHex(int d) {
String digits = "0123456789ABCDEF"; String digits = "0123456789ABCDEF";
if (d <= 0) if (d <= 0)
return "0"; return "0";
@ -54,10 +54,10 @@ public class OctalToHexadecimal {
String oct = input.next(); String oct = input.next();
// Pass the octal number to function and get converted deciaml form // 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 // 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); System.out.println("The Hexadecimal equivalant is: " + hex);
input.close(); input.close();
} }

View File

@ -41,7 +41,7 @@ public class DynamicArray<E> implements Iterable<E> {
} }
public void put(final int index, E element) { public void put(final int index, E element) {
Objects.checkIndex(index, this.size); // Objects.checkIndex(index, this.size);
this.elements[index] = element; this.elements[index] = element;
} }
@ -79,7 +79,7 @@ public class DynamicArray<E> implements Iterable<E> {
} }
private E getElement(final int index) { private E getElement(final int index) {
Objects.checkIndex(index, this.size); // Objects.checkIndex(index, this.size);
return (E) this.elements[index]; return (E) this.elements[index];
} }

View File

@ -23,7 +23,7 @@ start vertex, end vertes and weights. Vertices should be labelled with a number
* @param v End vertex * @param v End vertex
* @param c Weight * @param c Weight
*/ */
Edge(int a,int b,int c) public Edge(int a,int b,int c)
{ {
u=a; u=a;
v=b; v=b;

View File

@ -127,8 +127,7 @@ class AdjacencyMatrixGraph {
* @return returns a string describing this graph * @return returns a string describing this graph
*/ */
public String toString() { public String toString() {
String s = new String(); String s = " ";
s = " ";
for (int i = 0; i < this.numberOfVertices(); i++) { for (int i = 0; i < this.numberOfVertices(); i++) {
s = s + String.valueOf(i) + " "; s = s + String.valueOf(i) + " ";
} }

View File

@ -117,7 +117,21 @@ public class HeapElement {
* @return true if the keys on both elements are identical and the additional info objects * @return true if the keys on both elements are identical and the additional info objects
* are identical. * are identical.
*/ */
public boolean equals(HeapElement otherHeapElement) { @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 (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;
}
} }

View File

@ -49,9 +49,9 @@ public class MaxHeap implements Heap {
// Toggle an element up to its right place as long as its key is lower than its parent's // Toggle an element up to its right place as long as its key is lower than its parent's
private void toggleUp(int elementIndex) { private void toggleUp(int elementIndex) {
double key = maxHeap.get(elementIndex - 1).getKey(); double key = maxHeap.get(elementIndex - 1).getKey();
while (getElementKey((int) Math.floor(elementIndex / 2)) < key) { while (getElementKey((int) Math.floor(elementIndex / 2.0)) < key) {
swap(elementIndex, (int) Math.floor(elementIndex / 2)); swap(elementIndex, (int) Math.floor(elementIndex / 2.0));
elementIndex = (int) Math.floor(elementIndex / 2); elementIndex = (int) Math.floor(elementIndex / 2.0);
} }
} }
@ -101,7 +101,7 @@ public class MaxHeap implements Heap {
maxHeap.set(elementIndex - 1, getElement(maxHeap.size())); maxHeap.set(elementIndex - 1, getElement(maxHeap.size()));
maxHeap.remove(maxHeap.size()); maxHeap.remove(maxHeap.size());
// Shall the new element be moved up... // 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 ? // ... or down ?
else if (((2 * elementIndex <= maxHeap.size()) && (getElementKey(elementIndex) < getElementKey(elementIndex * 2))) || else if (((2 * elementIndex <= maxHeap.size()) && (getElementKey(elementIndex) < getElementKey(elementIndex * 2))) ||
((2 * elementIndex < maxHeap.size()) && (getElementKey(elementIndex) < getElementKey(elementIndex * 2)))) ((2 * elementIndex < maxHeap.size()) && (getElementKey(elementIndex) < getElementKey(elementIndex * 2))))

View File

@ -44,9 +44,9 @@ public class MinHeap implements Heap {
// Toggle an element up to its right place as long as its key is lower than its parent's // Toggle an element up to its right place as long as its key is lower than its parent's
private void toggleUp(int elementIndex) { private void toggleUp(int elementIndex) {
double key = minHeap.get(elementIndex - 1).getKey(); double key = minHeap.get(elementIndex - 1).getKey();
while (getElementKey((int) Math.floor(elementIndex / 2)) > key) { while (getElementKey((int) Math.floor(elementIndex / 2.0)) > key) {
swap(elementIndex, (int) Math.floor(elementIndex / 2)); swap(elementIndex, (int) Math.floor(elementIndex / 2.0));
elementIndex = (int) Math.floor(elementIndex / 2); elementIndex = (int) Math.floor(elementIndex / 2.0);
} }
} }
@ -96,7 +96,7 @@ public class MinHeap implements Heap {
minHeap.set(elementIndex - 1, getElement(minHeap.size())); minHeap.set(elementIndex - 1, getElement(minHeap.size()));
minHeap.remove(minHeap.size()); minHeap.remove(minHeap.size());
// Shall the new element be moved up... // 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 ? // ... or down ?
else if (((2 * elementIndex <= minHeap.size()) && (getElementKey(elementIndex) > getElementKey(elementIndex * 2))) || else if (((2 * elementIndex <= minHeap.size()) && (getElementKey(elementIndex) > getElementKey(elementIndex * 2))) ||
((2 * elementIndex < minHeap.size()) && (getElementKey(elementIndex) > getElementKey(elementIndex * 2)))) ((2 * elementIndex < minHeap.size()) && (getElementKey(elementIndex) > getElementKey(elementIndex * 2))))

View File

@ -14,7 +14,7 @@ public class CircleLinkedList<E> {
//For better O.O design this should be private allows for better black box design //For better O.O design this should be private allows for better black box design
private int size; private int size;
//this will point to dummy node; //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; //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() { public CircleLinkedList() {

View File

@ -86,9 +86,12 @@ public class DoublyLinkedList {
public Link deleteHead() { public Link deleteHead() {
Link temp = head; Link temp = head;
head = head.next; // oldHead <--> 2ndElement(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; tail = null;
} else {
head.previous = null; // oldHead --> 2ndElement(head) nothing pointing at old head so will be removed
}
return temp; return temp;
} }
@ -100,10 +103,13 @@ public class DoublyLinkedList {
public Link deleteTail() { public Link deleteTail() {
Link temp = tail; Link temp = tail;
tail = tail.previous; // 2ndLast(tail) <--> oldTail --> null tail = tail.previous; // 2ndLast(tail) <--> oldTail --> null
tail.next = null; // 2ndLast(tail) --> null
if (tail == null) { if (tail == null) {
head = null; head = null;
} else{
tail.next = null; // 2ndLast(tail) --> null
} }
return temp; return temp;
} }

View File

@ -74,7 +74,7 @@ public class NodeStack<Item> {
} else { } else {
newNs.setPrevious(NodeStack.head); newNs.setPrevious(NodeStack.head);
NodeStack.head.setNext(newNs); NodeStack.head.setNext(newNs);
NodeStack.head = newNs; NodeStack.head.setHead(newNs);
} }
NodeStack.setSize(NodeStack.getSize() + 1); NodeStack.setSize(NodeStack.getSize() + 1);
@ -89,7 +89,7 @@ public class NodeStack<Item> {
Item item = (Item) NodeStack.head.getData(); Item item = (Item) NodeStack.head.getData();
NodeStack.head = NodeStack.head.getPrevious(); NodeStack.head.setHead(NodeStack.head.getPrevious());
NodeStack.head.setNext(null); NodeStack.head.setNext(null);
NodeStack.setSize(NodeStack.getSize() - 1); NodeStack.setSize(NodeStack.getSize() - 1);

View File

@ -15,8 +15,8 @@ public class LevelOrderTraversal {
// Root of the Binary Tree // Root of the Binary Tree
Node root; Node root;
public LevelOrderTraversal() { public LevelOrderTraversal( Node root) {
root = null; this.root = root;
} }
/* function to print level order traversal of tree*/ /* function to print level order traversal of tree*/

View File

@ -19,11 +19,9 @@ public class LevelOrderTraversalQueue {
} }
} }
Node root;
/* Given a binary tree. Print its nodes in level order /* Given a binary tree. Print its nodes in level order
using array for implementing queue */ using array for implementing queue */
void printLevelOrder() { void printLevelOrder(Node root) {
Queue<Node> queue = new LinkedList<Node>(); Queue<Node> queue = new LinkedList<Node>();
queue.add(root); queue.add(root);
while (!queue.isEmpty()) { while (!queue.isEmpty()) {

View File

@ -13,14 +13,13 @@ public class ValidBSTOrNot {
} }
//Root of the Binary Tree //Root of the Binary Tree
Node root;
/* can give min and max value according to your code or /* can give min and max value according to your code or
can write a function to find min and max value of tree. */ can write a function to find min and max value of tree. */
/* returns true if given search tree is binary /* returns true if given search tree is binary
search tree (efficient version) */ search tree (efficient version) */
boolean isBST() { boolean isBST(Node root) {
return isBSTUtil(root, Integer.MIN_VALUE, return isBSTUtil(root, Integer.MIN_VALUE,
Integer.MAX_VALUE); Integer.MAX_VALUE);
} }

View File

@ -22,7 +22,7 @@ public class LongestIncreasingSubsequence {
private static int upperBound(int[] ar, int l, int r, int key) { private static int upperBound(int[] ar, int l, int r, int key) {
while (l < r - 1) { while (l < r - 1) {
int m = (l + r) / 2; int m = (l + r) >>> 1;
if (ar[m] >= key) if (ar[m] >= key)
r = m; r = m;
else else

View File

@ -25,7 +25,7 @@ public class MatrixChainMultiplication {
count++; count++;
} }
for (Matrix m : mArray) { 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(); size = mArray.size();

View File

@ -52,6 +52,6 @@ public class GCD {
// call gcd function (input array) // call gcd function (input array)
System.out.println(gcd(myIntArray)); // => 4 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
} }
} }

View File

@ -95,6 +95,32 @@ class Graph {
return Integer.compare(dist, other.dist); return Integer.compare(dist, other.dist);
} }
@Override
public boolean equals(Object object) {
if (this == object) return true;
if (object == null || getClass() != object.getClass()) return false;
if (!super.equals(object)) return false;
Vertex vertex = (Vertex) object;
if (dist != vertex.dist) return false;
if (name != null ? !name.equals(vertex.name) : vertex.name != null) return false;
if (previous != null ? !previous.equals(vertex.previous) : vertex.previous != null) return false;
if (neighbours != null ? !neighbours.equals(vertex.neighbours) : vertex.neighbours != null) return false;
return true;
}
@Override
public int hashCode() {
int result = super.hashCode();
result = 31 * result + (name != null ? name.hashCode() : 0);
result = 31 * result + dist;
result = 31 * result + (previous != null ? previous.hashCode() : 0);
result = 31 * result + (neighbours != null ? neighbours.hashCode() : 0);
return result;
}
@Override @Override
public String toString() { public String toString() {
return "(" + name + ", " + dist + ")"; return "(" + name + ", " + dist + ")";
@ -125,7 +151,7 @@ class Graph {
*/ */
public void dijkstra(String startName) { public void dijkstra(String startName) {
if (!graph.containsKey(startName)) { if (!graph.containsKey(startName)) {
System.err.printf("Graph doesn't contain start vertex \"%s\"\n", startName); System.err.printf("Graph doesn't contain start vertex \"%s\"%n", startName);
return; return;
} }
final Vertex source = graph.get(startName); final Vertex source = graph.get(startName);
@ -172,7 +198,7 @@ class Graph {
*/ */
public void printPath(String endName) { public void printPath(String endName) {
if (!graph.containsKey(endName)) { if (!graph.containsKey(endName)) {
System.err.printf("Graph doesn't contain end vertex \"%s\"\n", endName); System.err.printf("Graph doesn't contain end vertex \"%s\"%n", endName);
return; return;
} }
@ -189,4 +215,5 @@ class Graph {
System.out.println(); System.out.println();
} }
} }
} }

View File

@ -50,6 +50,7 @@ public class TopKWords {
} finally { } finally {
try { try {
// you always have to close the I/O streams // you always have to close the I/O streams
if (fis != null)
fis.close(); fis.close();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();

View File

@ -12,7 +12,7 @@ class TowerOfHanoi {
// Shift function is called in recursion for swapping the n-1 disc from the startPole to the intermediatePole // Shift function is called in recursion for swapping the n-1 disc from the startPole to the intermediatePole
shift(n - 1, startPole, endPole, intermediatePole); shift(n - 1, startPole, endPole, intermediatePole);
System.out.println("\nMove \"" + n + "\" from " + startPole + " --> " + endPole); // Result Printing System.out.println("%nMove \"" + n + "\" from " + startPole + " --> " + endPole); // Result Printing
// Shift function is called in recursion for swapping the n-1 disc from the intermediatePole to the endPole // Shift function is called in recursion for swapping the n-1 disc from the intermediatePole to the endPole
shift(n - 1, intermediatePole, startPole, endPole); shift(n - 1, intermediatePole, startPole, endPole);
} }

View File

@ -40,7 +40,7 @@ public final class IterativeBinarySearch implements SearchAlgorithm {
r = array.length - 1; r = array.length - 1;
while (l <= r) { while (l <= r) {
k = (l + r) / 2; k = (l + r) >>> 1;
cmp = key.compareTo(array[k]); cmp = key.compareTo(array[k]);
if (cmp == 0) { if (cmp == 0) {

View File

@ -64,7 +64,7 @@ class QuickSort implements SortAlgorithm {
**/ **/
private static <T extends Comparable<T>> int partition(T[] array, int left, int right) { private static <T extends Comparable<T>> int partition(T[] array, int left, int right) {
int mid = (left + right) / 2; int mid = (left + right) >>> 1;
T pivot = array[mid]; T pivot = array[mid];
while (left <= right) { while (left <= right) {

View File

@ -125,6 +125,8 @@ public class Caesar {
case 'D': case 'D':
case 'd': case 'd':
System.out.println("DECODED MESSAGE IS \n" + decode(message, shift)); System.out.println("DECODED MESSAGE IS \n" + decode(message, shift));
default:
System.out.println("default case");
} }
input.close(); input.close();
} }

View File

@ -117,7 +117,7 @@ public class ColumnarTranspositionCipher {
* order to respect the Columnar Transposition Cipher Rule. * order to respect the Columnar Transposition Cipher Rule.
*/ */
private static int numberOfRows(String word) { private static int numberOfRows(String word) {
if ((double) word.length() / keyword.length() > word.length() / keyword.length()) { if (word.length() / keyword.length() > word.length() / keyword.length()) {
return (word.length() / keyword.length()) + 1; return (word.length() / keyword.length()) + 1;
} else { } else {
return word.length() / keyword.length(); return word.length() / keyword.length();

View File

@ -31,6 +31,15 @@ public final class ClosestPair {
* Minimum point length. * Minimum point length.
*/ */
private static double minNum = Double.MAX_VALUE; private static double minNum = Double.MAX_VALUE;
public static void setMinNum(double minNum) {
ClosestPair.minNum = minNum;
}
public static void setSecondCount(int secondCount) {
ClosestPair.secondCount = secondCount;
}
/** /**
* secondCount * secondCount
*/ */
@ -213,7 +222,7 @@ public final class ClosestPair {
for (int i = 0; i < totalNum; i++) { for (int i = 0; i < totalNum; i++) {
double xGap = Math.abs(divideArray[divideX].x - divideArray[i].x); double xGap = Math.abs(divideArray[divideX].x - divideArray[i].x);
if (xGap < minValue) { if (xGap < minValue) {
secondCount++; // size of the array ClosestPair.setSecondCount(secondCount + 1); // size of the array
} else { } else {
if (divideArray[i].x > divideArray[divideX].x) { if (divideArray[i].x > divideArray[divideX].x) {
break; break;
@ -250,7 +259,7 @@ public final class ClosestPair {
minValue = length; minValue = length;
// Conditional for registering final coordinate // Conditional for registering final coordinate
if (length < minNum) { if (length < minNum) {
minNum = length; ClosestPair.setMinNum(length);
point1 = firstWindow[i]; point1 = firstWindow[i];
point2 = firstWindow[j]; point2 = firstWindow[j];
} }
@ -260,7 +269,7 @@ public final class ClosestPair {
} }
} }
} }
secondCount = 0; ClosestPair.setSecondCount(0);
return minValue; return minValue;
} }
@ -288,7 +297,7 @@ public final class ClosestPair {
length = Math.sqrt(Math.pow(xGap, 2) + Math.pow(yGap, 2)); length = Math.sqrt(Math.pow(xGap, 2) + Math.pow(yGap, 2));
// Conditional statement for registering final coordinate // Conditional statement for registering final coordinate
if (length < minNum) { if (length < minNum) {
minNum = length; ClosestPair.setMinNum(length);
} }
point1 = arrayParam[0]; point1 = arrayParam[0];
@ -311,7 +320,7 @@ public final class ClosestPair {
minValue = length; minValue = length;
if (length < minNum) { if (length < minNum) {
// Registering final coordinate // Registering final coordinate
minNum = length; ClosestPair.setMinNum(length);
point1 = arrayParam[i]; point1 = arrayParam[i];
point2 = arrayParam[j]; point2 = arrayParam[j];
} }