Date class,Java programming: In this program write a single source file that con
ID: 3571378 • Letter: D
Question
Date class,Java programming:
In this program write a single source file that contain 2 classes: one class will contain you main and the other class will be named Date. A Date class object will contain the following:
A String describing the weather on that date,e.g. "humid".
An int giving the year, which is supposed to be in the range [2000,2099].
An int giving the month, which is supposed to be in the range [1,12](for January through December)
An int giving the day of the month. For this program, assume every month always has exactly 30 days.So, the value of the day of the month should be in the range [1,30]. As usual, data members should be private.
Your class should have the following: w, y, m, and d give the weather, year, month, and day. Any weather is fine, but make sure the year, month, and day are all in the above described rranges.
public Date(String w, int y, int m, int d) Date class should have assessors & mutators for each data member. Your mutators for year, month, and day should make sure they're in allowed range.
public String getWeather()
Public int getYear()
Public int getMonth()
Public int getDay()
public void setWeather(String w)
public void setYear(int y)
public void setMonth(int m)
public void setDay(int d)
The compare method also lives in the Date class.It chronologically compares d1 and d2: if d1 comes before d2, compare returns- 1;if d1 comes after d2, compare returns 1 ; if d1 and d2 represent the same day, compare returns 0.
public static int compare(Date d1, Date d2)
Your main class(that is, the class that contains your main method) should have a method named earliest. earliest may assume without checking that dates contains at least one Date object and that all the Date objects in dates represent different dates. earliest's job is to use the compare method to return a reference to the earliest Date in the array. earliest doesn't have to make a copy of the earliest Date, it just returns a reference to the Date object in the array.
public static Date earliest(Date[] dates)
(with sample run of the program please)
Explanation / Answer
class Date{
private String weather;// private field for weather
private int year;// private field for weather
private int month;// private field for weather
private int day;// private field for weather
//Constructor to set the values
public Date(String w, int y, int m, int d){
setWeather(w);
setYear(y);
setMonth(m);
setDay(d);
}
//accesssor for getting the weather
public String getWeather() {
return weather;
}
//mutators for setting the weather
public void setWeather(String weather) {
this.weather = weather;
}
//accesssor for getting the year
public int getYear() {
return year;
}
//mutators for setting the year
public void setYear(int year) {
if(year>=2000 && year<=2099)
this.year = year;
else
throw new IllegalArgumentException("Year should be in range [2000-2099]");
}
//accesssor for getting the month
public int getMonth() {
return month;
}
//mutators for setting the month
public void setMonth(int month) {
if(month>=1 && month<=12)
this.month = month;
else
throw new IllegalArgumentException("month should be in range [1-12]");
}
//accesssor for getting the day
public int getDay() {
return day;
}
//mutators for setting the month
public void setDay(int day) {
if(day>=1 && day<=30)
this.day = day;
else
throw new IllegalArgumentException("day should be in range [1-30]");
}
//method to compare 2 dates object. It chronologically compares d1 and d2: if d1 comes before d2, compare returns- 1;
//if d1 comes after d2, compare returns 1 ;
//if d1 and d2 represent the same day, compare returns 0.
public static int compare(Date d1, Date d2){
if(d1.getYear()<d2.getYear()){
return -1;
}else if(d1.getYear()>d2.getYear()){
return 1;
}else{
if(d1.getMonth()<d2.getMonth()){
return -1;
}else if(d1.getMonth()>d2.getMonth()){
return 1;
}else{
if(d1.getDay()<d2.getDay()){
return -1;
}else if(d1.getDay()>d2.getDay()){
return 1;
}else{
return 0;
}
}
}
}
//overriding the toString method to return the format of the current date object. This is used for pretty printing purpose only
@Override
public String toString() {
// TODO Auto-generated method stub
return "Weather: "+this.getWeather()+" Year: "+this.getYear()+" Month: "+this.getMonth()+" Day: "+this.getDay()+" ";
}
}
public class DateTest {
/**
* @param args
*/
public static void main(String[] args) {
Date dates[]= new Date[3];
dates[0]= new Date("Sunny", 2012, 3, 1);
dates[1]= new Date("Rainny", 2004, 11, 15);
dates[2]= new Date("Winter", 2004, 6, 1);
Date earliestDate=earliest(dates);
System.out.println("Earliest date is: "+earliestDate.toString());
}
//earliest mehtod which return the earliest date reference back;
public static Date earliest(Date[] dates) {
Date earliestDate=dates[0];//setting the first as earliest date
for(int i=1;i<dates.length;i++)
{
//check if date[i] is coming before earliestDate
if(Date.compare(earliestDate,dates[i])==1){
earliestDate=dates[i];
}
}
return earliestDate;
}
}
-----out--------
Earliest date is: Weather: Winter Year: 2004 Month: 6 Day: 1
-----output-----
Note: please note that we have hardocoded the valu through each constructor. Please let us know if you want a interactive program to get the values for weather, year, month and day
Feel free to ask doubts/Queries. God bless you!
class Date{
private String weather;// private field for weather
private int year;// private field for weather
private int month;// private field for weather
private int day;// private field for weather
//Constructor to set the values
public Date(String w, int y, int m, int d){
setWeather(w);
setYear(y);
setMonth(m);
setDay(d);
}
//accesssor for getting the weather
public String getWeather() {
return weather;
}
//mutators for setting the weather
public void setWeather(String weather) {
this.weather = weather;
}
//accesssor for getting the year
public int getYear() {
return year;
}
//mutators for setting the year
public void setYear(int year) {
if(year>=2000 && year<=2099)
this.year = year;
else
throw new IllegalArgumentException("Year should be in range [2000-2099]");
}
//accesssor for getting the month
public int getMonth() {
return month;
}
//mutators for setting the month
public void setMonth(int month) {
if(month>=1 && month<=12)
this.month = month;
else
throw new IllegalArgumentException("month should be in range [1-12]");
}
//accesssor for getting the day
public int getDay() {
return day;
}
//mutators for setting the month
public void setDay(int day) {
if(day>=1 && day<=30)
this.day = day;
else
throw new IllegalArgumentException("day should be in range [1-30]");
}
//method to compare 2 dates object. It chronologically compares d1 and d2: if d1 comes before d2, compare returns- 1;
//if d1 comes after d2, compare returns 1 ;
//if d1 and d2 represent the same day, compare returns 0.
public static int compare(Date d1, Date d2){
if(d1.getYear()<d2.getYear()){
return -1;
}else if(d1.getYear()>d2.getYear()){
return 1;
}else{
if(d1.getMonth()<d2.getMonth()){
return -1;
}else if(d1.getMonth()>d2.getMonth()){
return 1;
}else{
if(d1.getDay()<d2.getDay()){
return -1;
}else if(d1.getDay()>d2.getDay()){
return 1;
}else{
return 0;
}
}
}
}
//overriding the toString method to return the format of the current date object. This is used for pretty printing purpose only
@Override
public String toString() {
// TODO Auto-generated method stub
return "Weather: "+this.getWeather()+" Year: "+this.getYear()+" Month: "+this.getMonth()+" Day: "+this.getDay()+" ";
}
}
public class DateTest {
/**
* @param args
*/
public static void main(String[] args) {
Date dates[]= new Date[3];
dates[0]= new Date("Sunny", 2012, 3, 1);
dates[1]= new Date("Rainny", 2004, 11, 15);
dates[2]= new Date("Winter", 2004, 6, 1);
Date earliestDate=earliest(dates);
System.out.println("Earliest date is: "+earliestDate.toString());
}
//earliest mehtod which return the earliest date reference back;
public static Date earliest(Date[] dates) {
Date earliestDate=dates[0];//setting the first as earliest date
for(int i=1;i<dates.length;i++)
{
//check if date[i] is coming before earliestDate
if(Date.compare(earliestDate,dates[i])==1){
earliestDate=dates[i];
}
}
return earliestDate;
}
}
-----out--------
Earliest date is: Weather: Winter Year: 2004 Month: 6 Day: 1
-----output-----
Note: please note that we have hardocoded the valu through each constructor. Please let us know if you want a interactive program to get the values for weather, year, month and day
Feel free to ask doubts/Queries. God bless you!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.