Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that opens a file of the users choice that contains a list of bi

ID: 3864248 • Letter: W

Question

Write a program that opens a file of the users choice that contains a list of birthdays. Extract from this file two things: (1) the date with the most common birthday (all of them) and (2) the month with the most people born. We will not test for a tie in either of these statistics. The file tested will always be in the format: mm/dd/yyyy

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Sample birthday files:

bday1.txt

-------------------------------------------------------------------------------------------------------------------------------------------

bday2.txt

------------------------------------------------------------------------------------------------------------------

Example 1(user input is Italic):

Enter file name:

bday1.txt

Most common birthday:

1/1

Most common birthday month: 3

Example 2(user input is underlined):

Enter file name:

bday2.txt

Most common birthday:

8/15

Most common birthday month:

8

Explanation / Answer

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;


public class Bday {

   public static void main(String a[]) throws Exception{
       Scanner sc=new Scanner(System.in);
      
       System.out.println("Enter file name: ");
       String file=sc.nextLine();
      
       BufferedReader br=new BufferedReader(new FileReader("C:\Users\Darshit Pathak\Desktop\"+file));
       String line="";
      
      
       List<String> month=new ArrayList<String>();
      
       List<String>bdate=new ArrayList<String>();
      
       while((line=br.readLine())!=null){
          
           String date[]=line.split("/");
           month.add(date[0]);
           bdate.add(line);
       }
      
      
       Map<String,Integer> counts=new HashMap<String, Integer>();
       List<String> visited=new ArrayList<>();
       for(int i=0;i<bdate.size();i++){
           int count=1;
           for(int j=i+1;j<bdate.size();j++){
               if((bdate.get(i).equals(bdate.get(j)) && !visited.contains(bdate.get(i)))){
                   count++;
               }
           }
           counts.put(bdate.get(i), count);
           visited.add(bdate.get(i));
       }
      
       int max=counts.get(bdate.get(0));
       String maxDate=bdate.get(0);
       for(int i=1;i<counts.size();i++){
          
           if(counts.get(bdate.get(i))>max){
               max=counts.get(bdate.get(i));
           maxDate=bdate.get(i);
           }
       }
      
       System.out.println("Most Common Birthday: ");
       System.out.println(maxDate.substring(0,maxDate.lastIndexOf("/")));
      
       Map<String,Integer> counts1=new HashMap<String, Integer>();
       List<String> visited1=new ArrayList<>();
       for(int i=0;i<month.size();i++){
           int count=1;
           for(int j=i+1;j<month.size();j++){
              
              
               if(month.get(i).equals(month.get(j))&&!visited1.contains(month.get(i))){
                   count++;
               }
           }
           counts1.put(month.get(i), count);
           visited1.add(month.get(i));
       }
      
      
       max=counts1.get(month.get(0));
       maxDate=month.get(0);
       for(int i=1;i<counts1.size();i++){
          
           if(counts1.get(month.get(i))>max){
               max=counts1.get(month.get(i));
           maxDate=month.get(i);
           }
       }

       System.out.println("Most Common Birthday Month: ");
       System.out.println(maxDate);
}  
      
  
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote