Hello.
My teacher gave us an assignment.
Menu to be displayed for the user:
WEIGHT CONVERSION
1. Pounds to kilograms
2. Kilograms to pounds
Note: To indicate the type of conversion, the user will enter the number of the conversion, NOT the type of conversion. For example, if the user wants to convert pounds to kilograms, he/she would type 1 (not “Pounds to kilograms”). Please refer to the Sample Output file for full details of what the program should look like when it runs.
Your program MUST contain the following three user-defined methods, in addition to the main function:
1. Convert pounds to kilograms
2. Convert kilograms to pounds
3. Print converted weight
I've managed to create my program and it runs fine. The thing I don't understand is the 3rd method (Print converted weight).
I did the first 2 methods.
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package weightconversion; import java.util.Scanner; /** * * @author jamesbottom */ public class WeightConversion { /** * @param args the command line arguments */ public static void main(String[] args) { // Declare variables Scanner input = new Scanner(System.in); int nMenuOption = 0; int nPounds = 0; int nKilograms = 0; double dPoundsToKilograms = 0.0; double dKilogramsToPounds = 0.0; //Print the menu System.out.println("WEIGHT CONVERSION" + '\n'+ "1. Pounds to kilograms" + '\n' + "2. Kilograms to pounds"); //Create a blank line System.out.println(); //Prompt user for the type of conversion System.out.print("Please select the type of conversion you would like to make: "); nMenuOption = input.nextInt(); if (nMenuOption == 1) { System.out.print("Please enter the pounds: "); nPounds = input.nextInt(); dPoundsToKilograms = convertPoundsToKilograms(nPounds); System.out.print(nPounds + " pounds is " + dPoundsToKilograms + " kilograms"); } else if (nMenuOption == 2) { System.out.print("Please enter the kilograms: "); nKilograms = input.nextInt(); dKilogramsToPounds = convertKilogramsToPounds (nKilograms); System.out.print(nKilograms + " kilograms is " + dKilogramsToPounds + " pounds."); } else if (nMenuOption != 1 || nMenuOption != 2) { System.out.println("Invalid conversion selection. Please " + "run the program again."); }//end if/else for menu selection process }//end main method public static double convertPoundsToKilograms(double dPoundsEntered) { //Declare variables double dKilo = 0.0; final double POUNDS_IN_KILO = 2.204; //Calculate miles traveled dKilo = dPoundsEntered / POUNDS_IN_KILO; return dKilo; } //end method convertPoundsToKilograms public static double convertKilogramsToPounds(double dKilogramsEntered) { //Declare variables double dlbs = 0.0; final double KILO_TO_POUNDS = 0.453592; //Calculate miles traveled dlbs = dKilogramsEntered / KILO_TO_POUNDS; return dlbs; } //end method convertKilogramsToPounds }//end class WeightConversion
My Professor then responded to my inquiry with this:
Your code looks good! The third method should print the last line of output to the screen. Depending what the user enters, you should print
System.out.print(nPounds + " pounds is " + dPoundsToKilograms + " kilograms");
or
System.out.print(nKilograms + " kilograms is " + dKilogramsToPounds + " pounds.");
Hope this helps –
I was hoping someone could give me a little insight. It would be greatly appreciated!