Solved

base conversion

Posted on 2003-10-28
8
383 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
  • 5
  • 2
8 Comments
 
LVL 8

Expert Comment

by:harris_c
Comment Utility
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
Comment Utility
when i click the "convert" button, it stuck.................
0
 

Author Comment

by:crystalfish0318
Comment Utility
when i click the "convert" button, it stuck.................

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

0
 

Author Comment

by:crystalfish0318
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

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

Expert Comment

by:grim_toaster
Comment Utility
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
Comment Utility
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 20 total points
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
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…

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now