-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhomeworkTwo
More file actions
136 lines (133 loc) · 4.04 KB
/
homeworkTwo
File metadata and controls
136 lines (133 loc) · 4.04 KB
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
package SeptNinth;
public class homeworkTwo {
// Loops, Control Flow, Arrays and Methods
// Note: Create all your static methods outside on the main() method!
//
//
// Question 1
//
// public static int getLastIndex(String[] names) {
// int getLast = names.length - 1;
// return getLast;
// }
// public static void main(String[] args) {
// String[] names = {"Mary", "Harry", "Billy", "Lily", "Tily"};
// System.out.println(getLastIndex(names));
// }
//
// Question 2
// public static int getSecondToLastIndex(String[] names) {
// int getSecLast = names.length - 2;
// return getSecLast;
//}
// public static void main(String[] args) {
// String[] names = {"Mary", "Harry", "Billy", "Lily", "Tily"};
// System.out.println(getSecondToLastIndex(names));
// }
//
// Question 3
// public static String getFirstElement(String[] names) {
// String getFirst = names[0];
// return getFirst;
// }
//
// public static void main(String[] args) {
// String[] names = {"Mary", "Harry", "Billy", "Lily", "Tily"};
// System.out.println(getFirstElement(names));
// }
//
// Question 4
// public static String getLastElement(String[] names) {
// String getLast = names[names.length - 1];
// return getLast;
// }
//
// public static void main(String[] args) {
// String[] names = {"Mary", "Harry", "Billy", "Lily", "Tily"};
// System.out.println(getLastElement(names));
// }
//
// Question 5
// public static String getSecondToLastElement(String[] names) {
// String secondLast = names[names.length - 2];
// return secondLast;
// }
//
// public static void main(String[] args) {
// String[] names = {"Mary", "Harry", "Billy", "Lily", "Tily"};
// System.out.println(getSecondToLastElement(names));
// }
//
// Question 6
// public static int getSum(int[] ints) {
// int theSum = 0;
// for (int i = 0; i < ints.length; i++)
// theSum += ints[i];
// return theSum;
// }
// public static void main(String[] args) {
// int[] ints = {2, 4, 6, 8, 10, 12};
// System.out.println(getSum(ints));
// }
//
// Question 7
// public static int getAverage(int[] ints) {
// int theSum = 0;
// for (int i = 0; i < ints.length; i++) {
// theSum += ints[i];}
//
// int theAverage = theSum / ints.length;
// return theAverage;
// }
//
//
// public static void main(String[] args) {
// int[] ints = {2, 4, 6, 8, 10, 12};
// System.out.println(getAverage(ints));
// }
//
// Question 8
// public static String extractAllOddNumbers(int[] numbers) {
// String odds = "";
// for (int i = 0; i < numbers.length; i++){
// if (numbers[i] % 2 == 1 ){
// odds += numbers[i] + " ";
// }
// }
// return odds;
// }
// public static void main(String[] args) {
// int[] ints = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
// System.out.println(extractAllOddNumbers(ints));
// }
//
// Question 9
// public static String extractAllEvenNumbers(int[] numbers) {
// String evens = "";
// for (int i = 0; i < numbers.length; i++){
// if (numbers[i] % 2 == 0 ){
// evens += numbers[i] + " ";
// }
// }
//
// return evens;
// }
// public static void main(String[] args) {
// int[] ints = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
// System.out.println(extractAllEvenNumbers(ints));
// }
//
// Question 10
// public static boolean contains(String[] names, String element) {
// for (int i = 0; i < names.length; i++) {
// if (names[i].equals(element)){
// return true;
// }
// }
// return false;
// }
//
// public static void main(String[] args) {
// String[] theArray = {"Tony", "Tomy", "69", "420"};
// System.out.println(contains(theArray, "69"));
// }