Question

In: Computer Science

public class Date { private int dMonth; //variable to store the month private int dDay; //variable...

public class Date
{
    private int dMonth;         //variable to store the month
    private int dDay;           //variable to store the day
    private int dYear;          //variable to store the year

                //Default constructor
                //Data members dMonth, dDay, and dYear are set to
                //the default values
                //Postcondition: dMonth = 1; dDay = 1; dYear = 1900;
    public Date()
    {
                dMonth = 1;
                dDay = 1;
                dYear = 1900;
    }

        //Constructor to set the date
                //Data members dMonth, dDay, and dYear are set
        //according to the parameters
                //Postcondition: dMonth = month; dDay = day;
        //               dYear = year;
    public Date(int month, int day, int year)
    {
                setDate(month, day, year);
    }

              //Method to set the date
              //Data members dMonth, dDay, and dYear are set
              //according to the parameters
              //Postcondition: dMonth = month; dDay = day;
              //                         dYear = year;
    public void setDate(int month, int day, int year)
    {
                if (year >= 1)
                        dYear = year;
        else
                        dYear = 1900;

                if (1 <= month && month <= 12)
                        dMonth = month;
                else
                        dMonth = 1;

                switch (dMonth)
                {
                case 1: case 3: case 5: case 7:
                case 8: case 10: case 12: if (1 <= day && day <= 31)
                                                                          dDay = day;
                                                                  else
                                                                          dDay = 1;
                                                                  break;
                case 4: case 6:
                case 9: case 11: if (1 <= day && day <= 30)
                                                         dDay = day;
                                                 else
                                                         dDay = 1;
                                                 break;
                case 2: if (isLeapYear())
                                {
                                        if (1 <= day && day <= 29)
                                                      dDay = day;
                                            else
                                                      dDay = 1;
                                }
                                else
                                {
                                        if (1 <= day && day <= 28)
                                                dDay = day;
                                        else
                                                dDay = 1;
                                }
                }
    }


              //Method to return the month
              //Postcondition: The value of dMonth is returned
    public int getMonth()
    {
                return dMonth;
    }

              //Method to return the day
              //Postcondition: The value of dDay is returned
    public int getDay()
    {
        return dDay;
    }

              //Method to return the year
              //Postcondition: The value of dYear is returned
    public int getYear()
    {
                return dYear;
    }

              //Method to return the date in the form mm-dd-yyyy
    public String toString()
    {
                        return (dMonth + "-" + dDay + "-" + dYear);
    }

    public boolean isLeapYear()
    {
                if ((dYear % 4 == 0 && dYear % 100 != 0) || (dYear % 400 == 0))
                        return true;
                else
                        return false;
    }

    public void setMonth(int  m)
    {
                dMonth = m;
    }

    public void setDay(int d)
    {
                dDay = d;
    }

    public void setYear(int y)
    {
                dYear = y;
    }

    public int getDaysInMonth()
    {
                int noOfDays = 0;

                switch (dMonth)
                {
                case 1: case 3: case 5:
                case 7: case 8: case 10: case 12: noOfDays = 31;
                                                                          break;
        case 4: case 6: case 9: case 11:  noOfDays = 30;
                                                                                  break;
                case 2: if (isLeapYear())
                                        noOfDays = 29;
                        else
                                noOfDays = 28;
                }

                return noOfDays;
    }

    public int numberOfDaysPassed()
    {
                int[] monthArr = {0,31,28,31,30,31,30,31,31,30,31,30,31};

                int sumDays = 0;
                int i;

                for (i = 1; i < dMonth; i++)
                        sumDays = sumDays + monthArr[i];

                if (isLeapYear() && dMonth > 2)
                        sumDays = sumDays + dDay + 1;
        else
                        sumDays = sumDays + dDay;

                return sumDays;
    }

    int numberOfDaysLeft()
    {
                if (isLeapYear())
                        return 366 - numberOfDaysPassed();
                else
                        return 365 - numberOfDaysPassed();
    }

    public void incrementDate(int nDays)
    {
                int[] monthArr = {0,31,28,31,30,31,30,31,31,30,31,30,31};
                int daysLeftInMonth;

                daysLeftInMonth = monthArr[dMonth] - dDay;

                if (daysLeftInMonth >= nDays)
                        dDay = dDay + nDays;
                else
                {
                        dDay = 1;
                        dMonth++;
                        nDays = nDays - (daysLeftInMonth + 1);

                while (nDays > 0)
                                if (nDays >= monthArr[dMonth])
                        {
                                nDays = nDays - monthArr[dMonth];

                                if ((dMonth == 2) && isLeapYear())
                                        nDays--;

                                dMonth++;
                                if (dMonth > 12)
                                {
                                        dMonth = 1;
                                        dYear++;
                                }

                        }
                        else
                        {
                                dDay = dDay+nDays;
                                nDays = 0;
                        }
                }
    }

