Question with extending a date class in Java

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

33 Replies - 1626 Views - Last Post: 31 March 2013 - 07:04 PM Rate Topic: -----

#1 NewGirlXtina  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 17-February 13

Question with extending a date class in Java

Posted 29 March 2013 - 04:25 PM

I need to work on a project for class that extends a date class. My problem right off the bat is I do not understand what I am doing wrong in the very first constructor. I get an error saying that there is no return for this method. I have been trying to find help on-line and reading my book, but I am not understanding this error. In the examples I see that a bicycle class can be extended by another class named BMXBicyle and that the super class(Bicycle) may have a seat, pedals, and tires but the BMXBicycle may also need a handlebar. But in this project I am not sure what to do because I am not really adding a new item.... Any help and advice would be appreciated. Thank you.

package Final;

public class ExtenedDate extends Date {

	//  Default constructor
	//  Data members are set according to super defaults
	
	//  Constructor that accepts parameters
	public ExtendedDate (int month, int day, int year)  {
		super.getDay();
		super.getMonth();
		super.getYear();
		
	}
	
	//  Method to set the date - override inherited setDate method
	//  If data is invalid, do not change the reciever
	public void setDate (int monthInt, int dayInt, int yearInt)  {
		
	}
	
	//  Method to set month ensuring that only valid changes are made
	public void setMonth (int anInt)  {
		
	}
	
	//  Method to set the day ensuring that only valid changes are made
	public void setDay (int anInt)  {
		
	}
	
	//  Method to set the year ensuring that only valid changes are made
	public void setYear (int anInt)  {
		
	}
	
	//  New method to verify date
	//  Valid range is 1-1-1900 through 12-31-2999
	public static boolean isValidDate (int month, int day, int year)  {
		return true;
	}
	
	//  Method to test for leap year
	public static boolean isLeapYear (int aYear)  {
		return true;
	}
	
	//  Instance method of the above
	public boolean  isLeapYear()  {
		return true;
	}
	
	//  Return number of days in a month
	public int daysInMonth ()  {
		return 0;
	}
	
	//  Return number of days passed in year including current day
	public int daysToDate()  {
		return 0;
	}
	
	//  Return number of days remaining in year
	public int daysRemainingInYear ()  {
		return 0;
	}
	
	//  Create a new ExtendedDate object with the values that result from adding a number of days to an existing ExtendedDate
	//  object.  Cannot result in an invalid ExtendedDate being created.  If an invalid date would be created, return a 
	//  ExtendedDate with default values.
	public ExtendedDate futureDate (int inDay)  {
		return new ExtendedDate();
	}
		

	}



package Final;

public class Date {

	private int month; //instance variable for value of the date's month
	private int day; //instance variable for value of the date's day
	private int year; //instance variable for the value of the dates
	
	//Default constructor: set the instance variable to default values: month = 1; day = 1; year = 1900;
	public Date()  {
		month = 1;
		day = 1;
		year = 1900;
	}
	
	//Constructor to set the date
	//The instance variables month, day, and year are set according to received parameters.
	public Date (int month, int day, int year)  {
		this.month = month;
		this.day = day;
		this.year = year;
	}
	
	//Method to set the date
	// The instance variables month, day, and year are set according to received parameters.
	public void setDate(int month, int day, int year)  {
		this.month = month;
		this.day = day;
		this.year = year;
	}
	
	// Method to return the date's month value
	public int getMonth()  {
		return month;
	}
	
	//  Method to return the date's day value
	public int getDay()  {
		return day;
	}
	
	//  Method to return the date's year value
	public int getYear()  {
		return year;
	}
	
