ForumsProgramming ForumJava Shenanigans

28 13662
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
ExplosionsHurt
offline
ExplosionsHurt
249 posts
Nomad

Nice

I have absolutely no idea how that works, which means that it is really good.

snowguy13
offline
snowguy13
2,159 posts
Nomad

Nice

I have absolutely no idea how that works, which means that it is really good.

Haha, thanks! If you want me to write up a quick pseudo code, I can (later this evening, it would be)! :P

I'm also working on a game right now! Unfortunately, it's only text, as I do not know how to work with images in Java (yet). But I will post parts of its code sometime, too!
PixelSmash
offline
PixelSmash
566 posts
Nomad

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

Carlytoon
offline
Carlytoon
324 posts
Nomad

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?

Carlytoon
offline
Carlytoon
324 posts
Nomad

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

snowguy13
offline
snowguy13
2,159 posts
Nomad

why didn't you make it a static class

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?
But I wonder, how do you show the output?

The output? Well, in general, you can tell Java to display something using the code:

System.out.print("what to print here"

However, I don't like how that appears, so I used JOptionPane, which shows up as dialogs like this.

Furthermore, I think I must clarify that there are actually two classes there; one creates an object that doesn't actually display anything to the user, and the other uses the object created by the first and displays information.

I'm working on a pseudo code to show, when I can. It should clarify some things.
sorry for double post but, why did you define an empty function

First of all, that's actually a constructor. Constructors are methods that are automatically called when you create an object. For example, in my ToHexTest class, take the code:

Converter test = new Converter();

When Java gets to new Converter(), it looks for the method "Converter" which is actually the constructor.

Now, you may ask why I have two constructors:

public Converter(long bTenVal)
{

baseTenValue = bTenVal;

}

public Converter()
{
}


One constructor, the one with an argument (long bTenVal), allows a coder to define a Converter object while simultaneously setting the value it is meant to convert. The other constructor that has no arguments just creates a Converter object, and doesn't set any values.
snowguy13
offline
snowguy13
2,159 posts
Nomad

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);

}

It works! MUAHAHAHAHAA!
master565
offline
master565
4,107 posts
Nomad

I've created something that now deletes any non-numerical characters


Why not just have (i don't know java) something along the lines of

If (the character) is not >= 0

That way it automatically checks if it's a number or not.

And on an irrelevant note, i believe there is a function in C++ that does this automatically for you.

Whoo! I finally deem myself worthy of posting here!


That is among the easiest things to accomplish in life.
PixelSmash
offline
PixelSmash
566 posts
Nomad

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?


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.
master565
offline
master565
4,107 posts
Nomad

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.

snowguy13
offline
snowguy13
2,159 posts
Nomad

If (the character) is not >= 0

I would get an "incompatible types" error. The left side of the logical test would be a character, and the right would be an integer.
And on an irrelevant note, i believe there is a function in C++ that does this automatically for you.

A friend of mine told me that Java's String class has a "replaceAll" method. I believe the syntax is like this:
That is among the easiest things to accomplish in life.

-_- Thank you for that kind comment.
StringName.replaceAll(replaceWhat, withWhat)
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.

Ah, YES! That is EXACTLY what I wanted to do in the first place! However, I tried that and ended up with little success... Probably just a silly syntax error (I do that a lot).
snowguy13
offline
snowguy13
2,159 posts
Nomad

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)

master565
offline
master565
4,107 posts
Nomad

-_- Thank you for that kind comment.


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.
Graellic
offline
Graellic
4 posts
Jester

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.

snowguy13
offline
snowguy13
2,159 posts
Nomad

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.

Yeah, my apologies for overreacting. D:

@Graellic

Thanks a ton! I think that was what I wanted to do all along, and just didn't know how. I wanted the Converter to be like JOptionPane, where you simply call upon the class without creating an object, which is what I predict your suggestions above will allow me to do! Thanks, again!
Showing 1-15 of 28