0% found this document useful (0 votes)
17 views9 pages

JBKCORE9004 Class Method Calling

Uploaded by

swapnilavate2011
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views9 pages

JBKCORE9004 Class Method Calling

Uploaded by

swapnilavate2011
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

www.thekiranacademy.com JBKCORE9004-class-method-calling Mob No.

:8888809416

Class method and variable calling

1. This example will demonstrate


a. Float variable
b. How to call methods
c. How to print global variables in method
d. How to communicate between 2 classes one with main method and
other without main.

public class Exercise1 {


int a; float
b; String
str;

public void operation1() {a


= 10;
b = 15.26f;// float value should be defined as decimal with f
str = "Java";
}

public void operation2() {a


= 20;
b = 27f;
str = "SELENIUM";
}

public void display() {


System.out.println("The Integer Value is :" + a);
System.out.println("The Float Value is :" + b);
System.out.println("The String Value is: " + str);
}
}

1|Page www.jbktest.com
www.thekiranacademy.com JBKCORE9004-class-method-calling Mob No.:8888809416

Calling above class by main method.

class mainExercise1 {

public static void main(String args[]) {


Exercise1 obj = new Exercise1();
obj.operation1();
obj.display();
obj.operation2();
obj.display();
}
}

2. This example will demonstrate


a. Three different classes with one method each
b. Calling three different classes from main method with separate
class to get output.

class AA {
void display1() {
System.out.println("This is Class - 1");
}
}

class BB {
void display2() {
System.out.println("This is Class - 2");
}
}

class CC {

2|Page www.jbktest.com
www.thekiranacademy.com JBKCORE9004-class-method-calling Mob No.:8888809416

void display3() {
System.out.println("This is Class - 3");
}
}

Calling from separate class with main method.

class mainExercise2 {

public static void main(String[] args) {


AA obj1 = new AA();
BB obj2 = new BB();
CC obj3 = new CC();
obj1.display1();
obj2.display2();
obj3.display3();
}
}

3|Page www.jbktest.com
www.thekiranacademy.com JBKCORE9004-class-method-calling Mob No.:8888809416

3. This example will demonstrate


a. One method and in same class we will have main method

public class DataType_Int {


int a = 15000;
int b = -20000;

void add() {
int c = a + b;
System.out.println("The int Value is : " + c);
}

public static void main(String[] args) {


DataType_Int obj = new DataType_Int();
obj.add();
}
}

4. This example will demonstrate


a. boolean type of variable
b. Calling method and variable from main method

public class DataType_Boolean {


boolean a = true;

void check() {
if (a == true) {
a = false;
System.out.println("The Boolean Value is : " + a);
}
}

public static void main(String args[]) {


DataType_Boolean obj = new DataType_Boolean();
obj.check();

4|Page www.jbktest.com
www.thekiranacademy.com JBKCORE9004-class-method-calling Mob No.:8888809416

System.out.println(obj.a);
}

5. This example will demonstrate


a. Char type of variable
b. Calling method and variable from main method

public class DataType_Char {


char a = 'J';
char b =
'A';char c =
'V';char d =
'A';

void join() {
System.out.println("The Characters Value is : " + a + b + c + d);
}

public static void main(String[] args) {


DataType_Char obj = new DataType_Char();
obj.join();
}
}

5|Page www.jbktest.com
www.thekiranacademy.com JBKCORE9004-class-method-calling Mob No.:8888809416

6. This example will demonstrate


a. int and float type of variable
b. Calling method and variable from main method
c. Doing arithmetic operations in a program
d. How to call method of same class without creating a object.
i. Display method is called from various other methods of
same class

public class
Method_Ex1 {
int x = 10, y =
20; float z;

void add() {
z=x
+ y;
displa
y(z);
}

void sub() {
z=x
- y;
displa
y(z);
}

void mult() {
z=x
* y;
displa
y(z);
}

void div() {
z=x
/ y;
displa
y(z);
6|Page } www.jbktest.com

void display(float ans) {


System.out.println(
www.thekiranacademy.com JBKCORE9004-class-method-calling Mob No.:8888809416

public class Method_Ex1_Test {


public static void main(String[] args) {
Method_Ex1 obj = new Method_Ex1();
obj.add();
obj.sub();
obj.mult();
obj.div();
}
}

7. This example will demonstrate


a. Method arguments
b. While calling sending parameters and receiving output in main
method.

public class AddOperation {


int add_int(int x, int y) {
return x + y;
}

public static void main(String[] args) {


AddOperation addOperation = new AddOperation();
int z = addOperation.add_int(2, 4);
System.out.println(z);
}

7|Page www.jbktest.com
www.thekiranacademy.com JBKCORE9004-class-method-calling Mob No.:8888809416

8. This example will demonstrate to calculate area


a. Method arguments
b. While calling sending parameters and receiving output in main
method.

public class Area {


double getArea(double x, double y) {
return x * y;
}

public static void main(String[] args) {


Area area = new Area();
double z = area.getArea(10.2, 23.4);
System.out.println(z);
}
}

9. This example will demonstrate to calculate area


a. Method arguments
b. Directly printing output of a method

public class RectangleArea {


int length;
int breadth;

void Rectangle(int l, int b) {


length = l;
breadth = b;
}

public int getArea() {


return length * breadth;
}
}

8|Page www.jbktest.com
www.thekiranacademy.com JBKCORE9004-class-method-calling Mob No.:8888809416

public class RectangleAreaTest {


public static void main(String[] args) {
RectangleArea r = new RectangleArea();
r.Rectangle(6, 7);
System.out.println(r.getArea());
}
}

Homework
1. Data should be sent through main method or while calling
methods
1. Write a program to calculate cube input will be sent through method
arguments
2. Write a program to calculate simple interest
3. Write a program to calculate compound interest
4. The marks obtained by a student in 5 different subjects are input
through the method call. The student gets a division as per the
following rules:
a. Percentage above or equal to 60 - First division
b. Percentage between 50 and 59 - Second division
c. Percentage between 40 and 49 - Third division
d. Percentage less than 40 - Fail
e. Write a program to calculate the division obtained by the
student.

Download – [not recommended.]

https://drive.google.com/drive/folders/1xTkRzHymIbLXvSNfAfA8JdxAiqOLIP
mR?usp=sharing

9|Page www.jbktest.com

You might also like