	//Override of inherited toString() method to return the date in the form of mm-dd-yyy
	public String toString() {
		return (month + "-" + day + "-" + year);
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Question with extending a date class in Java

#2 Flukeshot  Icon User is offline

  • A little too OCD
  • member icon

Reputation: 415
  • View blog
  • Posts: 1,030
  • Joined: 14-November 12

Re: Question with extending a date class in Java

Posted 29 March 2013 - 04:40 PM

public class ExtenedDate extends Date {
//                ^ was this a typo? :)/>/>



Please post the error message you got, sleepy me can't find fault with this program so far. (Assuming it is under construction)
Was This Post Helpful? 1
  • +
  • -

#3 NewGirlXtina  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 17-February 13

Re: Question with extending a date class in Java

Posted 29 March 2013 - 04:45 PM

View PostFlukeshot, on 29 March 2013 - 06:40 PM, said:

public class ExtenedDate extends Date {
//                ^ was this a typo? :)/>/>/>



Please post the error message you got, sleepy me can't find fault with this program so far. (Assuming it is under construction)



Yes! Oh man...I did spell that wrong. Now the error is gone. Geeezzz. Okay, so I think I am good for now. Now that I know I am not a complete idiot anyway! Thanks again! :wheelchair:
Was This Post Helpful? 0
  • +
  • -

#4 Flukeshot  Icon User is offline

  • A little too OCD
  • member icon

Reputation: 415
  • View blog
  • Posts: 1,030
  • Joined: 14-November 12

Re: Question with extending a date class in Java

Posted 29 March 2013 - 04:46 PM

You're welcome :)
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Question with extending a date class in Java

Posted 29 March 2013 - 05:57 PM

???

	//  Constructor that accepts parameters
	public ExtendedDate (int month, int day, int year)  {
		super.getDay();
		super.getMonth();
		super.getYear();
		
	}


in your constructor that calls its super class Date what is the use of calling
getDay();
getMonth();
getYear();
if you actually do nothing with those returned value ?

And as you class does not have methods named getDay(), getMonth() and getYear() you don't need the this. keyword before the call

So finally, as you ExtendedDate constructor does absolutly nothing, what do you need it ?
Just have you class with its methods without a useless constructor

And by the way the constructor Date(int year, int month, int day) is deprecated since years. You shouldn't use it
Was This Post Helpful? 1
  • +
  • -

#6 NewGirlXtina  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 17-February 13

Re: Question with extending a date class in Java

Posted 30 March 2013 - 12:16 PM

View Postpbl, on 29 March 2013 - 07:57 PM, said:

???

	//  Constructor that accepts parameters
	public ExtendedDate (int month, int day, int year)  {
		super.getDay();
		super.getMonth();
		super.getYear();
		
	}


in your constructor that calls its super class Date what is the use of calling
getDay();
getMonth();
getYear();
if you actually do nothing with those returned value ?

And as you class does not have methods named getDay(), getMonth() and getYear() you don't need the this. keyword before the call

So finally, as you ExtendedDate constructor does absolutly nothing, what do you need it ?
Just have you class with its methods without a useless constructor

And by the way the constructor Date(int year, int month, int day) is deprecated since years. You shouldn't use it


Hello, thanks for your comment. All of the methods and constructors are mandatory for this exercise. I don't know why they were chosen. I think it is because I am a student and they want us to be able to do these things so we learn Java. There is also a class that is provided that is supposed to run the application so a user will enter the day, month, and year and this extended date class is supposed to validate data, I think, or that is what I understand it to be.
Was This Post Helpful? 0
  • +
  • -

#7 Flukeshot  Icon User is offline

  • A little too OCD
  • member icon

Reputation: 415
  • View blog
  • Posts: 1,030
  • Joined: 14-November 12

Re: Question with extending a date class in Java

Posted 30 March 2013 - 12:21 PM

My assumption was that you'll develop this code over the course of a few lessons. A lot of that happened during my OCJP course. But pbl is right (no surprises :P). Question your tutor if this isn't the case, because the program wastes time and resources(granted, it's probably a nominal amount) if it calls a method and then does nothing with the information collected.
Was This Post Helpful? 0
  • +
  • -

#8 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1371
  • View blog
  • Posts: 3,022
  • Joined: 05-April 11

Re: Question with extending a date class in Java

Posted 30 March 2013 - 12:35 PM

NewGirlXtina you will want the ExtendedDate's constructor to call the constructor of the super class
public ExtendedDate (int month, int day, int year)  {
    //The call to another constructor must be the first statement
    //The constructor of the Date class will be called and used
    super(month, day, year);


Was This Post Helpful? 1
  • +
  • -

#9 NewGirlXtina  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 17-February 13

Re: Question with extending a date class in Java

Posted 30 March 2013 - 01:00 PM

View PostCasiOo, on 30 March 2013 - 02:35 PM, said:

NewGirlXtina you will want the ExtendedDate's constructor to call the constructor of the super class
public ExtendedDate (int month, int day, int year)  {
    //The call to another constructor must be the first statement
    //The constructor of the Date class will be called and used
    super(month, day, year);



Thank you for your reply! :) Ok, so here is what I have so far. A lot of errors, but it is something I can work my way through. So now my question is about the leap year. The instructor wants us to write a method to test for a leap year and return true and then write a instance method version of the test method with return of true. I am confused on what a method to test and an instance method is. Am I writing this portion of the code properly? Also, when I put in getYear() the error is cannot make a static reference to a non static getyear....I am so confused on this.

Here is my ExtendedDate class in progress so far:
import java.util.Calendar;
import java.util.GregorianCalendar;

public class ExtendedDate extends Date {

	//  Default constructor
	//  Data members are set according to super defaults

	//  Constructor that accepts parameters
	public ExtendedDate (int month, int day, int year)  {
		super.getDay();
		super.getMonth();
		super.getYear();
		super.setDate(month, day, year);
	}
	
	//  Method to set the date - override inherited setDate method
	//  If data is invalid, do not change the reciever
	public void setDate (int monthInt, int dayInt, int yearInt)  {
		super.setDate(getMonth(), getDay(), getYear());
	}
	
	//  Method to set month ensuring that only valid changes are made
	public void setMonth (int anInt)  {
		super.getMonth();
	}
	
	//  Method to set the day ensuring that only valid changes are made
	public void setDay (int anInt)  {
		super.getDay();
	}
	
	//  Method to set the year ensuring that only valid changes are made
	public void setYear (int anInt)  {
		super.getYear();
	}
	
	//  New method to verify date
	//  Valid range is 1-1-1900 through 12-31-2999
	public static boolean isValidDate (int month, int day, int year)  {
		if (1900 < year || year < 2999)
            return false;
        if (month < 1 || month > 12 )
            return false;
        if (day < 1 || day > daysInMonth[month])
            return false;
        if (month == 2 && day == 29 && !isLeapYear(year))
            return false;
            return true;
	}
	
	//  Method to test for leap year
	public static boolean isLeapYear (int aYear)  {
		if(getYear() %4==0 && (getYear() %100 != 0 || year %400 == 0)){
		      return true;
		}
	 }
		    
	//  Instance method of the above
	public boolean  isLeapYear()  {
		Calendar cal = Calendar.getInstance();
		((GregorianCalendar) cal).isLeapYear(cal.get(GregorianCalendar.YEAR));
		return true;
	}
	
	//  Return number of days in a month
	public int daysInMonth ()  {
		if (getMonth() >= 1 && getMonth() <= 12)
            if (getMonth() != 2)
                return daysInMonth[getMonth()];
            else if (isLeapYear(getYear()))
                return 29;
            else
                return 28;
        	else
        		return 0;
	}
	
	//  Return number of days passed in year including current day
	public int daysToDate()  {
		Calendar cal1 = new GregorianCalendar();
        Calendar cal2 = new GregorianCalendar();
        cal1.set([U]dYear, dMonth, dDay[/U]);
        cal2.set([U]dYear[/U], 1, 1);
 
        Date d1 = [U]cal1.getTime()[/U];
        Date d2 = [U]cal2.getTime()[/U];
 
        System.out.println("Days passed in a year to a date "
                + ((d1.[U]getTime()[/U] - d2.[U]getTime()[/U]) / (1000 * 60 * 60 * 24)));
 
		return 0;
	}
	
	//  Return number of days remaining in year
	public int daysRemainingInYear ()  {
		Calendar cal1 = new GregorianCalendar();
        Calendar cal2 = new GregorianCalendar();
        cal1.set([U]dYear, dMonth, dDay[/U]);
        cal2.set[U](dYear[/U], 12, 31);
 
        Date d1 = [U]cal1.getTime()[/U];
        Date d2 = [U]cal2.getTime()[/U];
 
        System.out.println("Days remaining in a year "
                + ((d2.[U]getTime([/U]) - d1.[U]getTime()[/U]) / (1000 * 60 * 60 * 24)));
		return 0;
	}
	
	//  Create a new ExtendedDate object with the values that result from adding a number of days to an existing ExtendedDate
	//  object.  Cannot result in an invalid ExtendedDate being created.  If an invalid date would be created, return a 
	//  ExtendedDate with default values.
	public ExtendedDate futureDate (int inDay)  {
		//if (dYear< 1900 || dYear > 2999 )
        //return this.setDate; System.out.println("Date "+ setDate);
    Calendar c1 = Calendar.getInstance();
    c1.set([U]dYear, dMonth, dDay[/U]);
    c1.add(Calendar.DATE, days);
    if (isGodDate(c1.get(Calendar.MONTH), c1.get(Calendar.DAY_OF_MONTH),
            c1.get(Calendar.YEAR))) {
        [U]dYear[/U] = c1.get(Calendar.YEAR);
         
        [U]dMonth[/U] = c1.get(Calendar.MONTH);
        [U]dDay [/U]= c1.get(Calendar.DAY_OF_MONTH);
    }
 
    return [U]dMonth[/U] + "/" + [U]dDay[/U] + "/" + [U]dYear[/U];
		return new ExtendedDate();
	}
		

	}



:bananaman: While this can be very frustrating for a student to learn Java, it sure can be a lot of fun when it comes together :)
Was This Post Helpful? 0
  • +
  • -

#10 NewGirlXtina  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 17-February 13

Re: Question with extending a date class in Java

Posted 30 March 2013 - 01:06 PM

View PostFlukeshot, on 30 March 2013 - 02:21 PM, said:

My assumption was that you'll develop this code over the course of a few lessons. A lot of that happened during my OCJP course. But pbl is right (no surprises :P/>). Question your tutor if this isn't the case, because the program wastes time and resources(granted, it's probably a nominal amount) if it calls a method and then does nothing with the information collected.


I am used to people asking me why I am asking questions on what appears to be a waste of code or time on these things. I don't know if it is the people who put the classes together or the people who teach them, but it always seems off a bit from what is currently taking place in technology. The instructor said that he wants us to use what we have been learning in our books and any other resources to accomplish this task. Everyone also needs to write a test app and share it with the others in order for us to do that we need to have everything named the same so I think that might be another reason he has use writing methods in this fashion. Anyway, I knew nothing about Java when I started and I can now at least understand some code and even write some code :) :w00t: :w00t:
Was This Post Helpful? 0
  • +
  • -

#11 NewGirlXtina  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 17-February 13

Re: Question with extending a date class in Java

Posted 30 March 2013 - 01:12 PM

View Postpbl, on 29 March 2013 - 07:57 PM, said:

???

	//  Constructor that accepts parameters
	public ExtendedDate (int month, int day, int year)  {
		super.getDay();
		super.getMonth();
		super.getYear();
		
	}


in your constructor that calls its super class Date what is the use of calling
getDay();
getMonth();
getYear();
if you actually do nothing with those returned value ?

And as you class does not have methods named getDay(), getMonth() and getYear() you don't need the this. keyword before the call

So finally, as you ExtendedDate constructor does absolutly nothing, what do you need it ?
Just have you class with its methods without a useless constructor

And by the way the constructor Date(int year, int month, int day) is deprecated since years. You shouldn't use it


I re-read this again. I am supposed to do something with the dates from the date class and extend the class using the ExtendedDate class to verify that the dates are valid and do things like leap year and days in month...so I am wondering if by your comment you mean that I have started to write the code wrong. Am I supposed to write code for getting the day, month, year from the date class and then setting them? Wow, I am really struggling with this lesson...
Was This Post Helpful? 0
  • +
  • -

#12 Flukeshot  Icon User is offline

  • A little too OCD
  • member icon

Reputation: 415
  • View blog
  • Posts: 1,030
  • Joined: 14-November 12

Re: Question with extending a date class in Java

Posted 30 March 2013 - 01:58 PM

Well the constructor in it's current state will obtain 3 pieces of information and then forget they exists.

You can, in your constructor, have an assignment (variable = pieceOfInformation) which will then record the data you recieved in this instance (if it is non-static) or this class (if it is static). You appear to have written code to call the superclass(Date) though, so casiOo's example is a good way to do this.
Was This Post Helpful? 0
  • +
  • -

#13 NewGirlXtina  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 17-February 13

Re: Question with extending a date class in Java

Posted 30 March 2013 - 04:16 PM

View PostFlukeshot, on 30 March 2013 - 03:58 PM, said:

Well the constructor in it's current state will obtain 3 pieces of information and then forget they exists.

You can, in your constructor, have an assignment (variable = pieceOfInformation) which will then record the data you recieved in this instance (if it is non-static) or this class (if it is static). You appear to have written code to call the superclass(Date) though, so casiOo's example is a good way to do this.


Man, I need to have this done by Sunday just before midnight and I am just pulling my hair out. Please help if you can. Now I am stuck with everything beginning with the methods for leap year and down. I am just stuck and running out of time and my book has been little help. In the book it says that I can take a Gregorian time and do code that reads Date StartDate = gregStartDate.getTime(); and the error is cannot convert from java util date to final date or that gregStartDate cannot be resolved even though the code is right out of the book....Sorry, very stressed right now. Also, I don't think I am really getting any dates at all.... Here is all classes and a tester class. I've been working on this all day and you can see I am not doing very well.
package Final;

import java.util.Calendar;
import java.util.GregorianCalendar;

public class ExtendedDate extends Date {

	//  Default constructor
	//  Data members are set according to super defaults
	private static final int[] daysInMonth = { 0, 31, 29, 31, 30, 31, 30, 31,
        31, 30, 31, 30, 31 };
     
   	//  Constructor that accepts parameters
	public ExtendedDate (int month, int day, int year)  {
		super(month, day, year);
	}
	
	//  Method to set the date - override inherited setDate method
	//  If data is invalid, do not change the reciever
	public void setDate (int monthInt, int dayInt, int yearInt)  {
		super.setDate(getMonth(), getDay(), getYear());
	}
	
	//  Method to set month ensuring that only valid changes are made
	public void setMonth (int anInt)  {
		super.getMonth();
	}
	
	//  Method to set the day ensuring that only valid changes are made
	public void setDay (int anInt)  {
		super.getDay();
	}
	
	//  Method to set the year ensuring that only valid changes are made
	public void setYear (int anInt)  {
		super.getYear();
	}
	
	//  New method to verify date
	//  Valid range is 1-1-1900 through 12-31-2999
	public static boolean isValidDate (int month, int day, int year)  {
		if (1900 < year || year < 2999)
            return false;
        if (month < 1 || month > 12 )
            return false;
        if (day < 1 || day > daysInMonth[month])
            return false;
        if (month == 2 && day == 29 && !isLeapYear(year))
            return false;
            return true;
	}
	
	//  Method to test for leap year
	public static boolean isLeapYear (int aYear)  {
		 {
			 if (year % 4 != 0) {
				    return false;
				  } else if (year % 400 == 0) {
				    return true;
				  } else if (year % 100 == 0) {
				    return false;
				  } else {
				    return true;
				  }
		 }
	}    
	//  Instance method of the above
	public boolean  isLeapYear()  {
		 return ExtendedDate.isLeapYear(getYear());
		return true;
		
		Calendar cal = Calendar.getInstance();
		  cal.set(Calendar.YEAR, year);
		  return cal.getActualMaximum(DAY_OF_YEAR) > 365;
		}
	}
	
	//  Return number of days in a month
	public int daysInMonth ()  {
		if (getMonth() >= 1 && getMonth() <= 12)
            if (getMonth() != 2)
                return daysInMonth[getMonth()];
            else if (isLeapYear(getYear()))
                return 29;
            else
                return 28;
        	else
        		return 0;
	}
	
	//  Return number of days passed in year including current day
	public int daysToDate()  {
		GregorianCalendar gregEndDate = new GregorianCalendar();
		Date startDate = gregStartDate.getTime();
		Date endDate = gregEndDate.getTime();
		long startDateMS = startDate.getTime();
		long endDateMS = endDate.getTime();
		long elapsedMS = endDateMS - startDateMS;
		long elaspedDays = elapsedMS / (24 * 60 * 60 * 1000);
        return 0;
	}
	
	//  Return number of days remaining in year
	public int daysRemainingInYear ()  {
		Calendar cal1 = new GregorianCalendar();
        Calendar cal2 = new GregorianCalendar();
        cal1.set([U]dYear, dMonth, dDay[/U]);
        cal2.set[U](dYear[/U], 12, 31);
 
        Date d1 = [U]cal1.getTime()[/U];
        Date d2 = [U]cal2.getTime()[/U];
 
        System.out.println("Days remaining in a year "
                + ((d2.[U]getTime([/U]) - d1.[U]getTime()[/U]) / (1000 * 60 * 60 * 24)));
		return 0;
	}
	
	//  Create a new ExtendedDate object with the values that result from adding a number of days to an existing ExtendedDate
	//  object.  Cannot result in an invalid ExtendedDate being created.  If an invalid date would be created, return a 
	//  ExtendedDate with default values.
	public ExtendedDate futureDate (int inDay)  {
		//if (dYear< 1900 || dYear > 2999 )
        //return this.setDate; System.out.println("Date "+ setDate);
    Calendar c1 = Calendar.getInstance();
    c1.set([U]dYear, dMonth, dDay[/U]);
    c1.add(Calendar.DATE, days);
    if (isGodDate(c1.get(Calendar.MONTH), c1.get(Calendar.DAY_OF_MONTH),
            c1.get(Calendar.YEAR))) {
        [U]dYear[/U] = c1.get(Calendar.YEAR);
         
        [U]dMonth[/U] = c1.get(Calendar.MONTH);
        [U]dDay [/U]= c1.get(Calendar.DAY_OF_MONTH);
    }
 
    return [U]dMonth[/U] + "/" + [U]dDay[/U] + "/" + [U]dYear[/U];
		return new ExtendedDate();
	}
		

	}




package Final;

public class Date {

	private int month; //instance variable for value of the date's month
	private int day; //instance variable for value of the date's day
	private int year; //instance variable for the value of the dates
	
	//Default constructor: set the instance variable to default values: month = 1; day = 1; year = 1900;
	public Date()  {
		month = 1;
		day = 1;
		year = 1900;
	}
	
	//Constructor to set the date
	//The instance variables month, day, and year are set according to received parameters.
	public Date (int month, int day, int year)  {
		this.month = month;
		this.day = day;
		this.year = year;
	}
	
	//Method to set the date
	// The instance variables month, day, and year are set according to received parameters.
	public void setDate(int month, int day, int year)  {
		this.month = month;
		this.day = day;
		this.year = year;
	}
	
	// Method to return the date's month value
	public int getMonth()  {
		return month;
	}
	
	//  Method to return the date's day value
	public int getDay()  {
		return day;
	}
	
	//  Method to return the date's year value
	public int getYear()  {
		return year;
	}
	
	//Override of inherited toString() method to return the date in the form of mm-dd-yyy
	public String toString() {
		return (month + "-" + day + "-" + year);
	}
}




package Final;



	import java.util.Scanner;
	//Author: Kelly McGraw
	//Date: 3/30/2013
	//IT-501 X2424
	//ExtendedDate Application Tester

	public class TestKelly extends ExtendedDate {
		
		public static void  main(String[] args){
			
			//Welcome message
			System.out.println("*******************************************************************");
			System.out.println("\t" + "\t"+"Date Tester Application");
			System.out.println("*******************************************************************");
			@SuppressWarnings("resource")
			Scanner sc = new Scanner (System.in);
			String choice = "y";
			
			
			while (choice.equalsIgnoreCase("y"))
			{
				ExtendedDate test = new ExtendedDate();
				
				
				System.out.println("Please Enter a month (1-12):");
				int monthInt = sc.nextInt();
				sc.nextLine();
				
				System.out.println();
				System.out.println("Please Enter a day (1-31):");
				int dayInt = sc.nextInt();
				sc.nextLine();
				
				System.out.println();
				
				System.out.println("Enter a year (1900-2999)(yyyy):");
				int yearInt = sc.nextInt();
				sc.nextLine();
				
				System.out.println();
				
				// System out put results
				//Need method to print if yes entered valid date
				System.out.println("*******************************************************************");
				System.out.println("\t" + "\t"+"Results");
				System.out.println("*******************************************************************");
				test.setDate(monthInt, dayInt, yearInt);
				System.out.println("Date Entered:" + " "+test.toString());
				System.out.println();
				System.out.println("Entered a known Leap Year such as (1980, 1984, 1988, 2012)"+"\t"+ test.isLeapYear());
				System.out.println();
				test.setDate(monthInt, dayInt, yearInt);
				System.out.println("Number of days that have passed since " +" "+test.toString()+":(including this day)" + test.daysToDate());
				System.out.println();
				test.setDate(monthInt, dayInt, yearInt);
				System.out.println("Number of days remaing for year entered:" + test.daysRemainingInYear());
				System.out.println();
				test.setDate(monthInt, dayInt, yearInt);
				System.out.println("The New Date is:" +" "+ test.futureDate(30)+" "+ "after adding 30 more days.");
				System.out.println();
				System.out.println("Would you like to Continue testing?: (y/n)");
				choice = sc.nextLine();
				System.out.println();
			}
			
			
	}}

Was This Post Helpful? 0
  • +
  • -

#14 Flukeshot  Icon User is offline

  • A little too OCD
  • member icon

Reputation: 415
  • View blog
  • Posts: 1,030
  • Joined: 14-November 12

Re: Question with extending a date class in Java

Posted 30 March 2013 - 05:07 PM

The date class looks like it was given to you as an example to follow, the comments look like instructions. Can you show me the original copy of the ExtendedDate class?

And could you post your error directly to here, in code tags? You've no idea how much it helps us figure out the problem. :)

See comments in the code below for extra info on constructors.

package Final;

public class Date {

	private int month; //instance variable for value of the date's month
	private int day; //instance variable for value of the date's day
	private int year; //instance variable for the value of the dates
	
	//Default constructor: set the instance variable to default values: month = 1; day = 1; year = 1900;

	public Date()  {
             //This is where the program will go if you give it no details.
             //you do that with this: 'Date d = new Date();'
             //the reason is that if you give no info, you must supply
             //something or something horrible will happen.
             //this constructor gives DEFAULT values for a date.
		month = 1;
		day = 1;  
		year = 1900;
	}                   
	//Constructor to set the date
	//The instance variables month, day, and year are set according to received parameters.

	public Date (int month, int day, int year)  {
             //this constructor comes into play if
             //your 'Date d = new Date' has 3 integer parameters.
             //these parameters are temporary, so they
             //must be assigned to a variable.
             //'this.day = day' just means "assign the temporary
             //day parameter to Date d's day instance variable"
		this.month = month;     
		this.day = day;         
		this.year = year;       
	}                               
	                                
	
	//Method to set the date
	// The instance variables month, day, and year are set according to received parameters.
	public void setDate(int month, int day, int year)  {
		this.month = month;
		this.day = day;
		this.year = year;
	}

	// Method to return the date's month value
	public int getMonth()  {
		return month;
	}
	
	//  Method to return the date's day value
	public int getDay()  {
		return day;
	}
	
	//  Method to return the date's year value
	public int getYear()  {
		return year;
	}
	
	//Override of inherited toString() method to return the date in the form of mm-dd-yyy
	public String toString() {
		return (month + "-" + day + "-" + year);
	}
}



Edit: comments got a bit messy

This post has been edited by Flukeshot: 30 March 2013 - 05:16 PM

Was This Post Helpful? 0
  • +
  • -

#15 NewGirlXtina  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 17-February 13

Re: Question with extending a date class in Java

Posted 30 March 2013 - 05:33 PM

View PostFlukeshot, on 30 March 2013 - 07:07 PM, said:

The date class looks like it was given to you as an example to follow, the comments look like instructions. Can you show me the original copy of the ExtendedDate class?

And could you post your error directly to here, in code tags? You've no idea how much it helps us figure out the problem. :)/>

See comments in the code below for extra info on constructors.

package Final;

public class Date {

	private int month; //instance variable for value of the date's month
	private int day; //instance variable for value of the date's day
	private int year; //instance variable for the value of the dates
	
	//Default constructor: set the instance variable to default values: month = 1; day = 1; year = 1900;

	public Date()  {  //This is where the program will go if you give it no details.
		month = 1;//you do that with this: 'Date d = new Date();'
		day = 1;  //the reason is that if you give no info, you must supply something
		year = 1900;//or something horrible will happen.
	}                   //this constructor gives DEFAULT values for a date.
	
	//Constructor to set the date
	//The instance variables month, day, and year are set according to received parameters.

	public Date (int month, int day, int year)  {//this constructor comes into play if
		this.month = month;     //your 'Date d = new Date' has 3 integer parameters
		this.day = day;         //these parameters are temporary, so they
		this.year = year;       //must be assigned to a variable.
	}                               //'this.day = day' just means "assign the temporary
	                                //day parameter to Date d's day instance variable
	
	//Method to set the date
	// The instance variables month, day, and year are set according to received parameters.
	public void setDate(int month, int day, int year)  {
		this.month = month;
		this.day = day;
		this.year = year;
	}

	// Method to return the date's month value
	public int getMonth()  {
		return month;
	}
	
	//  Method to return the date's day value
	public int getDay()  {
		return day;
	}
	
	//  Method to return the date's year value
	public int getYear()  {
		return year;
	}
	
	//Override of inherited toString() method to return the date in the form of mm-dd-yyy
	public String toString() {
		return (month + "-" + day + "-" + year);
	}
}



Edit: comments got a bit messy



All of my errors are in my ExtendedDate class - the other classes were provided. I am not sure how to do the tags. I will start with a question and then show a couple of errors by snippets of code instead of the entire thing.

1. Did I set this first part up right according to the comments in the code? I know I need to call super() but do I also need to create an instance of day, month, year in my ExtendedDate class which extends the Date class?

package Final;

import java.util.Calendar;
import java.util.GregorianCalendar;

public class ExtendedDate extends Date {

	//  Default constructor
	//  Data members are set according to super defaults
	    
   	//  Constructor that accepts parameters
	public ExtendedDate (int month, int day, int year)  {
		super.getMonth();
		super.getDay();
		super.getYear();
		month = 0;
		day = 0;
		year = 0;
	}	



2. When I need to do calculations on the dates, do I use the month day year of the ExtendedDate class or do I use a get method from super to do this? For example in this next snippet of code I do not get any errors for year, but is the year in the if statement really getting the year from the right place? The next code that looks for leap year does have errors all relating to the same problem I am having with understanding how the information from the user in the test app is getting the day month year and allowing me to validate it and then check it. You can see that I have setters, am I supposed to use those to run the tests on?

//  Method to set the date - override inherited setDate method
	//  If data is invalid, do not change the receiver
	public void setDate (int monthInt, int dayInt, int yearInt)  {
		super.setDate(getMonth(), getDay(), getYear());
	}
	
	//  Method to set month ensuring that only valid changes are made
	public void setMonth (int anInt)  {
		super.getMonth();
	}
	
	//  Method to set the day ensuring that only valid changes are made
	public void setDay (int anInt)  {
		super.getDay();
	}
	
	//  Method to set the year ensuring that only valid changes are made
	public void setYear (int anInt)  {
		super.getYear();
	}
	
[color="#FF0000"]//////NO ERROR ON THIS NEXT CODE BUT IS IT PULLING THE RIGHT YEAR?[/color]
	//  New method to verify date
	//  Valid range is 1-1-1900 through 12-31-2999
	private static final int[] daysInMonth = { 0, 31, 29, 31, 30, 31, 30, 31,
        31, 30, 31, 30, 31 };
	public static boolean isValidDate (int month, int day, int year)  {
		
		if (1900 < year || year < 2999)
            return false;
        if (month < 1 || month > 12 )
            return false;
        if (day < 1 || day > daysInMonth[month])
            return false;
        if (month == 2 && day == 29 && !isLeapYear(year))
            return false;
            return true;
	}

[color="#FF0000"]/////ERRORS ON THIS CODE I TRIED aYear and getYear() AND STILL GOT ERRORS.[/color]	
	//  Method to test for leap year
	public static boolean isLeapYear (int aYear)  {
		 {
			 if (aYear() % 4 != 0) {
				    return false;
				  } else if (aYear % 400 == 0) {
				    return true;
				  } else if (ayear % 100 == 0) {
				    return false;
				  } else {
				    return true;
				  }
		 }
	}    



I think once I figure out what to call or use in my calculations I will get through this ok. Sorry for so many questions. I really am so new to this.

Thank you :) :helpsmilie: :donatello:
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3