CS 145 Lecture 16 – Arrays
Agenda
- named data vs. numbered data
- arrays
- mapping month numbers to names
- the birthday problem
Code
Birthdays
9 6
3 30
9 26
10 11
7 6
12 7
1 9
2 10
6 21
3 2
5 12
2 25
7 3
1 17
4 29
5 19
12 8
4 2
3 31
5 30
11 30
5 30
3 8
9 15
9 5
10 24
6 5
8 8
6 14
4 29
11 15
4 7
2 14
6 29
4 7
2 16
8 31
6 29
8 21
4 18
2 10
7 18
2 16
9 15
8 10
12 21
10 24
12 9
3 23
4 1
6 25
4 14
2 14
1 10
2 14
10 24
3 15
4 16
9 11
10 14
10 19
10 1
9 28
1 10
9 28
8 30
4 25
6 21
5 21
6 14
8 14
11 11
5 17
1 20
11 6
10 7
4 20
12 27
4 9
12 3
11 29
1 20
10 26
10 26
3 27
8 9
11 18
5 1
7 29
3 3
6 14
3 29
5 10
Birthdayarator.java
package lecture; import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class Birthdayarator { public static void main(String[] args) throws FileNotFoundException { Scanner in = new Scanner(new File("/home/cjohnson/Desktop/bdays.txt")); String[] names = new String[12]; names[0] = "January"; names[1] = "February"; names[2] = "March"; names[3] = "April"; names[4] = "May"; names[5] = "June"; names[6] = "July"; names[7] = "August"; names[8] = "September"; names[9] = "October"; names[10] = "November"; names[11] = "December"; int[] counts = new int[12 * 31]; System.out.println(names.length); while (in.hasNextInt()) { int month = in.nextInt(); int day = in.nextInt(); int doy = (month - 1) * 31 + day; ++counts[doy]; String monthName = names[month - 1]; System.out.print(monthName); // if (month == 1) { // System.out.print("January"); // } else if (month == 2) { // System.out.print("February"); // } else if (month == 3) { // System.out.print("March"); // } else if (month == 4) { // System.out.print("April"); // } else if (month == 5) { // System.out.print("May"); // } else if (month == 6) { // System.out.print("June"); // } else if (month == 7) { // System.out.print("July"); // } else if (month == 8) { // System.out.print("August"); // } else if (month == 9) { // System.out.print("September"); // } else if (month == 10) { // System.out.print("October"); // } else if (month == 11) { // System.out.print("November"); // } else if (month == 12) { // System.out.print("December"); // } else { // System.out.println("YOUR PROGRAM IS BJORKED"); // } System.out.println(" " + day); } for (int i = 0; i < counts.length; ++i) { if (counts[i] > 1) { System.out.println(i); } } } }
Haiku
numbered but not named
they want you processed and out
so just be zero
they want you processed and out
so just be zero