Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 14 of 14

Thread: Working with Methods

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

    /*
     * 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!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Feb 2013
    Location
    Canada
    Posts
    54
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Default 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().

  4. #4
    Junior Member
    Join Date
    Feb 2013
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Working with Methods

    You've removed all the other code where the variables are defined.
    Who suggested that you remove all that code?
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Feb 2013
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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
    Please enter the kilograms: 23
    0 kilograms is 0.0 pounds

    /*
     * 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

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Working with Methods

    Are these variable's values what you are seeing printed:
          nNum1 = 0;
          dNum2 = 0.0;

    If you don't want those values printed, remove the above statements from the method.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Junior Member
    Join Date
    Feb 2013
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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
    Please enter the kilograms: 50
    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:
       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.

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Working with Methods

    does this make sense
    Do the methods return the correct values?
    50 kilograms is 50.0 pounds
    The above does not look correct.

    What is this variable for: dKilogramsFromPounds? It isn't used for anything.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Junior Member
    Join Date
    Feb 2013
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Working with Methods

    What you posted for the program's output was confusing: 50 kilograms is 50.0 pounds

    Glad you got it working.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #12
    Junior Member
    Join Date
    Feb 2013
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Working with Methods

    Yeah that was the problem I was having because thats not correct. But I guess my program is solid then.

  13. #13
    Junior Member nicolerivers3201's Avatar
    Join Date
    May 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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!
    Ideyatech
    "Where ideas and technologies meet"

  14. #14
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Working with Methods

    Quote Originally Posted by nicolerivers3201 View Post
    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.

Similar Threads

  1. Why and where abstract methods & classes and static methods are used?
    By ajaysharma in forum Object Oriented Programming
    Replies: 7
    Last Post: July 14th, 2012, 01:16 AM
  2. I need help with my methods please?
    By THeAnnonymous in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 11th, 2011, 09:22 AM
  3. Working with Methods
    By duckman in forum Object Oriented Programming
    Replies: 3
    Last Post: November 9th, 2009, 08:27 PM
  4. Replies: 4
    Last Post: January 27th, 2009, 12:03 AM