Java Array

Newbie Member
9May2011,18:53   #1
lary's Avatar
Hi. Please i need help urgently with the question below:

A date structure has a day/month format. A day ranges from 1 to 30 and a month is one of the months of the year. Specify an array of integers called “day” to hold the values of day, and an array of String called “month” to hold the possible values for a month e.g. January, February, etc. Assume all months have 30 days.
a) Create and print out on the screen a random date such as 24/December. Note the “/” between the day and the month.
b) Declare an array called “year” to hold the 360 dates in a year. Initialise a year with the 360 dates, where a date is of the day/month format.

So far I have answered "a" but kind of confused about "b". I have used a nested for loop to select the 360 days but don't know how to populate the year array with the date. Here is my code so far.
Code:
import java.util.Random;

public class Test {
private static class Date {
        public int day;
        public String month;
    }
    public static void main(String[] args)
    {
        int[] day={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,20,30};
        String[] month={"January","February","March","April","May","June","July","August","September","October","November","December"};
        Random rand = new Random();
        Date someDate = new Date();
        someDate.day = day[rand.nextInt(day.length)];
        someDate.month = month[rand.nextInt(month.length)];
        System.out.println("Date: "+someDate.day+"/" +someDate.month);
        Date[] year = new Date[360];

       
       
       for(int i=0; i<month.length; i++)
       {
           for(int j=0; j<day.length; j++)
           {
               String date = day[j]+"/"+month[i]+"/2011";
             }
       }
        

    }
}
Please would really appreciate your help.
Go4Expert Member
9May2011,22:28   #2
Avenger625's Avatar
Code:
 
 int ctr=0;
       for(int i=0; i<month.length; i++)
       {
           for(int j=0; j<day.length; j++)
           {
              year[ctr].day=day[j];
              year[ctr].month=month[i];
              ctr++;
             }
       }
then you can override the toString() to get the string representation of the "Date" in the desired format(01/May/2011).

Code:
public String toString()
{
    String date=this.day+"/"+this.month+"/"+"2011";
    return date;
}
Go4Expert Member
9May2011,22:55   #3
Avenger625's Avatar
Code:
import java.util.Random;

public class Test {
private static class Date {
        public int day;
        public String month;
	public String toString()
	{
    		String date=this.day+"/"+this.month+"/"+"2011";
    		return date;
	}
    }
    public static void main(String[] args)
    {
        int[] day={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,20,30};
        String[] month={"January","February","March","April","May","June","July","August","September","October","November","December"};
        Random rand = new Random();
        Date someDate = new Date();
        someDate.day = day[rand.nextInt(day.length)];
        someDate.month = month[rand.nextInt(month.length)];
        System.out.println("Date: "+someDate.day+"/" +someDate.month);
        Date[] year = new Date[360];

       
       int ctr=0;
       for(int i=0; i<month.length; i++)
       {
           for(int j=0; j<day.length; j++)
           {
              year[ctr]=new Date();
		year[ctr].day=day[j];
              year[ctr].month=month[i];
              ctr++;
             }
       }
       
	// write code here
        
	System.out.println("A Date in the Year: "+ year[rand.nextInt(year.length)].toString());
    }
	
}
If u dont want the toString() in your "Date" structure then you have to write the following code in the main() at "// write code here".

Code:
        int k=rand.nextInt(year.length);
	String date=year[k].day+"/"+year[k].month+"/"+"2011";
        
	System.out.println("Year: "+ date);
The problem (b) was not very clear to me. I would have liked a little more clarifications on it to help you better. I guess, what I understood, is what you wanted to mean!! Hope, this helps.....
lary, shabbir likes this
Newbie Member
9May2011,23:26   #4
lary's Avatar
Thanks alot. You are a life saver. Am really grateful. the code works perfectly. Exactly what i needed.


Thanks once again. May God bless you abundantly.