Question
I need sub-questions e, f, g, h.
SaffSpring 2019 Professor Question 1: Answer ALL of the following questions based on this provided int daya int montha Date i
e) Change the constructor in class Date so that it only creates valid objects according to the rule above. If the parameters
SaffSpring 2019 Professor Question 1: Answer ALL of the following questions based on this provided int daya int montha Date int dnt int y day-d year class Event ring locatson eventbat Event (5tring , Dated ocatlon- event Date-d pubiie String toStringt String detalla-Locatlons"LocatlonDatesevent Date.day return detalia eventDate-year publie statie weld mai arg Event 2ew EventCBayan" hew Date1.3,20191 Systen.out peintil The event n (11.201,as scheduled-ee . 내 1 eentDate.yeari ystem.st-printeinte yatem.ost-printcin(e2) a) What is the output of the above program? b) Add to class Date equals method which receives and object nd it returns true if day of class Date as a parameter, a month, and year of both Date objects (the current object and the object passed as a parameter). Otherwise, the method should return false. c) What will be the output after the method equals has been d) Add private access modifier to the data members of class Date, and add a setter an a getter for each of the data members according to the following rules: i. The year should be a positive number ii. The month should be between 1 and 12 iii. The day should be between and 31, and it should match the month knowing that the months (4,6, 9, 11) have 30 days only and February may have up to 29 days only
e) Change the constructor in class Date so that it only creates valid objects according to the rule above. If the parameters passed to the constructor are invalid, then the following default values should be used instead: i. Year: 2019 ii. Month: 1 iii. Day: 1 f) Add private access modifier to the data members of class Event, and add a setter an a getter for each of the data members according to the following rules: i. The location should contain at least two characters. ii. The date should be in year 2019 or after g) Change the constructor in class Event so that it only creates valid objects according to the rule above. If the parameters passed to the constructor are invalid, then the following default values should be used instead: i. Location: Mishref ii. The year of the date object should be set to 2019 while the day and month should stay the same as the object date passed as a parameter to the constructor h) When setting the data members of class Date and class Event to private, you will get several errors in class Event as well as in the main class. Correct these errors making sure that the output of the main class will still be the same.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find the required program after adding the codes for all the above questions in the program. Also find the its corresponding output:


class Date {
    private int day;
    private int month;
    private int year;

    public Date(int day, int month, int year) {
        if(day <= 29 && month == 2)
            this.day = day;
        else if(month == 4 || month == 6 || month == 9 || month == 11){
            if(day>30)
                this.day = 1;
            else
                this.day = day;
        }else {
            this.day = day;
        }
        if(month >= 1 && month <= 12)
            this.month = month;
        else
            this.month = 1;
        if(year > 0)
            this.year = year;
        else
            this.year = 2019;
    }

    public int getDay() {
        return day;
    }

    public void setDay(int day) {
        if(this.day <= 29 && this.month == 2)
            this.day = day;
        else if(this.month == 4 || this.month == 6 || this.month == 9 || this.month == 11){
            if(this.day>30)
                System.out.println("Invalid day");
            else
                this.day = day;
        }else {
            this.day = day;
        }
    }

    public int getMonth() {
        return month;
    }

    public void setMonth(int month) {
        if(month >= 1 && month <= 12)
            this.month = month;
        System.out.println("Invalid month");
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        if(year > 0)
            this.year = year;
        else
            System.out.println("Invalid year");
    }

    public boolean equals(Date that){
        return (this.day == that.day)&&
                (this.month == that.month)&&
                (this.year == that.year);
    }
}

class Event {
    private String location;
    private Date eventDate;

    public Event(String l, Date d) {
        if(l.length() >= 2)
            this.location = l;
        else
            this.location = "Mishref";

        if(d.getYear() >= 2019)
            this.eventDate = d;
        else {
            this.getEventDate().setYear(2019);
            this.getEventDate().setYear(d.getMonth());
            this.getEventDate().setYear(d.getDay());
        }
    }

    public String toString(){
        String details = "Location:" + location + ", Date: "+eventDate.getDay()+ "/" +
                eventDate.getMonth() + "/" + eventDate.getYear();
        return details;
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        if(location.length() >= 2)
            this.location = location;
        else
            System.out.println("Invalid location");
    }

    public Date getEventDate() {
        return eventDate;
    }

    public void setEventDate(Date eventDate) {
        if(eventDate.getYear() >= 2019)
            this.eventDate = eventDate;
        else
            System.out.println("Invalid date");
    }
}

