Trace the following Java code snippets: Assume the user will enter: 12 10 2015 3
ID: 3839813 • Letter: T
Question
Trace the following Java code snippets: Assume the user will enter: 12 10 2015 3 Scanner input = new Scanner (System.in); String str = " "; System.out.println ("Enter a day"); int day = input.nextInt (); str = str + day+ ":"; System.out.println ("Enter a month"); int month = input.nextInt (); str = str + month+ ":"; System.out.println ("Enter a year"); int year = input.nextInt (); str = str + year; System.out.println ("Enter the format"); int f = input.nextInt (); f = f % 3; if (f == 1) {str = str.replace (':', '/');} else if (f == 2) {str = str.replace (':', '-');} System.out.println ("Your date is" +str);Explanation / Answer
Line 2 sets str = ""
Line 3 asks for day from user
Line 4 stores 12 in 'day' variable
Line 5 sets str = "12:"
Line 6 asks for month from user
Line 7 stores 10 in month variable
Line 8 sets str = "12:10:"
Line 9 asks for year from yser
Line 10 stores 2015 in year variable
Line 11 sets set = "12:10:2015"
Line 12 asks for format
Line 13 sets f = 3
Line 14 sets f = 0
Line 15-19 are not run, as f == 0
Line 20 prints "Your date is 12:10:2015"
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.