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

Lecture 05 - Methods

The document contains code that calculates the sum of integers within different ranges using for loops and prints the results. It then refactors the code into a method that takes the lower and upper bounds as parameters and returns the sum, calling this from main and printing the results.

Uploaded by

Lusper Mhango
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)
25 views

Lecture 05 - Methods

The document contains code that calculates the sum of integers within different ranges using for loops and prints the results. It then refactors the code into a method that takes the lower and upper bounds as parameters and returns the sum, calling this from main and printing the results.

Uploaded by

Lusper Mhango
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/ 71

int sum = 0;
for (int i = 1; i <= 10; i++)
sum += i;
System.out.println("Sum from 1 to 10 is " + sum);

sum = 0;
for (int i = 20; i <= 30; i++)
sum += i;
System.out.println("Sum from 20 to 30 is " + sum);

sum = 0;
for (int i = 35; i <= 45; i++)
sum += i;
System.out.println("Sum from 35 to 45 is " + sum);
int sum = 0;
for (int i = 1; i <= 10; i++)
sum += i;
System.out.println("Sum from 1 to 10 is " + sum);

sum = 0;
for (int i = 20; i <= 30; i++)
sum += i;
System.out.println("Sum from 20 to 30 is " + sum);

sum = 0;
for (int i = 35; i <= 45; i++)
sum += i;
System.out.println("Sum from 35 to 45 is " + sum);









public class MethodDemo
{
public static int sum(int i1, int i2)
{
int sum = 0;
for (int i = i1; i <= i2; i++)
sum += i;
return sum;
}

public static void main(String[] args)


{
System.out.println("Sum from 1 to 10 is " + sum(1, 10));
System.out.println("Sum from 20 to 30 is " + sum(20, 30));
System.out.println("Sum from 35 to 45 is " + sum(35, 45));
}
}




public static void main(String[] args)


{
// Statements;
}























animation
animation
animation
animation
animation
animation
animation
animation
animation
animation


animation
animation
animation
animation
animation
animation
animation
animation
animation
animation





















• sin(double a) Examples:

• cos(double a)
Math.sin(0) returns 0.0
• tan(double a) Math.sin(Math.PI / 6)
returns 0.5
• acos(double a) Math.sin(Math.PI / 2)
• asin(double a) returns 1.0
Math.cos(0) returns 1.0
• atan(double a) Math.cos(Math.PI / 6)
returns 0.866
Math.cos(Math.PI / 2)
returns 0
• exp(double a) Examples:
e a
Math.exp(1) returns 2.71
• log(double a)
Math.log(2.71) returns 1.0
a
Math.pow(2, 3) returns 8.0
• log10(double a) Math.pow(3, 2) returns 9.0
Math.pow(3.5, 2.5) returns
a 22.91765
• pow(double a, double b) Math.sqrt(4) returns 2.0
Math.sqrt(10.5) returns 3.24
a b
• sqrt(double a)
a
• double ceil(double x)

• double floor(double x)

• double rint(double x)

• int round(float x)

• long round(double x)
Math.ceil(2.1) returns 3.0
Math.ceil(2.0) returns 2.0
Math.ceil(-2.0) returns –2.0
Math.ceil(-2.1) returns -2.0
Math.floor(2.1) returns 2.0
Math.floor(2.0) returns 2.0
Math.floor(-2.0) returns –2.0
Math.floor(-2.1) returns -3.0
Math.rint(2.1) returns 2.0
Math.rint(2.0) returns 2.0
Math.rint(-2.0) returns –2.0
Math.rint(-2.1) returns -2.0
Math.rint(2.5) returns 2.0
Math.rint(-2.5) returns -2.0
Math.round(2.6f) returns 3
Math.round(2.0) returns 2
Math.round(-2.0f) returns -2
Math.round(-2.6) returns -3
• max(a, b) min(a, b) Examples:

Math.max(2, 3) returns 3
• abs(a) Math.max(2.5, 3) returns
3.0
Math.min(2.5, 3.6)
• random() returns 2.5
double Math.abs(-2) returns 2
Math.abs(-2.1) returns
2.1










You might also like