Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java - NEED HELP FOR COLLECTING INPUT FOR THE IDEAL GAS LAW USING JOPTIONPANE My code produces all the correct output for my assignment. What

Java - NEED HELP FOR COLLECTING INPUT FOR THE IDEAL GAS LAW USING JOPTIONPANE

My code produces all the correct output for my assignment. What the assignment wants me to do is use the Ideal Gas Law to find the answer to either pressure, temperature, or volume. As you can see in my code, it asks the user to select which one he/she would like to solve for. The problem is I need to implement logic that makes it so, for example, the input box for pressure doesn't appear if the user is solving for pressure. I'm only allowed to use showInputDialog once each, which is shown on lines 46-49, which means I can't duplicate those lines. So I'm very confused on how I can make this work. All we've learned is if statements, no loops are allowed. Please don't use any methods that shortcut this because chances are I will get a 0. I included my methods as well just so you can further see how my program works. The only part I need to know how to do is if the user wants to solve for pressure, it only prompts them for the temperature, volume, and moles. If the user wants to solve for temperature, it only prompts them for the pressure, volume, and moles. And pressure, temperature, and moles if the user solves for volume.

Thank you!

image text in transcribed

image text in transcribed

// show title // function: System.out.println System.out.println("Ideal Gas Law Program" ); System.out.println( // prompt user for which item to find (pressure, temperature, volume) // function: JOptionPane.showInputDialog userChoice = JOptionPane.showInputDialog("Enter name of value" + " to be found (e.g, Pressure, Temperature, Volume)" ); // find result flag // function: findResultFlag chosenFlag = findResultFlag( userChoice ); // get the appropriate inputs - ONLY ONE INPUT OPERATION FOR EACH ITEM tempStr = JOptionPane. showInputDialog("Enter temperature (K)"); pressureStr = JOptionPane.showInputDialog("Enter pressure (N/m^2)"); volumeStr = JOptionPane.showInputDialog("Enter volume (m^3)"); molsStr = JOptionPane. showInputDialog("Enter number of mols"); temp = Double.parseDouble(tempStr); volume = Double.parseDouble(volumeStr); mols = Double.parseDouble (molsStr); pressure = Double.parseDouble(pressureStr); calculatedResult - findResult(chosenFlag, pressure, temp, volume, mols); displayResult(chosenFlag, pressure, temp, volume, mols, calculatedResult); private static double findResult( int flag, double press, double temp, double vol, double numMols ) if ( flag == PRESSURE_RESULT ) return numMols * CONSTANT_R * temp) / vol; else if ( flag == TEMPERATURE_RESULT ) return (press * vol) / (numMols * CONSTANT_R); else if ( flag == VOLUME_RESULT ) return (numMols * CONSTANT_R * temp) / press; else return 0.0; private static int findResultFlag( String inputStr ) int sameString = 0; if (compareStrings "pressure", inputStr ) == sameString ) return PRESSURE_RESULT; else if (compareStrings( "temperature", inputStr ) == sameString ) return TEMPERATURE_RESULT; else if (compareStrings( "volume", inputStr ) == sameString) return VOLUME_RESULT; else return FAILED_INPUT; // show title // function: System.out.println System.out.println("Ideal Gas Law Program" ); System.out.println( // prompt user for which item to find (pressure, temperature, volume) // function: JOptionPane.showInputDialog userChoice = JOptionPane.showInputDialog("Enter name of value" + " to be found (e.g, Pressure, Temperature, Volume)" ); // find result flag // function: findResultFlag chosenFlag = findResultFlag( userChoice ); // get the appropriate inputs - ONLY ONE INPUT OPERATION FOR EACH ITEM tempStr = JOptionPane. showInputDialog("Enter temperature (K)"); pressureStr = JOptionPane.showInputDialog("Enter pressure (N/m^2)"); volumeStr = JOptionPane.showInputDialog("Enter volume (m^3)"); molsStr = JOptionPane. showInputDialog("Enter number of mols"); temp = Double.parseDouble(tempStr); volume = Double.parseDouble(volumeStr); mols = Double.parseDouble (molsStr); pressure = Double.parseDouble(pressureStr); calculatedResult - findResult(chosenFlag, pressure, temp, volume, mols); displayResult(chosenFlag, pressure, temp, volume, mols, calculatedResult); private static double findResult( int flag, double press, double temp, double vol, double numMols ) if ( flag == PRESSURE_RESULT ) return numMols * CONSTANT_R * temp) / vol; else if ( flag == TEMPERATURE_RESULT ) return (press * vol) / (numMols * CONSTANT_R); else if ( flag == VOLUME_RESULT ) return (numMols * CONSTANT_R * temp) / press; else return 0.0; private static int findResultFlag( String inputStr ) int sameString = 0; if (compareStrings "pressure", inputStr ) == sameString ) return PRESSURE_RESULT; else if (compareStrings( "temperature", inputStr ) == sameString ) return TEMPERATURE_RESULT; else if (compareStrings( "volume", inputStr ) == sameString) return VOLUME_RESULT; else return FAILED_INPUT

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Refactoring Databases Evolutionary Database Design

Authors: Scott Ambler, Pramod Sadalage

1st Edition

0321774515, 978-0321774514

More Books

Students also viewed these Databases questions

Question

Solve problem 5.x + 2 > 1

Answered: 1 week ago