/** * Weight Calculator > DialogInput.java * * Displays a table of calculated weights on Jupiter using JOptionPane for input * and console output. It also illustrates DecimalFormat for output. * * @author Dale Reed */ import javax.swing.JOptionPane; // This class is used for dialog-based I/O import java.text.DecimalFormat; // Used for formatting output public class DialogInput { public static void main(String[] args) { // variable declarations double earthWeight = 100.0; // weight on the earth, declared and initialized double weightMultiplier = 2.628099173553719; // factor for finding Jupiter weight given earth's // Prompt for and get the weight on earth, using a JOptionPane String userInput = JOptionPane.showInputDialog(null, "Enter your weight on earth:"); // Now convert the input string into a numerical value earthWeight = Double.parseDouble( userInput); // display weight on Jupiter double jupiterWeight = earthWeight * weightMultiplier; System.out.print( "If on earth you weigh " + earthWeight + ", "); System.out.println( "then on Jupiter you weigh " + jupiterWeight ); // Repeat output of weight on Jupiter, this time formatting the output // First create the decimal format object DecimalFormat pattern0dot00 = new DecimalFormat("0.00"); // Now use the decimal format object in displaying the output System.out.print( "If on earth you weigh " + earthWeight + ", "); System.out.println( "then on Jupiter you weigh " + pattern0dot00.format(jupiterWeight) ); System.exit(0); } } /* Output: If on earth you weigh 220.0, then on Jupiter you weigh 578.1818181818182 If on earth you weigh 220.0, then on Jupiter you weigh 578.18 */