Question

In: Computer Science

Write a class in Java called 'RandDate' containing a method called 'getRandomDate()' that can be called...

Write a class in Java called 'RandDate' containing a method called 'getRandomDate()' that can be called without instantiating the class and returns a random Date between Jan 1, 2000 and Dec 31, 2010.

Solutions

Expert Solution

RandDate.java

import java.util.Random;

public class RandDate {
  
private static String getMonthInWords(int m)
{
String mon = "";
switch(m + 1)
{
case 1:
mon = "Jan";
break;
case 2:
mon = "Feb";
break;
case 3:
mon = "Mar";
break;
case 4:
mon = "Apr";
break;
case 5:
mon = "May";
break;
case 6:
mon = "Jun";
break;
case 7:
mon = "Jul";
break;
case 8:
mon = "Aug";
break;
case 9:
mon = "Sept";
break;
case 10:
mon = "Oct";
break;
case 11:
mon = "Nov";
break;
case 12:
mon = "Dec";
break;
}
return mon;
}
  
public static void getRandomDate()
{
int daysNotLeap[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int daysLeap[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int d, m, y;
// We need to generate 3 random numbers for day, month and year
Random random = new Random(System.currentTimeMillis());
// 1. Random number between 2000 & 2010 for year
y = random.nextInt((2010 - 2000) + 1) + 2000;
// now we need to check whether the year generated is a leap year or not
if(!isLeapYear(y))
{
// NOT A LEAP YEAR
// 2. generate random number between 0 and 11 for month (11, because array index starts from zero)
m = random.nextInt((11 - 0) + 1) + 0;
// 3. generate random number between 1 and daysNotLeap[m]
d = random.nextInt((daysNotLeap[m] - 1) + 1) + 1;
}
else
{
// A LEAP YEAR
// 2. generate random number between 1 and 11 for month (11, because array index starts from zero)
m = random.nextInt((11 - 0) + 1) + 0;
// 3. generate random number between 1 and daysLeap[m]
d = random.nextInt((daysLeap[m] - 1) + 1) + 1;
}
  
// finally display the date in the format: Jan 1, 2000
System.out.println("Date generated: " + getMonthInWords(m) + " " + d + ", " + y);
}
  
private static boolean isLeapYear(int y)
{
boolean isLeap;
if(y % 4 == 0)
{
if(y % 100 == 0)
{
if(y % 400 == 0)
isLeap = true;
else
isLeap = false;
}
else
isLeap = true;
}
else
isLeap = false;
return isLeap;
}
}

RandDateTest.java (Main class)

public class RandDateTest {
  
public static void main(String[] args) {
RandDate.getRandomDate();
}
}

********************************************************** SCREENSHOT ******************************************************

SAMPLE RUN 1 :

SAMPLE RUN 2 :

SAMPLE RUN 3 :


Related Solutions

Java programming. Write a public Java class called DecimalTimer with public method displayTimer, that prints a...
Java programming. Write a public Java class called DecimalTimer with public method displayTimer, that prints a counter and then increments the number of seconds. The counter will start at 00:00 and each time the number of seconds reaches 60, minutes will be incremented. You will not need to implement hours or a sleep function, but if minutes or seconds is less than 10, make sure a leading zero is put to the left of the number. For example, 60 seconds:...
Java Write a class called Triangle that can be used to represent a triangle. Write a...
Java Write a class called Triangle that can be used to represent a triangle. Write a class called Describe that will interface with the Triangle class The Server • A Triangle will have 3 sides. It will be able to keep track of the number of Triangle objects created. It will also hold the total of the perimeters of all the Triangle objects created. • It will allow a client to create a Triangle, passing in integer values for the...
Write a complete java program that Define a class named sample containing: A method named division...
Write a complete java program that Define a class named sample containing: A method named division that receives two integers x and y and returns the division of the two numbers. It must handle any possible exceptions. A method named printArray that receives an array of double arr and an integer index and prints the element in the positon index. It must handle any possible exceptions. main method that recievs input from user and repeat if wrong input. Then it...
In Java Create a class called "TestZoo" that holds your main method. Write the code in...
In Java Create a class called "TestZoo" that holds your main method. Write the code in main to create a number of instances of the objects. Create a number of animals and assign a cage and a diet to each. Use a string to specify the diet. Create a zoo object and populate it with your animals. Declare the Animal object in zoo as Animal[] animal = new Animal[3] and add the animals into this array. Note that this zoo...
Write a public Java class called DecimalTimer with public method displayTimer, that prints a counter and...
Write a public Java class called DecimalTimer with public method displayTimer, that prints a counter and then increments the number of seconds. The counter will start at 00:00 and each time the number of seconds reaches 60, minutes will be incremented. You will not need to implement hours or a sleep function, but if minutes or seconds is less than 10, make sure a leading zero is put to the left of the number. For example, 60 seconds: 00:00   00:01...
Java Programming Using the class below, please ), write a static method called parse that parses...
Java Programming Using the class below, please ), write a static method called parse that parses a String for balanced parentheses. we seek only to determine that the symbol ‘{‘ is balanced with ‘}’. parse accepts a single String parameter and returns an int. If parse returns a minus 1, then there are no errors, otherwise, parse should return the position within the String where an error occurred. For example parse(“{3 + {4/2} }”)   would return -1 parse(“{ { 4*X}”)...
in Java language, in most simple algorithm Using a stack class, write a static method called...
in Java language, in most simple algorithm Using a stack class, write a static method called parse that parses a String for balanced parentheses. we seek only to determine that the symbol ‘{‘ is balanced with ‘}’. parse accepts a single String parameter and returns an int. If parse returns a minus 1, then there are no errors, otherwise, parse should return the position within the String where an error occurred. For example parse(“{3 + {4/2} }”)   would return -1...
Write a Java class called CityDistances in a class file called CityDistances.java.    1. Your methods...
Write a Java class called CityDistances in a class file called CityDistances.java.    1. Your methods will make use of two text files. a. The first text file contains the names of cities. However, the first line of the file is a number specifying how many city names are contained within the file. For example, 5 Dallas Houston Austin Nacogdoches El Paso b. The second text file contains the distances between the cities in the file described above. This file...
Java - Write an abstract class called Shape with a string data field called colour. Write...
Java - Write an abstract class called Shape with a string data field called colour. Write a getter and setter for colour. Write a constructor that takes colour as the only argument. Write an abstract method called getArea()
Java Write a valid Java method called printReverse that takes a String as a parameter and...
Java Write a valid Java method called printReverse that takes a String as a parameter and prints out the reverse of the String. Your method should not return anything. Make sure you use the appropriate return type.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT