mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-08 02:04:31 +08:00
Comment revisions
This commit is contained in:
@ -5,7 +5,7 @@ import java.util.Scanner;
|
||||
/**
|
||||
* This class converts a Decimal number to a Binary number
|
||||
*
|
||||
* @author Unknown
|
||||
*
|
||||
*/
|
||||
class DecimalToBinary {
|
||||
|
||||
|
@ -1,5 +1,6 @@
|
||||
package Conversions;
|
||||
|
||||
//hex = [0 - 9] -> [A - F]
|
||||
class DecimalToHexaDecimal {
|
||||
private static final int sizeOfIntInHalfBytes = 8;
|
||||
private static final int numberOfBitsInAHalfByte = 4;
|
||||
|
@ -5,7 +5,7 @@ import java.util.Scanner;
|
||||
/**
|
||||
* This class converts Decimal numbers to Octal Numbers
|
||||
*
|
||||
* @author Unknown
|
||||
*
|
||||
*/
|
||||
public class DecimalToOctal {
|
||||
/**
|
||||
@ -13,6 +13,8 @@ public class DecimalToOctal {
|
||||
*
|
||||
* @param args Command line Arguments
|
||||
*/
|
||||
|
||||
//enter in a decimal value to get Octal output
|
||||
public static void main(String[] args) {
|
||||
Scanner sc = new Scanner(System.in);
|
||||
int n, k, d, s = 0, c = 0;
|
||||
|
@ -1,5 +1,7 @@
|
||||
package Conversions;
|
||||
|
||||
//Hex [0-9],[A-F] -> Binary [0,1]
|
||||
|
||||
public class HexaDecimalToBinary {
|
||||
|
||||
private final int LONG_BITS = 8;
|
||||
@ -9,7 +11,7 @@ public class HexaDecimalToBinary {
|
||||
int conHex = Integer.parseInt(numHex, 16);
|
||||
// Hex a Binary:
|
||||
String binary = Integer.toBinaryString(conHex);
|
||||
// Presentation:
|
||||
// Output:
|
||||
System.out.println(numHex + " = " + completeDigits(binary));
|
||||
}
|
||||
|
||||
|
@ -1,9 +1,29 @@
|
||||
package Conversions;
|
||||
|
||||
/**
|
||||
* Converting Integers into Roman Numerals
|
||||
*
|
||||
*('I', 1);
|
||||
*('IV',4);
|
||||
*('V', 5);
|
||||
*('IV',9);
|
||||
*('X', 10);
|
||||
*('XL',40;
|
||||
*('L', 50);
|
||||
*('XC',90);
|
||||
*('C', 100);
|
||||
*('D', 500);
|
||||
*('M', 1000);
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
public class IntegerToRoman {
|
||||
private static int[] allArabianRomanNumbers = new int[]{1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
|
||||
private static String[] allRomanNumbers = new String[]{"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
|
||||
|
||||
//Value must be > 0
|
||||
|
||||
public static String integerToRoman(int num) {
|
||||
if (num <= 0) {
|
||||
return "";
|
||||
|
@ -13,6 +13,7 @@ public class RomanToInteger {
|
||||
put('D', 500);
|
||||
put('M', 1000);
|
||||
}};
|
||||
//Roman Number = Roman Numerals
|
||||
|
||||
/**
|
||||
* This function convert Roman number into Integer
|
||||
|
@ -15,7 +15,7 @@ public class AbsoluteMax {
|
||||
}
|
||||
|
||||
/**
|
||||
* get the value, it's absolute value is max
|
||||
* get the value, return the absolute max value
|
||||
*
|
||||
* @param numbers contains elements
|
||||
* @return the absolute max value
|
||||
|
@ -15,7 +15,7 @@ public class AbsoluteMin {
|
||||
}
|
||||
|
||||
/**
|
||||
* get the value, it's absolute value is min
|
||||
* get the value, returns the absolute min value min
|
||||
*
|
||||
* @param numbers contains elements
|
||||
* @return the absolute min value
|
||||
|
@ -1,25 +1,28 @@
|
||||
package Maths;
|
||||
|
||||
//change around 'n' for different factorial results
|
||||
public class Factorial {
|
||||
public static void main(String[] args) {
|
||||
int n = 5;
|
||||
System.out.println(n + "! = " + factorial(n));
|
||||
}
|
||||
|
||||
//Factorial = n! = n1 * (n-1) * (n-2)*...1
|
||||
|
||||
/**
|
||||
* Calculate factorial
|
||||
* Calculate factorial N
|
||||
*
|
||||
* @param n the number
|
||||
* @return the factorial of {@code n}
|
||||
*/
|
||||
public static long factorial(int n) {
|
||||
if (n < 0) {
|
||||
throw new ArithmeticException("n < 0");
|
||||
throw new ArithmeticException("n < 0"); //Dont work with less than 0
|
||||
}
|
||||
long fac = 1;
|
||||
for (int i = 1; i <= n; ++i) {
|
||||
fac *= i;
|
||||
}
|
||||
return fac;
|
||||
return fac; //Return factorial
|
||||
}
|
||||
}
|
||||
|
@ -1,11 +1,12 @@
|
||||
package maths;
|
||||
|
||||
//POWER (exponentials) Examples (a^b)
|
||||
public class Pow {
|
||||
public static void main(String[] args) {
|
||||
assert pow(2, 0) == Math.pow(2, 0);
|
||||
assert pow(0, 2) == Math.pow(0, 2);
|
||||
assert pow(2, 10) == Math.pow(2, 10);
|
||||
assert pow(10, 2) == Math.pow(10, 2);
|
||||
assert pow(2, 0) == Math.pow(2, 0); // == 1
|
||||
assert pow(0, 2) == Math.pow(0, 2); // == 0
|
||||
assert pow(2, 10) == Math.pow(2, 10); // == 1024
|
||||
assert pow(10, 2) == Math.pow(10, 2); // == 100
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user