• Welcome Visitor! Please take a few seconds and Register for our forum. Even if you don't want to post, you can still 'Like' and react to posts.

Anyone here good at programming in java?


Musick17

Well-Known Member
Joined
Mar 5, 2009
Messages
643
City
Lincoln, IL
Vehicle Year
2002
Transmission
Manual
Im working on writing a program in java for a class and am kinda confused on which loops would be best to use and was wondering if anyone here has any kind of experience.

here is my program description:

"your program should first ask for a decimal number to convert, then perform the conversion from decimal to binary and display the binary number. then the program should ask the user if they would like to repeat the process. The program end when the user chooses not to convert another number. "

I have the first dialog box formatted correctly which gets the input and stores it in a variable. But I do not know what kind of loops past this point. I am doing this in the JOptionPane class.

thanks to any advice in advance
 
Not too much of a Java guy, but code is code. I haven't had to do anything with Java in a long time, and I am glad I have not had to. This is in Perl, crude, but should work.

** is "to the power of"

Code:
my $decimal = <INPUT>;
my $binary  = '';

my $max_bit = 0;

while ( (2 ** $max_bit) < $decimal ) { $max_bit++; }

for ( my $a = $max_bit; $a >= 0; $a-- )
{
    my $current_decimal = 2 ** $a;

    if ( $current_decimal <= $decimal )
    {
        $binary = $binary . "1";
        $decimal = $decimal - $current_decimal;
    }
    else
    {
        $binary = $binary . "0";
    }
}

print "$binary\n";

Edit - as far as what loop to use, that is up to you really as a programmer. Instead of a for loop, I could have used "while ( $decimal > 0 )." FWIW, I despise while loops, but use them occasionally when the amount I need to loop has not been determined. The first while loop determines how many times the for loop needs to loop. Just need to be careful. A lot of times I will add in a control variable to while loops make sure the loop breaks within a reasonable amount of loops to prevent it from going into an infinite loop.
 
Last edited:
Thought of this half way through a 6 pack of Mickey's just a sec ago LOL simpler though, just uses an "and" operator.

Code:
my $decimal = <INPUT>;
my $binary  = '';

my $max_bit = 0;

while ( (2 ** $max_bit) < $decimal ) { $max_bit++; }

for (var $a = $max_bit; $a >= 0; $a--)
{
    $binary = $binary . ( $decimal & (2 ** $a) ) ? "1" : "0";
}

Wish I remembered the java syntax and usage as far as the objects, but it's been too long to remember. My biggest complaints with Java is the over head, how I've seen SSL certificates handled, exploits, etc. Be aware that the example above will require some thought into the data types used, mixing large integers can cause some strange behavior (eg 32bit integer against 64bit). I am just not a big fan, but it is good to be familiar with object oriented programming, which I think more or less is why they teach it.
 
Last edited:
thanks Psychopete, I used your code as a reference to get mine started. Ive got everything working correctly with my code now except one little thing.


After converting the decimal number to a binary number, when i display the binary number it comes out in one long string like...

155 would be 10011011
200 would be 11001000

but the requirement for my code says that every 4 spots there must be a space, for ease of reading, like..

155 would be 1001 1011
200 would be 1100 1000

But since my binary number is stored as a String I dont know how to implement and interrupter ever 4 spaces. Any idea on that one?
 
Kind of a tricky one, the code I gave you could potentially output only 10 bits, so you'll need to find the next factor up

Code:
// If you had 10 bits, 
// 10 / 4 = 2.5
// ceiling of 2.5 is 3
// 3 * 4 = 12 (ex 0000 0000 0000 versus the 00 0000 0000)

my $groups = ceiling(length($binary) / 4);
my $target_length =  $groups * 4; 

// Then 
// ($target_length-length($binary)) will equal 2
// Should be some function to string a character "0" X amount of times.

$binary = ("0" x $target_length-length($binary)) . $binary;

That will make sure that you always have 4 length segments of the string.


Or if you have a sprintf like function
Code:
$binary = sprintf("%0${target_width}s", $binary);

I don't know enough tricks in Java to have something strait forward here to do the actual segments. Probably something similar;

Code:
my $display_string = '';
my $binary = '100101101010';

for (my $a = 0; $a < $groups; $a++)
{
    $display_string = $display_string . substr($binary, $a*4, 4) . " ";
}

Then trim off the space at the end, OR if you like using arrays

Code:
my @display = (); // Array
my $binary = '100101101010';

for (my $a = 0; $a < $groups; $a++)
{
    push @display, substr($binary, $a*4, 4); // Probably more like arr.push in Java
}

then you can join(" ", @display); or probably "display.join(' ')" into your text field.

*********

You could also set a $max_bits to a constant to always have out put a 32-bit integer (for example, not sure about the scope of the input) to get around this also. The example I wrote above will "adapt" to any integer you give it. But if you know max bits will always return the right length string of bits, you can add a control variable to add a space to the string every 4 counts. A trick that I use (and you will find very useful later on in your career :)), is to use the modulus of a counting variable.

for example, something like this -
Code:
$count = 1;

** loop ** 
{

    $binary = $binary . $new_bit;

    if ($count%4==0)
    {
        $binary = $binary . " ";
        $count++;
    }
}
 
Last edited:

Sponsored Ad


Sponsored Ad

TRS Events

Member & Vendor Upgrades

For a small yearly donation, you can support this forum and receive a 'Supporting Member' banner, or become a 'Supporting Vendor' and promote your products here. Click the banner to find out how.

Recently Featured

Want to see your truck here? Share your photos and details in the forum.

Ranger Adventure Video

TRS Merchandise

Follow TRS On Instagram

TRS Sponsors


Sponsored Ad


Sponsored Ad


Amazon Deals

Sponsored Ad

Back
Top