class Main {
    public static void main(String[] args) {
        Event e1 = new Event("Mishref", new Date(1,1,2019));
        Event e2 = new Event("Bayan", new Date(1,1,2019));
        System.out.println("The event is " + e1.getLocation()+ " is scheduled on " +
                e1.getEventDate().getDay()+"/" + e1.getEventDate().getMonth()+
                e1.getEventDate().getYear());

        if(e1.getEventDate().equals(e2.getEventDate())){
            System.out.println(e1.getLocation() + " and " + e2.getLocation()+ "are scheduled on the same date");
        }else {
            System.out.println("The two events are not scheduled on the same date");
        }
        System.out.println(e1);
        System.out.println(e2);
    }
}

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

OUTPUT:

The event is Mishref is scheduled on 1/12019 Mishref and Bayanare scheduled on the same date Location:Mishref, Date: 1/1/2019

Add a comment
Know the answer?
Add Answer to:
SaffSpring 2019 Professor Question 1: Answer ALL of the following questions based on this provide...
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
  • C++ Programming Step 1. Design a new ADT that implements a class named Date, add 3 private member variables month, day, year along with their appropriate public constructor / getter / setter member fu...

    C++ Programming Step 1. Design a new ADT that implements a class named Date, add 3 private member variables month, day, year along with their appropriate public constructor / getter / setter member functions inside Date.h and Date.cpp. Step 2. Design another ADT that implements a class named Watch, add 3 private member variables hour, minute, second, along with their appropriate public constructor / getter / setter member functions inside Watch.h and Watch.cpp. Step 3. Next, implement the additional SmartWatch...

  • 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:...

  • You are to create a class Appointment.java that will have the following: 5 Instance variables: private...

    You are to create a class Appointment.java that will have the following: 5 Instance variables: private String month private int day private int year private int hour private int minute 1 default constructor public Appointment() 1 non-default constructor that accepts arguments for all instance variables, your constructor must call the setter methods below to set the values of the instance variables public Appointment(String monthPassed, int dayPassed, int yearPassed, int hourPassed, int minutePassed) 5 setter methods (one for each instance variable)...

  • QUESTION 5 (15 Marks) Inheritance and Polymorphism Design a Ship class that has the following members:...

    QUESTION 5 (15 Marks) Inheritance and Polymorphism Design a Ship class that has the following members: • A private String data field named name for the name of the ship. • A private String data field named yearBuilt for the year that the ship was built. • A constructor that creates a ship with the specified name and the specified year that the ship was built. • Appropriate getter and setter methods. A toString method that overrides the toString method...

  • 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...

  • Part 1. (60 pts) 1. Define an Address class in the file Address.h and implement the...

    Part 1. (60 pts) 1. Define an Address class in the file Address.h and implement the Address class in Address.cpp. a. This class should have two private data members, m_city and m_state, that are strings for storing the city name and state abbreviation for some Address b. Define and implement public getter and setter member functions for each of the two data members above. Important: since these member functions deal with objects in this case strings), ensure that your setters...

  • answer the questions throughout this program public class Day implements Comparable {    Private Boolean atWork;...

    answer the questions throughout this program public class Day implements Comparable {    Private Boolean atWork;    Private String Weather;    Private int fuNumber; } public Day ( boolean Atwork, String Weather, int funNumber) {    Atwork = Atwork;    Weather = Weather;    funNumber = funNumber; } //question 1: implement the getter and setter (mutatto of the funNumber data member. if the number is outside the range 0-10 make the fuNumebr=5; public boolean equals (Day rhs) {    //...

  • 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...

  • For this project a Date class(specifiedin the file Project_10.h) will be constructed and all function definitions will be written in the file Project_10.cpp. A makefileis provided to compile this project. The file Project_10_main.cppcontainsthe main fun

    Project_10_base_files.zipMakefile (2).txtProject_10_main 1.JPGProject_10_main 2.JPGProject_10_main 3.JPGProject_10 h.JPGProject_10 cpp.JPGAny C++ technique covered in Chapters 1 through 12is allowedexcept for global variablesYou are not allowed to use any global variables.If necessary, global constants may be used.Project 10DescriptionFor this project a Date class(specifiedin the file Project_10.h) will be constructed and all function definitions will be written in the file Project_10.cpp.  A makefileis provided to compile this project.  The file Project_10_main.cppcontainsthe main functionthat is used to run the program.On Canvas, download the files Project_10.h, Project_10_main.cpp...

  • In this hormework, you will implement a simple caledar application The implernentation shauld inc...

    Do that with C++ and please add more comment that make it understandable In this hormework, you will implement a simple caledar application The implernentation shauld include a class narned Calendar, an abstract class named Event and two concrete classes narmed Task and Appointment which irherit from the Evernt class. The Event Class This will be an abstract class. It will have four private integer members called year, month, day and hour which desigate the tirne of the event. It...

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