Menu
Question Index
...

What will be the output of the following program?

import java.text.*;
import java.util.*;
public class CuckooClock {
    public static void main(String[] args) throws ParseException {
        printCuckoos("02:15", "04:55");
        printCuckoos("15:15", "17:55");
        printCuckoos("06:00", "09:00");
    }
    protected static void printCuckoos(String start, String end) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        Calendar startDate = Calendar.getInstance();
        startDate.setTime(sdf.parse(start));
        Calendar endDate = Calendar.getInstance();
        endDate.setTime(sdf.parse(end));
        while (startDate.before(endDate)) {
            String currentDate = sdf.format(startDate.getTime());
            if (currentDate.endsWith(":00")) {
                printCuckoo(Integer.parseInt(currentDate.split(":")[0]));
            }
            startDate.add(Calendar.MINUTE, 1);
        }
        System.out.print("|");
    }
    private static void printCuckoo(int hours) {
        int i = 0;
        while (i < hours % 12) {
            System.out.print("Cuckoo".charAt(i++ % 6));
        }
        System.out.print("*");
    }
}


Cuc*Cuck*|Cuck*Cucko*|Cuckoo*CuckooC*CuckooCu*|
Cuc*Cuck*|CuckooCuckooCuck*CuckooCuckooCucko*|Cuckoo*CuckooC*CuckooCu*|
Cuc*Cuck*|Cuck*Cucko*|Cuckoo*CuckooC*CuckooCu*CuckooCuc*|
Some other output or Compilation Error
Runtime Error

Doubts

Problems

Topic: Calendar In Java - java.util.Calendar Class

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App