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)
Code :
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
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.>.<
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.
Code :
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
Re: Need help with a program
Quote:
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.
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
Code :
System.out.println(date(11, 4, 2003));
So from this I am calling on the date method which may look like this
Code :
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
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.
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?
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.
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):
Code :
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):
Code :
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):
Code :
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