Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

base conversion

Posted on 2003-10-28
8
Medium Priority
?
391 Views
Last Modified: 2010-03-31
Here's my codes:
import java.io.*;
import javax.swing.*;
import java.util.*;
import java.math.*;
import java.awt.*;
import java.awt.event.*;

public class Conversion extends JFrame
{
      private JLabel oldNumLabel;
      private JLabel newNumLabel;
      private JLabel oldBaseLabel;
      private JLabel newBaseLabel;
      private JTextField oldNumTF;
      private JTextField newNumTF;
      private JTextField oldBaseTF;
      private JTextField newBaseTF;
      private JButton conversionB;
      private JButton exitB;
      private ConversionButtonHandler cbHandler;
      private ExitButtonHandler ebHandler;
      
      public static final int WIDTH = 400;
      public static final int HEIGHT = 300;
      
      public Conversion()
      {
            //Creates four lables
            oldNumLabel = new JLabel("Enter a positive integer: ", SwingConstants.RIGHT);
            oldBaseLabel = new JLabel("Enter a base for the positive integer: ", SwingConstants.RIGHT);
            newBaseLabel = new JLabel("Enter the new base for the positive integer: ", SwingConstants.RIGHT);
            newNumLabel = new JLabel("Result: ", SwingConstants.RIGHT);
            
            //Creates four text fields
            oldNumTF = new JTextField(10);
            oldBaseTF = new JTextField(10);
            newBaseTF = new JTextField(10);
            newNumTF = new JTextField(10);
            
            //Creates Conversion Button
            conversoinB = new JButton("Convert");
            cbHandler = new ConversionButtonHandler();
            conversionB.addActionListener(cbHandler);
            
            //Creates Exit Button
            exitB = new JButton("Exit");
            ebHandler = new ExitButtonHandler();
            exitB.addActionListener(ebHandler);
            
            //Sets the title of the window
            setTitle("Base Conversion");
            
            //Sets the container
            Container pane = getContentPane();
            
            //Sets the layout
            pane.setLayout(new GridLayout(4,2));
            
            //Places the components in the pane
            pane.add(oldNumLabel);
            pane.add(oldBaseLabel);
            pane.add(newBaseLabel);
            pane.add(newNumLabel);
            pane.add(oldNumTF);
            pane.add(oldBaseTF);
            pane.add(newBaseTF);
            pane.add(newNumTF);
            pane.add(conversionB);
            pane.add(exitB);
            
            //Sets the size of the window and display it
            setSize(WIDTH,HEIGHT);
            setVisible(true);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
      }
      
      private class ConversionButtonHandler implements ActionListener
      {
            public void actionPerformed(ActionEvent e)
            {
                  //declares variables
                  int oldNum, oldBase, newBase, newNum, counter, numDigits;
                  double temp;
                  int interSum = 0;
                  
                  oldNum = Integer.parseInt(oldNumTF.getText());
                  oldBase = Integer.parseInt(oldBaseTF.getText());
                  newBase = Integer.parseInt(newbaseTF.getText());
                  
                  if ((oldBase < newBase) || (newBase > oldBase))
                  {
                        for(numDigits--; (numDigits = Math.log(oldNum) / Math.log(10) + 1);
                              numDigits = 0)
                        {
                              //get the first digit
                              temp = oldNum / Math.pow(10,numDigits);
                              //find out the sum of individual digits times its base power
                              intermSum = intermSum + temp * (Math.pow(oldBase,numDigits));
                              //get the digits remained after previous executions
                              temp = temp % (Math.pow(10,numDigits));
                        }
                        
                        temp = intermSum % newBase;
                              
                        int quotient = interSum / newBase;
                        
                        private class ExitButtonHandler implements ActionListener
                        {
                              public void actionPerformed(ActionEvent e)
                              {
                                    System.exit(0);
                              }
                        }
                  }
                  
                  
            }
      }
      
      public static void main(String[] args)
                  {
                        BaseConversion baseObject = new BaseConversion();
                  }
}

there are about 12 errors, but I just don't know how to fix.
please compile this java first.

thanks
0
Comment
Question by:crystalfish0318
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
8 Comments
 
LVL 8

Expert Comment

by:harris_c
ID: 9639260
Combine ButtonHandlers.


Best is let a GUI do the formatting/setting for you and edit it afterwards.

import java.io.*;
import javax.swing.*;
import java.util.*;
import java.math.*;
import java.awt.*;
import java.awt.event.*;

public class Converstion extends JFrame
{
    private JLabel oldNumLabel;
    private JLabel newNumLabel;
    private JLabel oldBaseLabel;
    private JLabel newBaseLabel;
    private JTextField oldNumTF;
    private JTextField newNumTF;
    private JTextField oldBaseTF;
    private JTextField newBaseTF;
    private JButton conversionB;
    private JButton exitB;

    public static final int WIDTH = 400;
    public static final int HEIGHT = 300;

