Write a Java code that creates a variable of int type named 'mode' and sets its initial value to 12. Create a method named 'setTime()' of void type that take four parameters: (1) one int parameter named 'h', (2) one int parameter named “m”, (3) one int parameter named “s”, and (4) one char parameter named 'ap'. In the body of the “setTime()” method, use an if statement to check whether or not the value of “h” is greater than or equal to 12. If so, subtract 12 from 'h'. Then, assign 12 as value to the 'mode' variable. Display the value of hour, minute, second, and AM/PM being set as well as the mode. Create another form of 'setTime()' method of void type that take only three parameters: (1) one int parameter named 'h', (2) one int parameter named 'm', and (3) one int parameter named 's'. In the body of the 2nd form of 'setTime()' method, assign 24 as value to the 'mode' variable. Display the value of hour, minute, and second being set as well as the mode.
public class Time {
static int mode = 12;
public static void main(String args[]) {
setTime(15, 21, 53, 'P');
setTime(15, 21, 53, 'A');
setTime(7, 21, 53, 'A');
setTime(7, 21, 53, 'P');
setTime(15, 21, 53);
setTime(7, 21, 53);
}
public static void setTime(int h, int m, int s, char ap) {
if (h >= 12) {
h -= 12;
ap = 'P';
}
mode = 12;
System.out.println(
"Current time: \n" + h + ":" + m + ":" + s + " " + (ap == 'A' ? "AM" : "PM") + ", " + mode + " mode.");
}
public static void setTime(int h, int m, int s) {
mode = 24;
System.out.println("Current time: \n" + h + ":" + m + ":" + s + ", " + 24 + " mode.");
}
}
// is it right?
Comments
Leave a comment