Reputation: 43
The program consists of the two following classes and should print time.
Within the class Melons
Eclipse gives me an error under the method String.format
and I do not understand why
public class Melons {
private int hour;
private int minute;
private int second;
public void setTime(int h, int m, int s)
{
hour = ((h>=0 || h<24) ? h : 0);
minute = ((m>=0 || m<60) ? m : 0);
second = ((s>=0 || s<24) ? s : 0);
}
public String toString()
{
// Problem here
return String.format("%d:%02d:%02d %s",
((hour==0||hour==12) ? 12 : hour%12),
minute,
second,
(hour<12 ? "AM" : "PM"));
}
}
class Apples
{
public static void main(String args[])
{
Melons melonsObject = new Melons();
System.out.println(melonsObject.toString());
melonsObject.setTime(13, 35, 9);
System.out.println(melonsObject.toString());
}
}
Upvotes: 1
Views: 685