Give me detailed summary this lesson---3/25
Ln 3: user defined method °
Function
|
---------------------------------
In built function user defined function
Eq: Math.sqrt (x) Ex: double area(s)
Different Names of method used in Java)
i) method nmodule
ii) Behaviour
iii) Action
iv) Function
V) member method
uses of Function
* To cope with complexity
* Hiding details
* Reuse
* Easy to Debugging - correcting the error
Function:
Syntax:
[ access specifier ] [ modifier ] return type method name ( Parameter list )
Access Specifier:
1. Public
2. Private
3. Protected
4. default
Modifier
1. Final (const form through out the Program)
2. Native
3. Synchronized } out of Scope
4. Volatile
Here is the extracted content from the image:
**Section: Syntax**
{
body of the method;
return (value);
}
**Section: Eg**
return type
return type method name
Public double area (double a, double b)
Parameter list
{
double a = l * bi;
return (a);
}
**Section: written type**
1. void
2. written return
**Section: void**
Accept only printing statement, which does not written any value
**Section: writt return**
* It is used to return the value to the function (Specific) It accepts calculation.
* It is given at the end of the method block
* No statement in the method body con be executed
After the written statement
* A method can return only one value to
its function (Caller)
* In case of more than one return statement
The first one gets executed.
**Section: Egy**
{
double a = 8 + 8;
double P = 4 + 8;
}
Here is the extracted content from the image:
Eq 1:
{
return (a);
return (p);
} & only first statement is executed
Eq 2:
{
if (a > b)
return a;
else
return b;
} Based on the condition
either one is executed
Eg 3:
i return constant - return 10;
ii return variable - return a;
iii return expression - return a + b;
in use:
Not to return 2 variables
1 - return (a, b) only one variable is accepted
2 Will not return equations
eg: c = a + b
3 we cannot use 2 return type in single function
Eg: return (a); ✓
return (b); x
Function Prototype:
The first line of the function definition is known as function prototype which has the combination of return type, method name, and parameter list.
Here is the extracted content from the image:
Syntax:
Prints n
return type method name (Parameter list);
Eg: void add (inta, int b);
Questions:
1. Write the prototype of the function Product which receives the parameter, float value, and integer value. And return in integer.
int Product (float a, int b);
2. Give a Prototype of the function check which recives a sentence (string) and word (string) and written (written is likely meant to be "returns") in bolean (boolean).
boolean = 0/1
Lint (int) datatype
int check (string a, string b);
3. write the function prototype for the following. The function of poschar which takes a string argument and character argument and returns a integer value.
int Poschar (string a, char b);
4. Give the Prototype of the function Search which recives a Sentence (string) and a word (string) and return 1/0.
(1/0 likely represents a boolean true/false, often represented as integer 1 or 0)
variable name
int Search (string sentse, string wrd);
Question 5:
find a method prototype of a function Position
which receives a letter Character and boolean b does
not return any value
void
void Position (Char a, boolean b);
Function signature:
It is the Part of method prototype which
has the combination of method name and
Parameter list.
Method Signature:
Syntax:
method name (parameter list)
Check (String x, String y)
Program (first program of This kind/language)
7 Define a Class Sum (Sum is crossed out)
member variable or Data member - Int a, int b,
int c
Member method or Function.
* void accept() - Accept the value of a and b from
the user.
* void calculate() - To find sum of the numbers
* void display() - Display all the details.
* write the main method to create an object
and call the above functions
import java.util.*;
Class first Program at this In
{
int a;
int b;
int c;
void accept()
{
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
}
void calculate()
{
C = a + b;
}
void display()
{
System.out.println(a);
System.out.println(b);
System.out.println("The sum of two numbers =" + c);
}
Public static void main()
{
// First program of this In
Sum obj = new Sum();
obj.accept();
obj.calculate();
obj.display();
}
}
**Table Content:**
| unit | Rate |
| -------- | ---- |
| 1st 100 | ₹5 |
| Next 200 | ₹10 |
| Next 100 | ₹15 |
| above | ₹20 |
**Java Code:**
```java
import java.util.*;
class electricity
{
int unit;
String name;
double bill;
void input()
{
Scanner sc = new Scanner(System.in);
int unit = sc.nextInt();
String name = sc.nextLine();
}
void calculate()
{
// Code logic for calculation based on unit and rates
// This method body is empty in the image
}
void display()
{
System.out.println("name " + name + "\t" + "unit used" + unit + "\t" + "Amount to be paid" + bill);
}
public static void main(String[] args)
{
electricity obj = new electricity();
obj.input();
obj.calculate();
obj.display();
}
}
```
```plain text
Question Stem:
3. Define a class discout
Data members, int price
String nome, double ommount
vord occpet to occpet the price and name
vord colculoе to colculote the ommout tobe Paid
olter the Discounl as to
colwlotе thе givеn chilcries
Table Content:
| Price | Discounl |
|--------------|----------|
| 1 - 1000 | 2% |
| 1001 - 10000 | 5% |
| 10001 - 100000 | 10% |
| obove | 15% |
Other Relevant Text:
vord out put to dispog the giron detolks
write a moin method to crecle on objecL
ond coll oll the obpeL funcLion
Partial Code Snippet:
import Java.util.*;
class Discount
{
int price;
String nome;
double ommount;
vord occpet ()
{
Scanner sc = new Scanner (Systеm.in);
name = sc.nexl Linе ();
```
```
Price = sc.next Int();
}
void calculate()
{
if (price >= 1 & price <= 1000)
amount = price - (price * 2 / 100);
else if (price >= 1001 & price <= 10000)
amount = price - (price * 5 / 100);
else if (price >= 10001 & price >= 20000)
amount = price - (price * 10 / 100);
else
amount = price - (price * 15 / 100);
}
void output()
{
System.out.Println(name);
System.out.Println(price);
System.out.Println(amount);
}
public static void main(String args[])
{
Discount obj = new Discount();
obj.accept();
obj.calculate();
obj.output();
}
Variable Description:
Variable name | Data type | Description
--------------|-----------|-------------
Price | int | To store Price value
Name | String | To store the customer name to be paid
Amount | double | To store the amount
```
Datatype
1. Primitive data type:
numeric categories:
byte (3rd, 5th, lesser weight)
short
int
long
float } Floating datatype
double }
very less:
Booleon - True or false
char - 'A', 'B', etc.
Types Of Function
| Pure Function | impure Function |
| :----------------------------------------------------------------------------------------------------------- | :---------------------------------------------------------------------------------------------------------------------------------------------- |
| * A function which returns a value to its caller module is called a Pure function. | * A function which cause a change in the state of an object that is, the value of actual parameters gets modified or changed. |
| * It is also called accessor method. | * It is also called mutator function. |
| * Eg: class abc { abc (int x, int y) { Z = x + y; return Z; } } | * Eg: class abc { void sum (int x) { int Z = 1; Z += x System.out.println (Z); } } |
| 3. public static void main (string args[]) { abc obj = new abc(); obj.sum(5, 5); } (Note: The example calls `sum(5,5)` on `abc` object, but `sum` function is defined in the impure function section with a different signature `void sum(int x)`. This part seems inconsistent in the original notes.) | public static void main() { abc obj = new obj(); } (Note: The constructor call `new obj()` is likely a typo and should probably be `new abc()`) |
| 3 } 3 | |
[Other Text]
PAGE NO:
DATE: 1
Here is the extracted content from the image:
* It does not change the state of an object.
Obj sum(obj);
{
}
Method invoke
1. call by value → Pass by value
2. call by Reference → Pass by Reference
| Pass by value | Pass by reference |
| :--------------------------------------------------------- | :-------------------------------------------------------------- |
| * Pass by value is the process of passing a copy of actual arguments to the formal Parameters | Pass by reference is the process of passing the reference (address) of actual arguments to the formal Parameters |
| * Any changes made in the formal Parameters does not reflect on the actual arguments | Any change made in the formal Parameters will be reflected on the actual arguments |
| * It Passes all Primitive datatypes | It Passes all non-primitive datatypes |
formal Parameter
* formal Parameters appear in the method definitions
Actual parameters
Actual parameters apper in the method coll statement
1. Egs for both:
class abc
{
}
let sum (int x, int y) // formal parameter
{
}
```
PAGE NO
DATE:
C = x + y;
return C;
}
public static void main (String args[])
{
abc obj = new abc();
obj.sum (10, 6); //actual parameter
}
Function overloading
More than one method or function with same name but different signature (Parameters)
4 Write a Program to design a class shop
Data members:
- int unit; - String name
- double Kg; - double bill;
- double rate;
member method
- void accepte() accept the name of the costomer, Kg.
- void Calculate() calculate the bill as per the rate given bellow
| Kg | Rate/Kg |
|--------------|---------|
| first 5 Kg | RS 200 |
| next 5 Kg | RS 100 |
| next 10 Kg | RS 80 |
| next 10 Kg | RS 60 |
| next Above | RS 100 |
```
```
-> void display(): Display the name, Kg and Bill.
Write the main method and create an object to call the above methods.
import java.util.*;
class Shop
{
String name;
double Kg;
double Bill;
void accept()
{
Scanner sc = new Scanner(System.in);
name = sc.nextLine();
Kg = sc.nextDouble();
Bill = 0.0;
}
void calculate()
{
if (Kg >= 1 && Kg <= 5)
Bill = Kg * 200;
else if (Kg > 5 && Kg <= 10)
Bill = 5 * 200 + (Kg - 5) * 100;
else if (Kg > 10 && Kg <= 20)
Bill = 5 * 200 + 5 * 100 + (Kg - 10) * 50;
else if (Kg > 20 && Kg <= 30)
Bill = 5 * 200 + 5 * 100 + 10 * 50 + (Kg - 20) * 60;
else
Bill = 5 * 200 + 5 * 100 + 10 * 50 + 10 * 60 + (Kg - 30) * 100;
}
}
```
```text
}
display
void output()
{
System.out.println("Kg:");
System.out.println("Rate:");
System.out.println("Bill:");
}
public static void main(String args[])
{
Shop obj = new Shop();
obj.void aapet();
obj.calculate();
obj.output();
display
}
}
variable description
| variable | dataType | Description |
|----------|----------|----------|
| Kg | double | To Store the value of Weight |
| rate | double | To store the value of the Product |
| Bill | double | To Store the total amount |
| name | string | To store the name of the Customer |
Function overloading program
7 write a class with a name volume using function overloading that computes the volume of a cube, a sphere and cuboid.
```
```plain text
Formulae:
volume of cube (VCJ) = s*s*s
volume of sphere (Ves) = (4/3) * pi * r * r * r
volume of cuboid (vcud) = l*b*h
import java.util.*;
class Overloading
{
void volume (float s)
{
System.out.println("The volume of cube = " + (s*s*s));
}
void volume(double r)
{
System.out.println("The volume of Sphere = " + (4/3 * 3.14 * r*r*r));
}
void volume (double l, double b, double h)
{
System.out.println("The volume of cuboid = " + (l*b*h));
}
public static void main (string args[])
{
Overloading obj = new Overloading();
obj.volume (6.82);
obj.volume (8.2);
obj.volume( 8.1, 12.4, 7.6);
}
}
```
**Extraction Content:**
**Question/Requirements:**
2. Design a class for area of the following
i. int area (int s) - return area of square
ii. int area (int l, int b) - return area of Rectangle
iii. double area (double r) - return area of Circle
**Java Code:**
```java
import java.util.*;
class AnotherOverloading
{
int area(int s) // function definition
{
return (s*s);
}
int area(int l, int b)
{
return (l*b);
}
double area(double r)
{
return (3.14 * r * r);
}
public static void main (String arg[])
{
AnotherOverloading obj = new AnotherOverloading();
Obj. area (5.1); // Calling the method with a double argument
}
}
```
**Note:** There was a crossed-out section of code before the extracted code block, which included lines like `import java.util.*;`, `class AnotherOverloading`, `{`, `int area`, `void volume(int s)`, `{`, `System.out.println("`); this crossed-out section was not extracted as per the requirement to extract the relevant content, and the subsequent, non-crossed-out code is the relevant part providing the implementation. The call `Obj. area (5.1);` in the `main` method is present in the image.
```
obj.area (5, 6);
Obj. area (5.6);
Define/Design a class Overload 2
i double Perimeter (double r) - return the perimeter of circle (2πr)
ii int Perimeter (int a) - return the perimeter of square (4a)
iii int Perimeter (int l, int b) - return the perimeter of rectangle (2(l+b))
import java.util.*;
class Overload 2
{
double Perimeter (double r)
{
return (2 * 3.1415 * r);
}
int Perimeter (int a)
{
return (a * 4);
}
int Perimeter (int l, int b)
{
return (l + b);
}
public static void main (C)
{
Overload 2 obj = new Overload 2();
Obj. Perimeter (1, 1);
}
}
```
Here is the extracted content from the image:
**Method Calls/Object Usage:**
obj.perimeter(5);
obj.perimeter(6,10);
**Question/Description i):**
i) double perform(double r, double h) return the value of l, l = sqrt(r^2 + h^2)
**Question/Description ii):**
ii) void perform(int m, int n, int c)
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
**Question/Description iii):**
iii) void perform(int m, int n, char ch) - to Print the quotient of the division of m and n if ch is 'q' - Print the reminder of the devision of m & n If ch is 'r'
**Code Snippet:**
import java.util.*;
class Overload 3
{
double perform(double r, double h)
{
double l = 0.0d;
l = Math.sqrt(r*r + h*h);
double area = 0.0d;
area = Math.PI * r * l;
}
}
```plain text
return;
}
void perform(int r, int c)
{
For perform(r, c);
{
for(int i=1; i <= r; i++)
{
for(int j=1; j <= c; j++)
{
System.out.print( j );
}
System.out.println( i );
}
}
}
void perform (int m, int n, char ch)
{
if (ch == 'q')
{
System.out.println (m / n);
}
else if (ch == 'r')
{
System.out.println (m % n);
}
else
{
System.out.println ("Invalid");
}
}
```
视频信息
答案文本
视频字幕
Methods in Java are fundamental building blocks that allow us to organize code into reusable units. A method is a block of code that performs a specific task and can be called multiple times throughout a program. Methods help us manage complexity by breaking large problems into smaller, manageable pieces. They also promote code reuse and make debugging easier by isolating functionality.