Circuuspeatnutz
Circuuspeatnutz

Reputation: 41

Method error in Java

this may be simple or fairly difficult...I have no idea either way. Here's my code:

package looping;

import java.util.Scanner;

public class PayrollMethods2 {

    public static void main(String[] args) {

        int hours = 0;
        double wr = 0;
        double pay;

        getData(hours, wr);

        pay = calculatePay(hours, wr);

        System.out.println(pay);
        // TODO Auto-generated method stub

    }


    public static void getData(Integer hours, Double wr)
    {
        Scanner kb = new Scanner(System.in);


        System.out.print("Please enter your wage rate ");
        wr = kb.nextDouble();

        System.out.print("Please enter your hours work ");
        hours = kb.nextInt();
    }



    public static double calculatePay(int hours, double wr)
    {
        if(hours <= 40)
            return hours * wr;
        else
            return (40 * wr) + ((hours - 40) * (1.5* wr));
    }

}

I want to return the method "getData()" so that when I enter hours and wr on the keyboard, it will return the total. However, it return a total of 0.0 no matter what I press. I know it has something to do with me assigning a value of 0 to the variables and also that the method is "void". But I have no idea how to modify the getData() method so that it returns the proper values.

Upvotes: 4

Views: 1614

Answers (4)

Kakarot
Kakarot

Reputation: 4252

The issue you are facing is because the variables hours & wr are being passed by value to getData() so getData function has its own copy of the variables hours & wr.

main :(initialize)

Variable    Value    Memory Address
hours         0       44444
wr            0.0     55555

getData : hours & wr are different than main so they have different address

Variable    Value    Memory Address
hours         0       66666
wr            0.0     77777

user enters values now : hours - 10 , wr - 20

Variable    Value    Memory Address
hours         10       66666
wr            20.0     77777

main : When control comes back to main the value of hours & wr is stil 0

Variable    Value    Memory Address
hours         0       44444
wr            0.0     55555

Solution :

This problem can be resolved by encapsulating the salary info into a object and then passing that object around( Objects are passed by Reference in Java) :

class Salary
{
  int hours;
  double wr;
}

Now in getData you can create an object of data Salary based on user input info and pass that Object to calculatePay

public static Salary getData()
{
    Scanner kb = new Scanner(System.in);
    Salary sal = new Salary();

    System.out.print("Please enter your wage rate ");
    sal.wr = kb.nextDouble();

    System.out.print("Please enter your hours work ");
    sal.hours = kb.nextInt();

    return sal;
}

public static double calculatePay(Salary sal)
{
}

Upvotes: 3

Rainbolt
Rainbolt

Reputation: 3660

Normally, when you modify an object inside of a function, the changes are reflected outside of that function. Why do Integer objects behave differently? Take a look at the following code.

psv main (String[] args) {
    Integer i = new Integer(1);
    increment(i);
    System.out.println(i); // Prints 1!
}

void increment(Integer i) { i = i + 1; }

We expected a 2 to be printed, but a 1 is printed instead. This is because Integer is immutable.

Typically, if you pass an object into a function an modify its contents, you get a modified object. Because, when immutable objects are modified, a completely new object is created instead. If you create a completely new object, you are no longer modifying the old object!

Therefore, incrementing our Integer created an entirely new Integer that is local to the increment function.

Upvotes: -1

Nicholas
Nicholas

Reputation: 7521

Java is pass-by-value. It looks like you are trying to modify the parameters, which are locally scoped so you won't see the changes in main.

Java doesn't allow multiple returns, so you'll either have to create a class to pass back, or you can put the calls from the getData method into the main method.

Upvotes: 2

Tim B
Tim B

Reputation: 41208

You need to either make wr and hours into static variables inside the class, or you need to have two getData methods and have them return, or you need to make a separate object to store wr and hours.

When you call a method variables are copied into the method. So when you change them inside the method it doesn't change anything outside the method. (If you pass an object in then a reference to the object is passed by value, so in that cases changes inside the object will be seen).

Upvotes: 2

Related Questions