Question

In: Computer Science

Inheritance Constructors Accessibility Accessibility Rules private         accessible within the same class. unspecified (friendly) (default)         additionally...

Inheritance

Constructors

Accessibility

Accessibility Rules

private

        accessible within the same class.

unspecified (friendly) (default)

        additionally accessible to classes within the same package (folder).

protected

        additionally accessible to child classes.

public

        accessible to all classes.

Description

This assignment has two parts. Do the part I first and test it using the data in test run 1. Then do the second part and test it using data in test run 2 and test run 3. Submit the output from test run 1, 2, 3 and source code from both part I and II.

Part I.

Write a program that would determine the day number in a non-leap year. For example, in a non-leap year, the day number for Dec 31 is 365; for Jan 1 is 1, and for February 1 is 32. This program will ask the user to input day and month values. Then it will display the day number day number corresponding to the day and month values entered assuming a non-leap year. (See part II to this exercise below).

Optional:

Optionally provide the date validation code in the main method. The validation code will verify that the values entered by the user for the day and month are valid.

Implementation

For this exercise, create the following classes:

class Day

Create a public class Day that provides the following:

(This class keeps day and month value. It does not deal with a year value).

·       A private variable for holding day value.

·       A protected variable for holding month value.

·       A two-parameter public constructor to initialize day and month values

·       A public accessor methods getDay ( ) that returns day value.

·       A public accessor method getMonth ( ) that returns month value.

·       A public method findDayNum ( ) that calculates the day number corresponding to the day and the month values stored in the object. It calculates the day number assuming that it is a non-leap year.

class TestDay

Create a class TestDay containing the main ( ) method. In the main( ) method, do the following:

·       Prompt the user for entering day and month values.

·       Create an object of class Day and pass its constructor the day and month values entered by the user.

·       Call the method findDayNum( ) of Day object to obtain day number. (This does not take into account the leap year).

·       Display the original date and the day number for that date assuming that it is a non-leap year.

Testing:

Run the program use the following data:

Test Run 1:

Input:

Enter day

10

Enter month

3

Output:

Day Number for 3/10 is 69

PART II

Description

Enhance the above exercise so that it can determine the day number for both leap and non-leap years. This program will ask the user to enter the day, month, and year. Then it will display the day number corresponding to the date entered taking into account the leap year.

Implementation

Create the following two additional classes:

class LeapDay       

Create a LeapDay that extends the class Day created in part I and additionally provides the following:

·       A private variable year to hold the year value.

·       A public constructor with three parameters to initialize day, month and year values.

This constructor initializes the day and month values by calling the parent constructor.

·       A public accessor method getYear ( ) that returns the year value.

·       A public method findDayNum ( ) which over-rides the same method in the parent class and calculates the day number of the date stored in the object. It does the following:

1.     It accesses the day variable by calling the method getDay. Since day is defined private in the parent class, it cannot be accessed directly from the child class.

2.     It accesses the month variable directly. Since month is defined protected in the parent class, it can be accessed directly from the child class.

3.     It calls the parent method findDayNum and passes it day and month values from above. It receives the day number returned from the call calculated as if it is non-leap year.

4.     On return from the call to parent findDayNum above, it adds one to the day number received if it’s a leap year and the month is greater than 2

5.     It returns the day number to the caller of this method.

class TestLeapDay

Create a class TestLeapDay containing the main ( ) method. In the main( ) method, do the following:

·       Prompt the user to enter day, month and year values.

·       Create an object of class LeapDay and pass its constructor the day and month values entered by the user.

·       Call the method findDayNum( ) of LeapDay object to obtain the day number. (This method takes into account the leap year).

·       Display the original date and the day number for that date. For displaying the original date, call the object’s accessor methods.

Solutions

Expert Solution

please refer to below attached photos for clear understanding and implmentation of the program :

code :

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

class Day{
    private int day;
    protected int month;
    
    public Day(int day , int month){
        this.day = day;
        this.month = month;
    }
    
