Help with returning variable from method
Code :
//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
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]
Re: Help with returning variable from method
ok thanks :)
and sorry this is my first post hehe ill get it edited
Re: Help with returning variable from method
Did you get the links I added to my last post?
Re: Help with returning variable from method
yes! thanks
i'm working on it now... ill post back when i get done
thanks again
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
Re: Help with returning variable from method
Quote:
Im trying to return netpay's value
this line here:
Code java:
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.
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:
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:$" + 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
Re: Help with returning variable from method
You have your plain hello world program:
Code java:
/** 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:
Code java:
/** 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:
Code java:
/** 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.
Re: Help with returning variable from method
am I on the right track?! :/
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("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
}
}
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?
Re: Help with returning variable from method
Sorry im just soo confused at this moment
does this look kinda closer to the solution?
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("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;
}
}
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?
Re: Help with returning variable from method
when I compile and execute I get this
Code :
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:
Code :
System.out.println(calculateNetPay());
not sure like i said im confused :/
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:
Code :
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:
Code :
System.out.println("Enter the pay rate:");
Here you call the println() method and pass it one arg, a String.
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?
Re: Help with returning variable from method
sorry for being a pain. i think i got it now
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("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
Re: Help with returning variable from method
Re: Help with returning variable from method
thank you soo soo much both of you
and sorry for my stupidity or confusion
Code :
//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!!!! :)
Re: Help with returning variable from method
Don't feel sorry. It's called the learning process.