    public void makeCopy(Date otherDate)
        {
        dMonth = otherDate.dMonth;
        dDay = otherDate.dDay;
        dDay = otherDate.dDay;
    }

    public Date getCopy()
        {
                Date temp = new Date();

                temp.dMonth = dMonth;
                temp.dDay = dDay;
                temp.dYear = dYear;

                return temp;
    }
}

*************************************************************************************************************************************************************************

Given: the class Date that can prints the date in numerical form. Some applications might require the date to be printed in another form, such as March 24, 2005. Please do the following:

  1. Derive the class ExtDate so that the date can be printed either form.
  2. Add a data member to the class ExtDate so that the month can also be stored in string form. Add a method to output the month in the string format followed by the year, for instance, in the form March 2005.
  3. Write the definition of the methods to implement the operations for the class ExtDate and write a test program to test your program.

Solutions

Expert Solution

Thanks for the question.

Here is the completed code for this problem. 

Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. 


If you are satisfied with the solution, please rate the answer. Thanks


===========================================================================

//Derive the class ExtDate so that the date can be printed either form.
public class ExtDate extends Date {

    //Add a data member to the class ExtDate so that the
    // month can also be stored in string form.
    private String monthName;

    public ExtDate(){
        super();
    }

    public ExtDate(int month, int day, int year) {
        super(month, day, year);
    }

    public ExtDate(String monthName, int day, int year) {
        int monthNumber = getMonthNumber(monthName);
        setDate(monthNumber, day, year);
    }

    // getter method
    public String getMonthName() {
        return monthName;
    }

    // setter method
    public void setMonthName(String monthName) {
        this.monthName = monthName;
        int monthNumber = getMonthNumber(monthName);
        setMonth(monthNumber);
    }

    // returns the month number for the month name
    private int getMonthNumber(String name) {

        if (name.equals("January")) return 1;
        else if (name.equals("February")) return 2;
        else if (name.equals("March")) return 3;
        else if (name.equals("April")) return 4;
        else if (name.equals("May")) return 5;
        else if (name.equals("June")) return 6;
        else if (name.equals("July")) return 7;
        else if (name.equals("August")) return 8;
        else if (name.equals("September")) return 9;
        else if (name.equals("October")) return 10;
        else if (name.equals("November")) return 11;
        else if (name.equals("December")) return 12;
        else return 1;

    }

    // update the month name for a given month number
    private void updateMonthName() {

        if (getMonth() == 1) monthName = "January";
        else if (getMonth() == 2) monthName = "February";
        else if (getMonth() == 3) monthName = "March";
        else if (getMonth() == 4) monthName = "April";
        else if (getMonth() == 5) monthName = "May";
        else if (getMonth() == 6) monthName = "June";
        else if (getMonth() == 7) monthName = "July";
        else if (getMonth() == 8) monthName = "August";
        else if (getMonth() == 9) monthName = "September";
        else if (getMonth() == 10) monthName = "October";
        else if (getMonth() == 11) monthName = "November";
        else if (getMonth() == 12) monthName = "December";
    }
    // Add a method to output the month in the string format
    // followed by the year, for instance, in the form March 2005.

    public void printFormattedDate() {
        updateMonthName();
        System.out.println(monthName + " " + getYear());
    }

}

==========================================================================

public class ExDateTester {


