Forums

ForumsProgramming Forum

Java Shenanigans

Posted Oct 12, '11 at 8:53pm

snowguy13

snowguy13

700 posts

Whoo! I finally deem myself worthy of posting here! :D

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! :D

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! :D

 

Posted Oct 13, '11 at 6:36am

ExplosionsHurt

ExplosionsHurt

247 posts

Nice :D

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

 

Posted Oct 13, '11 at 7:42am

snowguy13

snowguy13

700 posts

Nice :D

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! :D

 

Posted Oct 13, '11 at 11:30am

PixelSmash

PixelSmash

567 posts

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

 

Posted Oct 13, '11 at 7:35pm

Carlytoon

Carlytoon

329 posts

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?

 

Posted Oct 13, '11 at 7:41pm

Carlytoon

Carlytoon

329 posts

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

 

Posted Oct 13, '11 at 8:58pm

snowguy13

snowguy13

700 posts

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.

 

Posted Oct 14, '11 at 10:37pm

snowguy13

snowguy13

700 posts

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!

 

Posted Oct 15, '11 at 8:25pm

master565

master565

3,874 posts

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! :D

That is among the easiest things to accomplish in life.

 

Posted Oct 16, '11 at 8:28am

PixelSmash

PixelSmash

567 posts

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.

 
Reply to Java Shenanigans

You must be logged in to post a reply!