Answer to Question #201105 in Java | JSP | JSF for Hamza

Question #201105

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. Consider the sample data:


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


In this case, first convert 2010 in base 3 into the equivalent number in base 10 as follows:


2 * 33 + 0 * 32 + 1 * 3 + 0 = 54 + 0 + 3 + 0 = 57


To convert 57 to base 4, you need to 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.


1
Expert's answer
2021-06-03T06:30:03-0400
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class ConvertorJFrame extends JFrame {
	private JTextField txtNumber;
	private JTextField txtBase;
	private JTextField txtNewBase;
	private JTextField txtConvertedNumber;
	
	private JButton btnConvert;
	/**
	 * Constructor
	 */
	public ConvertorJFrame() {
		setTitle("Converter");
		setSize(300, 180);
		setLayout(new BorderLayout());
		setLocationRelativeTo(null);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		//add the main Panel
		JPanel pnlControls = new JPanel(new GridLayout(4, 2));
		add("Center", pnlControls);
		
		//add Labels and TextFields
		pnlControls.add(new JLabel("Enter the number: "));
		txtNumber = new JTextField();
		pnlControls.add(txtNumber);
		
		pnlControls.add(new JLabel("Enter the base: "));
		txtBase = new JTextField();
		pnlControls.add(txtBase);
		
		pnlControls.add(new JLabel("Enter a new base: "));
		txtNewBase = new JTextField();
		pnlControls.add(txtNewBase);
		
		pnlControls.add(new JLabel("Converted number: "));
		txtConvertedNumber = new JTextField();
		txtConvertedNumber.setEditable(false);
		pnlControls.add(txtConvertedNumber);
		
		//add the button "Convert" 
		btnConvert = new JButton("Convert");
		btnConvert.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				try {
					int number=Integer.parseInt(txtNumber.getText());
					int base=Integer.parseInt(txtBase.getText());
					int newBase=Integer.parseInt(txtNewBase.getText());
					if(base<=0 || base>10) {
						throw new Exception("The base value must be positive numbers [1-10].");
					}
					if(newBase<=0 || newBase>10) {
						throw new Exception("A new base value must be positive numbers [1-10].");
					}
					int numberBase10 = convertToNumberBase10(number, base);
					txtConvertedNumber.setText(""+convertToNewBase(numberBase10, newBase));
				} catch (Exception exception) {
					JOptionPane.showMessageDialog(null, exception.getMessage());
				}
			}
		});
		this.add("South", btnConvert);
	}


	public static void main(String[] args) {
		ConvertorJFrame convertorJFrame = new ConvertorJFrame();
		convertorJFrame.setVisible(true);
	}


	/**
	 * Converts to a new base
	 * 
	 * @param numberBase10
	 * @param newBase
	 * @return
	 */
	private static int convertToNewBase(int numberBase10, int newBase) {
		int newBaseNumber = 0, product = 1;
		while (numberBase10 != 0) {
			newBaseNumber += (numberBase10 % newBase) * product;
			numberBase10 /= newBase;
			product *= 10;
		}
		return newBaseNumber;
	}
	/**
	 * Converts to decimal number
	 * @param number
	 * @param base
	 * @return
	 */
	private static int convertToNumberBase10(int number, int base) {
		int decimalNumber = 0, exponent = 0;
		while (number != 0) {
			decimalNumber += (number % 10) * Math.pow(base, exponent);
			exponent++;
			number /= 10;
		}
		return decimalNumber;
	}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS