2014-10-22 82 views
1

我有我的主類Date.Java如何調用和打印方法

Date.Java做:
-Sets月份日期和年份,並確保其有效輸入
- 確定有多少天是在你輸入一個月
- 確定,因爲這一天有多少天過去了,你進入
- 確定有多少天是從一天離開你進入
- 確定,如果該年是閏年

我也有一個測試類TestDate.java
- 它會要求輸入用戶並將它們發送到Date.java

我的問題是,我只能讓程序打印用戶輸入的日期。我將如何去調用其他方法並讓他們打印他們返回的內容?

Date.java

public class Date 
{ 
int Day; 
int Month; 
int Year; 
int numberOfDays; 
int daysPassed; 
int daysRemaining; 
int M; 
int D; 
int Y; 



public Date (int Day, int Month, int Year)   
{ 
    setDate(Month, Day, Year); 

} 

public void setDate (int Day, int Month, int Year) 
{ 
    setMonth(Month); 
    setDay(Day); 
    setYear(Year); 
} 


//-------------------SETTERS---------------------- 
public void setMonth(int Month) 
{ 
    M = ((Month>0&&Month<13) ?Month:1);  //conditional statement that checks to see if the Month is valid 
} 

public void setDay(int Day) 
{ 
    D = ((Day>=1&&Day<=365) ?Day:1);  //conditional statement that checks to see if the day is valid 

} 

public void setYear(int Year) 
{ 
    Y = ((Year>=1000&&Year<=9999) ?Year:1900);  //conditional statement that checks to see if the Year is valid 
} 

    //-------------------GETTERS---------------------- 
public int getMonth() 
{ 
    return M; 

} 

public int getDay() 
{ 
    return D; 

} 


public int getYear() 
{ 
    return Y; 
} 

public String toString() 
{ 
    return String.format("%d-%d-%d", getMonth(), getDay(), getYear()); 
} 

public static boolean isLeapYear(int getYear) 
{ 

    if (getYear%4 == 0) 
     return true; 


    else 
     return false; 



} 

public int daysOfMonth(int getMonth, int numberOfDays, boolean isLeapYear) 
{ 
    if (getMonth==1)  //jan 
     numberOfDays = 31; 

    //---------------------------------------Feb 
    if (isLeapYear == true) //feb 
    { 
    if (getMonth==2) 
     numberOfDays = 29; 
    } 
    if (isLeapYear == false) //feb 
    { 
    if (getMonth==2) 
     numberOfDays = 28; 
    } 
    //---------------------------------------Feb 

    if (getMonth==3)  //march 
     numberOfDays = 31; 
    if (getMonth==4)  //april 
     numberOfDays = 30; 
    if (getMonth==5)  //may 
     numberOfDays = 31; 
    if (getMonth==6)  //june 
     numberOfDays = 30; 
    if (getMonth==7)  //july 
     numberOfDays = 31; 
    if (getMonth==8)  //august 
     numberOfDays = 31; 
    if (getMonth==9)  //sept 
     numberOfDays = 30; 
    if (getMonth==10)  //oct 
     numberOfDays = 31; 
    if (getMonth==11)  //nov 
     numberOfDays = 30; 
    if (getMonth==12)  //dec 
     numberOfDays = 31; 


    return numberOfDays; 
} 

public int daysPassedInYear(int getMonth, int Day, int getDay, int numberOfDays, boolean isLeapYear) 
{ 
    if (getMonth==1) 
    Day = getDay-31; 


    if (isLeapYear = true) 
    { 
    if (getMonth==2) 
    daysPassed = (numberOfDays-getDay)-60; 
    if (getMonth==3) 
    daysPassed = (numberOfDays-getDay)-91; 
    if (getMonth==4) 
    daysPassed = (numberOfDays-getDay)-121; 
    if (getMonth==5) 
    daysPassed = (numberOfDays-getDay)-152; 
    if (getMonth==6) 
    daysPassed = (numberOfDays-getDay)-182; 
    if (getMonth==7) 
    daysPassed = (numberOfDays-getDay)-213; 
    if (getMonth==8) 
    daysPassed = (numberOfDays-getDay)-244; 
    if (getMonth==9) 
    daysPassed = (numberOfDays-getDay)-274; 
    if (getMonth==10) 
    daysPassed = (numberOfDays-getDay)-305; 
    if (getMonth==11) 
    daysPassed = (numberOfDays-getDay)-335; 
    if (getMonth==12) 
    daysPassed = (numberOfDays-getDay)-366; 
    } 




    if (isLeapYear = false) 
    { 
    if (getMonth==2) 
    daysPassed = (numberOfDays-getDay)-59; 
    if (getMonth==3) 
    daysPassed = (numberOfDays-getDay)-90; 
    if (getMonth==4) 
    daysPassed = (numberOfDays-getDay)-120; 
    if (getMonth==5) 
    daysPassed = (numberOfDays-getDay)-151; 
    if (getMonth==6) 
    daysPassed = (numberOfDays-getDay)-181; 
    if (getMonth==7) 
    daysPassed = (numberOfDays-getDay)-212; 
    if (getMonth==8) 
    daysPassed = (numberOfDays-getDay)-243; 
    if (getMonth==9) 
    daysPassed = (numberOfDays-getDay)-273; 
    if (getMonth==10) 
    daysPassed = (numberOfDays-getDay)-304; 
    if (getMonth==11) 
    daysPassed = (numberOfDays-getDay)-334; 
    if (getMonth==12) 
    daysPassed = (numberOfDays-getDay)-365; 
    } 


    return daysPassed; 

} 



    public int daysRemainingInYear(int dayspassed, boolean isLeapYear, int daysRemaining) 
    { 
     if (isLeapYear = true)  
      daysRemaining = (366 - dayspassed); 


     if (isLeapYear = false)   
      daysRemaining = (365 - dayspassed); 

     return daysRemaining; 
    } 






} 

TestDate.java

import javax.swing.JOptionPane; 



public class TestDate { 
public static void main(String[] args) { 


int month =Integer.parseInt(JOptionPane.showInputDialog("What month do you want(in number form ex. Jan = 1?")); 
int day =Integer.parseInt(JOptionPane.showInputDialog("What day do you want within the month?")); 
int year =Integer.parseInt(JOptionPane.showInputDialog("What year do you want")); 


Date setDateObject = new Date(month, day, year); 
System.out.println(setDateObject.toString()); 

} 

} 

回答

1

您可以調用Date對象上的其他方法並打印它們的返回值。

Date date = new Date(1, 2, 2003); 

System.out.println(date.getYear()); 
System.out.println(date.isLeapYear()); 

使用參數的方式是奇怪..像得到月參數,NUMBEROFDAYS,isLeapYear用於傳遞信息到函數。 getMonth和isLeapYear對於正常工作的函數是必不可少的,但numberOfDays並不是必需的。你可以這樣聲明函數:

public int daysOfMonth(int getMonth, boolean isLeapYear) 
{ 
    if (getMonth==1)  //jan 
     return 31; 

    //---------------------------------------Feb 
    if (isLeapYear && getMonth==2) 
     return 29; 
    } 

    if (/* !isLeapYear && */ getMonth==2) 
     return 29; 
    } 
    //---------------------------------------Feb 

    if (getMonth==3)  //march 
     return 31; 
    if (getMonth==4)  //april 
     return 30; 
    if (getMonth==5)  //may 
     return 31; 
    if (getMonth==6)  //june 
     return 30; 
    if (getMonth==7)  //july 
     return 31; 
    if (getMonth==8)  //august 
     return 31; 
    if (getMonth==9)  //sept 
     return 30; 
    if (getMonth==10)  //oct 
     return 31; 
    if (getMonth==11)  //nov 
     return 30; 
    if (getMonth==12)  //dec 
     return 31; 


    return -1; // error 
} 

然後你可以像這樣創建一個日期並調用daysOfMonth函數。

Date date = (2, 29, 2004); 
System.out.println(date); 
System.out.println("Is this date a leap year? " + date.isLeapYear()); 
System.out.println("How many days in this month? " + date.daysOfMonth(date.getMonth(), date.isLeapYear()); 
+0

是否有任何其他方式可以打印來自「public int daysOfMonth」或「public int daysPassedInYear」等其他方法的回報? – Hammy23 2014-10-22 03:56:57

+0

是的,看我的編輯。 – DucRP 2014-10-22 04:13:44

+0

真棒!非常感謝!我完全意識到我現在做錯了什麼。 – Hammy23 2014-10-22 04:19:13

0

System.out.println(yourobject.yourMethod()); 

這將打印什麼都你的方法yourMethod()正在恢復。

此外,您可以打印任何函數內的值。

public void yourMethod(){ 
System.out.println(yourAtrribute); 
} 

爲你的類,你可以做如下:

Date setDateObject = new Date(month, day, year); 
//do other stuffs here 
System.out.println(setDateObject.getYear()); 

這將打印一年得到年()方法返回。