0/30 Days Completed 0 Learning Days
Day 14 of 30 · Week 2 — Building Java Basics
Week 2 mini challenge: Number Toolkit
Practical review day1 Learn
Review day! Week 2 covered methods, parameters, return values, arrays and Strings.
Build a small toolkit of methods and use them from main — this is how larger programs are organized.
2 See the Code
Java
public class Toolkit {
static int max(int[] a) {
int m = a[0];
for (int x : a) if (x > m) m = x;
return m;
}
static String reverse(String s) {
String r = "";
for (int i = s.length() - 1; i >= 0; i--) r += s.charAt(i);
return r;
}
public static void main(String[] args) {
int[] nums = {4, 19, 7, 12};
System.out.println("Max: " + max(nums));
System.out.println("Reverse: " + reverse("java"));
}
}- max() walks through the array keeping the biggest value.
- reverse() builds a new String from the last character to the first.
Output
Max: 19 Reverse: avaj
3 Practice
- Challenge 1: Add min(int[] a).
- Challenge 2: Add isPalindrome(String s) using reverse() and equals().
- Challenge 3: Add sum(int[] a) and print the average using it.
Try it in your own Java setup or write it in the Practice Notebook.
4 Quick Check
1. reverse("abc") returns?
2. Which is a palindrome?
3. What must max() do with its result?
Step 5
Answer and check the Quick Check above to unlock this.