Skip to content

Commit 7e37152

Browse files
authored
Create method
1 parent 41274f5 commit 7e37152

File tree

1 file changed

+178
-0
lines changed

1 file changed

+178
-0
lines changed

Diff for: method

+178
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
public class methods {
2+
public static void main(String args[]){
3+
// // an instance (object) of methods class
4+
methods me = new methods();
5+
// // calling non-static method
6+
me.myMethod1();
7+
// // calling static method
8+
myMethod2();
9+
10+
// call summation method
11+
summation(5, 4);
12+
13+
// call celsiusToFahrenheit method
14+
me.celsiusToFahrenheit(45);
15+
16+
// calling power method
17+
me.power(4, 2);
18+
19+
// call array findMax method
20+
int[] numbers = {};
21+
int maxNumber = findMax(numbers);
22+
System.out.println("Maximum Number: " + maxNumber);
23+
24+
// array average method
25+
int[] myArray_1 = {};
26+
double average = calculateAverage(myArray_1);
27+
System.out.println("Average of elements: " + average);
28+
29+
// search for element in array
30+
int[] myArray_2 = {1, 2, 3, 4, 5};
31+
int targetElement = 3;
32+
boolean contains = containsElement(myArray_2, targetElement);
33+
System.out.println("Array contains " + targetElement + ": " + contains);
34+
35+
// calling factorial method
36+
int result = factorial(5);
37+
System.out.println("Factorial: " + result);
38+
39+
// array 2D
40+
int rows = 3;
41+
int cols = 4;
42+
// // Initialize a 2D array
43+
int[][] myArray_3 = initializeArray(rows, cols);
44+
// // Print the 2D array
45+
System.out.println("Original 2D Array:");
46+
printArray(myArray_3);
47+
// // Calculate and print the sum of all elements in the 2D array
48+
int sum = calculateSum(myArray_3);
49+
System.out.println("\nSum of all elements: " + sum);
50+
51+
}// TODO: end of main method
52+
53+
// non-static method
54+
public void myMethod1(){
55+
System.out.println("My first method");
56+
}
57+
// static method
58+
public static void myMethod2(){
59+
System.out.println("My first method");
60+
}
61+
62+
public static int summation(int x, int y){
63+
int sum;
64+
sum = x + y;
65+
return sum;
66+
}
67+
68+
// TODO: Method to convert Celsius to Fahrenheit
69+
public double celsiusToFahrenheit(double celsius) {
70+
return (celsius * 9/5) + 32;
71+
}
72+
73+
// TODO: Method to calculate the power of a number
74+
public double power(double base, int exponent) {
75+
return Math.pow(base, exponent);
76+
}
77+
78+
// TODO: Method to simulate a coin toss (Heads or Tails)
79+
public String coinToss() {
80+
return Math.random() < 0.5 ? "Heads" : "Tails"; // 50% chance for Heads or Tails
81+
}
82+
83+
// TODO: Method to generate a random integer between two given values (inclusive)
84+
public int randomIntInRange(int min, int max) {
85+
return min + (int) (Math.random() * (max - min + 1)); // Random integer in the range [min, max]
86+
}
87+
88+
// TODO: Method to find the maximum value in an array
89+
public static int findMax(int[] array) {
90+
if (array.length == 0) {
91+
System.out.println("Array is empty");
92+
}
93+
int max = array[0];
94+
for (int i = 1; i < array.length; i++) {
95+
if (array[i] > max) {
96+
max = array[i];
97+
}
98+
}
99+
return max;
100+
}
101+
102+
// TODO: Method to calculate the average of elements in a 1D array
103+
public static double calculateAverage(int[] array) {
104+
if (array.length == 0) {
105+
System.out.println("Array is empty");
106+
}
107+
int sum = 0;
108+
for (int value : array) {
109+
sum += value;
110+
}
111+
return (double) sum / array.length;
112+
}
113+
114+
// TODO: Method to find the sum of even numbers in an array
115+
public int sumEvenNumbers(int[] array) {
116+
int sum = 0;
117+
for (int num : array) {
118+
if (num % 2 == 0) {
119+
sum += num;
120+
}
121+
}
122+
return sum;
123+
}
124+
125+
// TODO: Method to check if an element exists in a 1D array
126+
public static boolean containsElement(int[] array, int target) {
127+
for (int value : array) {
128+
if (value == target) {
129+
return true;
130+
}
131+
}
132+
return false;
133+
}
134+
135+
// TODO: Method to calculate the factorial of a number
136+
public static int factorial(int n) {
137+
if (n < 0) {
138+
System.out.println("Factorial is not defined for negative numbers");
139+
}
140+
int result = 1;
141+
for (int i = 1; i <= n; i++) {
142+
result *= i;
143+
}
144+
return result;
145+
}
146+
147+
// TODO: Method to initialize a 2D array with random values
148+
public static int[][] initializeArray(int rows, int cols) {
149+
int[][] array = new int[rows][cols];
150+
for (int i = 0; i < rows; i++) {
151+
for (int j = 0; j < cols; j++) {
152+
array[i][j] = (int) (Math.random() * 100);
153+
}
154+
}
155+
return array;
156+
}
157+
158+
// TODO: Method to print the contents of a 2D array
159+
public static void printArray(int[][] array) {
160+
for (int i = 0; i < array.length; i++) {
161+
for (int j = 0; j < array[i].length; j++) {
162+
System.out.print(array[i][j] + " ");
163+
}
164+
System.out.println();
165+
}
166+
}
167+
168+
// TODO: Method to calculate the sum of all elements in a 2D array
169+
public static int calculateSum(int[][] array) {
170+
int sum = 0;
171+
for (int[] row : array) {
172+
for (int value : row) {
173+
sum += value;
174+
}
175+
}
176+
return sum;
177+
}
178+
}

0 commit comments

Comments
 (0)