Credits

Friday, July 2, 2010

Another Sample Java Program on Methods

You can pass arguments to your Java method(s) and using an instance of a class or using static methods, this method can be freely accessed within the file or outside the file.

[+/-] 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