Forums

ForumsProgramming Forum

Java Shenanigans

Posted Oct 16, '11 at 8:33am

master565

master565

4,288 posts

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.

 

Posted Oct 16, '11 at 9:55am

snowguy13

snowguy13

2,172 posts

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

Posted Oct 16, '11 at 9:56am

snowguy13

snowguy13

2,172 posts

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)

 

Posted Oct 16, '11 at 10:58am

master565

master565

4,288 posts

-_- 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.
 

Posted Oct 16, '11 at 5:19pm

Graellic

Graellic

4 posts

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.

 

Posted Oct 16, '11 at 10:38pm

snowguy13

snowguy13

2,172 posts

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!
 

Posted Oct 16, '11 at 10:42pm

snowguy13

snowguy13

2,172 posts

Oh, whoops; forgot to ask: when you say...

it would, probably be wise to overload the method

...do you mean create multiple methods with the same name that do the same thing except they accept different data types? So, for example:
public static String toHex(long helper){
/*Put code here*/
}

public static String toHex(int helper){
/*Put same code here*/
}

public static String toHex(short helper){
/*Put same code here*/
}

public static String toHex(byte helper){
/*Put same code here*/
}


And then for the empty argument exception:

public static String toHex(){
return null;
}
 

Posted Oct 17, '11 at 5:19pm

Graellic

Graellic

4 posts

Exactly

 

Posted Oct 17, '11 at 5:35pm

Graellic

Graellic

4 posts

Oh, regarding that (sorry, didn't think to add this until after I posted the "Exactly&quot, you do not need to duplicate the code (i.e. where you typed "Put same code here&quot

Instead try:

public static String toHex(long helper){
** Put code here to convert an long to hex **}

Then, put code in overloaded methods to call the method for converting from a long and send it the argument cast as a long **

As an example:
public static String toHex(byte helper){
return toHex( (long)helper&0xFF );
}

public static String toHex(int helper){
return toHex( helper );
} **Note that an int should not need a cast as it should promote automatically, it's demoting that is usually an issue.

And so on. This way you're only doing the work in one method and just using the overloaded methods to convert to that data type. Of course, you'll need to decide which data types are actually in the scope of your methods (i.e. do you really need to be able to convert floats, longs, doubles, etc.?)

 

Posted Oct 17, '11 at 6:02pm

snowguy13

snowguy13

2,172 posts

@Graellic

Now that I think about it, I should actually only have to make one method. I do not intend to convert doubles, as I do not plan to devote any time attempting to understand hexadecimal-decimals (heh, decimal-decimals); and bytes, shorts, and ints are all within the range of long (therefore I shouldn't have to convert anything).

What I mean is that in Java, this code is acceptable:

int number;
long otherNumber;

otherNumber = number;


Because long has a wider range than int, there is no error storing an int in a long variable.
 
Reply to Java Shenanigans

You must be logged in to post a reply!