So my java pprgram is actually a GUI I did all of it except I cant figure out how to convert between bases example:
number=2239
base=3
newbase=4
first i need to convert 2239 to base 3 which would be
2 * 3^3 + 2* 3^2 + .....
then convert that number to base 4
whatever base 3 equals %4 until i is <=base

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.Math;
public class Assignment3_2 extends JFrame
{
private JLabel NumberL, OriginalBaseL, NewBaseL, NewNumberL;
private JTextField NumberTF, OriginalBaseTF, NewBaseTF, NewNumberTF;
private JButton CalculateB, ExitB;
private CalculateButtonHandler cbHandler;
private ExitButtonHandler ebHandler;

private static final int WIDTH = 500;
private static final int HEIGHT = 300;

public Assignment3_2()
{
NumberL = new JLabel("Enter the Original Number: ",SwingConstants.RIGHT);
OriginalBaseL = new JLabel("Enter the Original Base: ",SwingConstants.RIGHT);
NewBaseL = new JLabel("Enter the Base to change the number into: ",SwingConstants.RIGHT);
NewNumberL = new JLabel("Number with Base Changed: ",SwingConstants.RIGHT);

NumberTF = new JTextField(10);
OriginalBaseTF = new JTextField(10);
NewBaseTF = new JTextField(10);
NewNumberTF = new JTextField(10);

CalculateB = new JButton("Calculate");
cbHandler = new CalculateButtonHandler();
CalculateB.addActionListener(cbHandler);

ExitB = new JButton("Exit");
ebHandler = new ExitButtonHandler();
ExitB.addActionListener(ebHandler);

setTitle("Base Conversion");

Container pane = getContentPane();

pane.setLayout(new GridLayout(5,2));

pane.add(NumberL);
pane.add(NumberTF);
pane.add(OriginalBaseL);
pane.add(OriginalBaseTF);
pane.add(NewBaseL);
pane.add(NewBaseTF);
pane.add(NewNumberL);
pane.add(NewNumberTF);
pane.add(CalculateB);
pane.add(ExitB);

setSize(WIDTH,HEIGHT);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}

private class CalculateButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
double Number, OriginalBase, NewBase, NewNumber,tempbase;

Number = Double.parseDouble(NumberTF.getText());
OriginalBase = Double.parseDouble(OriginalBaseTF.getText());
NewBase = Double.parseDouble(NewBaseTF.getText());
tempbase+= (original%4)
for( int i=0;i<=Originalbase; i++)

NewNumberTF.setText("" + NewNumber);
}
}
private class ExitButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}
public static void main(String[] args)
{
Assignment3_2 baseObject = new Assignment3_2();
}
}

how would I find the newbase? would I do something like:
tempbase+= (original%4)
for( int i=0;i<=Originalbase; i++)

Recommended Answers

All 2 Replies

You probably need to keep the input number in its original string format so you can parse it according to the base/radix. Then you just need two methods
double parse(String input, int base)
String format(double value, int base)
and you can convert from any base to any other base.
ps There is a parseInt method in Integer that allows you to specify the base (it calls it "radix"), but there doesn't appear to be an equivalent in Double. You may get some good ideas by having a look at the source code for Integer.

So my java pprgram is actually a GUI I did all of it except I cant figure out how to convert between bases example:
number=2239
base=3
newbase=4
first i need to convert 2239 to base 3 which would be
2 * 3^3 + 2* 3^2 + .....
then convert that number to base 4
whatever base 3 equals %4 until i is <=base

Yes; I think you have the right idea: 2239 in base three = ((2*3+2)*3+3)*3+9 = 90

(...except that '2239' is clearly not a base three number, as it contains both a '3' and a '9'. Only the digits '0', '1' and '2' are valid in base three numbers. ;-)

Then 90%4 is 2. 90/4 is 22. 22%4 is 4. 22/4 is 5. 5%4 is 1. 5/4 is 1. 1%4 is 1. 1/4 is zero (in integer math), so we're done. Taking the '%' numbers and reversing their order = 1122 in base 4.

You might want to use only 'int' instead of 'double'. ...unless your instructor told you that you're supposed to handle fractional values -- like 'DEAD.BEEF in base 16'!

I find it helpful to think of the numbers in the running program (as in an 'int' type) as not really having a base. Yes, they're stored in base 2, but that's often not really "visible" to you as a Java programmer. So your program will convert the "input base" to Java's internal 'int' representation. And then you'll convert from 'int' to the target base. 'int' happens to use a twos complement binary representation, but that probably won't matter to your code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.