0% found this document useful (0 votes)
5 views

Notebook work - User Defined Methods - Object Oriented Programming - Part I

A method in Java is a block of statements that performs a specific task, defined by its return type, name, parameters, and body. Object Oriented Programming (OOP) in Java involves creating classes that serve as blueprints for objects, which contain data members (attributes) and method members (actions). To create an object, the 'new' keyword is used, allowing access to the object's data and methods through the dot operator.

Uploaded by

sawaichouhan0007
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)
5 views

Notebook work - User Defined Methods - Object Oriented Programming - Part I

A method in Java is a block of statements that performs a specific task, defined by its return type, name, parameters, and body. Object Oriented Programming (OOP) in Java involves creating classes that serve as blueprints for objects, which contain data members (attributes) and method members (actions). To create an object, the 'new' keyword is used, allowing access to the object's data and methods through the dot operator.

Uploaded by

sawaichouhan0007
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/ 4

User Defined Methods

What is a Method in Java ?

A method in Java is a block of statements grouped together to perform a specific task. A


method has a name, a return type, an optional list of parameters, and a body.

What is the structure of a Java method ?

The structure of a Java method is as below:

return_type method_name(list of parameters separated by commas)

statements

return statement

What do you mean by return statement and return_type ?

Ans. The statements end with an optional return statement that returns a value back to the
calling method. The return type is the type of the value that the method returns. Example of an
User defined method : Let us write a method that given the length and breadth of a rectangle
as parameters returns the area of the rectangle.

static double rectangle_area (double length, double breadth)

return (length * breadth);

The static method allows us to call this method without an invoking object.

Let us call/invoke the rectangle_area method from the main method.

public static void main(String[] args)

double a = 0;

a = rectangle_area(45.5, 78.5);

System.out.println("Area of the rectangle = "+ a);

}
SQP – 2024-25

Consider the following co and answer the following questions:

static int MyMethod(int N)


{
return (N*N);
}
public static void main(String[] args)
{
System.out.println(MyMethod(7));
}
i)Name the user defined method.
ii)What will be the output on executing the above code?

Java's most fundamental feature – Classes and Objects.

(Object Oriented Programming)

Q1. What is meant by Object Oriented Programming in Java? Explain with an example.
Ans. Java is an Object Oriented Programming (OOP) language where a program is a collection of
objects that interact with each other to solve a problem. Each object is an instance of a class.
For example, in a bookstore database application, each book can be an object with attributes
like Title, Author, Publisher, and Price. Actions like displaying details or finding the price can be
performed on these objects.

Q2. What is a class in Java? How are data and method members defined in a class?
Ans. In Java, a class is a logical or physical entity that serves as a blueprint for creating objects.
It contains attributes (data members) and actions (method members).
For example, in the class Book, data members could include title, author, publisher, genre, and
price. Method members could include actions like displaying the book details or getting its
price. These method members can access and modify the data members of the class.

(Please write this above code in the image in the notebook as an example)
Class Design
Q. How is class designed/Explain the structure of a class
Ans. A class in Java begins with the keyword class followed by the name of the class. The
body of the class is enclosed within curly braces. The body contains the definitions of the
data and method members of the class.

Q. What are data members in a Java class?


Ans. Data members in a Java class are variables that hold the attributes or properties of the
class. They are defined by specifying their type and are like global variables — they can be
accessed by all the method members of the class.

Q. What are method members (methods) in a Java class?


Ans. Method members in a Java class are blocks of code that define the actions or behaviors of
the class. They have access to all the data members of the class and can use or modify them in
their body.

Q.How do we create an object in java ? Which keyword is used to create a new


object ? Explain with an example
Ans. To create an object of a class we use the keyword new.
The following statement creates an object named book1 of the class Book.
public static void main(String[] args)
{
Book book1 = new Book();
}
Data members and method members of an object are accessed using the
dot( . )operator.
e.g. :
book1.title = “Game of Thrones”;
book1.display();
(2023-24 – 2m)

You might also like