    public int getDay(){
        return day;
    }
    
    public int getMonth(){
        return month;
    }
    
    public int findDayNum(){
        int current_days = 0;
        for(int m=1;m<month;m++){
            if(m%2 == 0){
                if(m == 2){
                    current_days += 28;
                }
                else{
                    current_days += 30;
                }
            }
            else{
                current_days += 31;
            }
        }
        return current_days + day;
    }
}

class LeapDay extends Day{
    private int year;
    public LeapDay(int day , int month , int year){
        super(day , month);
        this.year = year;
    }
    
    public int getYear(){
        return year;
    }
    
    public int findDayNum(){
        int day_number = super.findDayNum();
        if((year % 4 == 0 || year % 400 == 0) && year % 100 != 0){
            if(month > 2){
                day_number += 1;
            }
        }
        return day_number;
    }
}

public class Main{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        System.out.print("Enter day : ");
        int d = in.nextInt();
        System.out.print("Enter month : ");
        int m = in.nextInt();
        Day day = new Day(d ,m);
        System.out.println("Day Number for "+day.getDay()+"/"+day.getMonth()+" is "+day.findDayNum());
        
        System.out.println("****************************");
        System.out.print("Enter day : ");
        int ld = in.nextInt();
        System.out.print("Enter month : ");
        int lm = in.nextInt();
        System.out.print("Enter year : ");
        int ly = in.nextInt();
        LeapDay lday = new LeapDay(ld ,lm, ly);
        System.out.println("Day Number for "+lday.getDay()+"/"+lday.getMonth()+" is "+lday.findDayNum());
        
    }
}

Related Solutions

Code a simple Editor class with 2 constructors (one default, one with a string for a...
Code a simple Editor class with 2 constructors (one default, one with a string for a file name) The Editor class in private has an instance of a LinkedList (of strings) and the position of the user (use our Point class) create a short file - at least 5 lines - could be a program main: string fileName("input.txt"); Editor miniVi (fileName); miniVi.displayLines(); C++ explain in detail plz Thank you
Define the exception class called TornadoException. The class should have two constructors including one default constructor....
Define the exception class called TornadoException. The class should have two constructors including one default constructor. If the exception is thrown with the default constructor, the method getMessage should return "Tornado: Take cover immediately!" The other constructor has a single parameter, m, of int type. If the exception is thrown with this constructor, the getMessage should return "Tornado: m miles away; and approaching!" Write a Java program to test the class TornadoException.
The Person Class Uses encapsulation Attributes private String name private Address address Constructors one constructor with...
The Person Class Uses encapsulation Attributes private String name private Address address Constructors one constructor with no input parameters since it doesn't receive any input values, you need to use the default values below: name - "John Doe" address - use the default constructor of Address one constructor with all (two) parameters one input parameter for each attribute Methods public String toString() returns this object as a String, i.e., make each attribute a String, concatenate all strings and return as...
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
This class has two constructors. The default constructor (the one that takes no arguments) should initialize the first and last names to "None", the seller ID to "ZZZ999", and the sales total to 0.
For this assignment, implement and use the methods for a class called Seller that represents information about a salesperson.The Seller classUse the following class definition:class Seller { public:   Seller();   Seller( const char [], const char[], const char [], double );        void print();   void setFirstName( const char [] );   void setLastName( const char [] );   void setID( const char [] );   void setSalesTotal( double );   double getSalesTotal(); private:   char firstName[20];   char lastName[30];   char ID[7];   double salesTotal; };Data MembersThe data members for the class are:firstName holds the Seller's first namelastName holds the Seller's last nameID holds the Seller's id numbersalesTotal holds the Seller's sales totalConstructorsThis class has two constructors. The default constructor (the one that takes...
Describe the rules governing identifiers. Can identifiers be re-used in the same Java class file? Provide...
Describe the rules governing identifiers. Can identifiers be re-used in the same Java class file? Provide three examples of different kinds of invalid identifiers. Java
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT