![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Jul 2005
Posts: 1
Rep Power: 0
![]() |
User Input for Number Format
Hello,
I am trying to allow a user to set the number of zeros that come after the decimal point. I have been working at this for a while now and I can't get it figured out. I already have an existing Rational class that I wrote. Here is my RationalTester class. Thanks, Eric import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.DecimalFormat;
// The following are javadoc comments
// These MUST be placed just before the class declaration
public class RationalTester extends JFrame
{
private Rational rat1 = new Rational();
private Rational rat2 = new Rational();
private Rational ratsResult;
private String output = "";
String userDigits1 = "";
String userDigits2 = "";
int decimalPlaces1 = 0;
int decimalPlaces2 = 0;
DecimalFormat userSpecifiedDigitFormat1 = new DecimalFormat( userDigits1 );
DecimalFormat userSpecifiedDigitFormat2 = new DecimalFormat( userDigits2 );
private JTextField firstNumerField, firstDenomField,
secondNumerField, secondDenomField,
decimalPoints1Field, decimalPoints2Field;
JTextArea outputArea;
public RationalTester()
{
super( "Rational Tester" );
Container container = getContentPane();
outputArea = new JTextArea();
// create panels
JPanel topPanel = new JPanel();
topPanel.setLayout( new GridLayout( 4, 4, 0, 3 ) );
topPanel.setBackground( new Color( 150, 255, 150 ) );
JPanel middlePanel = new JPanel();
middlePanel.setLayout( new GridLayout( 3, 4, 3, 3 ) );
middlePanel.setBackground( new Color( 150, 255, 150 ) );
// create components for top panel and add them to top panel
JLabel firstRatLabel = new JLabel( " First Rational" );
firstRatLabel.setFont( new Font( "Monospaced", Font.BOLD, 18 ) );
JLabel blank1Label = new JLabel( " " );
JLabel secondRatLabel = new JLabel( " Second Rational" );
secondRatLabel.setFont( new Font( "Monospaced", Font.BOLD, 18 ) );
JLabel blank2Label = new JLabel( " " );
JLabel blank3Label = new JLabel( " " );
JLabel blank4Label = new JLabel( " " );
JLabel firstNumerLabel = new JLabel( " Numerator" );
firstNumerLabel.setFont( new Font( "Monospaced", Font.BOLD, 16 ) );
firstNumerField = new JTextField( 10 );
firstNumerField.addActionListener
(
new ActionListener() // anonymous inner class
{
// read value from user
public void actionPerformed( ActionEvent event )
{
rat1.setNumer( Integer.parseInt( firstNumerField.getText() ) );
}
} // end anonymous inner class
); // end call to addActionListener
JLabel firstDenomLabel = new JLabel( " Denominator" );
firstDenomLabel.setFont( new Font( "Monospaced", Font.BOLD, 16 ) );
firstDenomField = new JTextField( 10 );
firstDenomField.addActionListener
(
new ActionListener() // anonymous inner class
{
// read value from user
public void actionPerformed( ActionEvent event )
{
if( rat1.denomIsNotZero() == true )
{
rat1.setDenom( Integer.parseInt( firstDenomField.getText() ) );
}
}
} // end anonymous inner class
); // end call to addActionListener
JLabel secondNumerLabel = new JLabel( " Numerator" );
secondNumerLabel.setFont( new Font( "Monospaced", Font.BOLD, 16 ) );
secondNumerField = new JTextField( 10 );
secondNumerField.addActionListener
(
new ActionListener() // anonymous inner class
{
// read value from user
public void actionPerformed( ActionEvent event )
{
rat2.setNumer( Integer.parseInt( secondNumerField.getText() ) );
}
} // end anonymous inner class
); // end call to addActionListener
JLabel secondDenomLabel = new JLabel( " Denominator" );
secondDenomLabel.setFont( new Font( "Monospaced", Font.BOLD, 16 ) );
secondDenomField = new JTextField( 10 );
secondDenomField.addActionListener
(
new ActionListener() // anonymous inner class
{
// read value from user
public void actionPerformed( ActionEvent event )
{
if( rat2.denomIsNotZero() == true )
{
rat2.setDenom( Integer.parseInt( secondDenomField.getText() ) );
}
}
} // end anonymous inner class
); // end call to addActionListener
JLabel decimalPoints1Label = new JLabel( " Decimal Points" );
decimalPoints1Label.setFont( new Font( "Monospaced", Font.BOLD, 16 ) );
decimalPoints1Field = new JTextField( 10 );
decimalPoints1Field.addActionListener
(
new ActionListener() // anonymous inner class
{
// read value from user
public void actionPerformed( ActionEvent event )
{
setDecimalPlaces1( decimalPoints1Field.getText() );
String decimalPlaces1Array[] = new String[ decimalPlaces1 ];
for( int counter = 0; counter < decimalPlaces1Array.length; counter++ )
{
decimalPlaces1Array[ counter ] = "0";
}
String decimalString1 = "";
for( int counter = 0; counter < decimalPlaces1Array.length; counter++ )
{
decimalString1 += decimalPlaces1Array[ counter ];
}
userDigits1 = "." + decimalString1;
setDecimalPlaces1( userDigits1 );
}
} // end anonymous inner class
); // end call to addActionListener
JLabel decimalPoints2Label = new JLabel( " Decimal Points" );
decimalPoints2Label.setFont( new Font( "Monospaced", Font.BOLD, 16 ) );
decimalPoints2Field = new JTextField( 10 );
decimalPoints2Field.addActionListener
(
new ActionListener() // anonymous inner class
{
// read value from user
public void actionPerformed( ActionEvent event )
{
setDecimalPlaces2( decimalPoints2Field.getText() );
String decimalPlaces2Array[] = new String[ decimalPlaces2 ];
for( int counter = 0; counter < decimalPlaces2Array.length; counter++ )
{
decimalPlaces2Array[ counter ] = "0";
}
String decimalString2 = "";
for( int counter = 0; counter < decimalPlaces2Array.length; counter++ )
{
decimalString2 += decimalPlaces2Array[ counter ];
}
userDigits2 = "." + decimalString2;
}
} // end anonymous inner class
); // end call to addActionListener
topPanel.add( firstRatLabel );
topPanel.add( blank1Label );
topPanel.add( secondRatLabel );
topPanel.add( blank2Label );
topPanel.add( firstNumerLabel );
topPanel.add( firstNumerField );
topPanel.add( secondNumerLabel );
topPanel.add( secondNumerField );
topPanel.add( firstDenomLabel );
topPanel.add( firstDenomField );
topPanel.add( secondDenomLabel );
topPanel.add( secondDenomField );
topPanel.add( decimalPoints1Label );
topPanel.add( decimalPoints1Field );
topPanel.add( decimalPoints2Label );
topPanel.add( decimalPoints2Field );
// create components for middle panel and add them to middle panel
JButton addButton = new JButton( "Add Rationals" );
addButton.setFont( new Font( "Monospaced", Font.BOLD, 18 ) );
addButton.setForeground( Color.CYAN );
addButton.setBackground( Color.BLACK );
addButton.addActionListener
(
new ActionListener() // anonymous inner class
{
// read value from user
public void actionPerformed( ActionEvent event )
{
if( ( rat1.denomIsNotZero() == true ) && ( rat2.denomIsNotZero() == true ) )
{
rat1.setNumer( Integer.parseInt( firstNumerField.getText() ) );
rat1.setDenom( Integer.parseInt( firstDenomField.getText() ) );
rat2.setNumer( Integer.parseInt( secondNumerField.getText() ) );
rat2.setDenom( Integer.parseInt( secondDenomField.getText() ) );
ratsResult = rat1.addRats( rat2 );
displayResult( " Sum: " );
}
else
{
output += "The denominator cannot be zero.";
displayOutput();
reset();
}
}
} // end anonymous inner class
); // end call to addActionListener
JButton subtractButton = new JButton( "Subtract Rationals" );
subtractButton.setFont( new Font( "Monospaced", Font.BOLD, 18 ) );
subtractButton.setForeground( Color.MAGENTA );
subtractButton.setBackground( Color.BLACK );
subtractButton.addActionListener
(
new ActionListener() // anonymous inner class
{
// read value from user
public void actionPerformed( ActionEvent event )
{
rat1.setNumer( Integer.parseInt( firstNumerField.getText() ) );
rat1.setDenom( Integer.parseInt( firstDenomField.getText() ) );
rat2.setNumer( Integer.parseInt( secondNumerField.getText() ) );
rat2.setDenom( Integer.parseInt( secondDenomField.getText() ) );
ratsResult = rat1.subtractRats( rat2 );
displayResult( " Difference: " );
}
} // end anonymous inner class
); // end call to addActionListener
JButton multiplyButton = new JButton( "Multiply Rationals" );
multiplyButton.setFont( new Font( "Monospaced", Font.BOLD, 18 ) );
multiplyButton.setForeground( Color.YELLOW );
multiplyButton.setBackground( Color.BLACK );
multiplyButton.addActionListener
(
new ActionListener() // anonymous inner class
{
// read value from user
public void actionPerformed( ActionEvent event )
{
rat1.setNumer( Integer.parseInt( firstNumerField.getText() ) );
rat1.setDenom( Integer.parseInt( firstDenomField.getText() ) );
rat2.setNumer( Integer.parseInt( secondNumerField.getText() ) );
rat2.setDenom( Integer.parseInt( secondDenomField.getText() ) );
ratsResult = rat1.multiplyRats( rat2 );
displayResult( " Product: " );
}
} // end anonymous inner class
); // end call to addActionListener
JButton divideButton = new JButton( "Divide Rationals" );
divideButton.setFont( new Font( "Monospaced", Font.BOLD, 18 ) );
divideButton.setForeground( Color.ORANGE );
divideButton.setBackground( Color.BLACK );
divideButton.addActionListener
(
new ActionListener() // anonymous inner class
{
// read value from user
public void actionPerformed( ActionEvent event )
{
rat1.setNumer( Integer.parseInt( firstNumerField.getText() ) );
rat1.setDenom( Integer.parseInt( firstDenomField.getText() ) );
rat2.setNumer( Integer.parseInt( secondNumerField.getText() ) );
rat2.setDenom( Integer.parseInt( secondDenomField.getText() ) );
ratsResult = rat1.divideRats( rat2 );
displayResult( " Quotient: " );
}
} // end anonymous inner class
); // end call to addActionListener
JButton clearButton = new JButton( "Clear" );
clearButton.setFont( new Font( "Monospaced", Font.BOLD, 18 ) );
clearButton.setForeground( Color.WHITE );
clearButton.setBackground( Color.BLACK );
clearButton.addActionListener
(
new ActionListener() // anonymous inner class
{
// read value from user
public void actionPerformed( ActionEvent event )
{
reset();
}
} // end anonymous inner class
); // end call to addActionListener
middlePanel.add( addButton );
middlePanel.add( subtractButton );
middlePanel.add( multiplyButton );
middlePanel.add( divideButton );
middlePanel.add( outputArea );
middlePanel.add( clearButton );
Container myContainer = getContentPane();
myContainer.setLayout( new BorderLayout() );
myContainer.add( topPanel, BorderLayout.NORTH );
myContainer.add( middlePanel, BorderLayout.CENTER );
setSize( 900, 750 );
setVisible( true );
rat1 = new Rational();
rat2 = new Rational();
displayResult( "" );
}
private void reset()
{
displayClear();
rat1 = new Rational();
rat2 = new Rational();
displayResult( "" );
}
public void setDecimalPlaces1( String decimalValue1 )
{
userDigits1 = decimalValue1;
}
public void setDecimalPlaces2( String decimalValue2 )
{
userDigits2 = decimalValue2;
}
private void displayResult( String operation )
{
int numer1 = rat1.getNumer();
int denom1 = rat1.getDenom();
int numer2 = rat2.getNumer();
int denom2 = rat2.getDenom();
output += "\n First Rational Fraction: " + numer1 + " / " + denom1 + " Decimal: " + userSpecifiedDigitFormat1.format( rat1.toDouble() ) + "\n";
output += " Second Rational Fraction: " + numer2 + " / " + denom2 + " Decimal: " + userSpecifiedDigitFormat2.format( rat2.toDouble() ) + "\n";
if( operation.equals( " Sum: " ) || operation.equals( " Difference: " ) ||
operation.equals( " Product: " ) || operation.equals( " Quotient: " ) )
{
output += operation + ratsResult.toString() + " Decimal: " + ratsResult.toDouble() + "\n";
/////////////////
output += "uD1 " + userDigits1 + " " + "uD2 " + userDigits2; // to test for the values of decimal format
/////////////////
}
displayOutput();
}
private void displayOutput()
{
outputArea.setText( output );
}
private void displayClear()
{
output = "";
outputArea.setText( output );
}
public static void main( String args[] )
{
RationalTester rt = new RationalTester();
rt.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
} |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|