Hello everyone,

I am an engineering student in first year computer programming and ive run into some problems with this course. I am a strong math student, and even stronger with computers so I thought this course would come naturally. Apparently not. Hopefully I can get some clarity from this forum as my teacher is insanely un-approachable and makes getting extra help a nightmare. So here goes:

The purpose of the lab (which I will post) is to create a base conversion program.

I am completely lost. It just won't click in my head. I can tackle the most complex physics problems, yet falter at a basic java lab. I am very new to java and have only covered the basics so far. I am in no way asking you to do my homework! I just need to know how to get started. Any pointers would be awesome! Thanks very much. Below are the instructions, and I will post my code so far when I switch over to my Windows partition.

"Topic: More Arithmetic

In this lab, we will write two more short programs which use Java arithmetic operators. In the problems below, assume that the user will be well-behaved and will always enter the expected kind of input.

BaseConvert.java:

One algorithm for converting a base 10 number to another base b involves repeatedly dividing by b. Each time a division is performed, the remainder and quotient are saved. At each step, the dividend is the quotient from the preceding step; the divisor is always b. The algorithm stops when the quotient is 0. The number in the new base is the sequence of remainders in reverse order (the last one computed goes first; the first one goes last).

Write a Java program which uses this algorithm to convert a base 10 number to a 4-digit number in another base (we haven't covered enough programming constructs yet to be able to convert any size number). The base 10 number and the new base (between 2 and 9) will be input to the program. Here's how your program should run (user inputs are shown as bold):

First run of the program:

Please enter a base in the range [2..9]: 2

Please enter a decimal number in the range [0..15]: 13 13(10) = 1101(2)

Second run of the program:

Please enter a base in the range [2..9]: 8

Please enter a decimal number in the range [0..4095]: 4095 4095(10) = 7777(8)

Third run of the program:

Please enter a base in the range [2..9]: 5

Please enter a decimal number in the range [0..624]: 0 0(10) = 0000(5)

Some programming notes:

The program will only work correctly for base 10 numbers that fit in 4 digits in the new base. We know that in base 2, the maximum unsigned integer that will fit in 4 digits is 11112, which equals 1510 (or 24 - 1). In base 8, the maximum number is 77778 which equals 409510 (or 84 - 1). In general, the maximum base 10 number that fits in 4 base b digits is b4 - 1. Your program should compute this maximum value, so that it can output an acceptable range of values for the input decimal number;

You may wish to save the partial results in separate variables (ie. one variable for each of the four result digits) and then concatenate the results in your output string at the end. Or you may wish to simply concatenate results onto an output string as you go along, and then output it at the end. Either technique is acceptable.

Try to get your output to behave and look exactly like the sample above, including spacing."

I have scoured the internet for anything relatively close to this lab for some clarity but no luck. Hopefully someone can give me a little push.

￼￼￼

# New to Java - Base Conversion lab

Page 1 of 1## 8 Replies - 11397 Views - Last Post: 10 November 2012 - 09:57 PM

##
**Replies To:** New to Java - Base Conversion lab

### #2

## Re: New to Java - Base Conversion lab

Posted 09 November 2012 - 07:28 PM

a lot of blablabla without a single line of code or an actual question based on a problem with your code/assignment

Please post the code you have problem with. And don't forget

Please post the code you have problem with. And don't forget

### #3

## Re: New to Java - Base Conversion lab

Posted 09 November 2012 - 09:13 PM

pbl, on 09 November 2012 - 07:28 PM, said:

a lot of blablabla without a single line of code or an actual question based on a problem with your code/assignment

Please post the code you have problem with. And don't forget />

Please post the code you have problem with. And don't forget />

Well if I knew what to do for this assignment I would!

What I am asking is if anyone could help me to the point where I could actually develop a code for the assignment!

I don't know the first thing about base conversions! Haven't done base conversions since high school.

### #4

## Re: New to Java - Base Conversion lab

Posted 09 November 2012 - 09:27 PM

Quote

One algorithm for converting a base 10 number to another base b involves repeatedly dividing by b. Each time a division is performed, the remainder and quotient are saved. At each step, the dividend is the quotient from the preceding step; the divisor is always b. The algorithm stops when the quotient is 0. The number in the new base is the sequence of remainders in reverse order (the last one computed goes first; the first one goes last).

Start here. Get a piece of paper, or a whiteboard, and write out a number like 481. Now, try to follow these directions and convert this number to binary.

(Hint: if you get 111100001, you've got the right idea)

Once you have it working on paper, it's easier to see how you'd do it in code.

### #5

## Re: New to Java - Base Conversion lab

Posted 09 November 2012 - 09:47 PM

jon.kiparsky, on 09 November 2012 - 09:27 PM, said:

Quote

One algorithm for converting a base 10 number to another base b involves repeatedly dividing by b. Each time a division is performed, the remainder and quotient are saved. At each step, the dividend is the quotient from the preceding step; the divisor is always b. The algorithm stops when the quotient is 0. The number in the new base is the sequence of remainders in reverse order (the last one computed goes first; the first one goes last).

Start here. Get a piece of paper, or a whiteboard, and write out a number like 481. Now, try to follow these directions and convert this number to binary.

(Hint: if you get 111100001, you've got the right idea)

Once you have it working on paper, it's easier to see how you'd do it in code.

I feel dumb for asking this, but what does Binary have to do with this?

### #6

## Re: New to Java - Base Conversion lab

Posted 09 November 2012 - 09:50 PM

As in base 2?

### #7

## Re: New to Java - Base Conversion lab

Posted 10 November 2012 - 12:00 PM

Numbers have different bases and you can convert any number into any base. Here are some common ones:

Binary = 2

Decimal = 10

Octa = 8

Hex = 16

But you can convert any base but there is a different procedure for that.

For example converting from decimal to binary

The easiest way is to continuously divide the decimal number by 2 to give a result and a remainder of either a "1" or a "0" until the final result equals zero.

Convert the decimal number (294) in base 10 (Which is decimal) into its binary number equivalent.

Dividing each number by "2" gives a result plus a remainder. The binary result is obtained by placing the remainders in order with the least significant bit (LSB) being at the top and the most significant bit (MSB) being at the bottom.

Number 294

divide by 2

result 147 remainder 0 (LSB)

divide by 2

result 73 remainder 1

divide by 2

result 36 remainder 1

divide by 2

result 18 remainder 0

divide by 2

result 9 remainder 0

divide by 2

result 4 remainder 1

divide by 2

result 2 remainder 0

divide by 2

result 1 remainder 0

divide by 2

result 0 remainder 1

Then, the decimal to binary conversion gives the decimal number (294) base 10 equivalent of (100100110)in base 2 in binary, reading from right to left.

Good luck />

Binary = 2

Decimal = 10

Octa = 8

Hex = 16

But you can convert any base but there is a different procedure for that.

For example converting from decimal to binary

The easiest way is to continuously divide the decimal number by 2 to give a result and a remainder of either a "1" or a "0" until the final result equals zero.

Convert the decimal number (294) in base 10 (Which is decimal) into its binary number equivalent.

Dividing each number by "2" gives a result plus a remainder. The binary result is obtained by placing the remainders in order with the least significant bit (LSB) being at the top and the most significant bit (MSB) being at the bottom.

Number 294

divide by 2

result 147 remainder 0 (LSB)

divide by 2

result 73 remainder 1

divide by 2

result 36 remainder 1

divide by 2

result 18 remainder 0

divide by 2

result 9 remainder 0

divide by 2

result 4 remainder 1

divide by 2

result 2 remainder 0

divide by 2

result 1 remainder 0

divide by 2

result 0 remainder 1

Then, the decimal to binary conversion gives the decimal number (294) base 10 equivalent of (100100110)in base 2 in binary, reading from right to left.

Good luck />

### #8

## Re: New to Java - Base Conversion lab

Posted 10 November 2012 - 09:37 PM

MattDelly, on 10 November 2012 - 12:00 PM, said:

Numbers have different bases and you can convert any number into any base. Here are some common ones:

Binary = 2

Decimal = 10

Octa = 8

Hex = 16

But you can convert any base but there is a different procedure for that.

For example converting from decimal to binary

The easiest way is to continuously divide the decimal number by 2 to give a result and a remainder of either a "1" or a "0" until the final result equals zero.

Convert the decimal number (294) in base 10 (Which is decimal) into its binary number equivalent.

Dividing each number by "2" gives a result plus a remainder. The binary result is obtained by placing the remainders in order with the least significant bit (LSB) being at the top and the most significant bit (MSB) being at the bottom.

Number 294

divide by 2

result 147 remainder 0 (LSB)

divide by 2

result 73 remainder 1

divide by 2

result 36 remainder 1

divide by 2

result 18 remainder 0

divide by 2

result 9 remainder 0

divide by 2

result 4 remainder 1

divide by 2

result 2 remainder 0

divide by 2

result 1 remainder 0

divide by 2

result 0 remainder 1

Then, the decimal to binary conversion gives the decimal number (294) base 10 equivalent of (100100110)in base 2 in binary, reading from right to left.

Good luck />/>

Binary = 2

Decimal = 10

Octa = 8

Hex = 16

But you can convert any base but there is a different procedure for that.

For example converting from decimal to binary

The easiest way is to continuously divide the decimal number by 2 to give a result and a remainder of either a "1" or a "0" until the final result equals zero.

Convert the decimal number (294) in base 10 (Which is decimal) into its binary number equivalent.

Dividing each number by "2" gives a result plus a remainder. The binary result is obtained by placing the remainders in order with the least significant bit (LSB) being at the top and the most significant bit (MSB) being at the bottom.

Number 294

divide by 2

result 147 remainder 0 (LSB)

divide by 2

result 73 remainder 1

divide by 2

result 36 remainder 1

divide by 2

result 18 remainder 0

divide by 2

result 9 remainder 0

divide by 2

result 4 remainder 1

divide by 2

result 2 remainder 0

divide by 2

result 1 remainder 0

divide by 2

result 0 remainder 1

Then, the decimal to binary conversion gives the decimal number (294) base 10 equivalent of (100100110)in base 2 in binary, reading from right to left.

Good luck />/>

Thank you, that actually helped quite a bit! I've googled a million programs that execute this function, but they are all insanely complex. All I am trying to do is create a program to convert decimal bases.

For example:

Inputting a base "2", Inputting a decimal number 13" and having the program spit out 13(10) //base 10 = 1101(2) //base 2

I think I am over thinking the difficulty of this program as I caught a glimpse of my teachers example and it was hardly 10 lines of code.

### #9

## Re: New to Java - Base Conversion lab

Posted 10 November 2012 - 09:57 PM

Quote

Thank you, that actually helped quite a bit!

Matt's advice was good, but you've had it once before: it's exactly what your teacher told you. This is why I suggested you try to actually work out the problem by hand.

I'm not sure how you're finding complex ways to solve this problem, it's really not much more than a few lines of code once you understand it. If you actually do the arithmetic yourself, you'll see how it works, and it should be very obvious how it goes. If you don't do the arithmetic for yourself, it's a lot harder to understand.

Page 1 of 1