java - How do i change the value of myDate1 and print out that new value within my main method? -
how use 3 individual set methods change mydate1 new date input?
heres 3 set methods
public void setmonth( int mm ) { month = ( mm >= 1 && mm <= 12 ? mm : 1 ); } /** setday * @param dd new value day * if dd legal day current month, sets day dd * otherwise, sets day 1 */ public void setday( int dd ) { day = ( dd >= 1 && isvalidday( dd ) ? dd : 1 ); } /** setyear * @param yyyy new value year * sets year yyyy */ public void setyear( int yyyy ) { year = yyyy; }
i want keep code write main method.
here rest of code.
import java.io.serializable; // object i/o file import java.util.scanner; //public class simpledate public class simpledateclientfl implements serializable { private int month; private int day; private int year; public static void main(string[] args) { scanner input = new scanner(system.in); int month; int day; int year; simpledate mydate1 = new simpledate(); simpledate mydate2 = new simpledate(); system.out.println(mydate1.tostring()); system.out.println(mydate1); mydate1.nextday(); mydate1.nextday(); mydate1.nextday(); system.out.println(mydate1); system.out.print("enter day: "); day = input.nextint(); system.out.print("enter month: "); month = input.nextint(); system.out.print("enter year: "); year = input.nextint(); system.out.println("my birthday: " + mydate1); } /** default constructor * sets month 1, day 1 , year 2000 */ public void simpledate( ) { setdate( 1, 1, 2000 ); } /** overloaded constructor * @param mm initial value month * @param dd initial value day * @param yyyy initial value year * * passes parameters set methods */ public void simpledate( int mm, int dd, int yyyy ) { setmonth( mm ); setyear( yyyy ); setday( dd ); } /* accessor methods */ int getmonth( ) { return month; } int getday( ) { return day; } int getyear( ) { return year; } /** mutator method */ /** setmonth * @param mm new value month * if mm between 1 , 12, sets month mm * otherwise, sets month 1 */ public void setmonth( int mm ) { month = ( mm >= 1 && mm <= 12 ? mm : 1 ); } /** setday * @param dd new value day * if dd legal day current month, sets day dd * otherwise, sets day 1 */ public void setday( int dd ) { day = ( dd >= 1 && isvalidday( dd ) ? dd : 1 ); } /** setyear * @param yyyy new value year * sets year yyyy */ public void setyear( int yyyy ) { year = yyyy; } /** sets date next day */ public void nextday( ) { if ( ! isvalidday( ++day ) ) { day = 1; if ( ++month > 12 ) { month = 1; year++; } } } private boolean isvalidday( int newday ) { int [] daysinmonth = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; if ( newday > daysinmonth[month] ) { if ( month == 2 && isleapyear( ) && newday == 29 ) return true; else return false; } else return true; } private boolean isleapyear( ) { return !( year % 4 != 0 ||( year % 100 == 0 && year % 400 != 0 ) ); } /** setdate * @param mm new value month * @param dd new value day * @param yyyy new value year * passes parameters setmonth, setday, , setyear */ public void setdate( int mm, int dd, int yyyy ) { setyear( yyyy ); // set year first (could leap year) setmonth( mm ); // set month next setday( dd ); // set day } /** tostring * @return string * returns date in mm/dd/yyyy format */ public string tostring( ) { return month + "/" + day + "/" + year; } /** equals * @param d object compare object * @return true if d equal object * false, otherwise */ public boolean equals( object d ) { if ( !( d instanceof simpledate ) ) return false; simpledateclientfl d1 = (simpledateclientfl)d; if ( month == d1.month && day == d1.day && year == d1.year ) return true; else return false; } }
thanks in advance. if need supply other information please let me know.
your must know when set filed
month = input.nextint();
you modify "this" object ,but not following object "new"
simpledate mydate1 = new simpledate();
so, when in static main method, must have access simpledate's filed.
simple, can make fields public,so can modify this:
mydate1.month = input.nextint();
or can add method set fields did! can call it
mydate1.setmonth(input.nextint());
Comments
Post a Comment