    public Converstion()
    {
         //Creates four lables
         oldNumLabel = new JLabel("Enter a positive integer: ", SwingConstants.RIGHT);
         oldBaseLabel = new JLabel("Enter a base for the positive integer: ", SwingConstants.RIGHT);
         newBaseLabel = new JLabel("Enter the new base for the positive integer: ", SwingConstants.RIGHT);
         newNumLabel = new JLabel("Result: ", SwingConstants.RIGHT);

         //Creates four text fields
         oldNumTF = new JTextField(10);
         oldBaseTF = new JTextField(10);
         newBaseTF = new JTextField(10);
         newNumTF = new JTextField(10);

         //Creates Conversion Button
         conversionB = new JButton("Convert");

         //Creates Exit Button
         exitB = new JButton("Exit");
         
              //{{REGISTER_LISTENERS
             SymAction lSymAction = new SymAction();
             conversionB.addActionListener(lSymAction);
             exitB.addActionListener(lSymAction);
             //}}
         

         //Sets the title of the window
         setTitle("Base Conversion");

         //Sets the container
         Container pane = getContentPane();

         //Sets the layout
         pane.setLayout(new GridLayout(4,2));

         //Places the components in the pane
         pane.add(oldNumLabel);
         pane.add(oldBaseLabel);
         pane.add(newBaseLabel);
         pane.add(newNumLabel);
         pane.add(oldNumTF);
         pane.add(oldBaseTF);
         pane.add(newBaseTF);
         pane.add(newNumTF);
         pane.add(conversionB);
         pane.add(exitB);

         //Sets the size of the window and display it
         setSize(WIDTH,HEIGHT);
         setVisible(true);
   
            //{{INIT_CONTROLS
            setDefaultCloseOperation(javax.swing.JFrame.DISPOSE_ON_CLOSE);
            setVisible(true);
            //}}
      }

      class SymAction implements java.awt.event.ActionListener
    {
            public void actionPerformed(java.awt.event.ActionEvent event)
            {
                  Object object = event.getSource();
                  if (object == conversionB)
                        conversionB_actionPerformed(event);
                  else if (object == exitB)
                        exitB_actionPerformed(event);
            }
      }

      void conversionB_actionPerformed(java.awt.event.ActionEvent event)
      {
            // to do: code goes here.

         //declares variables
              int oldNum, oldBase, newBase, newNum, counter;
              double numDigits;
              double temp;
              double intermSum = 0;

              oldNum = Integer.parseInt(oldNumTF.getText());
              oldBase = Integer.parseInt(oldBaseTF.getText());
              newBase = Integer.parseInt(newBaseTF.getText());

              if ((oldBase < newBase) || (newBase > oldBase))
              {
                    numDigits = (Math.log(oldNum) / Math.log(10) + 1);
                   
                    while (numDigits != 0)
                   {
                        //get the first digit
                        temp = oldNum / Math.pow(10,numDigits);
                        //find out the sum of individual digits times its base power
                        intermSum = intermSum + temp * (Math.pow(oldBase,numDigits));
                        //get the digits remained after previous executions
                        temp = temp % (Math.pow(10,numDigits));
                        numDigits--;
                   }

              temp = intermSum % newBase;

              int quotient = (int)intermSum / newBase;
              }
         }
         
      void exitB_actionPerformed(java.awt.event.ActionEvent event)
      {
            // to do: code goes here.
        System.exit(0);
    }
 
    public static void main(String[] args)
              {
                   Converstion baseObject = new Converstion();
                   baseObject.show();
              }


hec",)
0
 

Author Comment

by:crystalfish0318
ID: 9639319
when i click the "convert" button, it stuck.................
0
 

Author Comment

by:crystalfish0318
ID: 9639325
when i click the "convert" button, it stuck.................

my inupt is:
number = 2010, base = 3 and new base = 4.

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:crystalfish0318
ID: 9639366
wait, did i miss anything?

ok, here's the question:
Design and implement a GUI program to convert a positive number given in one base to another base. For this problem, assume that both bases are less than or equal to 10.

if
number = 2010, base = 3 and new base = 4

then the first thing is to convert 2010 in base 3 into the equivalent number in base 10 as follows:
2*3^3 + 0*3^2 + 1*3 + 0 = 54 + 0 + 3 + 0 = 57

To convert 57 to base 4, i find the remainders obtained by dividing by 4, as shown in the following:

57 % 4 = 1, quotient = 14
14 % 4 = 2, quotient = 3
3 % 4 = 3, quotient = 0.
Therefore, 57 in base 4 is 321.
0
 

Author Comment

by:crystalfish0318
ID: 9639511
is my while loop correct? how can i output the result correctly?
0
 
LVL 7

Expert Comment

by:grim_toaster
ID: 9641781
Your algorithm seems a bit more complicated than needed!  You can use the standard Java libraries to do most of what you're after (why re-invent the wheel?)

Use Integer.parseInt(String, radix) to parse your text field input to an int, and then use Integer.toString(int, radix) to return the String representation of the new radix.  I'll leave it up to you to look into the API's for the details on the min and max radix values.  But are you sure that you will only ever be dealing with ints?
0
 

Author Comment

by:crystalfish0318
ID: 9647659
import java.io.*;
import javax.swing.*;
import java.util.*;
import java.math.*;
import java.awt.*;
import java.awt.event.*;

public class ch6_num9 extends JFrame
{
      private JLabel oldNumLabel;
    private JLabel newNumLabel;
    private JLabel oldBaseLabel;
    private JLabel newBaseLabel;
    private JTextField oldNumTF;
    private JTextField newNumTF;
    private JTextField oldBaseTF;
    private JTextField newBaseTF;
    private JButton conversionB;
    private JButton exitB;
   

