In: Computer Science
Today is [date]!
The count is: [nnnn]
Hint: Since everyday is a different date, don’t hard code the date in your program. And the [nnnn] should be the number from your counter.
1).ANSWER:
GIVEN BELOW:
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
public class GenerateRandom {
public static void main(String[] args)throws Exception {
DateFormat dateFormat = new
SimpleDateFormat("MMddyyyy");//declaring the date format
Date date = new Date();//creating an object for the date
int s = Integer.parseInt(dateFormat.format(date));//converting the
date from string to int
int num = 0;//initializing num
int count =0;//initializing count
Random generator = new Random();//creating an object for random
class
//generator.setSeed(System.currentTimeMillis());
do{ //do while loop to iterate the loop until date equals to random
number
num = generator.nextInt(9999999) + 9999999; //generating 8 digit
random number
count ++;//incrementing the count
}while(s != num); //checking random number is equal to date or
not
if(s == num){//if date equals to random number then
for(int i = 0;i<5; i++){ //iterate for loop to print the message
5 times
System.out.println("Today date is "+num);//display date
System.out.println("The count is "+count);//display count
System.out.println("\n");//assign one line space
}
}
}
}
output: