Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Need help with a program

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Beginner programmer needing help with a basic program

    Hi guys,

    I'm a new Java programmer and trying to write a basic program for my class which will take dates as shown below and then use a method (in this case the 'date' method)

    System.out.println(date(11, 4, 2003));
    System.out.println(date(30, 6, 2009));

    to produce the following output

    April 11th, 2003
    June 30th, 2009

    We have been asked to use 3 arrays in this program the first one containing the names of the months, the second one displaying the different numbers of days in these months (e.g. 31 in December) and finally the third to display the different ending for the days of the month(e.g. st for the first).

    The date method should be public and static and its formal paramater list must consist of the following 3 integers (day, month and year).

    Logically speaking I think I can see what i need to be doing however I just cant see how I can code this. I dont expect any answers but just to be pointed in the right direction and what I should be looking to do.

    Any help would be greatly appreciated.

    Thanks
    Last edited by Tronez; November 25th, 2011 at 04:35 PM.


  2. #2
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: Need help with a program

    Have you created the arrays yet?

    Also, you will have to subtract one from the second parameter to get the appropriate index in the months array, and besides that its just a switch statement to get the ending.>.<

  3. #3
    Junior Member
    Join Date
    Nov 2011
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need help with a program

    Hi thanks for the reply. Yeah I've made the array's as shown below, but im not sure if this is correct.

    String [] monthsArray = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
    int [] daysinMothArray = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    String [] daysinMonthArray = {"st", "nd", "rd", "th"};

    How do I code it to get these values from the arrays using the date method?

    Thanks

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need help with a program

    How do I code it to get these values from the arrays
    You get values from arrays by using an index:
    theValue = theArray[theIndex];

    To get the indexes to be used, define a date method with arguments for each of the values/indexes you need to access each of the arrays.

  5. #5
    Junior Member
    Join Date
    Nov 2011
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need help with a program

    I think Ican see what I should be doing with the program but my main difficulty is actually coding it.

    So for example I can see in the main method we have the following code
    System.out.println(date(11, 4, 2003));


    So from this I am calling on the date method which may look like this
    public static String date (day, month, years)

    and the values form the main method will be put into these parameters.
    I then somehow need to use the values from my arrays to produce the desired output.

    I've been looking at this all day and just cant work it out, so if anybody would be so kind to help me and possibly show me an example of what a small amount of code would look like to get this to work then that would be great .

    Thanks

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need help with a program

    Take a look at the tutorial:
    Defining Methods (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

    Take the code you have add a println statement to the date() method that prints out the values of the args that are passed to it. Compile and execute it and see what you get.

  7. #7
    Junior Member
    Join Date
    Nov 2011
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need help with a program

    Ok I've done that and managed to get the different values printing out, how do I now get from this to applying the values of the array?

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need help with a program

    What code have you written that uses an array?
    Have you seen how arrays are used in programs? If not you will have a hard time doing this part of your assignment. What text or tutorial are you using to study Java? There must be examples there that show you how to use an array.

    The arguments passed to the date() method are indexes into the arrays that you have defined back in post #3.

  9. #9
    Junior Member
    Join Date
    Nov 2011
    Location
    California
    Posts
    3
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Need help with a program

    If I understand your question correctly then you already have a class declaration, the static arrays defined, and a main method written, and it looks something like this (warning this could be a portion of the answer):
    public class DateHomework {
     
    	static String [] monthsArray = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
    	static int [] daysinMothArray = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    	static String [] daysinMonthArray = {"st", "nd", "rd", "th"};
     
    	public static void main(String[] args){
    		System.out.println(date(11, 4, 2003));
    		System.out.println(date(30, 6, 2009));
    	}
    	//plus more...
    }
    you also have a method declaration for your date method and it looks something like this (warning this could be a portion of the answer):
    public static String date(int day, int month, int year){
    		//that date method...
    }
    you just need help understanding how to convert the integer representation of the date to the word representation, given that your teacher wants you to use those arrays defined earlier.

    If that's the case then here is a big hint on how to do that (warning this could be a HUGE portion of the answer):
    public class DateHomework {
     
    	static String [] monthsArray = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
    	static int [] daysinMothArray = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    	static String [] daysinMonthArray = {"st", "nd", "rd", "th"};
     
    	public static void main(String[] args){
    		System.out.println(date(11, 4, 2003));
    		System.out.println(date(30, 6, 2009));
    	}
     
    	public static String date(int day, int month, int year){
     
    		//create the string that the method will return
    		String result;
     
    		//make sure the month argument is valid
    		if ((month <= 12)&&(month > 0)){
    			//the first element in an array is element zero, but the first month of the year is month number one.
    			result = monthsArray[month - 1];
    		}else{
    			result = "invalid month";
    		}
     
    		//make sure the day argument is valid
    		if ((day <= daysinMothArray[month -1])&&(day > 0)){
    			result = result + " " + day;
    			//add the right day ending
    			if(((day > 3)&&(day < 21))||((day > 23)&&(day<31))){	//if it should end in "th"
    				result = result + daysinMonthArray[3];
    			}
    			//add code for the "st", "nd", and "rd" endings...
    		}else{
    			result = result + " invalid day";
    		}
     
    		//make sure the year argument is valid
    		if (year >= 1){
    			result = result + " " + year;
    		}else{
    			result = result + " invalid year";
    		}
     
    		return result;
    	}
    }

    On my machine the above code was able to compile and produce the correct results. However, I made many assumption, and this might not be the code you're looking for. Regardless I hope it helps

  10. The Following User Says Thank You to murphym18 For This Useful Post:

    Tronez (November 27th, 2011)

Similar Threads

  1. Help with class program!!! STUCK! Program not doing what I Want!!!
    By sketch_flygirl in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 4th, 2011, 07:29 AM