Question

Create a class called Date212 to represent a date. It will store the year, month and...

Create a class called Date212 to represent a date. It will store the year, month and day as integers (not as a String in the form yyyymmdd (such as 20161001 for October 1, 2016), so you will need three private instance variables. Two constructors should be provided, one that takes three integer parameters, and one that takes a String.

The constructor with the String parameter and should validate the parameter. As you are reading the dates you should check that the value read in is legal (8 digits), and if it is not, print it to the console and do not put it in the array of dates., and then use the substring method of class String to pull out the month, day and year, parse them as integers and call the three-argument constructor.

The three-argument constructor should make sure that the month and day values are legal. To call another constructor from within a constructor, use the method this :

public Date212 (String d) { // the one-argument constructor

… this(year, month, day) // invokes the three-argument constructor.

}

The dates will be taken from an .txt input file in the format :

20161001

20080912,20131120,19980927

20020202

20120104

ToString() for Class Date212. Create a toString method in class Date212 the will return the date in the form mm/dd/yyyy. Use this method to display the dates in DateGUI.

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

Java Program:

/* Java Program that reads date string from file and prints in the specified format */

import java.util.*;
import java.io.*;

//Class that holds date
class Date212
{
   //Private member variables
   private int year;
   private int month;
   private int day;
  
   //Three argument Constructor
   public Date212 (int y, int m, int d)
   {
       //Validating month
       if(m < 1 || m > 12)
           System.out.println("\n Invalid month... \n");
          
       //Validating day  
       else if(d < 1 || d > 31)
           System.out.println("\n Invalid day... \n");
       else
       {
           //Assigning values to member variables
           this.year = y;
           this.month = m;
           this.day = d;
       }
   }
  
   //Constructor with single argument
   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);
   }
}


//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("birthDates.txt");
      
       try
       {
           Scanner sc = new Scanner(file);
      
           //Reading date from file
           while (sc.hasNextLine())
           {
               //Reading a line
               String temp = sc.nextLine();
              
               //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("\n Date: " + dates.get(i) + " \n");
   }
}

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

Sample Output:

CWindowslsystem32\cmd.exe D:Java>javac Date212Driver.java D:\Java java Date212Driver C: Date: 10/01/2016 Date: 09/12/2008 Dat

Add a comment
Know the answer?
Add Answer to:
Create a class called Date212 to represent a date. It will store the year, month and...
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
  • Create class Date with the following capabilities: a) Output the date in multiple formats, such as...

    Create class Date with the following capabilities: a) Output the date in multiple formats, such as MM/DD/YYYY June 14, 1992 DDD YYYY b) Use overloaded constructors to create Date objects initialized with dates of the formats in part (a). In the first case the constructor should receive three integer values. In the second case it should receive a String and two integer values. In the third case it should receive two integer values, the first of which represents the day...

  • This is to be in C# and this project is being made in Microsoft Visual Studio...

    This is to be in C# and this project is being made in Microsoft Visual Studio Create a Date class that does the following: o A default constructor o A constructor with a string argument o An integer member variable that represents the date in the following format:  YYYYMMDD  i.e. 20070212 means February 12, 2007 o A string member variable that represents the date in the following format:  YYYY-MM-DD o An integer member variable that represents a...

  • JAVA programing Question 1 (Date Class):                                   &nbsp

    JAVA programing Question 1 (Date Class):                                                                                                     5 Points Create a class Date with the day, month and year fields (attributes). Provide constructors that: A constructor that takes three input arguments to initialize the day, month and year fields. Note 1: Make sure that the initialization values for the day, month and year fields are valid where the day must be between 1 and 31 inclusive, the month between 1 and 12 inclusive and the year a positive number. Note 2:...

  • Create an abstract class “Appointment” that represents an event on a calendar. The “Appointment” class will...

    Create an abstract class “Appointment” that represents an event on a calendar. The “Appointment” class will have four instance variables: • An instance variable called “year” which will be of type int • An instance variable called “month” which will be of type int • An instance variable called “day” which will be of type int • An instance variable called “description” which will be of type String The “Appointment” class must also implement the following methods: • A getter...

  • public class Date { private int month; private int day; private int year; /** default constructor...

    public class Date { private int month; private int day; private int year; /** default constructor * sets month to 1, day to 1 and year to 2000 */ public Date( ) { setDate( 1, 1, 2000 ); } /** overloaded constructor * @param mm initial value for month * @param dd initial value for day * @param yyyy initial value for year * * passes parameters to setDate method */ public Date( int mm, int dd, int yyyy )...

  • Need help writing beginner C# program, I will make sure to provide feedback to whoever can...

    Need help writing beginner C# program, I will make sure to provide feedback to whoever can help me figure it out! No public or global variables should be used. You need to consider passing arguments between the methods according to the ways described in the lecture. i.e. all variables should be declared inside the methods and passed to other methods by value/ref/out as needed Description: We want to design a Date class to represent a date using three integer numbers...

  • Please write the program in C++ Problem Description: Design a class called Date. The class should...

    Please write the program in C++ Problem Description: Design a class called Date. The class should store a date in three integers: month, day, and year.     Create the necessary set and get methods.       Design member functions to return the date as a string in the following formats:                                     12/25/2012                                     December 25, 2012                                     25 December 2012 Input Validation: Do not accept values for day greater than 30 or less than 1. Do not accept values for month...

  • Code should be in C# Create a class called SavingsAccount.  Use a static variable called annualInterestRate to...

    Code should be in C# Create a class called SavingsAccount.  Use a static variable called annualInterestRate to store the annual interest rate for all account holders.  Each object of the class contains a private instance variable savingsBalance, indicating the amount the saver currently has on deposit. Provide method CalculateMonthlyInterest to calculate the monthly interest by multiplying the savingsBalance by annualInterestRate divided by 12 – this interest should be added to savingsBalance.  Provide static method setAnnualInterestRate to set the annualInterestRate to a new value....

  • Design and implement a C++ class called Date that has the following private member variables month...

    Design and implement a C++ class called Date that has the following private member variables month (int) day (nt) . year (int Add the following public member functions to the class. Default Constructor with all default parameters: The constructors should use the values of the month, day, and year arguments passed by the client program to set the month, day, and year member variables. The constructor should check if the values of the parameters are valid (that is day is...

  • C++ PROGRAMMING: Define a class called Date that has 3 private members that represent the month,...

    C++ PROGRAMMING: Define a class called Date that has 3 private members that represent the month, day, and year respectively. They will be represented as integers. Include a constructor and a public void function called print that prints out the date. (For example, if the month is 2, the day is 25, and the year is 1946, print provides as output 2/25/1946.) Include the class in a program that prints out the date 2/25/1946. Also, create another function that will...

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