Tuesday, June 22, 2010

Java's Use of Methods

I am back to teaching Java fundamentals again and though I have had taught this subject before, I have to provide more sample programs and make the subject more understood.

I just have a problem though; I have more than 50 students and the dean only advised that I split the class during laboratory.

A method in Java is a way to modularize the solution and it may return a value or not. So, below is a sample program of method.

[+/-] show/hide

/*Sample program on methods
public class TuitionMethod {
double tuition;

public static void main(String args[]) throws IOException{
double tunits;
double unitprice;
BufferedReader br= new BufferedReader(new InputStreamReader(;
TuitionMethod myObject=new TuitionMethod();

System.out.println("Input tuition units:");
String totalunits=br.readLine();

System.out.println("Input unit price:");
String unitrate=br.readLine();

System.out.println("Your tuition is "+myObject.compute(tunits,unitprice ));

double compute(double myunits, double uprice)
{ tuition= myunits*uprice;
return tuition;}