JAVA: Can someone please change this public String toString to a if else fuction
ID: 3921087 • Letter: J
Question
JAVA:
Can someone please change this public String toString to a if else fuction instead of ? :
##########################################################################
public String toString() {
return " " + getStormYear() + ": " + getStormName() + " " +
((getStormMag() == -1) ? "(no info)" :
((getStormMag() == 0) ? "(tropical storm)" : "(hurricane level " + getStormMag() + ")")) + ": " +
((getStormStart().equals("")) ? "(no start)" : getStormStart().substring(0, 2) + "/" + getStormStart().substring(2)) +
" - " +
((getStormEnd().equals("")) ? "(no end)" : getStormEnd().substring(0, 2) + "/" + getStormEnd().substring(2));
}
Explanation / Answer
Hi,
I have break down this function with if else statement. Please find the below.
public String toString() {
String s = " " + getStormYear() + ": " + getStormName() + " " ;
if(getStormMag() == -1){
s= s + "(no info)";
}
else {
if((getStormMag() == 0)){
s = s + "(tropical storm)";
}
else{
s = s + "(hurricane level " + getStormMag() + ")";
}
if(getStormStart().equals("")){
s = s + "(no end)";
}
else{
s = s + getStormEnd().substring(0, 2) + "/" + getStormEnd().substring(2)+" - " ;
}
if(getStormEnd().equals("")){
s = s + "(no end)" ;
}
else{
s = s + getStormEnd().substring(0, 2) + "/" + getStormEnd().substring(2);
}
}
return s;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.