Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Programming Questions You are provided with a Date class and an incomplete IncDate class. The IncDate class inherits from the Date class and it is

Programming Questions

You are provided with a Date class and an incomplete IncDate class. The IncDate class inherits from the Date class and it is supposed to add a certain number of days to a date. This task is done by first creating an object of the IntDate class in the test driver with an initial date and then calling the addDays method on this object. For example, if the current IntDate object, say myDate, contains the date 1/1/2016, then after the addDays(20) method is called on myDate, the date in it would be 1/21/2016 because 20 days has been added to the original date. This is not a simple addition because you need to take care of different months as well as leap years.

a. Completing the IncDate class

Your task is to implement the addDays method in the IncDate class so that calling this method would add a certain number of days to the date contained in the current object. Note that you are only supposed to touch this method. You are NOT allowed to create any other methods, instance variables, or make any changes to methods other than addDays(int numDays) or files other than "IncDate.java". Points will be taken off if you fail to follow this rule.

b. Code Testing

You are provided with a test driver implemented by "TestIncDate.java" (Do not make any changes to this file!) so there is no need to write your own. You are also given a data file "testDates.dat" that contains 30 test dates. This is a binary file and you will not be able to view its content using a text editor. We have not covered how to read and write this type of files so do not worry if you have no idea how it works. We will introduce the topic later this semester. Depending on your programming environment, the data file might need to be placed in different folders so that you test driver can read it. For jGRASP, you can leave the data file in the same folder as your java files. For NetBeans, you should place it in your project folder in which you see directories like build, nbproject, and src, etc. Once you have completed the addDays method, you can run the test. You should create a plain text file named "output.txt", copy and paste the output (if your code crashes or does not compile, copy and paste the error messages) to this file and save it.

Sample Output:

The current date is 1/30/2010 and 2 days are added. The correct new date is 2/1/2010 and the one calculated by your program is 2/1/2010. Correct!

The current date is 2/28/2011 and 2 days are added. The correct new date is 3/2/2011 and the one calculated by your program is 3/2/2011. Correct!

The current date is 2/28/2012 and 2 days are added. The correct new date is 3/1/2012 and the one calculated by your program is 3/1/2012. Correct!

...

The current date is 12/8/2021 and 6392 days are added. The correct new date is 6/9/2039 and the one calculated by your program is 6/9/2039. Correct!

Total test cases: 30

Correct: 30

Wrong: 0

// The Date class that represents dates // Do not make any changes to this file!

import java.io.*;

public class Date implements Serializable { // instance variables protected int m_year; protected int m_month; protected int m_day; // copy constructor public Date(Date o) { m_year = o.m_year; m_month = o.m_month; m_day = o.m_day; } // constructor public Date(int month, int day, int year) { m_year = year; m_month = month; m_day = day; } // observers public int getYear() { return m_year; } public int getMonth() { return m_month; } public int getDay() { return m_day; } // return this date as a String public String toString() { return (m_month + "/" + m_day + "/" + m_year); } }

// The IncDate class // Do not make changes to anything other than the body of increment() method

public class IncDate extends Date { // copy constructor public IncDate(Date o) { super(o.m_month, o.m_day, o.m_year); }

// constructor public IncDate(int month, int day, int year) { super(month, day, year); }

public void addDays(int numDays) { // TODO: implement this method } }

/ Test driver for the IncDate class // Do not make any changes to this file!

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

public class TestIncDate { public static void main(String[] args) { try { ObjectInputStream in = new ObjectInputStream(new FileInputStream("testDates.dat")); ArrayList oldDates; ArrayList daysList; ArrayList newDates; oldDates = (ArrayList)in.readObject(); daysList = (ArrayList)in.readObject(); newDates = (ArrayList)in.readObject();

IncDate myDate; int numCorrect = 0;

for (int i = 0; i < oldDates.size(); i++) { System.out.println("The current date is " + oldDates.get(i) + " and " + daysList.get(i) + " days are added."); myDate = new IncDate(oldDates.get(i)); myDate.addDays(daysList.get(i)); System.out.println("The correct new date is " + newDates.get(i) + " and the one calculated by your program is " + myDate + ".");

if (myDate.toString().equals(newDates.get(i).toString())) { System.out.println("Correct! "); numCorrect++; } else System.out.println("Wrong! "); }

System.out.println("Total test cases: " + oldDates.size() + " Correct: " + numCorrect + " Wrong: " + (oldDates.size() - numCorrect)); } catch (Exception e) { System.out.println("Error occurred: " + e.getMessage()); } } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Samsung Galaxy S23 Ultra Comprehensive User Manual

Authors: Leo Scott

1st Edition

B0BVPBJK5Q, 979-8377286455

More Books

Students also viewed these Databases questions

Question

Explain basic guidelines for effective multicultural communication.

Answered: 1 week ago

Question

Identify communication barriers and describe ways to remove them.

Answered: 1 week ago

Question

Explain the communication process.

Answered: 1 week ago