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...
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...
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()
. Write a function in JAVA called shorten that is defined in the class P8 and...
. Write a function in JAVA called shorten that is defined in the class P8 and shortens each element of an array of strings. Every string with more than two characters is cut down to its first two characters. For example, a program that uses the function shorten follows. public class P8 { public static void main(String args[]) { String x[] = {"CSCI", "1", "11", "Queens", "College", "CUNY"}; shorten(x); for (int i = 0; i < 6; i++) System.out.print(x[i] +...
Write in Java * Create a new client class called Plants.java * Write code in the...
Write in Java * Create a new client class called Plants.java * Write code in the Plants class that solves problems 1,2,3 and 4 * Include the solutions of the different problems in different methods and call them from the main method * Use the BagInterface.java and ArrayBag.java, but do not add any code Problem 1: Create a bag plantsCart, which holds the following spring seedlings(represented by String) Rose, Daisy, Cabbage, Cucumber, Carrot, Cucumber, Daffodil, Daisy, Rose, Iris, Rose, Spinach....
Write a class file called lastname_digits.java. In it, write a method to compute and return the...
Write a class file called lastname_digits.java. In it, write a method to compute and return the total instances where the sum of digits of the array equals to the target. Write a demo file called lastname_digits_demo.java. Create an array of random 10 integers between 301 and 999. Also create an integer variable called target that is equal to a value between 5 and 10. Print the answer within this file. Example: If five of the 20 integers are equal to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT