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 7 of 7

Thread: Using methods improperly

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Using methods improperly

    I just recently started to learn about methods. I'm not that good with them nor do I fully understand them. This program I am writing is a simple program its asks the user if they want to convert pounds to kilograms or kilograms to pounds. In this assignment we are required to have 4 methods: The main method, P2k(pounds to kilos method), K2P, and a method that prints the results. I am not sure how to properly call the methods into the main method Here is my code if anyone can help me out.

     
     
    package weightconversion;
     
    import java.util.Scanner;
     
    /**
     *
     * @author Zach
     */
    public class WeightConversion {
     
        //Create global conversion rate
        public static final double CONVERSION_RATE =2.204;
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
           //Declare variables
            int nChoice=0;             //the user will choose pounds or kilos.
            Scanner input = new Scanner (System.in);     //Used to read input
            double dP2K=0;                //Converted pounds to kilos
            double  dK2P=0;               //Converted kilos to pounds
            double  dPounds=0;            //variable for pounds
            double  dKilos=0;             //variable for kilos
            double  dWeight=0;            //Weight variable
            double  dConvertedWeight=0;   //variable for weight converted
     
     
            //Display weight conversion
            System.out.println("WEIGHT CONVERSION");
            //Displays pounds to kilos
            System.out.println("1." + "Pounds to kilograms");
            //Displays kilos to pounds
            System.out.println("2." + "Kilograms to pounds");
     
            //Prompt user to select type of conversion
            System.out.print("Please select the type of conversion"
                    + " you would like to make:");
            //Record input
            nChoice = input.nextInt();
     
            //Prompt user to enter pounds
                if (nChoice == 1) {
                    //Prompt user to enter pounds
                    System.out.print("Please enter the pounds:");
                    //Record input
                    dPounds = input.nextDouble();
                    //Calls method P2k with parameter dPounds
                    dConvertedWeight = P2K(dPounds);
                }
                else if (nChoice == 2){
                    //Promp user to enter kilos
                    System.out.print("Please enter the kilograms:");
                    //Record input
                    dKilos = input.nextDouble();
                    //Calls method K2P with parameter dKilos
                    dConvertedWeight = K2P (dKilos);
                }
                //What happens if user incorrectly enters wrong integer
                else 
             //Tell user they need to run program again
             System.out.println("Error: Invalid conversion selection," + 
                     "Please run the program again. " );
                dWeight = dPounds + dWeight;
                dWeight = dKilos + dWeight;
        }
     
                 //Converts pounds to kilograms   
                public static double P2K(double dPounds){
                double dConvertedPounds = 0;         //The converted pounds
                dConvertedPounds = dPounds/CONVERSION_RATE;  //Formula
                return dConvertedPounds;           //Retrun statement
                }
     
             public static double K2P(double dKilos){
                 double dConvertedKilos = 0;           //The converted kilos
                 dConvertedKilos = dKilos/CONVERSION_RATE;   //Formula
                 return dConvertedKilos;      //Return statement
     
             }
             public static void PrintResults(dConvertedWeight,nChoice,dWeight){
     
             }
     
     
     
     
     
     
        }



    Im getting an error code for the last method saying it can not find the symbol, I know i'm doing it incorrectly, but not sure how to properly write it.


  2. #2
    Member GoodbyeWorld's Avatar
    Join Date
    Jul 2012
    Location
    Hidden command post deep within the bowels of a hidden bunker somewhere under a nondescrip building
    Posts
    384
    My Mood
    Angry
    Thanks
    46
    Thanked 63 Times in 63 Posts

    Default Re: Using methods improperly

    public static void PrintResults(dConvertedWeight,nChoice,dWeight){

    }


    You forgot to put the variable types.

    i.e.


    public static void PrintResults(double dConvertedWeight, int nChoice, double dWeight)


    Also, as for how to properly call a method inside the main method, you've already done it correctly here:

    dConvertedWeight = P2K(dPounds);

    dConvertedWeight = K2P (dKilos);

  3. The Following User Says Thank You to GoodbyeWorld For This Useful Post:

    d0mlnance (October 4th, 2013)

  4. #3
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,550
    My Mood
    Grumpy
    Thanks
    0
    Thanked 169 Times in 160 Posts

    Default Re: Using methods improperly

    When you get an error you should copy and paste the exact message and indicate which line it occurs on (so we don't have to count).
    Improving the world one idiot at a time!

  5. #4
    Junior Member
    Join Date
    Oct 2013
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Using methods improperly

    So i have the conversions working but I think im doing it without calling the printresults method could anyone tell me how i would incorporate the printresults method into my calculations, also when is displays it has to say 50 pounds but it says 50.0 pounds, should i change it to an int?


     
     
    */
    package weightconversion;
     
    import java.util.Scanner;
     
    /**
     *
     * @author Zach
     */
    public class WeightConversion {
     
        //Create global conversion rate
        public static final double CONVERSION_RATE =2.204;
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
           //Declare variables
            int nChoice=0;             //the user will choose pounds or kilos.
            Scanner input = new Scanner (System.in);     //Used to read input
            double dP2K=0;                //Converted pounds to kilos
            double  dK2P=0;               //Converted kilos to pounds
            double  dPounds=0;            //variable for pounds
            double  dKilos=0;             //variable for kilos
            double  dWeight=0;            //Weight variable
            double  dConvertedWeight=0;   //variable for weight converted
     
     
            //Display weight conversion
            System.out.println("WEIGHT CONVERSION");
            //Displays pounds to kilos
            System.out.println("1." + "Pounds to kilograms");
            //Displays kilos to pounds
            System.out.println("2." + "Kilograms to pounds");
     
            //Prompt user to select type of conversion
            System.out.print("Please select the type of conversion"
                    + " you would like to make:");
            //Record input
            nChoice = input.nextInt();
     
            //Prompt user to enter pounds
                if (nChoice == 1) {
                    //Prompt user to enter pounds
                    System.out.print("Please enter the pounds:");
                    //Record input
                    dPounds = input.nextDouble();
                    //Calls method P2k with parameter dPounds
                    dConvertedWeight = P2K(dPounds);
                    //Display calculation
                    System.out.print(dPounds +   " pounds is " +   dConvertedWeight 
                            + " kilograms." );
                }
                else if (nChoice == 2){
                    //Promp user to enter kilos
                    System.out.print("Please enter the kilograms:");
                    //Record input
                    dKilos = input.nextDouble();
                    //Calls method K2P with parameter dKilos
                    dConvertedWeight = K2P (dKilos);
                    //Display calculation
                    System.out.print(dKilos + "kilograms is " + dConvertedWeight 
                            + " pounds. ");
                }
                //What happens if user incorrectly enters wrong integer
                else 
             //Tell user they need to run program again
             System.out.println("Error: Invalid conversion selection," + 
                     "Please run the program again. " );
                dWeight = dPounds + dWeight;
                dWeight = dKilos + dWeight;
     
                if (nChoice == 1){
     
                }
        }
     
                 //Converts pounds to kilograms   
                public static double P2K(double dPounds){
                double dConvertedPounds = 0;         //The converted pounds
                dConvertedPounds = dPounds/CONVERSION_RATE;  //Formula
                return dConvertedPounds;           //Retrun statement
                }
     
             public static double K2P(double dKilos){
                 double dConvertedKilos = 0;           //The converted kilos
                 dConvertedKilos = dKilos*CONVERSION_RATE;   //Formula
                 return dConvertedKilos;      //Return statement
     
             }
             public static void PrintResults(double dConvertedWeight,int nChoice,
                     double dWeight){
     
             }
     
     
     
     
     
     
        }



    Here are the results

    WEIGHT CONVERSION
    1.Pounds to kilograms
    2.Kilograms to pounds
    Please select the type of conversion you would like to make:1
    Please enter the pounds:50
    50.0 pounds is 22.686025408348456 kilograms.BUILD SUCCESSFUL (total time: 6 seconds)



    Kilos to pounds :WEIGHT CONVERSION
    1.Pounds to kilograms
    2.Kilograms to pounds
    Please select the type of conversion you would like to make:2
    Please enter the kilograms:50
    50.0kilograms is 110.2 pounds. BUILD SUCCESSFUL (total time: 3 seconds)


    and the last one choosing neither :
    WEIGHT CONVERSION
    1.Pounds to kilograms
    2.Kilograms to pounds
    Please select the type of conversion you would like to make:-1
    Error: Invalid conversion selection,Please run the program again.
    BUILD SUCCESSFUL (total time: 2 seconds)

  6. #5
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,550
    My Mood
    Grumpy
    Thanks
    0
    Thanked 169 Times in 160 Posts

    Default Re: Using methods improperly

    Currently you have the print statements in your if statement. Move them to the printResults method and then call that method instead.
    Improving the world one idiot at a time!

  7. #6
    Junior Member
    Join Date
    Oct 2013
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Using methods improperly

    Okay so I moved it to the print method and called that method, but I messed up somehow because now when I convert something I says 0.0 pounds and does the conversion and repeats it twice.
     
     * and open the template in the editor.
     */
    package weightconversion;
     
    import java.util.Scanner;
     
    /**
     * This method converts kilograms to pounds or pounds to kilograms
     * @author Zach
     * @version 10/4/2013
     */
    public class WeightConversion {
        // This method prompts user to either select pounds or kilograms and then input a number which returns a conversion rate
     
        //Create global conversion rate
        public static final double CONVERSION_RATE =2.204;
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
           //Declare variables
            int nChoice=0;             //the user will choose pounds or kilos.
            Scanner input = new Scanner (System.in);     //Used to read input
            double dP2K=0;                //Converted pounds to kilos
            double  dK2P=0;               //Converted kilos to pounds
            double  dPounds=0;            //variable for pounds
            double  dKilos=0;             //variable for kilos
            double  dWeight=0;            //Weight variable
            double  dConvertedWeight=0;   //variable for weight converted
     
     
            //Display weight conversion
            System.out.println("WEIGHT CONVERSION");
            //Displays pounds to kilos
            System.out.println("1." + "Pounds to kilograms");
            //Displays kilos to pounds
            System.out.println("2." + "Kilograms to pounds");
     
            //Prompt user to select type of conversion
            System.out.print("Please select the type of conversion"
                    + " you would like to make:");
            //Record input
            nChoice = input.nextInt();
     
            //Prompt user to enter pounds
                if (nChoice == 1) {
                    //Prompt user to enter pounds
                    System.out.print("Please enter the pounds:");
                    //Record input
                    dPounds = input.nextDouble();
                    //Calls method P2k with parameter dPounds
                    dConvertedWeight = P2K(dPounds);
                    //Calls method to print results
                    PrintResults(dConvertedWeight,nChoice,dWeight);
     
                }
                else if (nChoice == 2){
                    //Promp user to enter kilos
                    System.out.print("Please enter the kilograms:");
                    //Record input
                    dKilos = input.nextDouble();
                    //Calls method K2P with parameter dKilos
                    dConvertedWeight = K2P (dKilos);
                     //Calls method to print results
                    PrintResults(dConvertedWeight,nChoice,dWeight);
     
                }
                //What happens if user incorrectly enters wrong integer
                else 
             //Tell user they need to run program again
             System.out.println("Error: Invalid conversion selection," + 
                     "Please run the program again. " );
                dWeight = dPounds + dWeight;
                dWeight = dKilos + dWeight;
     
                if (nChoice == 1){
     
                }
        }
     
                 //Converts pounds to kilograms   
                public static double P2K(double dPounds){
                double dConvertedPounds = 0;         //The converted pounds
                dConvertedPounds = dPounds/CONVERSION_RATE;  //Formula
                return dConvertedPounds;           //Retrun statement
                }
     
             public static double K2P(double dKilos){
                 double dConvertedKilos = 0;           //The converted kilos
                 dConvertedKilos = dKilos*CONVERSION_RATE;   //Formula
                 return dConvertedKilos;      //Return statement
     
             }
             public static void PrintResults(double dConvertedWeight,int nChoice,
                     double dWeight){
                 double dKilos=dWeight;
                 double dPounds =dWeight;
                  //Display calculation
                    System.out.print(dPounds +   " pounds is " +   dConvertedWeight 
                            + " kilograms." );
                    //Display calculation
                     //Display calculation
                    System.out.print(dKilos + "kilograms is " + dConvertedWeight 
                            + " pounds. ");
     
     
             } // end Method weightConversion
     
     
     
     
     
     
        } // end Class WeightConversion

    These are the results :
    WEIGHT CONVERSION
    1.Pounds to kilograms
    2.Kilograms to pounds
    Please select the type of conversion you would like to make:1
    Please enter the pounds:50
    0.0 pounds is 22.686025408348456 kilograms.0.0kilograms is 22.686025408348456 pounds. BUILD SUCCESSFUL (total time: 3 seconds)

    Kilos to pounds:
    WEIGHT CONVERSION
    1.Pounds to kilograms
    2.Kilograms to pounds
    Please select the type of conversion you would like to make:2
    Please enter the kilograms:50
    0.0 pounds is 110.2 kilograms.0.0kilograms is 110.2 pounds. BUILD SUCCESSFUL (total time: 3 seconds)

  8. #7
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,550
    My Mood
    Grumpy
    Thanks
    0
    Thanked 169 Times in 160 Posts

    Default Re: Using methods improperly

    Pretend you are the computer and execute each line of code. Look carefully at the printResults method. Surely you can see why it is printing out 2 messages. Another thing to think very carefully about is what is the dWeight variable for.
    Improving the world one idiot at a time!

  9. The Following User Says Thank You to Junky For This Useful Post:

    d0mlnance (October 4th, 2013)

Similar Threads

  1. Methods
    By kave2 in forum Java Theory & Questions
    Replies: 8
    Last Post: January 22nd, 2013, 11:56 AM
  2. 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, 02:16 AM
  3. Help with methods
    By CSUTD in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 8th, 2011, 11:39 PM
  4. I need help with METHODS!!!
    By Slone in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 14th, 2010, 08:33 AM
  5. Forum list improperly marks whats unread
    By Lord.Quackstar in forum The Cafe
    Replies: 14
    Last Post: June 18th, 2010, 02:14 AM