We may use cookies to help customize your experience, including performing
analytics and serving ads.
Learn More
| 28 | 5747 |
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;
}
}
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);
}
}
}
Nice
I have absolutely no idea how that works, which means that it is really good.
Nice
I have absolutely no idea how that works, which means that it is really good.
Just out of curiosity, why didn't you make it a static class (dunno if that's even something that exists in Java, but it does in AS3 anyway) so you could call something like Converter.toHexadeciman(number, bTenVal) ?
Other than that I think it looks pretty good, but I know practically nothing about Java
Please add comments to your code, I really want to know how each part of the code works, anyway, I find it a pretty good function. Is the first time that I analize a java code and is a lot similar to AS3. But I wonder, how do you show the output?
sorry for double post but, why did you define an empty function in:
public Converter()
{
}
woops, and sorry for the first comment, I really didnt see your code comments, sorry :3
why didn't you make it a static class
But I wonder, how do you show the output?
sorry for double post but, why did you define an empty function
Adding on to my execution program from yesterday, I've created something that now deletes any non-numerical characters, should some naughty person decide to try and sneak them in. :P
Here's the code:
noLetterHelp = help;
hLength = (byte)noLetterHelp.length();
int c = 0;
while(c < hLength)
{
if(noLetterHelp.charAt(c) != '0' &&
noLetterHelp.charAt(c) != '1' &&
noLetterHelp.charAt(c) != '2' &&
noLetterHelp.charAt(c) != '3' &&
noLetterHelp.charAt(c) != '4' &&
noLetterHelp.charAt(c) != '5' &&
noLetterHelp.charAt(c) != '6' &&
noLetterHelp.charAt(c) != '7' &&
noLetterHelp.charAt(c) != '8' &&
noLetterHelp.charAt(c) != '9' &&
c < hLength)
{
noLetterHelp = noLetterHelp.substring(0, c) + noLetterHelp.substring(c + 1);
hLength -= 1;
} else {
c += 1;
}
}
if(noLetterHelp != help)
{
JOptionPane.showMessageDialog(
null,
"Your input, " + help + ", contained illegal characters and was changed to " + noLetterHelp + ".",
"Notice",
JOptionPane.INFORMATION_MESSAGE);
}
I've created something that now deletes any non-numerical characters
Whoo! I finally deem myself worthy of posting here!
I'm afraid I don't understand what you mean... What I originally wanted it to do was not create an object at all, but just convert a given number; however, I don't yet know how to create a class like that in Java yet. But the static idea is interesting; could elaborate?
I think pixelsmash is saying, you should have an external Java file that contains a couple of functions, so that you can call on these functions more easily.
If (the character) is not >= 0
And on an irrelevant note, i believe there is a function in C++ that does this automatically for you.
That is among the easiest things to accomplish in life.
Well I can't say anything about Java, but in AS3 it's more or less like this: you have a static class, which contains static functions. These functions are accessible by using the classname.functionname, which would make this Converter.toHex or something like that. This also means you don't have to instantiate the class, ie. new Converter(), which is probably nicer memory-wise - though it's probably not really an issue with such a small class.
Whoops! Messed up my previous post.
A friend of mine told me that Java's String class has a "replaceAll" method. I believe the syntax is like this:
StringName.replaceAll(replaceWhat, withWhat)
-_- Thank you for that kind comment.
Snowguy13, instantiating a class uses memory. Granted, most machines have more than enough, but it is still a good practice to not use more than you need. That said, if you are using a class just for reference or computing type functions then you do not need to instantiate a class just to use a function within that class.
Therefore, you would find more use in using the following statements:
For class declaration:
public final class Converter (this will define the class and non-instantiating)
public static String toHex(long helper){
**use the same code you have here except omit references to baseTenPower since helper is loaded directly**}
Note, also, that it would, probably be wise to overload the method to allow for use of int, byte, etc to be sent (as well as an empty argument)
Using this setup, you call the method from within your program and send its argument directly.
As an example, using your existing test code:
JOptionPane.showMessageDialog(
null,
"Your number, " + number + ", in hexadecimal format is " + toHexadecimal(number) + ".",
"Result",
JOptionPane.INFORMATION_MESSAGE);
For reference to this type of setup, see the api for the java.lang.Math class. If you use Netbeans, add this line of code to your class (double x = pow(12,2), then right-click on it and select "Show Source" which will open the entire class source code and you can see the Sun code.
Don't know if all this helps or makes it worse for you, but, figured I'd throw it in.
Good Luck.
It's no offense to you, it's just that this isn't what someone would call a "good" programming forum, I would hardly call it a programming forum at all.
You must be logged in to post a reply!
We may use cookies to help customize your experience, including performing
analytics and serving ads.
Learn More