/** * Class Document is used to read data from disk files, * insert the data into a two dimensional array. * * @author David Thibodeau * @version 23-July-2002 modified 11-Aug-2002 */ import java.io.*; import java.util.*; public class Document { private String[][] conversionFactors; // holds 2D array of conversion data private int arrayX, arrayY; // holds # of elements in the array dimensions /** * readInFromFile method - This reads the specified file * * @param infilename the name of the file to be opened * @return void */ public void readInFromFile(String infilename) { int arraySize; // get # of elements in array String line; // holds a line of text brought in from the file // try - catch block of code from teacher's online CS151 // and modified by me to call addToLinkList method. try // this routine pulls in lines of text from the file { BufferedReader in = new BufferedReader( new FileReader( infilename ) ); line = in.readLine(); // read first line of text file // to get # of elements in array arraySize = Integer.parseInt( line ); arrayX = arraySize; arrayY = arraySize +1; conversionFactors = new String[arraySize][arraySize+1]; // init array for(int i = 0; i < arraySize; i++) { line = in.readLine(); addToArray( i, line ); } // End for in.close(); // closes text file } // end of try block catch ( IOException iox ) // couldn't find the file { System.out.println("Problem reading " + infilename ); } // end of catch block } // end of readInFromFile(String infilename) /** * addToTree method - Takes a String and tokenizes it, * then adds each token to the docTree object. * @param toAdd String to be tokenized * @param index Index of the array where data will be added * @return void */ private void addToArray(int indexX, String toAdd) { int indexY = 0; StringTokenizer tok; tok = new StringTokenizer( toAdd ); while ( tok.hasMoreTokens() ) { String st = tok.nextToken(); conversionFactors[indexX][indexY] = st; indexY++; } // end of while loop } // end of addToArray(int index, String toAdd) /** * getFirstIndex method - returns number of elements in first dimension * @param void - no data required * @return int - number of elements in first dimension of the * conversionFactors array */ public int getFirstIndex() { return arrayX; } // end getFirstIndex method /** * getSecondIndex method - returns number of elements in second dimension * @param void - no data required * @return int - number of elements in second dimension of the * conversionFactors array */ public int getSecondIndex() { return arrayY; } // end getSecondIndex method /** * getConversionFactor method - Gets the conversionFactors[][] array * @param int x - index of first element in array * @param int y - index of second element in array * @return String - returns data at conversionFactors[x][y] */ public String getConversionFactor(int x, int y) { return conversionFactors[x][y]; } // End of getConversionFactor(int x, int y) method } // End of Document Class Class Conversion: /** * Class Conversion uses the Document class to read conversion * factors from a text file. Once a file has been read, the data * is placed into an array. When conversion requests are passed * from the GUI, this class calculates and returns the proper * results. * @author David Thibodeau * @version 11-Aug-2002 */ public class Conversion { private Document conversionDocument; private String[] conversionString; private double[][] conversionRatio; /** * setFactors method - Gets the conversion factors from the * supplied filename. * * @param String infilename - The file to get the data from * @return void - no data returned */ public void setFactors(String infilename) { int indexX, indexY; conversionDocument = new Document(); // instantiate Document Class conversionDocument.readInFromFile( infilename ); // read data from disk indexX = conversionDocument.getFirstIndex(); // These two lines get the indexY = conversionDocument.getSecondIndex(); // index values of the array setConversionString( indexX ); // populate conversionString array setConversionRatio( indexX, indexY ); // populate conversionRatio array } // end setFactors(String infilename) method /** * setConversionString method - Gets the String descriptors of the units * of measure. * @param int x - number of String descriptors * @return void - no data returned */ private void setConversionString(int x) { conversionString = new String[x]; for(int i = 0; i < x; i++) { conversionString[i] = conversionDocument.getConversionFactor(i, 0); } } // end setConversionString(int x) method /** * setConversionRatio method - populates the conversionRatio array with * the conversion factors from the file read in from disk. * @param int x - number of elements in the first index of the array * @param int y - number of elements in the second index of the array * @return void - no data returned */ private void setConversionRatio(int x, int y) { conversionRatio = new double[x][y]; for(int i = 0; i < x; i++) { for(int j = 1; j < y; j++) { conversionRatio[i][j] = Double.parseDouble( conversionDocument.getConversionFactor(i, j) ); } } } // end setConversionRatio(int x, int y) method /** * getConversionString method - returns a String array with all of * the units of measurement decriptors. This method assumes that * getFactors(String infilename) has already been called. * @param void - no data required * @return String[] array - The english descriptors of the units * of measure. */ public String[] getConversionString() { // Assumes getFactors(String infilename) has already been run. return conversionString; } // end String[] getConversionString() method /** * doConversion method - Does the conversion between the two units * of measure. The indexes of the comboboxes on the GUI are passed * to the method. These values are used to calculate the location * of the proper ratio to use. This is done by using the two values * (the second get 1 added to it) to denote the proper location in * the conversionRatio array. * @param int x - first combobox index value * @param int y - second combobox index value * @param double convert - the value to convert * @return double - the converted value. */ public double doConversion(int x, int y, double convert) { return convert * conversionRatio[x][y + 1]; } // end doConversion(int x, int y, double convert) method } // end Conversion Class Class Conversion: /** * Class ConversionGUI provides a front end for the conversion class. * ConversionGUI creates the visual layout and ties the proper actions * to each interface element. * * @author David Thibodeau * @version 7-Aug-2002 */ import java.awt.* ; import java.awt.event.*; import javax.swing.*; public class ConversionGUI extends JFrame implements ActionListener { // data input panel JPanel inputPanel; JLabel inputLabel; JButton inputConvert; JTextField inputText; // data output panel JPanel outputPanel; JLabel outputLabel; JTextField outputText; // Unit of Measure Panel (UOM) JPanel uomPanel; JPanel uomStartPanel; JPanel uomEndPanel; JLabel uomStartLabel; JLabel uomEndLabel; JComboBox uomStartCombo; JComboBox uomEndCombo; // Type of Conversion Panel JPanel tomPanel; JRadioButton tomDistance; JRadioButton tomDryWeight; JRadioButton tomLiquidWeight; ButtonGroup tomType; // Conversion object to hold conversion ratios Conversion convert; /** * Constructor for objects of class ConversionGUI */ public ConversionGUI() { setTitle( "Measurement Conversion Example" ); setDefaultCloseOperation( EXIT_ON_CLOSE ); // Instantiate Conversion Objects convert = new Conversion(); convert.setFactors( "/Distance_Measurement.txt" ); // data input group inputLabel = new JLabel( "Input data to convert:" ); inputText = new JTextField(20); inputText.addActionListener(this); // detect when keystrokes are made // into this field inputText.setActionCommand( "Convert" ); inputConvert = new JButton( "Convert" ); inputConvert.addActionListener(this); inputConvert.setActionCommand( "Convert" ); inputPanel = new JPanel(); inputPanel.add( inputLabel ); inputPanel.add( inputText ); inputPanel.add( inputConvert ); // data output group outputLabel = new JLabel( "Converted measurement:" ); outputText = new JTextField(20); outputText.setEnabled( false ); // keep data from being changed outputPanel = new JPanel(); outputPanel.add( outputLabel ); outputPanel.add( outputText ); // Unit of Measure (UOM) Panel uomStartLabel = new JLabel( "Convert From Units: " ); uomEndLabel = new JLabel( "Convert To Units: " ); uomStartCombo = new JComboBox( convert.getConversionString() ); uomEndCombo = new JComboBox( convert.getConversionString() ); uomStartPanel = new JPanel(); uomStartPanel.setLayout( new BoxLayout( uomStartPanel, BoxLayout.Y_AXIS ) ); uomStartPanel.add( uomStartLabel ); uomStartPanel.add( uomStartCombo ); uomEndPanel = new JPanel(); uomEndPanel.setLayout( new BoxLayout( uomEndPanel, BoxLayout.Y_AXIS ) ); uomEndPanel.add( uomEndLabel ); uomEndPanel.add( uomEndCombo ); uomPanel = new JPanel(); uomPanel.setLayout( new BoxLayout( uomPanel, BoxLayout.X_AXIS ) ); uomPanel.add( uomStartPanel ); uomPanel.add( uomEndPanel ); // Type of Conversion Panel tomDistance = new JRadioButton( "Distance", true ); tomDistance.addActionListener(this); tomDistance.setActionCommand( "Distance" ); tomDryWeight = new JRadioButton( "Dry Weight", false ); tomDryWeight.addActionListener(this); tomDryWeight.setActionCommand( "DryWeight" ); tomLiquidWeight = new JRadioButton( "Liquid Weight", false ); tomLiquidWeight.addActionListener(this); tomLiquidWeight.setActionCommand( "LiquidWeight" ); tomType = new ButtonGroup(); tomType.add( tomDistance ); tomType.add( tomDryWeight ); tomType.add( tomLiquidWeight ); tomPanel = new JPanel(); tomPanel.add( tomDistance ); tomPanel.add( tomDryWeight ); tomPanel.add( tomLiquidWeight ); // content pane getContentPane().setLayout( new BoxLayout( getContentPane(), BoxLayout.Y_AXIS ) ); getContentPane().add( inputPanel ); getContentPane().add( outputPanel ); getContentPane().add( uomPanel ); getContentPane().add( tomPanel ); } /** * actionPerformed method. This method handles the GUI click events * for the application. The input section gets a value from the user. * Once the user selects a unit of measure to convert to, the * calculation is performed. The are 3 radio buttons that determine * the type of conversion to perform. The comboboxes are recreated * when a different radiobutton is selected. * @param ActionEvent supplied by Java's Swing event handler * @return void */ public void actionPerformed( ActionEvent evt ) { if ( evt.getActionCommand().equals( "Convert" ) ) { String input = inputText.getText(); if( input == null || input == "") { // prevents crash if user hasn't input data yet return; } double answer; answer = convert.doConversion( uomStartCombo.getSelectedIndex(), uomEndCombo.getSelectedIndex(), Double.parseDouble( input ) ); outputText.setText( String.valueOf( answer ) ); } else if( evt.getActionCommand().equals( "LiquidWeight" ) ) { setComboBoxes( "/LiquidWeight_Measurement.txt" ); } else if( evt.getActionCommand().equals( "Distance" ) ) { setComboBoxes( "/Distance_Measurement.txt" ); } else if( evt.getActionCommand().equals( "DryWeight" ) ) { setComboBoxes( "/DryWeight_Measurement.txt" ); } } // end of actionPerformed( ActionEvent evt ) method /** * setComboBoxes method - sets the combobox text components and * points the convert object to the desired conversion ratios. * @param String filename - The filename of the conversion ratios * @return void - No data returned * */ private void setComboBoxes( String filename ) { uomStartCombo.removeAllItems(); uomEndCombo.removeAllItems(); convert = new Conversion(); convert.setFactors( filename ); String temp[] = new String[convert.getConversionString().length]; temp = convert.getConversionString(); for(int i = 0; i < convert.getConversionString().length; i++) { uomStartCombo.addItem( temp[i] ); uomEndCombo.addItem( temp[i] ); } // end for inputText.setText( null ); // clear textfields outputText.setText( null ); } // end setComboBoxes( String filename ) method /** * main method. This creates a new instance of DocumentGUI, then * instantiates a new WindowQuitter() object and lastly sets the * size and visibility of the GUI window. */ public static void main ( String[] args ) { // New GUI instance ConversionGUI convertApp = new ConversionGUI(); // New WindowQuitter instance WindowQuitter wquit = new WindowQuitter(); convertApp.addWindowListener( wquit ); // Set window size & make visible convertApp.setSize( 500, 150 ); convertApp.setVisible( true ); } // end main } // End Class ConversionGUI class WindowQuitter extends WindowAdapter { public void windowClosing( WindowEvent e ) { System.exit( 0 ); } }