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

Thread: Help with returning variable from method

  1. #1
    Member
    Join Date
    Sep 2012
    Posts
    41
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Lightbulb Help with returning variable from method

     
    //Calculates the pay, after taxes for an employee
     
     
    import java.util.*;
     
    public class PayCalculator{
     
    	final static double OT_START = 40;
    	final static double OT_ADJUST = 0.5;
    	final static double TAX = 0.2;
     
    	public static void main(String[] arg){
     
                Scanner input = new Scanner(System.in);
     
                System.out.println("Please enter the total hours worked: ");   
                int hours = input.nextInt();
     
                System.out.println("Please enter the hourly rate: ");
                int payRate = input.nextInt();
     
                System.out.println("The net pay is:$" + netpay);
    	}
     
    	//This method calculates the netpay
    	private static double calculateNetPay(double hours, double rate){
     
                double grosspay, overtime, netpay;
     
                grosspay = hours * rate;
                overtime = ((hours * rate) - OT_START) * OT_ADJUST;
                netpay = overtime + grosspay - TAX;
     
    		return 0.0; //add so that sample will compile
    	}
     
        }

    Im trying to return the net pay... for some reason im lost
    Last edited by m7abraham; September 16th, 2012 at 12:57 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,066
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Help with returning variable from method

    What variable has the value of net pay that you are trying to return? Where is the method that is supposed to return the net pay?

    What do you mean by "return"? In many programming languages, return statements return a value to the caller of a method.

    See the tutorial: Defining Methods (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    http://docs.oracle.com/javase/tutori...turnvalue.html


    Your ending code tag is missing the /. It should be:[/code]
    Last edited by Norm; September 16th, 2012 at 12:57 PM. Reason: spelling
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Sep 2012
    Posts
    41
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Help with returning variable from method

    ok thanks
    and sorry this is my first post hehe ill get it edited

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,066
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Help with returning variable from method

    Did you get the links I added to my last post?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Sep 2012
    Posts
    41
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Help with returning variable from method

    yes! thanks
    i'm working on it now... ill post back when i get done
    thanks again

  6. #6
    Member
    Join Date
    Sep 2012
    Posts
    41
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Help with returning variable from method

    ok so I did one adjustment but im still stuck
    [code]

    import java.util.*;

    public class PayCalculator{

    final static double OT_START = 40;
    final static double OT_ADJUST = 0.5;
    final static double TAX = 0.2;

    public static void main(String[] arg){

    Scanner input = new Scanner(System.in);

    System.out.println("Please enter the total hours worked: ");
    int hours = input.nextInt();

    System.out.println("Please enter the hourly rate: ");
    int payRate = input.nextInt();

    System.out.println("The net pay is:$" + netpay);
    }

    //This method calculates the netpay
    private static double calculateNetPay(double hours, double rate){

    double grosspay, overtime, netpay;

    grosspay = hours * rate;
    overtime = ((hours * rate) - OT_START) * OT_ADJUST;
    netpay = overtime + grosspay - TAX;

    return netpay; //add so that sample will compile
    }

    }
    [\code]

    Im trying to return netpay's value so it can be displayed with the output
    the problem is im kinda confused because my teacher gave us the class

    [code]
    private static double calculateNetPay(double hours, double rate)
    [\code]
    and we cant change anything

  7. #7
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 262 Times in 231 Posts

    Default Re: Help with returning variable from method

    Im trying to return netpay's value
    this line here:
    return netpay; //add so that sample will compile
    that is the return for the method, and is required to compile as you noted. That is because what you were given by the instructor says your method has to return a variable of type double. You should read about returning values from methods.

  8. The Following User Says Thank You to jps For This Useful Post:

    m7abraham (September 17th, 2012)

  9. #8
    Member
    Join Date
    Sep 2012
    Posts
    41
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Help with returning variable from method

    thanks fir the help
    im more of a visual learner haha so it might take me a bit to get it...sorry
    anyways i did this:
     
    import java.util.*;
     
    public class PayCalculator{
     
    	final static double OT_START = 40;
    	final static double OT_ADJUST = 0.5;
    	final static double TAX = 0.2;
     
    	public static void main(String[] arg){
     
     
                Scanner input = new Scanner(System.in);
     
                System.out.println("Please enter the total hours worked: ");   
                int hours = input.nextInt();
     
                System.out.println("Please enter the hourly rate: ");
                int payRate = input.nextInt();
     
                System.out.println("The net pay is:$" + net);
     
    	}
     
    	//This method calculates the netpay
    	private static double calculateNetPay(double hours, double rate){
     
                double net;
                double grosspay, overtime;
                grosspay = hours * rate;
                overtime = ((hours * rate) - OT_START) * OT_ADJUST;
                net = overtime + grosspay - TAX;
     
     
    		return 0.0; //add so that sample will compile
    	}
     
        }

    I think i need to call the method in the main class but its either that im doing it wrong or its not working

    im adding

    calculateNetPay(); to the main class and its showing an error

  10. #9
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 262 Times in 231 Posts

    Default Re: Help with returning variable from method

    You have your plain hello world program:
    /** FILE: HelloWorldPlain.java */
    package test;
     
     
    /**@author jps<br> A plain hello world program.*/
    public class HelloWorldPlain {
     
    	/**@param args Not used */
    	public static void main(String[] args) {
    		//Here we see how to print text to the standard output
    		System.out.println("Hello World!");
    	}
     
    }
    The same program as above showing the use of a String variable:
    /** FILE: HelloWorldVariable.java */
    package test;
     
     
    /**@author srs<br>A hello world program showing the use of a variable. */
    public class HelloWorldVariable {
     
    	/**@param args Not used */
    	public static void main(String[] args) {
    		//Here we see the use of a variable in place of the literal text to do the same thing as before
    		System.out.println("Hello World!");
    		System.out.println(helloWorld);
    	}
     
    	private static String helloWorld = "Hello World from a variable!";
     
    }
    The same program as above using the return value of a method to print the last message:
    /** FILE: HelloWorldReturn.java */
    package test;
     
     
    /**@author srs<br>A hello world program showing the use of a variable and a method return. */
    public class HelloWorldReturn {
     
    	/**@param args Not used */
    	public static void main(String[] args) {
    		//Here we see a method call in place of the literal or variable to do the same thing.
    		System.out.println("Hello World!");
    		System.out.println(helloWorld);
    		System.out.println(helloWorldMethod());
    	}
     
    	private static String helloWorldMethod() {
     
    		return "Hello World method return style";
    	}
     
    	private static String helloWorld = "Hello World from a variable!";
    }
    That should give you an idea of the return value.

  11. #10
    Member
    Join Date
    Sep 2012
    Posts
    41
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Help with returning variable from method

    am I on the right track?! :/
     
    import java.util.*;
     
     
     
    public class PayCalculator
     
    {
     
        final static double OT_START = 40;
        final static double OT_ADJUST = 0.5;
        final static double TAX = 0.2;
     
        public static void main(String[] arg)
            {
                Scanner input = new Scanner(System.in);
                System.out.println("Enter the total hours worked for the week:");
                double hours=input.nextDouble();
                System.out.println("Enter the pay rate:");
                double rate=input.nextDouble();
     
                System.out.println(calculateNetPay(hours, rate));
     
            }
        //Calculate the net pay of an employee based on their hours and rate of pay less taxes
        private static double calculateNetPay(double hours, double rate)
            {
                double net = calculateNetPay(hours, rate);
                double grosspay, overtime;
                grosspay = hours * rate;
                overtime = ((hours * rate) - OT_START) * OT_ADJUST;
                net = overtime + grosspay - TAX;
     
                     return 0.0; //add so that sample will compile
        }
    }

  12. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,066
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Help with returning variable from method

    Does the code compile, execute and do what you want?

    Why does the method return 0.0 instead of the computed amount?
    If you don't understand my answer, don't ignore it, ask a question.

  13. #12
    Member
    Join Date
    Sep 2012
    Posts
    41
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Help with returning variable from method

    Sorry im just soo confused at this moment
    does this look kinda closer to the solution?
    import java.util.*;
     
     
     
    public class PayCalculator
     
    {
     
        final static double OT_START = 40;
        final static double OT_ADJUST = 0.5;
        final static double TAX = 0.2;
     
        public static void main(String[] arg)
            {
     
                Scanner input = new Scanner(System.in);
                System.out.println("Enter the total hours worked for the week:");
                double hours=input.nextDouble();
                System.out.println("Enter the pay rate:");
                double rate=input.nextDouble();
     
              System.out.println(calculateNetPay());
     
            }
        //Calculate the net pay of an employee based on their hours and rate of pay less taxes
        private static double calculateNetPay(double hours, double rate)
            {
     
                double net = calculateNetPay();
                double grosspay, overtime;
                grosspay = hours * rate;
                overtime = ((hours * rate) - OT_START) * OT_ADJUST;
                net = overtime + grosspay - TAX;
     
                     return net; //add so that sample will compile
        }
     
        private static double calculateNetPay() {
            double net = calculateNetPay();
            System.out.println(net);
            return net;
        }
     
    }

  14. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,066
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Help with returning variable from method

    What happens when you compile and execute it?
    what prints out?

    Where do you call the method and give it the values it needs to compute the net pay?
    If you don't understand my answer, don't ignore it, ask a question.

  15. #14
    Member
    Join Date
    Sep 2012
    Posts
    41
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Help with returning variable from method

    when I compile and execute I get this
    run:
    Enter the total hours worked for the week:
    34
    Enter the pay rate:
    4
    Exception in thread "main" java.lang.StackOverflowError
    	at PayCalculator.calculateNetPay(PayCalculator.java:43)
    	at PayCalculator.calculateNetPay(PayCalculator.java:43)
    	at PayCalculator.calculateNetPay(PayCalculator.java:43)
    	at PayCalculator.calculateNetPay(PayCalculator.java:43)
    	at PayCalculator.calculateNetPay(PayCalculator.java:43)
    	at PayCalculator.calculateNetPay(PayCalculator.java:43)
    	at PayCalculator.calculateNetPay(PayCalculator.java:43)
    	at PayCalculator.calculateNetPay(PayCalculator.java:43)
    	at PayCalculator.calculateNetPay(PayCalculator.java:43)

    I think its being called here:
    System.out.println(calculateNetPay());
    not sure like i said im confused :/

  16. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,066
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Help with returning variable from method

    That is a recursive call when a method calls itself repeatedly.

    Try calling the version of the method that takes the arguments needed to do the computation:
     calculateNetPay(double hours, double rate)
    The code reads in hours and rate, use them when you call the method. Get rid of the method that doesn't take any arguments. Delete it. It is no use.

    You know how to call a method and pass it some args:
     System.out.println("Enter the pay rate:");
    Here you call the println() method and pass it one arg, a String.
    If you don't understand my answer, don't ignore it, ask a question.

  17. #16
    Member
    Join Date
    Sep 2012
    Posts
    41
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Help with returning variable from method

    so is there more than one thing that im doing wrong?
    or its just one things that im missing?

  18. #17
    Member
    Join Date
    Sep 2012
    Posts
    41
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Help with returning variable from method

    sorry for being a pain. i think i got it now
     
    import java.util.*;
     
     
     
    public class PayCalculator
     
    {
     
        final static double OT_START = 40;
        final static double OT_ADJUST = 0.5;
        final static double TAX = 0.2;
     
        public static void main(String[] arg)
            {
                Scanner input = new Scanner(System.in);
                System.out.println("Enter the total hours worked for the week:");
                double hours=input.nextDouble();
                System.out.println("Enter the pay rate:");
                double rate=input.nextDouble();
                System.out.println("The net pay is:$"+(calculateNetPay(hours, 
                        rate)));
            }
        //Calculate the net pay of an employee based on their hours and rate of pay less taxes
        private static double calculateNetPay(double hours, double rate)
            {
     
                double grosspay, overtime, net;
                grosspay = hours * rate;
                overtime = ((hours * rate) - OT_START) * OT_ADJUST;
                net = ( overtime + grosspay - TAX);
                     return net; //add so that sample will compile
        }
     
    }
    my answer isn't right i think its just a mathematical problem thats left

  19. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,066
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Help with returning variable from method

    Now work on the math.
    If you don't understand my answer, don't ignore it, ask a question.

  20. #19
    Member
    Join Date
    Sep 2012
    Posts
    41
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Help with returning variable from method

    thank you soo soo much both of you
    and sorry for my stupidity or confusion
    //Calculates the pay, after taxes for an employee
    //Mikhail Abraham
    //Last Edited Date: 9/12/2012
     
    import java.util.*;
     
    public class PayCalculator {
     
        final static double OT_START = 40;
        final static double OT_ADJUST = 0.5;
        final static double TAX = 0.2;
     
        public static void main(String[] arg) {
            Scanner input = new Scanner(System.in);
            System.out.println("Enter the total hours worked for the week:");
            double hours = input.nextDouble();
            System.out.println("Enter the pay rate:");
            double rate = input.nextDouble();
            System.out.println("The net pay is:$" + (calculateNetPay(hours,
                    rate)));
        }
        //This method calculates the net pay
     
        private static double calculateNetPay(double hours, double rate) {
     
            double grosspay, overtime, net, tax;
            grosspay = hours * rate;
            overtime = ((hours  - OT_START) * rate) * OT_ADJUST;
            tax = (overtime + grosspay) * TAX;
            net = ((overtime + grosspay) - tax);
            return net; //add so that sample will compile
        }
    }

    I finally got it to work right!!!!

  21. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,066
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Help with returning variable from method

    Don't feel sorry. It's called the learning process.
    If you don't understand my answer, don't ignore it, ask a question.

  22. The Following User Says Thank You to Norm For This Useful Post:

    m7abraham (September 17th, 2012)

Similar Threads

  1. Returning multiple values from a method.
    By atar in forum Java Theory & Questions
    Replies: 14
    Last Post: July 31st, 2012, 04:59 PM
  2. returning variable and saving to file
    By ravencrest in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: November 18th, 2011, 08:04 AM
  3. [SOLVED] Help with method returning a double
    By Mike_Chase in forum What's Wrong With My Code?
    Replies: 9
    Last Post: July 22nd, 2011, 01:09 AM
  4. Method returning 0 for everything
    By JJTierney in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 4th, 2010, 07:51 PM
  5. Method returning boolean
    By Plural in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 13th, 2010, 05:45 PM