Input / Output - Arithmetic Equation

I am new to Java, started two weeks ago, and I am having problems with this issue. I have a problem in the class that I accept, which was taken earlier. Convert kilograms to kilograms and round to the second decimal place.

I can create the input side of things and invoke a dialog box to prompt the user to enter the weight. I can also create output that uses the equation I made to output the answer in the dialog box.

My question is how to take the information that is entered and use it to convert from kilograms to pounds?

I read my book and cleaned the Internet, trying to find the answer, and I think that maybe I thought about it. Thanks for the help.

Input.java

//This program asks a user to input a weight in kilograms. package module2; import javax.swing.*; public class Input { public static void main(String[] args) { String weight; weight = JOptionPane.showInputDialog("Enter weight in kilograms"); } } 

Output.java

 //This program outputs a converted weight from kilograms to pounds. package module2; import javax.swing.JOptionPane; public class Output { public static void main(String[] args) { double kg = 75.5; double lb = 2.2; double sum; sum = (kg * lb); JOptionPane.showMessageDialog(null,sum, "Weight Conversion", JOptionPane.INFORMATION_MESSAGE); } } 
+6
source share
2 answers

You now have 2 main methods. Both are entry points for the program. Since they must exchange information, it does not make sense that you have both.

I would recommend changing the main Output method to an instance method, taking one parameter: weight from Input .

Same:

 public void printOutput(final double weight){ //... } 

Then you can call this from the main Input method as follows:

 public static void main(String[] args) { String weight; weight = JOptionPane.showInputDialog("Enter weight in kilograms"); double kg = Double.parseDouble(weight); // Be sure to parse the weight to a number Output output = new Output(); // Create a new instance of the Output class output.printOutput(kg); // Call our method to display the output of the conversion } 

Another thing, since Output is currently used for only one method, you can simply move this method to Input .

+6
source
 // addition of two integers using JOptionPane import javax.swing.JOptionPane; public class Addition { public static void main(String[] args) { String firstNumber = JOptionPane.showInputDialog("Input <First Integer>"); String secondNumber = JOptionPane.showInputDialog("Input <Second Integer>"); int num1 = Integer.parseInt(firstNumber); int num2 = Integer.parseInt(secondNumber); int sum = num1 + num2; JOptionPane.showMessageDialog(null, "Sum is" + sum, "Sumof two Integers", JOptionPane.PLAIN_MESSAGE); } } 
0
source

Source: https://habr.com/ru/post/987600/


All Articles