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