Results 1 to 4 of 4
  1. #1
    PVL268 is offline Member
    Join Date
    Feb 2009
    Posts
    27
    Rep Power
    0

    Default Inheritance problems

    Hi folks

    I have some questions about what I did.

    The last tree method i'm getting some mistakes and I can't fix them?
    The another one is if I did the inheritance right in the first methods?

    here is the superclass


    Java Code:
    public class Date
    {
       private int dMonth;		//variable to store the month
       private int dDay;		//variable to store the day
       private int dYear;		//variable to store the year
    
       	public Date()
       	{
    	  	dMonth = 1;
    	  	dDay = 1;
    	  	dYear = 1900;
       	}
       	
       	public Date(int month, int day, int year)
       	{
    	  	dMonth = month;
    	  	dDay = day;
    	  	dYear = year;
       	}
    
      	public void setDate(int month, int day, int year)
       	{
    	  	dMonth = month;
    	  	dDay = day;
    	  	dYear = year;
       	}
    
     		
       	public int getMonth()
       	{
     	  	return dMonth;
       	}
    
    		
       	public int getDay()
       	{
     	  	return dDay;
       	}
    
    		
       	public int getYear()
       	{
     	  	return dYear;
       	}
    
    		//Method to return the date in the form mm-dd-yyyy
       	public String toString()
       	{
    	 	return (dMonth + "-" + dDay + "-" + dYear);
       	}
    }

    and here is What I did. I underline the words where I'm getting mistakes.
    I now that some of the words like dMonth is a private in superclass and still when I use getMonth I'm getting mistakes.




    Java Code:
    import java.util.*;
    
    public class GodDate extends Date{
    	
    	private static final int[] daysInMonth = { 0, 31, 29, 31, 30, 31, 30, 31,
    		31, 30, 31, 30, 31 };
    	
        public GodDate() {
        	super();
        }
    
    //Constructor that accepts parameters (2)
        public GodDate(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 receiver (2)
        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 (7a)
        public void setMonth(int anInt)
            {
        	super.getMonth();
            }
    
    //Method to set day ensuring that only valid changes are made (7b)
        public void setDay(int anInt)
            {
        	super.getDay();
            }
    
    //Method to set year ensuring that only valid changes are made (7c)
        public void setYear(int anInt)
            {
        	super.getYear();
            }
        
    
    //New method to verify a date
    //Valid range is 1-1-1900 through 12-31-2999
        public static boolean isGodDate(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 a leap year (7g)
        public static boolean isLeapYear(int aYear)
            {
        	if (aYear % 4 == 0)
    			return true;
    		else
    			return false;
    			
            }
    
    //Instance method version of the above (7g)
        public boolean isLeapYear()
            {
                return GodDate.isLeapYear(getYear());
            }
    
    //Return number of days in a month (7h)
        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 (includes current day  7i)
        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 number of days remaining in year (7j)
        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)));
    		
    	// as discussed in class, one way is to return days in year
    //less daysToDate()
    
            }
    
    //Create a new GoodDate object with the values that result from adding a number of days to an existing GoodDate object.  Cannot result in an invalid GoodDate being created.  If an invalid date would be created, return a GoodDate with default values. (7k)
        public GoodDate 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];
    }
    ANY SUGGESTION

  2. #2
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    7

    Default

    Java Code:
    private int dMonth;		//variable to store the month
       private int dDay;		//variable to store the day
       private int dYear;		//variable to store the year
    they cant direct access by subclass
    use getXXx() and setXXX()

    in your Date Class, no getTime() method found

    cal1.getTime() return java.util.Date, while not your Date Class type
    Last edited by mtyoung; 03-23-2009 at 03:52 AM.

  3. #3
    PVL268 is offline Member
    Join Date
    Feb 2009
    Posts
    27
    Rep Power
    0

    Default

    Thanks for the tip!!!
    This make sense. Do you know some another way to set the futureDate,
    daysRemainingInYear....
    Thank you

    and for the setMonth
    It should be like this or ...????
    Java Code:
    public void setMonth(int anInt)
            {
        	super.getMonth();
        	 month = anInt
            }

  4. #4
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    7

    Default

    futureDate...
    you can use cal.add(xxx, XXX), then cal.get(XXX) to get back day, month, year which used to create and return a new GodDate Object

    daysRemainingInYear...
    do you know calendar.DAY_OF_YEAR

    isLeapYear(int aYear)
    you checking is wrong, check the definition of leap year

Similar Threads

  1. inheritance
    By itaipee in forum New To Java
    Replies: 6
    Last Post: 01-20-2009, 09:18 PM
  2. Inheritance Program
    By Schaput in forum New To Java
    Replies: 6
    Last Post: 11-14-2008, 01:42 AM
  3. Inheritance
    By mew in forum New To Java
    Replies: 1
    Last Post: 12-07-2007, 07:08 PM
  4. Inheritance in GUI
    By Marty in forum SWT / JFace
    Replies: 2
    Last Post: 05-11-2007, 01:54 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •