forked from FRC-7525/Java-Basics-Training-Assignment-2025
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFundamentalsPractice.java
More file actions
197 lines (159 loc) · 6.44 KB
/
FundamentalsPractice.java
File metadata and controls
197 lines (159 loc) · 6.44 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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package challenges;
/**
* FundamentalsPractice - small helper methods + main for manual testing.
* Students will implement the methods below so unit tests can call them.
*/
import java.util.*;
import javax.swing.event.SwingPropertyChangeSupport;
public class FundamentalsPractice {
// Example: sum of two integers
public static int sum(int a, int b) { // The values in parentheses are set for you. Ex: Here a and b are assigned to integers and can be used in code
int total = a + b; // Your code goes here
return total; // To submit the answer, return it like this.
}
/**
* fizzbuzz - You are given an array of ints.
* Print "Fizz" for multiples of 3,
* "Buzz" for multiples of 5,
* and "FizzBuzz" for multiples of both 3 and 5.
* For other numbers, print the number itself.
* RETURN an array (NOT ARRAYLIST) that contains the number of times ["Fizz", "Buzz", "FizzBuzz"] were printed.
* Ex: Given [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15], you would RETURN: [3, 2, 1]
*/
public static int[] fizzbuzz(int[] arr) {
int[] fizzBuzzes = {0, 0, 0};
for (int i = 0; i < arr.length; i++) {
if (arr[i] % 3 == 0 && arr[i] % 5 == 0) {
System.out.println("FizzBuzz");
fizzBuzzes[2]++;
}
else if (arr[i] % 3 == 0) {
System.out.println("Fizz");
fizzBuzzes[0]++;
}
else if (arr[i] % 5 == 0) {
System.out.println("Buzz");
fizzBuzzes[1]++;
}
else {
System.out.println(arr[i]);
}
}
return fizzBuzzes;
}
/**
* reverseString - Given a String, return the reverse of the string.
* Ex: "hello" -> "olleh"
* Hint: You can convert an arary of characters back to a string like this:
* char[] charArray = {'h', 'e', 'l', 'l', 'o'};
* String str = new String(charArray); // str is "hello"
* Hint: You can convert a string to an array of characters like this:
* String str = "hello";
* char[] charArray = str.toCharArray(); // charArray is ['h', 'e', 'l', 'l', 'o']
*/
public static String reverseString(String input) {
char[] charArray = input.toCharArray();
char[] reversedChar = new char[charArray.length];
for (int i = charArray.length - 1; i >= 0; i--) {
reversedChar[charArray.length - i - 1] = charArray[i];
}
return new String(reversedChar);
}
/**
* maxInArray - Given an array of integers, return the largest value.
* Ex: [1, 5, 3, 9, 2] -> 9
*/
public static int maxInArray(int[] arr) {
int biggest = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] > biggest) {
biggest = arr[i];
}
}
return biggest;
}
public static boolean stringsAreSame(String a, String b) {
// If strings are equal, return true. Else, return false
// Hint: use .equals() to compare strings not ==
// Do not care about capitals HI is equal to hi look up how to do.
return a.equalsIgnoreCase(b);
}
/**
* countVowels - Given a string, return the number of vowels (a, e, i, o, u) in the string.
* Ex: "banana" -> 3
* Hint:
* - strings can be accessed like this: variable.charAt(0) returns the first character
*/
public static int countVowels(String input) {
int vowels = 0;
for (char i = 0; i < input.length(); i++) {
if (input.charAt(i) == 'a' || input.charAt(i) == 'e' || input.charAt(i) == 'i' || input.charAt(i) == 'o' || input.charAt(i) =='u') {
vowels++;
}
}
return vowels;
}
/**
* isPrime - Given an integer, return true if it is a prime number, false otherwise.
* Ex: 7 -> true, 8 -> false
* Hint: to check if n is prime, divide it by all integers from 2 to sqrt(n).
*/
public static boolean isPrime(int n) {
if (n == 1) {
return false;
}
for (int i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
/**
* factorial - Given a non-negative integer n, return n! (n factorial).
* Ex: 5 -> 120
* Hint: n! = n * (n-1) * (n-2) * ... * 1, and 0! = 1
* don't use recursion.
* THIS ONE IS HARD
*/
public static long factorial(int n) {
int previousFactorial = 1;
for (int i = n; i > 0; i--) {
previousFactorial = previousFactorial * i;
}
return previousFactorial;
}
// Use for Manual Testing
public static void main(String[] args) {
System.out.println("================ RUNNING CODE ================");
// sum
int a = 3;
int b = 5;
System.out.println("sum(" + a + ", " + b + ") = " + sum(a, b)); // Expected: 8
// fizzbuzz
int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 30, 35, 36};
System.out.println("fizzbuzz(arr) = " + Arrays.toString(fizzbuzz(arr))); // Expected: [5, 4, 3]
// reverseString
String str = "hello";
System.out.println("reverseString('" + str + "') = '" + reverseString(str) + "'"); // Expected: 'olleh'
// maxInArray
int[] nums = {1, 5, 3, 9, 2};
System.out.println("maxInArray([1,5,3,9,2]) = " + maxInArray(nums)); // Expected: 9
// stringsAreSame
String one = "hello";
String two = "HELLO";
System.out.println("stringAreSame = " + stringsAreSame(one, two));
// countVowels
String word = "banana";
System.out.println("countVowels('banana') = " + countVowels(word)); // Expected: 3
// isPrime
int primeTest = 7;
int notPrimeTest = 8;
System.out.println("isPrime(7) = " + isPrime(primeTest)); // Expected: true
System.out.println("isPrime(8) = " + isPrime(notPrimeTest)); // Expected: false
// factorial
int factTest = 5;
System.out.println("factorial(5) = " + factorial(factTest)); // Expected: 120
System.out.println("=============== DONE RUNNING ===============");
}
}