Contents
- ./Birthday.java
- ./FinallyTest.java
- ./ListIteratorTests.java
./Birthday.java 1/3
[top][prev][next]
/**
* Demonstrates use of throwing IllegalArgumentExceptions to prevent errors.
* @author sprenkle
*/
public class Birthday {
private int day;
private int month;
private static int[] DAYS_IN_MONTH = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
/**
* @param month
* @param day
*/
public Birthday(int month, int day) {
super();
this.day = day;
this.month = month;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append(month);
builder.append("/");
builder.append(day);
return builder.toString();
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
return super.equals(obj);
}
/**
* This is a better way to implement setting the month and the day because
* they are related to (not independent of) each other.
* @param month
* @param day
*/
public void setBirthday(int month, int day) {
if( month < 1 || month > 12) {
throw new IllegalArgumentException("month must be between 1 and 12");
}
if( day < 1 || day > 31 ) {
throw new IllegalArgumentException("day must be between 1 and 31");
}
/* The below code is clunky because we are handling the months in a big
if. It would be better to have an array of days in month and check that
to see if day is valid. */
if( month == 2 && day > 29) {
// handle Feb
throw new IllegalArgumentException("February has only 29 days.");
}
if( month == 4 || month == 6 || month == 9 || month == 11 ) {
throw new IllegalArgumentException(month + " has only 29 days.");
}
// ALTERNATIVELY:
// if( day > DAYS_IN_MONTH[month-1] )
// throw new IllegalArgumentException ...
// only set the month and day after we know that the month and day are valid.
this.month = month;
this.day = day;
}
/**
* @return the day
*/
public final int getDay() {
return day;
}
/**
*
* @param day
*/
public final void setDay(int day) {
this.day = day;
}
/**
* @return the month
*/
public final int getMonth() {
return month;
}
/**
* @param month
* the month
*/
public final void setMonth(int month) {
this.month = month;
}
/**
* @param args
*/
public static void main(String[] args) {
Birthday b = new Birthday(1, 1);
System.out.println(b);
// test calling the method with the exceptions:
b.setBirthday(1,32); // should throw an exception
b.setBirthday(2,30); // we'll never get to here because the last statement threw an exception
}
}
./FinallyTest.java 2/3
[top][prev][next]
/**
* Example using finally block
*
* @author Sara Sprenkle
*/
public class FinallyTest {
/**
* @param args
*/
public static void main(String[] args) {
try {
System.out.println("In try");
//throw new NullPointerException();
} catch (Exception e) {
System.out.println("In Catch");
} finally {
System.out.println("In Finally");
}
}
}
./ListIteratorTests.java 3/3
[top][prev][next]
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class ListIteratorTests {
/**
* Condition that we want for items in our list.
*
* @param item
* @return
*/
public static boolean condition(int item) {
return item % 2 == 0;
}
private static void displayList(List<Integer> myList, String string) {
System.out.println(string);
for (Integer i : myList) {
System.out.println(i);
}
}
public static void main(String[] args) {
// set up a list
List<Integer> myList = new ArrayList<>();
for (int i = 0; i < 10; i++) {
myList.add(i);
}
displayList(myList, "Original List:");
System.out.println();
Iterator<Integer> iter = myList.iterator();
while (iter.hasNext()) {
Integer item = iter.next();
if (!condition(item)) {
// alternatively, could mutate this object, if the object has
// mutator methods available.
// Can't replace this item or remove the item and add in a new
// object.
iter.remove();
}
}
displayList(myList, "Final List:");
}
}
Generated by GNU Enscript 1.6.6.