Wednesday, April 27, 2011

Java's Implementation of Methods

Like most programming languages, C, C++ and Pascal, they use procedures or / and functions to break down big programs into smaller chunks. With this programming technique, it makes the program easier to read and to maintain.

But, Java has methods as procedures and they can either return a value or not. So, depending on the type of value, we define our methods. For instance, the method definition below says that the method shall return a decimal - oriented number otherwise we declare this method as void.

double computer (double grosspay, double deductions)
   double netpay = grosspay - deductions;
   return netpay;

When we declare methods, we make sure that we determine if they are with parameters or none. In the codes above, the variables, grosspay and deductions, inside our method header line, are our parameters that hold values from the calling portion of our program. We use these values to be manipulated inside the body of our method.

The program below shows the use of methods and how they are defined and called.
[+/-] show/hide

/*Sample program on methods
public class ComputePayv2 {

double netpay;

public static void main(String args[]) throws IOException{
double hrate;
double hrsworked;
double tdeductions;
double sss;
double ph;
double others;

String name;
String position;
String payperiod;

BufferedReader br= new BufferedReader(new InputStreamReader(;
ComputePayv2 myObject=new ComputePayv2();

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

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

System.out.println("Input payroll period:");

System.out.println("Input employee's hourly rate:");

System.out.println("Input employee's hours worked:");

System.out.println("Input employee's SSS:");

System.out.println("Input employee's Philhealth:");

System.out.println("Input employee's other deductions:");
System.out.println("Payroll Period:"+payperiod);
System.out.println("Hourly Rate:"+hrate);
System.out.println("Hours worked:"+hrsworked);

System.out.println("Employee's Total Deductions: "+tdeductions);
System.out.println("Employee's Net Pay: "+myObject.computenetpay(hrate,hrsworked,tdeductions));

double computedeductions(double mysss, double myph, double myothersded)
{ double tdeduct= mysss+myph+myothersded;
return tdeduct;

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


No comments: