In: Computer Science
The Date Class
This class will function similarly in spirit to the Date class in the text, and behaves very much like a “standard” class should. It should store a month, day, and year, in addition to providing useful functions like date reporting (toString()), date setting (constructors and getters/setters), as well as some basic error detection (for example, all month values should be between 1-12, if represented as an integer). Start this by defining a new class called “Date” or use Date.java, and implement the data items and methods listed in your “contract” below. By declaring both data and methods that will operate on this data in one file, we are “wrapping up” chunks of dedicated functionality (methods) with the data items that they operate on in one clean and simple abstraction: the Class. To test your Date class use the driver ClassDesignIIDriver.java. If you are starting from an existing Date.java, carefully read the instructions bellow to be sure that you have added all new functionality.
Class Invariants
Enforce the following “rules” that your class should never violate. By doing this you maintain the coherency of your object’s internal state – this is also known as “never invalidating the state (data) of your object”. In our Fractions homework, this would involve modifying getters/setters so as to never accept a zero denominator. Here we’ll consider some Invariants for the Date class.
Data Members
Declare a variable for month, day, and year.
Method Members
Sample Output
PLEASE GIVE IT A THUMBS UP, I SERIOUSLY NEED ONE, IF YOU
NEED ANY MODIFICATION THEN LET ME KNOW, I WILL DO IT FOR
YOU
Que- Define these as private? public?
Ans- Define these as private for acheaving encapsulation and data hiding and expose these variables only through getters and setters.
Que- What is difference between the public and private access modifiers?
Ans- private access modifier is accessible only within class
public access modifier is accessible everywhere means outside class also.
Que- When data is define as a static?
Ans- When we want to share particular data value with all objects then we can define data as a static.
Ex-If in a college there are 500 students and all are having same college name so in this case we can declare college
name as a static
class students
{
static String collegeName="abc";
int rollNo;
String studentName;
}
Que- a) can it be accessed or read?
Ans- Yes it can be accessed or read
Que- b)can it be written to?
Ans-Yes
Que-If we had decaler one static variable and 10 objects declare in RAM,how manny static variables would also be in a memory?
Ans-There will be only one satic variable which will be created at the time of class loading and common to all 10 objects.
Que-a) When data is define as a final.
Ans- Data can be define as a final when we dont want to modify its value after its first initialization and we want its constant value
throught program life cycle.
Que-b) Can it be written other than first initialization?
Ans- We can not change constant variable value once it initialized. If you are using blank final variable then you should initializ this variable in
constructor.
Que-c) Why would it be Ok to declare a final (or static final) variable as a public?
Ans- We can access it outside class also. It is final variable so no one can change its value so no need to keep it as a private.
Que- Later: How does the concept of a final variable relate to Immutable class?
Ans- When we need to prevent particular class from extending and when we dont want to change the attributes/variables values of particular class
at that time we should make the class immutable by using final keyword.
Que- Should we use month=m;or setMonth(m)? What are the differences?
Ans- In your case you are writing validation logic in setter method so we need to use setter method in constructor to validate fields.
but in normal case it will not be good practice to use setter method in constructor beacause one can override your methods and can change their behaiviour
class Date {
private int day;
private int month;
private int year;
// default constructor
public Date() {
this.day = 0;
this.month = 0;
this.year = 0;
}
// parameterize constructor
public Date(int m, int d, int y) {
setDay(d);
setMonth(m);
setYear(y);
}
//copy constructor
public Date(Date other) {
this.day = other.day;
this.month = other.month;
this.year = other.year;
}
// getters and setters
public int getDay() {
return day;
}
public void setDay(int day) {
if (day >= 1 && day <= 31) this.day = day; else System.out.println(
"Error: Day value should be between 1 to 31"
);
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
if (month >= 1 && month <= 12) this.month = month; else System.out.println(
"Error: Month value should be between 1 to 12"
);
}
public int getYear() {
return year;
}
public void setYear(int year) {
if (year >= 0) this.year = year; else System.out.println(
"Error: Year value must have positive"
);
}
@Override
public String toString() {
return month + "." + day + "." + year;
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null) return false;
if (getClass() != obj.getClass()) return false;
Date other = (Date) obj;
if (day != other.day) return false;
if (month != other.month) return false;
if (year != other.year) return false;
return true;
}
public static void main(String[] args) {
Date a = new Date();
Date b = new Date(2, 1, 2030);
Date c = new Date(2, 1, 2030);
System.out.println("a is : " + a.toString());
System.out.println("b is : " + b.toString());
System.out.println("b is : " + c.toString());
System.out.println("B and A are equal : " + b.equals(a));
System.out.println("B and C are equal : " + b.equals(c));
}
}