    public static final int WIDTH = 600;
    public static final int HEIGHT = 300;

    public ch6_num9()
    {
          
        //Creates four lables
        oldNumLabel = new JLabel("Enter a positive integer: ", SwingConstants.RIGHT);
        oldBaseLabel = new JLabel("Enter a base for the positive integer: ", SwingConstants.RIGHT);
        newBaseLabel = new JLabel("Enter the new base for the positive integer: ", SwingConstants.RIGHT);
        newNumLabel = new JLabel("Result: ", SwingConstants.RIGHT);

        //Creates four text fields
        oldNumTF = new JTextField(10);
        oldBaseTF = new JTextField(10);
        newBaseTF = new JTextField(10);
        newNumTF = new JTextField(10);

        //Creates Conversion Button
        conversionB = new JButton("Convert");


        //Creates Exit Button
        exitB = new JButton("Exit");
      
         
         //REGISTER_LISTENERS
         SymAction lSymAction = new SymAction();
         conversionB.addActionListener(lSymAction);
         exitB.addActionListener(lSymAction);
         //***********
         

         //Sets the title of the window
         setTitle("Base Conversion");

         //Sets the container
         Container pane = getContentPane();

         //Sets the layout
         pane.setLayout(new GridLayout(4,2));

         //Places the components in the pane
             pane.add(oldNumLabel);
                pane.add(oldNumTF);
               
             pane.add(oldBaseLabel);
                pane.add(oldBaseTF);
               
             pane.add(newBaseLabel);
                pane.add(newBaseTF);
               
             pane.add(newNumLabel);
                pane.add(newNumTF);
                     
               
             pane.add(conversionB);
               pane.add(exitB);

         //Sets the size of the window and display it
             setSize(WIDTH,HEIGHT);
           setVisible(true);
             setDefaultCloseOperation(EXIT_ON_CLOSE);
   
         //INIT_CONTROLS
         setDefaultCloseOperation(javax.swing.JFrame.DISPOSE_ON_CLOSE);
         setVisible(true);
         //********************************
    }

      class SymAction implements java.awt.event.ActionListener
    {
          public void actionPerformed(java.awt.event.ActionEvent event)
        {
              Object object = event.getSource();
              
            if (object == conversionB)
            conversionB_actionPerformed(event);
           
            else
            if (object == exitB)
            exitB_actionPerformed(event);
        }
    }

    void conversionB_actionPerformed(java.awt.event.ActionEvent event)
      {
            // to do: code goes here.

        //declares variables
        int oldNum, oldBase, newBase, newNum, counter;
        double numDigits;
        double temp;
        double intermSum = 0.0;

        oldNum = Integer.parseInt(oldNumTF.getText());
        oldBase = Integer.parseInt(oldBaseTF.getText());
        newBase = Integer.parseInt(newBaseTF.getText());

        if ((oldBase < newBase) || (newBase > oldBase))
        {
              numDigits = (Math.log(oldNum) / Math.log(10) + 1);
           
            while (numDigits != 0)
            {
                  //get the first digit
                temp = oldNum / Math.pow(10,numDigits);
               
                //find out the sum of individual digits times its base power
                intermSum = intermSum + temp * (Math.pow(oldBase,numDigits));
               
                //get the digits remained after previous executions
                temp = temp % (Math.pow(10,numDigits));
                numDigits--;
            }

            temp = intermSum % newBase;

            int quotient = (int)intermSum / newBase;
            }
      }
         
    void exitB_actionPerformed(java.awt.event.ActionEvent event)
    {
          // to do: code goes here.
        System.exit(0);
    }
 
    public static void main(String[] args)
    {
          ch6_num9 baseObject = new ch6_num9();
        baseObject.show();
    }
}

the position of my converter is just not right.............
0
 
LVL 7

Accepted Solution

by:
grim_toaster earned 60 total points
ID: 9648290
The problem lies in your conversionB_actionPerformed method.  One of the problems is that you hold the number of digits in a primitive double type, and then loop to see if not equal to 0, looping to see if greater than 0 would have been better, as on my machine I had a value of 4.1224322 (or something like that), and subtracting one each time, it would never be zero!.

Here's a simpler version of the method:
    void conversionB_actionPerformed(java.awt.event.ActionEvent event)
    {
        int oldBase = Integer.parseInt(oldBaseTF.getText());
        int newBase = Integer.parseInt(newBaseTF.getText());

        int oldNum = Integer.parseInt(oldNumTF.getText(), oldBase);
        String newNum = Integer.toString(oldNum, newBase);
        newNumTF.setText(newNum);
    }

And you were never setting the result to the screen!
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question