Credits

Saturday, November 27, 2010

Another Sample of Java Methods

 Since Java is an extended procedural - programming paradigm, it employs the use of methods to break down the task.  A method can be defined just before or after the main method. Regardless of the number of methods defined, it is always the main method  that is searched and applied.

A method may return or not a value but should a method must return a value, then, the appropriate data type must be used.

A sample program below shows the use of method compute to return the required value for computing a pay.

[+/-] show/hide


/*Sample program on methods
*/
import java.io.*;
public class ComputePayQuiz {

double netpay;

public static void main(String args[]) throws IOException{
double hrate;
double hrsworked;
double tdeductions;
String name;
String position;
String payperiod;

BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
ComputePayQuiz myObject=new ComputePayQuiz();

System.out.println("Input employee's name:");
name=br.readLine();


System.out.println("Input employee's position:");
position=br.readLine();

System.out.println("Input payroll period:");
payperiod=br.readLine();

System.out.println("Input employee's hourly rate:");
hrate=Double.parseDouble(br.readLine());

System.out.println("Input employee's hours worked:");
hrsworked=Double.parseDouble(br.readLine());

System.out.println("Input employee's total deductions:");
tdeductions=Double.parseDouble(br.readLine());

System.out.println("Payroll Period:"+payperiod);
System.out.println("Name:"+name);
System.out.println("Position:"+position);
System.out.println("Hourly Rate:"+hrate);
System.out.println("Hours worked:"+hrsworked);
System.out.println("Employee's Net Pay: "+myObject.compute(hrate,hrsworked,tdeductions));
}


double compute(double hr, double hw, double td)
{ netpay= (hr*hw) - td;
return netpay;
}

}

AddThis