Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 15

BSCS 208: Advanced

Object Oriented
Programming

Lecture 3 - Simple class


templates
(Type parameterization)

1
Using Program Parameters
 A programmer can just write very specific code
and hope that the user will like it
 Alternatively, the programmer can define
several program options (parameters) to
provide the user with more flexibility
 Command-line programs simply take their data
as parameters in the form of command-line
arguments

2
Using Program Parameters –
cont’d
 GUI programs normally use Windows
forms to receive program parameters
 You can enter parameters to a Java
program through the following ways:
• Command line parameters
• Applet parameters
• Method parameters

3
Command line parameters
 The main class in Java takes a String
array as its parameter
 It contains the parameter arguments of
String type i.e.
• The main method is just like a regular method
with a parameter
 You call it by passing actual parameters
just like any other method e.g.

4
Command line parameters –
cont’d
public class A
{
public static void main(String args[ ])
{
String strings[ ] = {“Nairobi",
“Mombasa",“Nakuru"};
B.main(strings);
}
}

5
Command line parameters –
cont’d
public class B
{
public static void main(String args[])
{
for(int i=0; i<args.length; i++)
System.out.println(args[i]);
}
}

6
Command line parameters –
cont’d
 You provide Java with command-line
parameters then it gives you feedback
e.g.
• Program below calculates and returns a value
given two numbers and one of the four math
operator

7
Command line parameters –
cont’d
public class Calculator {
public static void main(String args[]) {
if(args.length != 3) {
System.out.println("Enter operand1 operator operand2");
System.exit(0);
}
int result=0;
switch(args[1].charAt(0)) {
case '+': result = Integer.parseInt(args[0])+Integer.parseInt(args[2]);
break;
case '-': result = Integer.parseInt(args[0])-Integer.parseInt(args[2]);
break;
case '*': result = Integer.parseInt(args[0])*Integer.parseInt(args[2]);
break;
case '/': result = Integer.parseInt(args[0])/Integer.parseInt(args[2]);
}
System.out.println(args[0]+ ' ' + args[1] + ' ' + args[2] + " = " + result); } }

8
Passing parameters to Java
methods
 We said earlier that parameters can be
passed as actual arguments to the main
method as command line arguments.
 Here we look more into how to pass
parameters to regular methods
 The Java syntax for method definition is
as follows:
type methodname (parameterlist) {
methodbody;
}

9
Passing parameters to Java
methods – cont’d
 You can choose to define parameterized
or non parameterized methods
depending on how you are going to use
them, as shown in the example below.

10
Passing parameters to Java
methods – cont’d
public class A {
private int x;
public A() { }
public int sampleMethod1(int y) { }
public int sampleMethod2(int a, int b) { }
}

11
Passing parameters to Java
methods – cont’d
 Defining parameters with varargs
• Apart from command-line parameters, other
parameters are usually static
• Beginning with Java 5.0, we can use the short
form varargs to allow us to pass a variable
number of arguments

12
Passing parameters to Java
methods – cont’d
 Format
<access specifier><static>void methodname(Object… arguments)
• The method above contains the argument
called varargs
• Object is the type of the argument
• ellipsis (…) is the key to varargs
• arguments is the name of variable

13
Passing parameters to Java
methods – cont’d
 Example
• public void method1(String username, String password, String
userId);
• This method may be simplified as follows:
• public void method2(String… var_name);
• where String… var_name means that we can
pass any number of String arguments to the
method as shown below.

14
Passing parameters to Java
methods – cont’d
public class VarargsTest {
VarargsTest(String... person) {
for(String name: person) {
System.out.println("Hello "+name);
}
}
public static void main(String args[]) {
VarargsTest arguments = new
VarargsTest("John","David","Mary");
}
}
15

You might also like