Java Homework #6 Model Solution 1. How do you create a Date for the current time? How do you display the current time? ANSWER Date now = new Date(); System.out.println( "Date is " + now.toString() ); // Don't need ".toString()" 2. How do you do the following? a. Create an ArrayList for storing double values? ANSWER - ArrayList d = new ArrayList(); b. Append an object to a list? ANSWER - list.add(obj) c. Insert an object at the beginning of a list? ANSWER - list.add(0,obj) d. Find the number of objects in a list? ANSWER = list.size(); e. Remove a given object from a list? ANSWER - list.remove(obj) f. Remove the last object from the list? ANSWER - list.remove(list.size() - 1) g. Check whether a given object is in a list? ANSWER - list.contains(obj) h. Retrieve an object at a specified index from a list? ANSWER - list.get(i) 3. Identify the errors in the following code. Array.list list = new ArrayList (); list.add("Denver"); list.add("Austin"); list.add(new java.util.Date()); String city = list.get(0); list.set(3, "Dallas"); System.out.println(list.get(3)); 4. Suppose the ArrayList list contains duplicate elements. Does the following code correctly remove the element from the array list? If not, correct the code. for ( int i = 0; i < list.size(); i++ ) list.remove(element); NO. This attempts to remove the same element list.size() times. To remove duplicates in a list, one way is like so: int pos = list.indexOf(element); // Position of 1st copy of element for ( int i = 0; i < list.size(); i++ ) { // Remove all copies of element if ( element.equals( list.get(i) ) list.remove(i); } list.add(pos, element); // Add back the first one, in the right position. There are several other possible ways: you could use an iterator in a while-loop. You could use a loop to remove all copies except the last one (I like this approach the best): if ( list.indexOf(element) != -1 ) { while ( list.indexOf(element) != list.lastIndexOf(element ) ) list.remove(element); } Finally, if the order of the elements doesn't matter, you should have used a Set instead of a List, which automatically discards duplicates. You can try this advanced technique to de-duplicate a list (stolen from a post on StackExchange): Set deDupSet = new LinkedHashSet(list); list.clear(); list.addAll( deDupSet ); 5. Explain why the following code displays [1, 3] rather than [2, 3]. ArrayList list = new ArrayList(); list.add(1); list.add(2); list.add(3); list.remove(1); System.out.println(list); ANSWER - list.remove(1) removed the index, not the value. Needed to do this: list.remove(new Integer(1)) 6. How do you create an object of the java.util.Calendar class, for the current time? ANSWER - Calendar calendar = Calendar.newInstance(); This is also okay: Calendar calendar = new GregorianCalendar(); 7. For a Calendar object c, how do you get its year, month, date, hour, minute, and second? ANSWER - Calendar calendar = Calendar.getInstance(); System.out.println ("year is " + calendar.get(Calendar.YEAR)); System.out.println ("month is " + calendar.get(Calendar.MONTH)); System.out.println ("date is " + calendar.get(Calendar.DAY_OF_MONTH)); System.out.println ("hour is " + calendar.get(Calendar.HOUR)); System.out.println ("minute is " + calendar.get(Calendar.MINUTE)); System.out.println ("second is " + calendar.get(Calendar.SECOND)); (Note how month is shown as 0..11, not 1..12!) 8. Using the java.time API, how can you add one month to a LocalDate object? How can you create a LocalDate object that represents the last Friday of the current month? ANSWER - import java.time.*; import static java.time.DayOfWeek.*; import static java.time.format.DateTimeFormatter.ofLocalizedDate; import static java.time.format.FormatStyle.FULL; import static java.time.temporal.ChronoUnit.*; // MONTHS import static java.time.temporal.TemporalAdjusters.lastInMonth; class Question8 { public static void main ( String [] args ) { LocalDate today = LocalDate.now(); // Add one month to today: LocalDate nextMonth = today.plus( 1, MONTHS ); // OR: LocalDate nextMonth = today.plusMonths( 1 ); System.out.println( "One month from now is: " + nextMonth.format( ofLocalizedDate(FULL) ) ); // Last Friday of current month: System.out.println( "Last Friday of current month is: " + today.with( lastInMonth(FRIDAY) ) .format( ofLocalizedDate(FULL) ) ); } }