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...
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 program Write a class called Animal that contains a static variable called count to keep...
Java program Write a class called Animal that contains a static variable called count to keep track of the number of animals created. Your class needs a getter and setter to manage this resource. Create another variable called myCount that is assigned to each animal for each animal to keep track of its own given number. Write a getter and setter to manage the static variable count so that it can be accessed as a class resource
In JAVA!!! write a method called Comparisons that will return the number of comparisons to find...
In JAVA!!! write a method called Comparisons that will return the number of comparisons to find an element in the tree. The main program calls this method for each element, adding the comparisons each time in order to count the total number of comparisons. The program then outputs the total number of comparisons and the average number. You may use the program BuildTreeWIthMethod to build your tree. Then, after you have made the call to inOrder(root), add the following code:...
In Java: Write a method called copy, which is passed A[], which is an array of...
In Java: Write a method called copy, which is passed A[], which is an array of int, and an integer n. The method returns a new array consisting of the first n items in A[]. Write a method called slice, which is passed A[], which is an array of int, an integer i and an integer j. The method returns a new array consisting of all of the items in A from A[i] to A[j] inclusive.
Write a method in JAVA, called binarySearch, that returns the index of the key element if...
Write a method in JAVA, called binarySearch, that returns the index of the key element if found in the array, otherwise it will return the value of minus(insertion point +1). In main, use an initializer list create an array of ints called nums holding the following values: 1, 4, 13, 43, -25, 17, 22, -37, 29. Test your binarySearch method on nums with a key value number in it (e.g. 4) and one that is not (e.g. 100). Ex. Given...
Write a Java class called GuessMyNumber that prompts the user for an integer n, tells the...
Write a Java class called GuessMyNumber that prompts the user for an integer n, tells the user to think of a number between 0 and n−1, then makes guesses as to what the number is. After each guess, the program must ask the user if the number is lower, higher, or correct. You must implement the divide-and-conquer algorithm from class. In particular, you should round up when the middle of your range is in between two integers. (For example, if...
*****Using Java 1.         There is a class called Wages. It should have one method: calculateWages. This...
*****Using Java 1.         There is a class called Wages. It should have one method: calculateWages. This method accepts from a user (1) an integer hourly rate and (2) an integer total number of hours worked in a week. It calculates and displays the total weekly wage of an employee. The company pays straight time for the first 40 hours worked by an employee and times and a half for all the hours worked in excess of 40. This class should...
language is java Use method overloading to code an operation class called CircularComputing in which there...
language is java Use method overloading to code an operation class called CircularComputing in which there are 3 overloaded methods as follows: computeObject(double radius)-compute area of a circle computeObject(double radius, double height)-compute area of a cylinder computeObject(double radiusOutside, double radiusInside, double height)-compute volume of a cylindrical object These overloaded methods must have a return of computing result in each Then override toString() method so it will return the object name, the field data, and computing result Code a driver class...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT