Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 395
  • Last Modified:

base conversion

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
crystalfish0318
Asked:
crystalfish0318
  • 5
  • 2
1 Solution
 
harris_cCommented:
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
 
crystalfish0318Author Commented:
when i click the "convert" button, it stuck.................
0
 
crystalfish0318Author Commented:
when i click the "convert" button, it stuck.................

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

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
crystalfish0318Author Commented:
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
 
crystalfish0318Author Commented:
is my while loop correct? how can i output the result correctly?
0
 
grim_toasterCommented:
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
 
crystalfish0318Author Commented:
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
 
grim_toasterCommented:
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

Industry Leaders: 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!

  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now