java - Variables lose their value somehow? -
when try run program, result null, 0 0. why values of monthname, day, , year not show when getday() method invoked , printed on screen.
public class assignment1 { public static void main(string[] args) { //creates array of type date filled 2 longdate objects date [] collectionofdates = { new longdate("february",2,1996), new longdate("february",13,1999) }; // loops through array , displays output of getdate() each object for( int = 0; < collectionofdates.length; i++ ) { system.out.println( collectionofdates[i].getdate() ); } } } for information, longdate class subclass of date class contains methods editday() , edityear() along several others. longdate method listed below.
any appreciated, thanks. also, feel free comment if want more information.
public class longdate extends date { private string monthname; private int day; private int year; public longdate() { } public longdate(string m, int d, int y) { super.editday(d); super.edityear(y); editmonth(m); } public void setdate(string m, int d, int y) { monthname = m; day = d; year = y; } public string getdate() { stringbuilder fulldate = new stringbuilder(); fulldate.append(monthname); fulldate.append(" "); fulldate.append(day); fulldate.append(", "); fulldate.append(year); return fulldate.tostring(); } public string getshortdate() { int month = 0; if (monthname == "january") { month = 1; } else if (monthname == "february") { month = 2; } else if (monthname == "march") { month = 3; } else if (monthname == "april") { month = 4; } else if (monthname == "may") { month = 5; } else if (monthname == "june") { month = 6; } else if (monthname == "july") { month = 7; } else if (monthname == "august") { month = 8; } else if (monthname == "september") { month = 9; } else if (monthname == "october") { month = 10; } else if (monthname == "november") { month = 11; } else if (monthname == "december") { month = 12; } stringbuilder shortdate = new stringbuilder(); shortdate.append(month); shortdate.append("/"); shortdate.append(day); shortdate.append("/"); shortdate.append(year); return shortdate.tostring(); } protected string editmonth(string m) { // asks person try again if month not capitalized , spelled if (m != "january" && m != "february" && m != "march" && m != "april" && m != "may" && m != "june" && m != "july" && m != "august" && m != "september" && m != "october" && m != "november" && m != "december") { m = input.getstring( "invalid month. please type month again." ); return m; } else return m; } }
there's nothing in constructor of longdate sets fields (monthname, day, , year) getdate() reads.
i assume date#editday() , date#edityear() functions similar longdate#editmonth(). note editmonth() not assign value monthname field!
Comments
Post a Comment