Question:
java- need toString explaination & help?
Brandon
2010-10-29 11:50:40 UTC
I have a project due monday for my intro java course that i really need a good grade for. I believe i need a toString statement at the end of my MyDate class, but am not sure how to construct it. Can someone please help? at the least i need an easy to understand explaination of toString, if you can point out specific pointer that would be awesome! here's my code:

public class MyDate {
public int month, day, year;
public MyDate(int month , int day, int year) {
MyDate date = new MyDate(month, day, year);
}

public boolean advanceByOneDay(){
boolean isAdvanced = true;
if(day+1> daysInMonth())
{
if(month+1 > 12)
{
month = 1;
day = 1;
year++;
}
else
{
month++;
day = 1;
}
}
else
{
day++;
}
return isAdvanced;
}


public static String getMonthInString(int n) {
String result = "";
switch(n) {
case 1: result = "January"; break;
case 2: result = "Febuary"; break;
case 3: result = "March"; break;
case 4: result = "April"; break;
case 5: result = "May"; break;
case 6: result = "June"; break;
case 7: result = "July"; break;
case 8: result = "August"; break;
case 9: result = "September"; break;
case 10: result = "October"; break;
case 11: result = "November"; break;
case 12: result = "December"; break;
default: result = "Error"; //break;
}
return result;
}

private boolean isLeapYear(){
boolean leap = false;
if (year % 4 == 0)
{
if (year % 100 == 0)
{
if (year % 400 == 0)
{
leap = true;
}
else
leap = false;
}
}
return leap;
}




private int daysInMonth() {
switch (month){
case 1:day = 31;break;
case 2:if (isLeapYear())
day = 29;
else
day = 28;break;
case 3:day = 31;break;
case 4:day = 30;break;
case 5:day = 31;break;
case 6:day = 30;break;
case 7:day = 31;break;
case 8:day = 31;break;
case 9:day = 30;break;
case 10:day = 31;break;
case 11:day = 30;break;
case 12:day = 31;break;
default:
if ((month < 1)||(month > 12));


}
return day;
}
Three answers:
siamese_scythe
2010-10-29 12:11:31 UTC
What format do you need the date to be in? In general it would just be a one-liner, like



return getMonthInString() + " " + day + ", " + year;



Some notes: It's much nicer to use an array instead of a switch statement for getting the month as a String.



static final String[] MONTHS = {"January", ... , "December"}; // can make private if you wish

...

if (month < 1 || month > 12) return "Error";

return MONTHS[month - 1];



Also, there is a much nicer switch statement on Oracle's tutorial for getting number of days in a month, which takes advantage of fall-through.



http://download.oracle.com/javase/tutorial/java/nutsandbolts/switch.html
deonejuan
2010-10-29 12:28:11 UTC
Here is an explanation about .toString(), which is a method of Object. When you make a custom Object from a class, you can override Object.toString(); It is very useful.



class Car {

int numWheels;

String sColor;

public Car ( int wheelno ) {

numWheels = wheelno;

sColor = "fire engine red";

}

public String toString() {

String s = "Car is " + sColor + ", it has " + numWheels + " wheels.";



return s;

}

}



// Then in the driver class with the main()

Car myCar = new Car( 5 );

Car car1 = new Car( 4 );



System.out.println( myCar );

System.out.println( car1 );



// in other words, the .toString() can gather the fields you are interested in and return to you a string representation. Useful if you were to archive this Object or just want to println( o );
nielsen
2016-12-03 08:43:28 UTC
For the toString() to artwork properly, as you're able to assume, you will possibly be able to desire to alter this: public String toString(int fee, int healthful) to public String toString() yet now you will desire some issues. a million. you're calling the toString approach from substantial. this may not artwork as substantial is static and toString isn't. carry jointly errors. 2. you will no longer have get admission to to the siut or fee variables interior the toString approach as they're defined interior the main important approach. easy techniques to restoration this. Create a constructor for the cardboard classification. public Card(int theValue, char theSuit) { fee = theValue; healthful = theSuit; } Make inner maximum istance variables for fee and healthful interior the cardboard classification. you may shop what you have interior the main important approach. inner maximum int fee; inner maximum char healthful; Now, interior the main important approach, earlier this line: card = toString(); Have this. Card aCard = new Card(fee, s); substitute the line: card = toString() ; to this: card = aCard.toString(); One final ingredient, the form you're asking for the healthful, you're asking for a letter. interior the code following that, you have become an int, no longer a char. Then interior the if statements, you're checking for ints no longer chars. So i could substitute this line: equipment.out.println("enter healthful Letter: "); to a minimum of something like: equipment.out.println("enter healthful (0 for:golf equipment, a million for diamonds, 2 for hearts, 3 for spades): "); Plus you will possibly be able to desire to alter this code: else if (healthful == 3) { cardsuit = "hearts"; } to else if (healthful == 2) { cardsuit = "hearts"; } as healthful == 3 is for spades no longer hearts. Hearts is two in accordance to what you have. good success with it.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...