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

I have the following class Date212 which reads from a text file and outputs the

ID: 3599120 • Letter: I

Question

I have the following class Date212 which reads from a text file and outputs the information in the form 10 01 , 2014. I need this class to be modified so that the output is instead October 1, 2014.

The textfile is in the format:

20141001
20080912,20131120,19980927
20020202
20120104


import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.StringTokenizer;

//Class that test Date212 class
class Date212Driver
{
//Main program
public static void main(String args[])
{
//Array list to hold list of dates
ArrayList<Date212> dates = new ArrayList<Date212>();
  
//Opening file
File file = new File("Date.txt");
  
try
{
Scanner sc = new Scanner(file);
  
//Reading date from file
while (sc.hasNextLine())
{
//Reading a line
String temp = sc.nextLine();
  
//Added code start here

if(temp.contains(","))//We are checking line contains comma or not
{

StringTokenizer l_token=new StringTokenizer(temp, ",");//We are tokenizing one by one value


while(l_token.hasMoreTokens())//Iterating one by one token
{
String l_date=(String)l_token.nextElement();//Getting token and storing value in the string

if(l_date.length() == 8)//Now we checking the siz of the string
{
//Creating an object
Date212 tdate = new Date212(l_date);//Creating date object
  
//Adding to array list
dates.add(tdate);
}
}

// System.out.println("temp:::::"+temp);
}
//Added code end here

//Validating length
if(temp.length() == 8)
{
//Creating an object
Date212 tdate = new Date212(temp);
  
//Adding to array list
dates.add(tdate);
}
}
  
sc.close();
}
catch (FileNotFoundException e)
{
//Handling exceptions
e.printStackTrace();
}
  
//Printing values in array list
for(int i=0; i<dates.size(); i++)
System.out.println( dates.get(i) + " ");
}
}

public class Date212{
private int month;
private int day;
private int year;
public Date212 (int y, int m, int d) {
//Validating month
if(m < 1 || m > 12)
throw new IllegalArgumentException("Month cannot be less than 1 or more than 12.");

//Validating day
if(d < 1 || d > 31)
throw new IllegalArgumentException("Day cannot be less than 1 or more than 32.");
{
month=m;
day=d;
year=y;
}
}
public int getday() {
return day;
}
public int getmonth() {
return month;
}
public int getyear() {
return year;
}
public void setday(int d) {
if(d<1 || d>31)
throw new IllegalArgumentException("Day must be more than 0 and less than or equal to 31.");
day=d;
}
public void setmonth(int m) {
if(m<1 || m>12)
throw new IllegalArgumentException("Month must be more than 0 and less than 13.");
month=m;
}
public void setyear(int y) {
year=y;
}
public Date212 (String d)
{
//Calling three argument constructor
this(Integer.parseInt(d.substring(0, 4)), Integer.parseInt(d.substring(4, 6)), Integer.parseInt(d.substring(6, 8)));
}

//Method that returns date in the required format
public String toString()
{
return String.format("%02d", month) + " " + String.format("%02d", day) + "," + String.format("%04d", year);
}
}

Explanation / Answer

I made a string array, and stored all the names of the months in there, such as January, February etc. Then using the month index, I got the value of the month name, and thus returned the name of the month in the toString function. All the new changes have been marked in blocks.

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.StringTokenizer;

//Class that test Date212 class
class Date212Driver
{
    //Main program
    public static void main(String[] args)
    {
        //Array list to hold list of dates
        ArrayList<Date212> dates = new ArrayList<Date212>();

        //Opening file
        File file = new File("Date.txt");

        try
        {
            Scanner sc = new Scanner(file);

            //Reading date from file
            while (sc.hasNextLine())
            {
                //Reading a line
                String temp = sc.nextLine();

                //Added code start here

                if(temp.contains(","))//We are checking line contains comma or not
                {

                   StringTokenizer l_token=new StringTokenizer(temp, ",");//We are tokenizing one by one value


                   while(l_token.hasMoreTokens())//Iterating one by one token
                   {
                       String l_date=(String)l_token.nextElement();//Getting token and storing value in the string

                       if(l_date.length() == 8)//Now we checking the siz of the string
                      {
                      //Creating an object
                      Date212 tdate = new Date212(l_date);//Creating date object

                      //Adding to array list
                      dates.add(tdate);
                      }
                   }

                   // System.out.println("temp:::::"+temp);
                }
                //Added code end here

                //Validating length
                if(temp.length() == 8)
                {
                    //Creating an object
                    Date212 tdate = new Date212(temp);

                    //Adding to array list
                    dates.add(tdate);
                }
            }
            sc.close();
        }
        catch (FileNotFoundException e)
        {
            //Handling exceptions
            e.printStackTrace();
        }

        //Printing values in array list
        for(int i=0; i<dates.size(); i++)
        System.out.println( dates.get(i) + " ");
    }
}

class Date212{
    private String monthsInYear[]={"","January","February","March","April","May","June","July","August","September","October","November","December"};
   
private int month;
    private String monthString;
    private int day;
    private int year;
    public Date212 (int y, int m, int d) {
        //Validating month
        if(m < 1 || m > 12)
        throw new IllegalArgumentException("Month cannot be less than 1 or more than 12.");

        //Validating day
        if(d < 1 || d > 31)
        throw new IllegalArgumentException("Day cannot be less than 1 or more than 32.");
        {
            month=m;
            day=d;
            year=y;
            monthString = monthsInYear[m];
        }
    }
    public int getday() {
      return day;
    }
    public int getmonth() {
      return month;
    }
    public int getyear() {
      return year;
    }
    public void setday(int d) {
      if(d<1 || d>31)
      throw new IllegalArgumentException("Day must be more than 0 and less than or equal to 31.");
      day=d;
    }
    public void setmonth(int m) {
      if(m<1 || m>12)
      throw new IllegalArgumentException("Month must be more than 0 and less than 13.");
      month=m;
    }
    public void setyear(int y) {
      year=y;
    }
    public Date212 (String d)
    {
      //Calling three argument constructor
      this(Integer.parseInt(d.substring(0, 4)), Integer.parseInt(d.substring(4, 6)), Integer.parseInt(d.substring(6, 8)));
    }

    //Method that returns date in the required format
    public String toString()
    {
      return monthString + " " + String.format("%02d", day) + "," + String.format("%04d", year);
    }
}

output :

October 01,2014

September 12,2008

November 20,2013

September 27,1998

February 02,2002

January 04,2012