Question

java code write a static method getRandomTime() that uses an instance of Random to generate a...

java code

write a static method getRandomTime() that uses an instance of Random to generate a random time in hour and minutes. the hour must be between 1 and 12, both inclusive. the minute must between 0 and 59, both inclusive. the method should return a string of the form hour:minute, such as 12:19 or 1:6. you did not need to worry about pending hour or minute with leading zeros.

import java.util.Random;

public class someclass{

public static string getRandomTime{

}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.Random;

public class someclass {

    public static String getRandomTime() {
        Random random = new Random();
        int hour = random.nextInt(12) + 1;
        int minute = random.nextInt(60);

        return hour + ":" + minute;
    }
}
Add a comment
Know the answer?
Add Answer to:
java code write a static method getRandomTime() that uses an instance of Random to generate a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT