Welcome to Dream.In.Code
Become a Java Expert!

Join 150,201 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,005 people online right now. Registration is fast and FREE... Join Now!




Inheritance issue :(

 
Reply to this topicStart new topic

Inheritance issue :(, probably an overlook any ideas?

outlaw107
23 Sep, 2008 - 09:48 PM
Post #1

New D.I.C Head
Group Icon

Joined: 1 Nov, 2007
Posts: 31


My Contributions
Alright so, I'm a little stuck on why my lillian method in the "Date" class isn't working in the "DaysBetween" class, nor is the MINYEAR variable passing over even though I have it as protected. Any ideas?


Here is the Date class
CODE

public class Date
{
  protected int year;
  protected int month;
  protected int day;
  public static final int MINYEAR = 1583;

  // Constructor
  public Date(int newMonth, int newDay, int newYear)
  {
    month = newMonth;
    day = newDay;
    year = newYear;
   }

  // Observers
  public int getYear()
  {
    return year;
  }

  public int getMonth()
  {
    return month;
  }

  public int getDay()
  {
    return day;
  }

  public int lilian()
  {
    // Returns the Lilian Day Number of this date.
    // Precondition: This Date is a valid date after 10/14/1582.
    //
    // Computes the number of days between 1/1/0 and this date as if no calendar
    // reforms took place, then subtracts 578,100 so that October 15, 1582 is day 1.
    
    final int subDays = 578100;  // number of calculated days from 1/1/0 to 10/14/1582

    int numDays = 0;

    // Add days in years.
    numDays = year * 365;

    // Add days in the months.
    if (month <= 2)
      numDays = numDays + (month - 1) * 31;
    else
      numDays = numDays + ((month - 1) * 31) - ((4 * (month-1) + 27) / 10);

    // Add days in the days.
    numDays = numDays + day;

    // Take care of leap years.
    numDays = numDays + (year / 4) - (year / 100) + (year / 400);

    // Handle special case of leap year but not yet leap day.
    if (month < 3)
    {
        if ((year % 4) == 0)   numDays = numDays - 1;
        if ((year % 100) == 0) numDays = numDays + 1;
        if ((year % 400) == 0) numDays = numDays - 1;
      }

    // Subtract extra days up to 10/14/1582.
    numDays = numDays - subDays;
    return numDays;
  }

  public String toString()
  // Returns this date as a String.
  {
    return(month + "/" + day + "/" + year);
  }

}



Here is the Days Between Class
CODE

import java.util.Scanner;

public class DaysBetween
{
  public static void main(String[] args)
  {
    Scanner conIn = new Scanner(System.in);
    int day, month, year;
        
    System.out.println("Enter two 'modern' dates: month day year");
    System.out.println("For example, January 12, 1954, would be: 1 12 1954");
    System.out.println();
    System.out.println("Modern dates occur after " + Date.MINYEAR + ".");
    System.out.println();
        
    System.out.println("Enter the first date:");
    month = conIn.nextInt();
     day = conIn.nextInt();
    year = conIn.nextInt();
    Date date1 = new Date(month, day, year);
        
    System.out.println("Enter the second date:");
    month = conIn.nextInt();
    day = conIn.nextInt();
    year = conIn.nextInt();
    Date date2 = new Date(month, day, year);
        
    if ((date1.getYear() <= Date.MINYEAR)
        ||
         (date2.getYear() <= Date.MINYEAR))
      System.out.println("You entered a 'pre-modern' date.");
    else
    {
      System.out.println("The number of days between");
      System.out.print(date1);
      System.out.print(" and ");
      System.out.print(date2);
      System.out.print(" is ");
      System.out.println(Math.abs(date1.lilian() - date2.lilian()));
    }
  }
}


This post has been edited by outlaw107: 23 Sep, 2008 - 09:50 PM
User is offlineProfile CardPM
+Quote Post

AmitTheInfinity
RE: Inheritance Issue :(
23 Sep, 2008 - 11:20 PM
Post #2

C Surfing ∞
Group Icon

Joined: 25 Jan, 2007
Posts: 1,166



Thanked: 45 times
Dream Kudos: 125
My Contributions
I don't think there is inheritance involved anywhere here.
User is offlineProfile CardPM
+Quote Post

baavgai
RE: Inheritance Issue :(
24 Sep, 2008 - 03:45 AM
Post #3

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,283



Thanked: 136 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua, Cheese

My Contributions
I fail to see inheritance. I would make an isLeapYear check in a separate method.

Also, I don't believe you're looking of "lilian". Rather, you probably want to find "Julian" as in Julian Day.

User is online!Profile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 05:00AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month