# Working with Methods

• February 16th, 2013, 12:57 PM
JBtheIV
Working with Methods
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.

Code Java:

```/* * 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!
• February 16th, 2013, 03:35 PM
Norm
Re: Working with Methods
Look at the lines the method is to print. It has two numbers and some Strings.
If you pass the two numbers to the method in the order they are to be put into the text of the message, then the only thing the method needs extra info about is which of two message texts to print. There are many ways to do that.
See if you can think of one based on your current java knowledge.
• February 16th, 2013, 03:53 PM
SunshineInABag
Re: Working with Methods
Your code looks quite clean and organized, plus you've already done the bulk of the work, so that's great! There are several simple ways you can get the third method to work. A hint that would help you out is to keep in mind, you can call methods inside of other methods. Alternatively, if you've learned method overriding, you could override toString().
• February 17th, 2013, 09:56 AM
JBtheIV
Re: Working with Methods
public static void main(){
nPounds = input.nextInt();
System.out.print(nPounds + " pounds is " + dPoundsToKilograms + " kilograms");
}

Why is it when I do this, nPounds and everything else become under marked with red and giving me errors? Isn't this what your suggesting I do? I'm a bit confused. Sorry.
• February 17th, 2013, 10:10 AM
Norm
Re: Working with Methods
You've removed all the other code where the variables are defined.
Who suggested that you remove all that code?
• February 17th, 2013, 10:30 AM
JBtheIV
Re: Working with Methods
Quick update! I reworked my code around this time and managed to create the 3rd method. The only problem is it will give me a result something like this. Basically, I got it to print, but now it isn't printing what the user input and the respected conversion:

Please select the type of conversion you would like to make: 2
0 kilograms is 0.0 pounds

Code Java:

```/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package practice;   import java.util.Scanner;   /** * * @author jamesbottom */ public class Practice {   /** * @param args the command line arguments */ public static void main(String[] args) { //Declare variables Scanner input = new Scanner (System.in); int nMenuOption = 0; int nWeight = 0; double dResult = 0.0;   // Display menu   System.out.print("WEIGHT CONVERSION" + '\n' + "1. Pounds to kilograms" + '\n' + "2. Kilograms to pounds"+ '\n');   //Create a blank line System.out.println();     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: "); nWeight = input.nextInt(); dResult = convertPoundsToKilograms (nWeight); printConvertedWeight(nMenuOption,nWeight,dResult);   } else if (nMenuOption == 2) { System.out.print("Please enter the kilograms: "); nWeight = input.nextInt(); dResult = convertKilogramsToPounds(nWeight); printConvertedWeight(nMenuOption,nWeight,dResult); } else if (nMenuOption != 1 || nMenuOption != 2) { System.out.print("Invalid conversion selection. Please run " + "the program again."); }                   }//end main method   public static double convertPoundsToKilograms (double dPounds){ final double POUNDS_IN_KILO = 2.204; double dKilogramsFromPounds = 0.0;   dKilogramsFromPounds = dPounds * POUNDS_IN_KILO;   return dPounds; }   public static double convertKilogramsToPounds (double dKilograms){ final double KILO_IN_POUNDS = 0.453592; double dPoundsFromKilograms = 0.0;   dPoundsFromKilograms = dKilograms * KILO_IN_POUNDS;   return dKilograms; }   public static void printConvertedWeight(int nNum0,int nNum1, double dNum2){   nNum1 = 0; dNum2 = 0.0; if (nNum0 == 1){ System.out.print(nNum1 + " pounds is " + dNum2 + " kilograms."); } else if (nNum0 == 2){ System.out.print(nNum1 + " kilograms is " + dNum2 + " pounds."); }     }   }//end class Practice```
• February 17th, 2013, 10:37 AM
Norm
Re: Working with Methods
Are these variable's values what you are seeing printed:
Code :

``` nNum1 = 0; dNum2 = 0.0;```

If you don't want those values printed, remove the above statements from the method.
• February 17th, 2013, 12:22 PM
JBtheIV
Re: Working with Methods
Oh ok, that makes alot of sense. But now when I test it again it reads something like this:

Please select the type of conversion you would like to make: 2
50 kilograms is 50.0 pounds

--- Update ---

Quick Update!

I was messing around with my code and I decided to change info in these 2 methods:
Code Java:

``` public static double convertPoundsToKilograms (double dPounds){ final double POUNDS_IN_KILO = 2.204; double dKilogramsFromPounds = 0.0;   dPounds = dPounds / POUNDS_IN_KILO;   return dPounds; }   public static double convertKilogramsToPounds (double dKilograms){ final double KILO_IN_POUNDS = 0.453592; double dPoundsFromKilograms = 0.0;   dKilograms = dKilograms / KILO_IN_POUNDS;   return dKilograms; }```

I was wondering, does this make sense because now my program runs successfully. But now it seems like theres stuff in the methods that I can delete or change.
• February 17th, 2013, 12:31 PM
Norm
Re: Working with Methods
Quote:

does this make sense
Do the methods return the correct values?
Quote:

50 kilograms is 50.0 pounds
The above does not look correct.

What is this variable for: dKilogramsFromPounds? It isn't used for anything.
• February 17th, 2013, 12:40 PM
JBtheIV
Re: Working with Methods
I said in my previous post that my program is running the way it should. 50 kilograms is 22.686.... and 50 pounds is 110 kilograms. I just wanted to make sure those 2 methods were set up right. I guess I'll go ahead and delete those unused variables then since they don't do anything.

I apologize for the confusion, but it feels like sometimes you reply in riddles and I sometimes don't understand what you mean. Sorry.
• February 17th, 2013, 12:46 PM
Norm
Re: Working with Methods
What you posted for the program's output was confusing: 50 kilograms is 50.0 pounds

• February 17th, 2013, 01:04 PM
JBtheIV
Re: Working with Methods
Yeah that was the problem I was having because thats not correct. But I guess my program is solid then.
• May 9th, 2013, 04:49 AM
nicolerivers3201
Re: Working with Methods
This is what we call the Clean Code Principle. In projects we accomplished, every method has a name which describes parameters that the method accepts and also the main procedure it does. An example of this is the method loadByUserName(String userName). At one glance , you already have an idea that this method accept a parameter username that will be use to retrieve a particular user. This demonstrates the usefulness of this principle.

Great job on this! :)
• May 9th, 2013, 08:28 AM
copeg
Re: Working with Methods
Quote:

Originally Posted by nicolerivers3201
This is what we call the Clean Code Principle. In projects we accomplished, every method has a name which describes parameters that the method accepts and also the main procedure it does. An example of this is the method loadByUserName(String userName). At one glance , you already have an idea that this method accept a parameter username that will be use to retrieve a particular user. This demonstrates the usefulness of this principle.

Please don't add to the confusion by posting things seemingly unrelated to a three month old, solved post. Doing so makes it look as though you are solely attempting to promote the site in your signature, which doesn't come across well to moderators.