Home > Sample essays > ImproveCoding Skills with Java: a Guide to Conversion Methods

Essay: ImproveCoding Skills with Java: a Guide to Conversion Methods

Essay details and download:

  • Subject area(s): Sample essays
  • Reading time: 8 minutes
  • Price: Free download
  • Published: 1 April 2019*
  • Last Modified: 23 July 2024
  • File format: Text
  • Words: 598 (approx)
  • Number of pages: 3 (approx)

Text preview of this essay:

This page of the essay has 598 words.



import java.awt.List;

import java.util.ArrayList;

import java.util.Arrays;

import java.util.Random;

public class Program {

public static String[] binaryInput =

{

"0010010",

"0100100",

"0010010",

"0100010",

"0001001",

"0100000",

"0010001",

"1000010",

"1000100",

"0010100"

};

public static String[] hexInput =

{

"87",

"32",

"11",

"43",

"F1",

"97",

"42",

"56",

"FF",

"6B",

};

public static String[]  base20Input =

{

"87",

"32",

"11",

"43",

"F1",

"97",

"42",

"56",

"FF",

"6B",

};

public static String[] base3input =

{

"0020010",

"0100200",

"0010010",

"0220010",

"0002201",

"0200020",

"0210021",

"1200010",

"1000100",

"0010100"

};

public static void main(String[] args) {

// TODO Auto-generated method stub

//////////////////////////////

///////////  MAIN  SUB  //\//////////

//////////////////////////////

//HEX TO INT TEST

String B20Array[] = {"1", "10", "AA", "1F", "2B"};

int testArray[];

testArray = ConvertBase20ToInt(B20Array);

for (int i = 0; i < ConvertBase20ToInt(B20Array).length; i++)

{

System.out.println(testArray[i]);

}

}

public static int[] ConvertBinaryToInt(String[] binaryArray) {

int[] resultsArray = new int[binaryArray.length];

for(int i = 0; i < binaryArray.length; i++)

{

resultsArray[i] = convertToBaseTwo(binaryArray[i]);

}

return resultsArray;

}

//FOR BINARY TO INT

public static int convertToBaseTwo(String toConvert) {

int result = 0;

char[] inputToCharArray = toConvert.toCharArray(); // Saves string to array of chars

int total = 0;

int columnMultiplier = 1;

for(int i = 0; i < inputToCharArray.length; i++) // Loop through array

{

int index = inputToCharArray.length – (i+1); // Uses index to loop backwards through array

if (inputToCharArray[index] == '1') // Multiplies positive values with place value

{

total = total + columnMultiplier; // Keeps running total for adding the values

}

columnMultiplier = columnMultiplier * 2; // Increases place value (since moving backwards)

}

//result = Integer.toString(total);

result = total;

return result;

}

public static int[] ConvertHexToInt(String[] hexArray) {

int[] resultsArray = new int[hexArray.length];

for(int i = 0; i < hexArray.length; i++)

{

resultsArray[i] = convertToBaseSix(hexArray[i]);

}

return resultsArray;

}

public static int convertToBaseSix(String toConvert) {

int result = 0;

char[] inputToCharArray = toConvert.toCharArray(); // Saves string to array of chars

int total = 0;

int columnMultiplier = 1;

//THIS IS UGLY AF BUT IT WORKS

for(int i = 0; i < inputToCharArray.length; i++) // Loop through array

{

int index = inputToCharArray.length – (i+1); // Uses index to loop backwards through array

if (inputToCharArray[index] == '1') // Multiplies positive values with place value

{

total = total + (1 * columnMultiplier); // Keeps running total for adding the values

}

if (inputToCharArray[index] == '2') // Multiplies positive values with place value

{

total = total + (2 * columnMultiplier); // Keeps running total for adding the values

}

if (inputToCharArray[index] == '3') // Multiplies positive values with place value

{

total = total + (3 * columnMultiplier); // Keeps running total for adding the values

}

if (inputToCharArray[index] == '4') // Multiplies positive values with place value

{

total = total + (4 * columnMultiplier); // Keeps running total for adding the values

}

if (inputToCharArray[index] == '5') // Multiplies positive values with place value

{

total = total + (5 * columnMultiplier); // Keeps running total for adding the values

}

if (inputToCharArray[index] == '6') // Multiplies positive values with place value

{

total = total + (6 * columnMultiplier); // Keeps running total for adding the values

}

if (inputToCharArray[index] == '7') // Multiplies positive values with place value

{

total = total + (7 * columnMultiplier); // Keeps running total for adding the values

}

if (inputToCharArray[index] == '8') // Multiplies positive values with place value

{

total = total + (8 * columnMultiplier); // Keeps running total for adding the values

}

if (inputToCharArray[index] == '9') // Multiplies positive values with place value

{

total = total + (9 * columnMultiplier); // Keeps running total for adding the values

}

if (inputToCharArray[index] == 'A') // Multiplies positive values with place value

{

total = total + (10 * columnMultiplier); // Keeps running total for adding the values

}

if (inputToCharArray[index] == 'B') // Multiplies positive values with place value

{

total = total + (11 * columnMultiplier); // Keeps running total for adding the values

}

if (inputToCharArray[index] == 'C') // Multiplies positive values with place value

{

total = total + (12 * columnMultiplier); // Keeps running total for adding the values

}

if (inputToCharArray[index] == 'D') // Multiplies positive values with place value

{

total = total + (13 * columnMultiplier); // Keeps running total for adding the values

}

if (inputToCharArray[index] == 'E') // Multiplies positive values with place value

{

total = total + (14 * columnMultiplier); // Keeps running total for adding the values

}

if (inputToCharArray[index] == 'F') // Multiplies positive values with place value

{

total = total + (15 * columnMultiplier); // Keeps running total for adding the values

}

columnMultiplier = columnMultiplier * 16; // Increases place value (since moving backwards)

}

result = total;

return result;

}

public static int[] Union(int[] intArrayA, int[] intArrayB) {

ArrayList<Integer> unionList = new ArrayList<Integer>();

int[] resultsArray = new int[unionList.size()];

for(int i = 0; i<unionList.size(); i++) {

resultsArray[i] = unionList.get(i);

}

return resultsArray;

}

public static int[] Intersection(int[] intArrayA, int[] intArrayB) {

ArrayList<Integer> intersectList = new ArrayList<Integer>();

int[] resultsArray = new int[intersectList.size()];

for(int i = 0; i<intersectList.size(); i++) {

resultsArray[i] = intersectList.get(i);

}

return resultsArray;

}

public static int[] Difference(int[] intArrayA, int[] intArrayB) {

ArrayList<Integer> differenceList = new ArrayList<Integer>();

int[] resultsArray = new int[differenceList.size()];

for(int i = 0; i<differenceList.size(); i++) {

resultsArray[i] = differenceList.get(i);

}

return resultsArray;

}

public static int Sum (int[] input) {

int result = 0;

for( int i = 0; i < input.length; i++)

{

result = result + input[i];

}

return result;

}

public static double Mean (int[] input) {

double output = 0;

output = Sum(input);

output = output / input.length;

return output;

}

public static int[] ConvertBase20ToInt(String[] b20Array) {

int[] resultsArray = new int[b20Array.length];

for(int i = 0; i < b20Array.length; i++)

{

resultsArray[i] = convertToBaseTwenty(b20Array[i]);

}

return resultsArray;

}

public static int convertToBaseTwenty(String toConvert) {

int result = 0;

char[] inputToCharArray = toConvert.toCharArray(); // Saves string to array of chars

int total = 0;

int columnMultiplier = 1;

//THIS IS UGLY AF BUT IT WORKS

for(int i = 0; i < inputToCharArray.length; i++) // Loop through array

{

int index = inputToCharArray.length – (i+1); // Uses index to loop backwards through array

if (inputToCharArray[index] == '1') // Multiplies positive values with place value

{

total = total + (1 * columnMultiplier); // Keeps running total for adding the values

}

if (inputToCharArray[index] == '2') // Multiplies positive values with place value

{

total = total + (2 * columnMultiplier); // Keeps running total for adding the values

}

if (inputToCharArray[index] == '3') // Multiplies positive values with place value

{

total = total + (3 * columnMultiplier); // Keeps running total for adding the values

}

if (inputToCharArray[index] == '4') // Multiplies positive values with place value

{

total = total + (4 * columnMultiplier); // Keeps running total for adding the values

}

if (inputToCharArray[index] == '5') // Multiplies positive values with place value

{

total = total + (5 * columnMultiplier); // Keeps running total for adding the values

}

if (inputToCharArray[index] == '6') // Multiplies positive values with place value

{

total = total + (6 * columnMultiplier); // Keeps running total for adding the values

}

if (inputToCharArray[index] == '7') // Multiplies positive values with place value

{

total = total + (7 * columnMultiplier); // Keeps running total for adding the values

}

if (inputToCharArray[index] == '8') // Multiplies positive values with place value

{

total = total + (8 * columnMultiplier); // Keeps running total for adding the values

}

if (inputToCharArray[index] == '9') // Multiplies positive values with place value

{

total = total + (9 * columnMultiplier); // Keeps running total for adding the values

}

if (inputToCharArray[index] == 'A') // Multiplies positive values with place value

{

total = total + (10 * columnMultiplier); // Keeps running total for adding the values

}

if (inputToCharArray[index] == 'B') // Multiplies positive values with place value

{

total = total + (11 * columnMultiplier); // Keeps running total for adding the values

}

if (inputToCharArray[index] == 'C') // Multiplies positive values with place value

{

total = total + (12 * columnMultiplier); // Keeps running total for adding the values

}

if (inputToCharArray[index] == 'D') // Multiplies positive values with place value

{

total = total + (13 * columnMultiplier); // Keeps running total for adding the values

}

if (inputToCharArray[index] == 'E') // Multiplies positive values with place value

{

total = total + (14 * columnMultiplier); // Keeps running total for adding the values

}

if (inputToCharArray[index] == 'F') // Multiplies positive values with place value

{

total = total + (15 * columnMultiplier); // Keeps running total for adding the values

}

if (inputToCharArray[index] == 'G') // Multiplies positive values with place value

{

total = total + (16 * columnMultiplier); // Keeps running total for adding the values

}

if (inputToCharArray[index] == 'H') // Multiplies positive values with place value

{

total = total + (17 * columnMultiplier); // Keeps running total for adding the values

}

if (inputToCharArray[index] == 'I') // Multiplies positive values with place value

{

total = total + (18 * columnMultiplier); // Keeps running total for adding the values

}

if (inputToCharArray[index] == 'J') // Multiplies positive values with place value

{

total = total + (19 * columnMultiplier); // Keeps running total for adding the values

}

columnMultiplier = columnMultiplier * 20; // Increases place value (since moving backwards)

}

result = total;

return result;

}

public static int Range (int[] input) {

int output = 0;

int highest = 0; // sets variables for bounds

int lowest = 999;

for(int i = 0; i < input.length; i++)

{

if (input[i] > highest) // updates upper bound

{

highest = input[i];

}

if (input[i] < lowest) // updates lower bound

{

lowest = input[i];

}

}

output = highest – lowest;

return output;

}

public static int[] ConvertBase3ToInt(String[] b3Array) {

int[] resultsArray = new int[b3Array.length];

for(int i = 0; i < b3Array.length; i++)

{

resultsArray[i] = convertToBaseThree(b3Array[i]);

}

return resultsArray;

}

public static int convertToBaseThree(String toConvert) {

int result = 0;

char[] inputToCharArray = toConvert.toCharArray(); // Saves string to array of chars

int total = 0;

int columnMultiplier = 1;

//THIS IS UGLY AF BUT IT WORKS

for(int i = 0; i < inputToCharArray.length; i++) // Loop through array

{

int index = inputToCharArray.length – (i+1); // Uses index to loop backwards through array

if (inputToCharArray[index] == '1') // Multiplies positive values with place value

{

total = total + (1 * columnMultiplier); // Keeps running total for adding the values

}

if (inputToCharArray[index] == '2') // Multiplies positive values with place value

{

total = total + (2 * columnMultiplier); // Keeps running total for adding the values

}

if (inputToCharArray[index] == '3') // Multiplies positive values with place value

{

total = total + (3 * columnMultiplier); // Keeps running total for adding the values

}

if (inputToCharArray[index] == '4') // Multiplies positive values with place value

{

total = total + (4 * columnMultiplier); // Keeps running total for adding the values

}

if (inputToCharArray[index] == '5') // Multiplies positive values with place value

{

total = total + (5 * columnMultiplier); // Keeps running total for adding the values

}

if (inputToCharArray[index] == '6') // Multiplies positive values with place value

{

total = total + (6 * columnMultiplier); // Keeps running total for adding the values

}

if (inputToCharArray[index] == '7') // Multiplies positive values with place value

{

total = total + (7 * columnMultiplier); // Keeps running total for adding the values

}

if (inputToCharArray[index] == '8') // Multiplies positive values with place value

{

total = total + (8 * columnMultiplier); // Keeps running total for adding the values

}

if (inputToCharArray[index] == '9') // Multiplies positive values with place value

{

total = total + (9 * columnMultiplier); // Keeps running total for adding the values

}

if (inputToCharArray[index] == 'A') // Multiplies positive values with place value

{

total = total + (10 * columnMultiplier); // Keeps running total for adding the values

}

if (inputToCharArray[index] == 'B') // Multiplies positive values with place value

{

total = total + (11 * columnMultiplier); // Keeps running total for adding the values

}

if (inputToCharArray[index] == 'C') // Multiplies positive values with place value

{

total = total + (12 * columnMultiplier); // Keeps running total for adding the values

}

if (inputToCharArray[index] == 'D') // Multiplies positive values with place value

{

total = total + (13 * columnMultiplier); // Keeps running total for adding the values

}

if (inputToCharArray[index] == 'E') // Multiplies positive values with place value

{

total = total + (14 * columnMultiplier); // Keeps running total for adding the values

}

if (inputToCharArray[index] == 'F') // Multiplies positive values with place value

{

total = total + (15 * columnMultiplier); // Keeps running total for adding the values

}

columnMultiplier = columnMultiplier * 3; // Increases place value (since moving backwards)

}

result = total;

return result;

}

public static int Mode (int[] input) {

int output = 0;

return output;

}

public static double Median (int[] input) {

int output = 0;

Arrays.sort(input);

output = input[(input.length / 2)];

return output;

}

public static double StandardDeviation (int[] input) {

double output = 0;

return output;

}

public static double HarmonicMean (int[] input) {

double output = 0;

return output;

}

public static double ComplexSumFunctionA (int[] input) {

double output = 0;

return output;

}

public static int[][] MatrixScalarMultiplication (int[][] matrix, int scalar){

int[][] resultMatrix = new int[matrix.length][matrix[0].length];

return resultMatrix;

}

public static int[][] ComplexMatrixOperation (int[][] matrix, int scalar){

int[][] resultMatrix = new int[matrix.length][matrix[0].length];

return resultMatrix;

}

public static double[] GenerateRandomDistributionForSpecificDie(int iterations) {

double[] resultsArray = new double[5121];

Random rng = new Random(1234);

for(int i = 0; i<iterations; i++) {

}

return resultsArray;

}

public static double[] GenerateRandomDistribution(int dieSides, int rolls, int iterations) {

double[] resultsArray = new double[(dieSides*rolls)+1];

Random rng = new Random(1234);

for(int i = 0; i<iterations; i++) {

}

return resultsArray;

}

public static double GetProbabilityOfResultOrHigher(int dieSides, int rolls, int sum) {

double[] distribution = GenerateRandomDistribution(dieSides,rolls,100000);

double result = 0;

return result;

}

}

About this essay:

If you use part of this page in your own work, you need to provide a citation, as follows:

Essay Sauce, ImproveCoding Skills with Java: a Guide to Conversion Methods. Available from:<https://www.essaysauce.com/sample-essays/2018-12-10-1544450794/> [Accessed 13-04-26].

These Sample essays have been submitted to us by students in order to help you with your studies.

* This essay may have been previously published on EssaySauce.com and/or Essay.uk.com at an earlier date than indicated.