Question

Create an application that instantiates a 20?20 two-dimensional array of Date objects, populates it with random...

Create an application that instantiates a 20?20 two-dimensional array of Date objects, populates it with random dates drawn from the range of 1/1/1970 to 12/31/2020, and then outputs the index and actual date of the row with the most recent date (relative to today) among all the rows and the index and actual date of the column with the oldest date (closest to 1/1/1970) among all the columns.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

/************************************************ Your Code begin here **********************************************/

import java.util.*;


public class Random {
public static void main(String[] args) {
Date[][] date_arr = new Date[20][20];
for (int i = 0; i < 20; i++) {
for(int j=0; j<20;j++){
Date randomDate = createRandomDate(1970, 2020); //create the random date between 1970 and 2020
date_arr[i][j] = randomDate;
// System.out.println(date_arr[i][j]);
}
}
Date d2 = new Date(); //Create the most recent date as per system
Date d3;
  
long max=0;
long diff=18250;
int row_diff=0;
int col_diff=0;
int row_max=0;
int col_max=0;
int k=0;
for (int i = 0; i < 20; i++) {
for(int j=0; j<20;j++){
long difference = (date_arr[i][j].getTime()-d2.getTime())/86400000; /*Calculate tthe difference between date in terms of days if you convert a time to days, then divided by 86400000*/
long c= Math.abs(difference);
//int c = date_arr[i][j].compareTo(d2);
if (diff > c) {
diff=c;
row_diff=i;
col_diff=j;
}
if(c>max){
c=max;
row_max=i;
col_max=j;
}
}
}
  
System.out.println("most recent row index:" + row_diff + "most recent column index:"+ col_diff + "most recent date:\n"+ date_arr[row_diff][col_diff]);
System.out.println("most oldest row index:" + row_max + "most oldest column index:"+ col_max + "most oldest date:\n"+ date_arr[row_max][col_max]);
  
}

public static int createRandomIntBetween(int start, int end) {
  
return start + (int) Math.round(Math.random() * (end - start));
}

public static Date createRandomDate(int startYear, int endYear) {
int day = createRandomIntBetween(1, 28);
int month = createRandomIntBetween(1, 12);
int year = createRandomIntBetween(startYear, endYear);

return new Date(year-1900, month, day);
}
}

/*****************************************************Code ends Here**************************************/

Add a comment
Know the answer?
Add Answer to:
Create an application that instantiates a 20?20 two-dimensional array of Date objects, populates it with random...
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
  • create a new Java application called "Scorer" (without the quotation marks) that declares a two-dimensional array...

    create a new Java application called "Scorer" (without the quotation marks) that declares a two-dimensional array of doubles (call it scores) with three rows and three columns and that uses methods and loops as follows. Use a method containing a nested while loop to get the nine (3 x 3) doubles from the user at the command line. Use a method containing a nested for loop to compute the average of the doubles in each row. Use a method to...

  • need help with this python progam using numpy Create a 4x4 two dimensional array with numbers...

    need help with this python progam using numpy Create a 4x4 two dimensional array with numbers from 1 thru 16. show this then: Change the last element by dividing it in half. Show that the original array has changed. show this then: Set all values in row 2 to zero. Show the original array contains this change show this then: Set all values in column 1 to one. Shoe the original array contains this change. show this then: Display the...

  • 48. values is a two-dimensional array of floats with 10 rows and 20 columns. Write code that sums...

    c++ 48. values is a two-dimensional array of floats with 10 rows and 20 columns. Write code that sums all the elements in the array and stores the sum in the variable total 48. values is a two-dimensional array of floats with 10 rows and 20 columns. Write code that sums all the elements in the array and stores the sum in the variable total

  • Create a program that uses a Jagged Array. The program will create an array with 50...

    Create a program that uses a Jagged Array. The program will create an array with 50 rows. Each of the values for each element of the array will be randomly generated. The random values will be between 1 and 20. Depending on the value generated for each row, you will create an array with that number of columns for that row. Each column created will contain the value to the left plus 1. After you create and populate the entire...

  • in java / You will: // 1- create a square 2 dimensional array of random size...

    in java / You will: // 1- create a square 2 dimensional array of random size (less than 11) // 2- fill the array with random integers from 1 to the size limit // 3- print the array // 4- prompt to see if the user wants to do another //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // 1- The square can use the same random value for x and y. Don't allow 0 size arrays. // 2- Maybe a nested set of for loops? to...

  • In Python import numpy as np Given the array a = np.array([[1, 2, 3], [10, 20,...

    In Python import numpy as np Given the array a = np.array([[1, 2, 3], [10, 20, 30], [100, 200, 300]]), compute and print the sums over all rows (should give [6, 60, 600]) the sums over all columns (the sum of he first column is 111) the maximum of the array the maxima over all rows the mean of the sub-array formed by omitting the first row and column the products over the first two columns (hint: look for an...

  • Write code to create a two-dimensional array named numbers containing 500 random numbers from 1 to...

    Write code to create a two-dimensional array named numbers containing 500 random numbers from 1 to 1000 inclusive. The array has 50 rows. Then walk through the array and output the random numbers 10 to a line. Format the numbers in right-aligned columns. Use the command prompt window for output. Note: do NOT create a class or main.

  • IN C#.Develop a program that prints out the location of the largest value in a two-dimensional...

    IN C#.Develop a program that prints out the location of the largest value in a two-dimensional array. The largest values may appear more than once in the array, so you must only print out the first instance. The program defines method locateLargest() that takes a two-dimensional array of integers and returns the location (row index and column index) of the first largest value as a single-dimensional array. The program main method prompts the user to enter a 3-by-4 matrix, prints...

  • Design and implement a Java class (name it SummerStats.java) that tracks statistics for summer job salaries...

    Design and implement a Java class (name it SummerStats.java) that tracks statistics for summer job salaries for a group of people over several years. The only data field you need is a 2-Dimenssional array of values representing salaries. The rows the represents the people and the columns represent the years. The constructor method takes two integers representing the number of people and the number of years, then randomly generates the annual salaries and fills the array. Other class methods include...

  • Design and implement a Java class (name it Summer Stats. java) that tracks statistics for summer...

    Design and implement a Java class (name it Summer Stats. java) that tracks statistics for summer job salaries for a group of people over several years. The only data field you need is a 2-Dimenssional array of values representing salaries. The rows the represents the people and the columns represent the years. The constructor method takes two integers representing the number of people and the number of years, then randomly generates the annual salaries and fills the array. Other class...

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
ADVERTISEMENT