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

Thread: Methods and passing variables

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

    Default Methods and passing variables

    This program functions as it should, but I need to rewrite it so that there are only local variables, and they should pass from one method to another. Also the main block of code should only contain method calls to carry out the tasks. Could someone please show me how to do that?


    import java.util.Scanner;

    public class Project_Modification_v1

    {

    public static String employeeName;
    public static int employeeID;
    public static double hourlyWage;
    public static double regularHours;
    public static double overtimeHours;
    public static double overtimePay;
    public static double regularPay;
    public static double totalPay;
    public static Scanner reader = new Scanner(System.in);


    public static void main(String args[])

    {

    enterInfo();
    calculateInfo();
    showResults();

    }


    public static void enterInfo()
    {
    System.out.print("Enter Employee Name: ");
    employeeName = reader.nextLine();
    System.out.print("Enter Employee ID: ");
    employeeID = reader.nextInt();
    System.out.print("Enter Hourly Wage: ");
    hourlyWage = reader.nextDouble();
    System.out.print("Enter Regular Hours: ");
    regularHours = reader.nextDouble();
    System.out.print("Enter Overtime Hours: ");
    overtimeHours = reader.nextDouble();
    }


    public static void calculateInfo()
    {
    overtimePay = (overtimeHours * (1.5*hourlyWage));
    regularPay = (regularHours*hourlyWage);
    totalPay = (overtimePay+regularPay);
    }


    public static void showResults()
    {
    System.out.print(employeeName + " (no." + employeeID + ") has a total weekly pay of $" + totalPay);
    }

    }


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

    Default Re: Methods and passing variables

    Quote Originally Posted by bamxmejia View Post
    I need to rewrite it so that there are only local variables, and they should pass from one method to another.
    Are you sure this is the requirement? As this makes it very difficult. However, changing everything from static to non-static is a much better approach and therefore all the static variables become instance variables. you would then need to create an object of your class and call the methods on that instance.
    Foo f = new Foo();
    f.methodA();
    f.methodB();
    [quote]
    Also the main block of code should only contain method calls to carry out the tasks.[/code]
    You have already done this but using static methods. You might want to consider my above advice.
    Improving the world one idiot at a time!

Similar Threads

  1. Please help! Passing variables between modules.
    By Aberdeen in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 7th, 2013, 11:18 PM
  2. passing variables from java applet to php
    By sabertooth in forum Java Networking
    Replies: 2
    Last Post: April 19th, 2013, 01:44 AM
  3. Help Passing Variables
    By jo15765 in forum What's Wrong With My Code?
    Replies: 12
    Last Post: May 7th, 2012, 09:22 PM
  4. Passing variables with void methods
    By knightmetal in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 21st, 2012, 09:46 PM
  5. Passing Arrays Between Methods
    By kigroy in forum What's Wrong With My Code?
    Replies: 14
    Last Post: September 10th, 2011, 11:10 PM

Tags for this Thread