    public static void main(String[] args) {

    //Write the definition of the methods to implement the operations for the class ExtDate and write a test program to test your program.
        ExtDate extDate = new ExtDate();

        extDate.setDate(11,19,1994);
        extDate.printFormattedDate();

        extDate.incrementDate(12);
        extDate.printFormattedDate();

        extDate.setMonthName("April");
        extDate.printFormattedDate();

        extDate.incrementDate(30);
        extDate.printFormattedDate();

        System.out.println("Month Name: "+extDate.getMonthName());
    }
}

=============================================================================


Related Solutions

public class SinglyLikedList {    private class Node{        public int item;        public...
public class SinglyLikedList {    private class Node{        public int item;        public Node next;        public Node(int item, Node next) {            this.item = item;            this.next = next;        }    }       private Node first;    public void addFirst(int a) {        first = new Node(a, first);    } } 1. Write the method add(int item, int position), which takes an item and a position, and...
Date - month: int - day: int - year: int +Date() +Date(month: int, day: int, year:...
Date - month: int - day: int - year: int +Date() +Date(month: int, day: int, year: int) +setDate(month: int, day: int, year: int): void -setDay(day: int): void -setMonth(month: int): void -setYear(year: int): void +getMonth():int +getDay():int +getYear():int +isLeapYear(): boolean +determineSeason(): string +printDate():void Create the class Constructor with no arguments sets the date to be January 1, 1900 Constructor with arguments CALLS THE SET FUNCTIONS to set the Month, then set the Year, and then set the Day - IN THAT ORDER...
class A { public: //constructors // other members private: int a; int b; }; Give declatations...
class A { public: //constructors // other members private: int a; int b; }; Give declatations of operator functions for each of the following ways to overload operator + You must state where the declatation goes, whether within the class in the public or private section or outside the class. The operator + may be overloaded. a) as friend function b) as member function c) as non-friend, non-member function
public class ProductThread { static class ProductThreads extends Thread{ private int begin, end; int[] v1, v2;...
public class ProductThread { static class ProductThreads extends Thread{ private int begin, end; int[] v1, v2; long ris; public ProductThreads(String name, int [] v1, int [] v2, int begin, int end) { setName(name); this.v1 = v1; this.v2 = v2; this.begin = begin; this.end = end; this.ris = 0; } public void run() { System.out.println("Thread " + Thread.currentThread().getName() + "[" + begin + "," + end + "] started"); ris = 1; for(int i = begin; i <= end; i++) ris...
import javax.swing.JOptionPane; public class Animal {    private int numTeeth = 0;    private boolean spots...
import javax.swing.JOptionPane; public class Animal {    private int numTeeth = 0;    private boolean spots = false;    public int weight = 0;       public Animal(int numTeeth, boolean spots, int weight){        this.numTeeth =numTeeth;        this.spots = spots;        this.weight =weight;    }       public int getNumTeeth(){        return numTeeth;    }    public void setNumTeeth(int numTeeth) {        this.numTeeth = numTeeth;    }       public boolean getSpots() {       ...
Room.java: public class Room { // fields private String roomNumber; private String buildingName; private int capacity;...
Room.java: public class Room { // fields private String roomNumber; private String buildingName; private int capacity; public Room() { this.capacity = 0; } /** * Constructor for objects of class Room * * @param rN the room number * @param bN the building name * @param c the room capacity */ public Room(String rN, String bN, int c) { setRoomNumber(rN); setBuildingName(bN); setCapacity(c); }    /** * Mutator method (setter) for room number. * * @param rN a new room number...
With the code that is being tested is: import java.util.Random; public class GVdate { private int...
With the code that is being tested is: import java.util.Random; public class GVdate { private int month; private int day; private int year; private final int MONTH = 1; private final int DAY = 9; private static Random rand = new Random(); /** * Constructor for objects of class GVDate */ public GVdate() { this.month = rand.nextInt ( MONTH) + 1; this.day = rand.nextInt ( DAY );    } public int getMonth() {return this.month; } public int getDay() {return this.day;...
public class SumMinMaxArgs { private int[] array; // You will need to write the following: //...
public class SumMinMaxArgs { private int[] array; // You will need to write the following: // // 1. A constructor that takes a reference to an array, // and initializes an instance variable with this // array reference // // 2. An instance method named sum, which will calculate // the sum of the elements in the array. If the array // is empty (contains no elements), then this will // will return 0. You will need a loop for...
Complete the required methods: public class SongList { // instance variables private Song m_last; private int...
Complete the required methods: public class SongList { // instance variables private Song m_last; private int m_numElements; // constructor // Do not make any changes to this method! public SongList() { m_last = null; m_numElements = 0; } // check whether the list is empty // Do not make any changes to this method! boolean isEmpty() { if (m_last == null) return true; else return false; } // return the size of the list (# of Song nodes) // Do...
package dealership; public abstract class Vehicle { private float dealerPrice; private int year; public Vehicle(float d,...
package dealership; public abstract class Vehicle { private float dealerPrice; private int year; public Vehicle(float d, int y) { dealerPrice = d; year = y; } public float getDealerPrice() { return dealerPrice; } public int getYear() { return year; } public abstract float getStickerPrice(); } In the space below write a concrete class Car in the dealership package that is a subclass of Vehicle class given above. You will make two constructors, both of which must call the superclass constructor....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT