Question

Please show public static void main(String args[]){}

also too!8. Given a non-empty list of dates, this method groups them based on the day of the wel on which they occurred; selects the e

0 0
Add a comment Improve this question Transcribed image text
Answer #1

// LatestByWeekDay.java

import java.util.Date;

import java.util.GregorianCalendar;

import java.util.Map;

import java.util.List;

import java.util.Set;

import java.util.TreeMap;

public class LatestByWeekDay {

       // method that groups a list of dates based on the day of the week and selects the earliest one in each group

       // return type is Map whose keys are the short day-of-the-week and values are the selected date

       // map is sorted on its keys

       public static Map<String,Date> latestByWeekday(List<Date> list)

       {

             Map<String,Date> result = new TreeMap<String,Date>();

             String mon;

             // loop over the list of dates

             for(int i=0;i<list.size();i++)

             {

                    mon = String.format("%ta",list.get(i)); // get the short day-of-the-week from the date

                    if(result.containsKey(mon)) // check if day is already present in map

                    {

                           // check if this date is earlier than the stored date, then replace it with this date

                           if(list.get(i).compareTo(result.get(mon)) < 0)

                                 result.put(mon, list.get(i));

                    }else // if day-of-the-week is not present, insert in Map

                           result.put(mon,list.get(i));

             }

            

             return result;

       }

public static void main(String[] args) throws IOException {

            

             // create a list of dates

             List<Date> list = new ArrayList<Date>();

             // add dates to list

             list.add(new GregorianCalendar(2017,2,30,23,30,00).getTime());

             list.add(new GregorianCalendar(2016,11,31,23,10,00).getTime());

             list.add(new GregorianCalendar(2017,3,1,6,00,00).getTime());

             list.add(new GregorianCalendar(2016,7,11,00,00,00).getTime());

             list.add(new GregorianCalendar(2016,11,31,00,15,00).getTime());

            

             // get the result

             Map<String,Date> result =latestByWeekday(list);

            

             // print the result

             Set<Map.Entry<String,Date>> resultSet= result.entrySet();

             for(Map.Entry<String, Date> entry:resultSet)

             {

                    System.out.println(entry.getKey() +" = "+entry.getValue());

             }

}

}

//end of program

Output:

Sat = Sat Dec 31 00:15:00 IST 2016 Thu = Thu Aug 11 00:00:00 IST 2016

Add a comment
Know the answer?
Add Answer to:
8. Given a non-empty list of dates, this method groups them based on the day of the wel on which ...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Copy all the classes given in classes Calendar, Day, Month, & Year into BlueJ and then...

    Copy all the classes given in classes Calendar, Day, Month, & Year into BlueJ and then generate their documentation. Examine the documentation to see the logic used in creating each class. (Code given below) import java.util.ArrayList; import java.util.Iterator; class Calendar { private Year year; public Calendar() { year = new Year(); } public void printCalendar() { year.printCalendar(); } } class Year { private ArrayList<Month> months; private int number; public Year() { this(2013); } public Year(int number) { this.number = number;...

  • The following selected transactions are from Lee Company. 2016 Dec. 16 Accepted a $22,800, 60-day, 10%...

    The following selected transactions are from Lee Company. 2016 Dec. 16 Accepted a $22,800, 60-day, 10% note dated this day in granting Carlos Sanchez a time extension on his past-due account receivable. 31 Made an adjusting entry to record the accrued interest on the Sanchez note. 2017 Feb. 14 Received Sanchez's payment of principal and interest on the note dated December 16. Mar. 2 Accepted a $11,000, 10%, 90-day note dated this day in granting a time extension on the...

  • Assignment You will be developing a speeding ticket fee calculator. This program will ask for a t...

    Assignment You will be developing a speeding ticket fee calculator. This program will ask for a ticket file, which is produced by a central police database, and your program will output to a file or to the console depending on the command line arguments. Furthermore, your program will restrict the output to the starting and ending dates given by the user. The ticket fee is calculated using four multipliers, depending on the type of road the ticket was issued: Interstate...

  • Code is in C# Your instructor would like to thank to Marty Stepp and Hélène Martin...

    Code is in C# Your instructor would like to thank to Marty Stepp and Hélène Martin at the University of Washington, Seattle, who originally wrote this assignment (for their CSE 142, in Java) This program focuses on classes and objects. Turn in two files named Birthday.cs and Date.cs. You will also need the support file Date.dll; it is contained in the starter project for this assignment. The assignment has two parts: a client program that uses Date objects, and a...

  • 4. Perform a SWOT analysis for Fitbit. Based on your assessment of these, what are some strategic options for Fitbit go...

    4. Perform a SWOT analysis for Fitbit. Based on your assessment of these, what are some strategic options for Fitbit going forward? 5. Analyze the company’s financial performance. Do trends suggest that Fitbit’s strategy is working? 6.What recommendations would you make to Fitbit management to address the most important strategic issues facing the company? Fitbit, Inc., in 2017: Can Revive Its Strategy and It Reverse Mounting Losses? connect ROCHELLE R. BRUNSON Baylor University MARLENE M. REED Baylor University in the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT