ForumsProgramming ForumJava Shenanigans

28 13675
snowguy13
offline
snowguy13
2,159 posts
Nomad

Whoo! I finally deem myself worthy of posting here!

I've been messing around with Java--yeah, it's not ActionScript, sorry--and I want to share my random creations here, and collaborate with others who love programming!
=====================================================================
First, to kick things off, here's a random program I wrote that converts base-ten integers to hexadecimal format!

This is the first class that creates an object. The object created has a method that allow it to be converted.

/**
* This class will eventually be able to
* convert among hexadecimal, binary,
* base-ten, and possibly systems with
* other bases.
*/

import java.lang.*;

public class Converter
{

String[] hexValues = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"};

long baseTenValue;

long helper;

String hexadecimal = "";

public Converter(long bTenVal)
{

baseTenValue = bTenVal;

}

public Converter()
{
}

public void setNumber(long num)
{

baseTenValue = num;

}

public String toHexadecimal()
{

hexadecimal = "";

final int STARTING_POWER = 100;

byte[] numericalPlaceValue = new byte[STARTING_POWER + 1];

String[] stringPlaceValue = new String[STARTING_POWER + 1];

helper = baseTenValue;

int p;

//Find what numerical values should go in each place (hexadecimal goes up to 15)

for(p = STARTING_POWER; p >= 0; p--) {

numericalPlaceValue[p] = (byte)(helper / (Math.pow(16, p)));
helper -= numericalPlaceValue[p] * (Math.pow(16, p));

}

//Convert the values from above into strings, and to letters if necessary (ie, 15 -> F)

for(p = STARTING_POWER; p >= 0; p--) {

stringPlaceValue[p] = hexValues[numericalPlaceValue[p]];

}

//Combine the values from above into one long string, which is the hexadecimal value

for(p = STARTING_POWER; p >= 0; p--) {

hexadecimal += stringPlaceValue[p];

}

//Cut off any zeros that precede the first value

while(hexadecimal.charAt(0) == '0' {

hexadecimal = hexadecimal.substring(1);

}

return hexadecimal;
}
}

----------

This class uses the class above by creating an instance, and then converting that instance. It also asks if another number should be converted, after converting and displaying the first one.
import javax.swing.JOptionPane;

public class ToHexTest
{

public static void main(String[] args)
{

Converter test = new Converter();

long number;

String help;

byte choice = 0;

while(choice == 0) {

help = JOptionPane.showInputDialog(
null,
"Input an integer to convert to hexadecimal format.",
"What to convert?",
JOptionPane.QUESTION_MESSAGE);

number = Long.parseLong(help);

test.setNumber(number);

JOptionPane.showMessageDialog(
null,
"Your number, " + number + ", in hexadecimal format is " + test.toHexadecimal() + ".",
"Result",
JOptionPane.INFORMATION_MESSAGE);

choice = (byte)JOptionPane.showConfirmDialog(
null,
"Evaulate another number?",
"Continue?",
JOptionPane.YES_NO_OPTION);
}
}
}

=====================================================================
I'd like to know what others think!
  • 28 Replies
Showing 31-30 of 28