Answer to Question #192405 in Java | JSP | JSF for Phil

Question #192405

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.


1
Expert's answer
2021-05-19T05:48:02-0400
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?

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS