Jump to content

Java Array

- - - - -

  • Please log in to reply
3 replies to this topic

#1
lary

lary

    Newbie

  • Members
  • Pip
  • 3 posts
Hi. Please i need help with the question below:
a) 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.
b) Create and print out on the screen a random date such as 24/December. Note the “/” between the day and the month.
c) 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 done a and b but don't understand c. Would really appreciate help. Here is my code



public class Test {


    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"};


        System.out.println("Date: "+day[6]+"/" +month[5]);


        //String[][] year = new String[day][month]; lost it here.

    }



}



#2
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
You need a structure to represent the date. A date will contain the name of the month and an int for the day, and can be a simple data type.
public class Test {

    private static class Date {

        public int day;

        public String month;

    }


    public static void main(String[] args)

    {

        Date[] days = new Date[360]

        // Perform nested loop to fill days array.

    }
NOTE: Simply because you randomly chose day 6 and month 5 does not mean that those numbers satisfy the requirement of creating a random date. You need to create a Random object and use something like nextInt() to get which array value.
        Random rand = new Random();

        someDate = new Date();

        someDate.day = day[rand.nextInt(day.length)];

        someDate.month = month[rand.nextInt(month.length)];

Wow I changed my sig!

#3
lary

lary

    Newbie

  • Members
  • Pip
  • 3 posts
Thanks alot. Am really grateful. Will do as you say.

#4
lary

lary

    Newbie

  • Members
  • Pip
  • 3 posts
Thanks so much, I have the code below which works with the randoms date stuff but i really dont know how to apply the nested for loop. Tried but i dont think i got it.

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[] days = new Date[360];

       for(int i=0; i<day.length; i++)

       {

           for(int j=0; j<month.length; j++)

           {

               days[i][j];

           }

       }

    }

}



Would really appreciate your help in solving it. Thanks




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users