|
Learn to Program JAVA. Learn the Basics.
by: Rodelio P. Barcenas
Our topic for
this session is about "Methods". The following examples
shows how to pass value to the methods inside the class file.
The second topic
is about how to use methods found in a class file and utilize
those methods in another class file. The example will focus on
"extends".
/*
Written
by: Rodelio P. Barcenas
July 23, 2001
This is a program written to perform arithmetic
operation such as addition, subtraction,
multiplication, and modulo.
The name of class file must be calc.class and
must be executed in the DOS command line.
Ex.
C:\>java calc 10 add 5
"10 + 5" are arguments passed to the class file
which will be equivalent to a sting array arg[].
a[0] = "10"
a[1] = "add"
a[2] = "5"
since a[0] and a[2] are string in structure, conversion
to integer must take place.
Integer.parseInt() function must be utilized.
Below the main() function, are other methods which will
perform the arithmetic operations.
Save the program below to calc.java and compile using
C:\>javac calc.java
To execute,
C:\>java calc 15 add 23
*/
public
class calc {
// Main Program
public static void main(String arg[]) {
int x,y;
String op = arg[1];
x = Integer.parseInt(arg[0],10);
y = Integer.parseInt(arg[2],10);
System.out.println(x);
System.out.println(op);
System.out.println(y);
// value of x and y are passed by value
to the
// methods below
if(op.equals("add")) {add(x,y);}
if(op.equals("sub")) {sub(x,y);}
if(op.equals("mul")) {mul(x,y);}
if(op.equals("div")) {div(x,y);}
if(op.equals("mod")) {mod(x,y);}
}
// methods performing arithmetic operations
public static void add(int a, int b){
System.out.println("Result
-> " + (a + b));
}
public static void sub(int a, int b){
System.out.println("Result
-> " + (a-b));
}
public static void mul(int a, int b){
System.out.println("Result
-> " + (a*b));
}
public static void div(int a, int b){
System.out.println("Result
-> " + (a/b));
}
public static void mod(int a, int b){
System.out.println("Result
-> " + (a%b));
}
}
/*
Written by: Rodelio P. Barcenas
July 23, 2001
This
program is written to explain how methods from other
class file can be utilized by other class files.
In
the keyword "extends calc", calc is the name of the
class file which contains the
methods (Arithmetic Operations) in which the current class
"calc2" will try to extend it's access to it.
In
the variable declaration, the "calculator" variable
is
of the class data-type "calc".
"calculator"
now inherits the properties or methods inside
the calc class.
calculator.add()
performs the addition.
This
class file can now be executed.
C:\>java
calc2 14 + 50
NOTE:
Before executing, make sure that the class "calc"
is present before executing.
*/
public class calc2 extends calc {
public static void main(String arg[]) {
int x,y;
String op = arg[1];
calc calculator = new calc();
/*
calculator no inherits the methods found in
the class "calc"
*/
x = Integer.parseInt(arg[0],10);
y = Integer.parseInt(arg[2],10);
System.out.println(x);
System.out.println(op);
System.out.println(y);
if(op.equals("add")) {calculator.add(x,y);}
if(op.equals("sub")) {calculator.sub(x,y);}
if(op.equals("mul")) {calculator.mul(x,y);}
if(op.equals("div")) {calculator.div(x,y);}
if(op.equals("mod")) {calculator.mod(x,y);}
}
}
|