• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

toSrting() method

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have a class month, day and year
my toSring() is
public String toString()
{
String report;
report = "+ month / + day / + year";
//report = report + "+ day";
//report = report + "+ year";
return report;
}
i take out the 2 lines of // because the assignment asks me to do
toString(): String [in the format of mm/dd/yyyy
is my toString method correct? thanks.
 
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It doesn't appear to do what you want. You said you have a class month day year, but since you have spaces I assume that means you have either 3 variables in a class or 3 classes with those names. I'll assume the first case.
If your class were something such as -

This should get you started. HTH.
Regards, Aaron R>
[ October 27, 2003: Message edited by: Aaron Roberts ]
 
Gordan Lee
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is my code: (it compiles fine and runs but i need to know if i am doing the assignment correctly. thanks)
public class Date
{
private int day;
private int month;
private int year;

public Date()
{

day = 0;
month =0;
year = 0;
}

public Date(int day, int month, int year)

{
this.day = day;
this.month = month;
this.year = year;
}

public int getMonth()
{
return month;
}

public int getDay()
{
return day;
}

public int getYear()
{
return year;
}


public void setMonth(int month)
{
this.month = month;
}

public void setDay(int day)
{
this.day = day;
}

public void setYear(int year)
{
this.year = year;
}


public String toString()
{
String report;
report = "+ month / + day / + year";
//report = report + "+ day";
//report = report + "+ year";
return report;
}

public boolean equals(Date anotherDate)

{
boolean isEqual;
isEqual = ((day == anotherDate.getDay()) &&
(month == anotherDate.getMonth()) &&
(year == anotherDate.getYear()));


return isEqual;


}

}
 
Aaron Roberts
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can't answer if you are doing the assignment correctly, as I don't know what the assignment is. If your assignment tells you exactly what your class should do and your class does that, everything must be right. Did you check to see if the toString method outputs the date in the format you wanted? When I was in school, I was scored based on how good my comments were, whether the routines actually worked, etc.
Regards,
Aaron R>
 
"Don't believe every tiny ad you see on the internet. But this one is rock solid." - George Washington
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic