-
Notifications
You must be signed in to change notification settings - Fork 0
/
BasicJava.java
170 lines (145 loc) · 3.54 KB
/
BasicJava.java
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
package basicArrayOperations;
public class BasicJava {
/**
* This method simply prints numbers from 1 to 225 inclusive
*/
public void print1to1225(){
for(int i = 1; i <= 225; i++)
System.out.println(i);
}
/**
* This method prints odd numbers between 1 and 225 inclusive
*/
public void printOddNumberBetween1And225(){
for(int i = 1; i <= 225; i++)
if(i%2 != 0)
System.out.println(i);
}
/**
* This method successively prints the sum of the numbers from 0 t0 225
*/
public void printSumFrom0To225(){
int sum = 0;
for(int i = 0; i <=225; i++){
sum += i;
System.out.println(sum);
}
}
/**
* This method receives an Integer array and prints out all its values
* @param array
*/
public void iterateThroughArrayAndPrintValues(int[] array){
for(int x:array)
System.out.println(x);
}
/**
* This method finds and prints out the max value;
* @param array an integer array
*/
public void printMax(int[]array){
int max =array[0];
for(int i =1; i < array.length; i++){
if(array[i] > max)
max = array[i];
}
System.out.println(max);
}
/**
* This method prints out the average value of an array of Integers
* @param array Integer array
*/
public void getAvg(int[]array){
int sum =0;
for(int i = 0; i <array.length; i++)
sum += array[i];
System.out.println(sum/array.length);
}
/**
* This method creates an array of odd integers
* @return an array
*/
public int[] arrayOfOddNumbers(){
int[]y = new int[225/2];
for(int i =1, j = 0; i <=225; i =+ 2, j++)
y[j] = y[i];
return y;
}
/**
* This method receives an array of Integers and a number, and returns the number
* of values greater than y
* @param array an array
* @param y a number
*/
public void printNumberofValuesGreaterThanY(int[]array, int y){
int count = 0;
for(int value:array)
if(value > y)
count++;
System.out.println(count);
}
/**
* This method squares each array value and returns the modified array
* @param array an array
* @return an array
*/
public int[] squareTheValues(int[]array){
for(int i = 0; i < array.length; i++)
array[i] = array[i]*array[i];
return array;
}
/**
* This method simply replaces any value of the given array by zero if the
* value is a negative number
* @param array an array
* @return an array
*/
public int[]eliminateNegativeNumber(int[]array){
for(int i = 0; i < array.length; i++)
if(array[i] < 0)
array[i] = 0;
return array;
}
public int[] maxMinAvg(int[]array){
int[]arr = new int[3];
int avg, sum = 0, max = array[0], min = array[0];
for(int i = 1; i < array.length; i++){
sum += array[i];
if(array[i] > max){
max = array[i];
}
if(array[i] < min){
min = array[i];
}
}
avg = sum/array.length;
arr[0] = max;
arr[1]= min;
arr[3] = avg;
return arr;
}
/**
* This method shifts the values of a given array by one to the front
* @param array an array
* @return an array
*/
public int[]shiftArrayValues(int[]array){
for(int i = 0; i < array.length-1; i++)
array[i] = array[i+1];
array[array.length-1] = 0;
return array;
}
public static void main(String[] args) {
int[]arr = {1, 5, 9, 2, 3};
BasicJava jav = new BasicJava();
//jav.print1to1225();
//jav.printOddNumberBetween1And225();
//jav.printSumFrom0To225();
//jav.printNumberofValuesGreaterThanY(arr, 2);
//jav.iterateThroughArrayAndPrintValues(arr);
//jav.getAvg(arr);
int [] array = jav.shiftArrayValues(arr);
for(int value: array)
System.out.print(value +" ");
}
}