4 Replies - 340 Views - Last Post: 02 April 2013 - 03:20 PM Rate Topic: -----

#1 javoraf  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 01-April 13

question about methods and restarting program

Posted 01 April 2013 - 11:08 PM

I have to write a date validation program. It takes user input, checks if it is a leap year, checks to make sure the date is valid, then gets the full month date name, and date number (jan 1 is 1 and dec 31 is 365). All of these must be in a new method. My code will not compile. I know I am messing up calling or writing the method. I don't really understand parameters. I will post my main method and part of another method to give you an idea of what I am doing.

I also need help inserting a line of code that if the date validation comes back invalid, it loops back to the beginning of the program to ask the user for another date, or to press Q to quit. I have no idea how to do this, only have 4 chapters of a beginning java book for help.

import java.util.Scanner;

/*
	Date Validation Program.
	This program is designed to take user input of a date in DD/MM/YYYY format and validate this date. 
	It is to find out of this year is a leap year. It is then going to say if the date entered was valid 
	for that year. It will then write out the full month name with the date. It will also give the date
	number of the year. This program will show the knowledge of creating working methods for use by a 
	main method.
	
	Written by: Anna Javorka
**/

public class DateValidation
{
	//This section is for the main method, which will later call the written methods.
	public static void main(String[] args)
	{
		//Declare Variables
		String Date;
		String Day;
		String Month;
		String Year;
		int day = 0;
		int month = 0;
		int year = 0;
		
		
		//import scanner
		Scanner in = new Scanner(System.in);
		
		//Request User Input. 
		
		System.out.print("Enter a date in MM/DD/YYY format:");
		String Date = in.next();
		
		//convert string to int for later comparison
		
		String Month = Date.substring(0, 1);
		int month = Integer.parseInt(Month);
		String Day = Date.substring(3, 4);
		int day = Integer.parseInt(Day);
		String Year = Date.substring(6, 9);
		int year = Integer.parseInt(Year);
		
		String LEAP = LeapYear(Date);
		String VAL = DateValidate(Date);
		{
		if (string VAL.equals(Invalid))
		{
			System.out.println("Error");
			//add system loop
		}
		String FULL = FullDate(Date);
		String NUM = DateNumber(Date);
		
		System.out.print("Input Date: " + Date);
		System.out.print("Full Date: " + FULL);
		System.out.println("Date Number: " + NUM);
		
		//Add loop back to top
	}
	/*
	Determines if input year is  Leap year by algorithim.
	Returns String of LeapYear or NotLeapYear
	**/
	
	public static string LeapYear(String LEAP)
	{
		Boolean isLeapYear = ((year/4 == 0) && (year/100 != 0) || (year/400 == 0));
	
		If (isLeapYear)
		{
			Return ISLeapYear
		}
		Else
		{
			Return NOTLeapYear
		}
	}
	
	/*
	This Method Validate's the user's input as a real date. 
	Month must be between 1-12. Day must be correct for it's month
	as well as take Leap Year into account.
	**/
	public static string DateValidate(String VAL);
	{
		//validate the date if in a leap year
		if (LEAP.equals(IsLeapYear))
			if (month == 1)
			{
				if ((day >=1) && (day <=31))
				{
					Return Valid;
				}
				else
				{
					Return Invalid;
				}
			}
			//February has 29 days
			else if (month == 2)
			{
				if ((day >=1) && (day <=29))
				{
					Return Valid;
				}
				else
				{
					Return Invalid;
				}
			}



Is This A Good Question/Topic? 0
  • +

Replies To: question about methods and restarting program

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: question about methods and restarting program

Posted 02 April 2013 - 01:07 AM

How I would attack a problem like this:

I took your description of the assignment and put the steps you outlined as comments in a main() method. Note that I moved the last bit, "return to beginning if invalid," to where it makes sense, after
"checks to see if valid":
	public static void main(String[] args)
	{
		// It takes user input
		
		// checks if it is a leap year, 
		
		// checks to make sure the date is valid, 

		// if the date validation comes back invalid,
		// it loops back to the beginning of the program to ask the user
		// for another date, or to press Q to quit
		
		// then gets the full month date name, and date number
		// (jan 1 is 1 and dec 31 is 365). All of these must be in a new method.

	} // end method main()

To that bare bones outline of the program, I'd then begin to fill in the details, adding some code and variables where needed. You could also define where loops start and end and what conditions cause looping, something like:
	public static void main(String[] args)
	{
		boolean valid = false;
		
		while ( !valid )
		{
			// It takes user input (or accepts 'Q' to quit)
		
			// checks if it is a leap year, 
		
			// checks to make sure the date is valid, 

			// if the date validation comes back invalid,
			valid = checkIfValid( date );
			// it loops back to the beginning of the program to ask the user
			// for another date, or to press Q to quit
		}
		
		// then gets the full month date name, and date number
		// (jan 1 is 1 and dec 31 is 365). All of these must be in a new method.

	} // end method main()

That shows how to return to the beginning that you asked about It also gives the concept of calling a method with a parameter and using the result to influence the program's execution.

Your variable naming 'convention' will cause you trouble down the road. Variable and method names should not be capitalized. Naming your String and integer variables with the same name but changing the capitalization is poor practice. Use names that clearly describe the value being stored.
Was This Post Helpful? 0
  • +
  • -

#3 javoraf  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 01-April 13

Re: question about methods and restarting program

Posted 02 April 2013 - 12:28 PM

How do you name something that is the same exact thing but a string or a int instead of the other? This is the only comp programming class I am taking. Is my calling of the methods correct? I don't understand that at all and my book tells me nothing that helps. What do I put in as a parameter if it is all user input?
Was This Post Helpful? 0
  • +
  • -

#4 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: question about methods and restarting program

Posted 02 April 2013 - 01:31 PM

They're not the same exact thing. They're not even close to being the same. I'm sure most programmers - people who think about this stuff all of the time - are often accused of being too literal, because to them distinctions between an object's characteristics are important. For example, the NAME of a thing and the NUMBER assigned to represent that same thing are entirely different.

dayOfWeek is commonly understood to mean the name of the days of the week. daysOfTheWeek[] would be a String array of the names of the days of the week. dayOfWeekIndex is a number, 0-6, probably the index of the desired name of the daysOfTheWeek[] array. 'date' is, of course, a number. dayOfMonth is probably a number from 1 to the number of days in the month. And so on.

Understanding these differences and naming your variables appropriately while respecting Java's naming conventions are all important considerations while coding.

Answering your last question about naming your methods: Not really. The entire line that describes a method is called the method declaration or signature. This is one of your method declarations:

public static string LeapYear(String LEAP)

You can learn what each of the parts of the declaration mean in this tutorial. 'string' is the return type and should be capitalized, because the type is String, not string. By Java convention, the name of the method should not be capitalized, so LeapYear() should be leapYear(). Variables with names in all caps, like LEAP, are expected to be constants. Declaring a method's variables as constants is possible and often desirable, but I don't think that's what you meant.

The rest of the method is, frankly, a mess. I could write several paragraphs about that method, and then there are problems with the next method, some similar, some different. But they're all fixable. Everything's always fixable.

Review that tutorial at that link I sent you, and come back when you're done. See if you can fix some of the errors in the methods you posted, and repost your updated code. Remember:

Java is case sensitive. If/Else are not the same as if/else.
Variables have to be declared and sometimes initialized.
Java statements end with a semi-colon, ';'.

Good luck! Keep coding, don't give up.
Was This Post Helpful? 0
  • +
  • -

#5 javoraf  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 01-April 13

Re: question about methods and restarting program

Posted 02 April 2013 - 03:20 PM

OK, i fixed my minor errors, I think. I am a psych major and had to take a computer class, so this class is my only knowledge. It is also all online, and the book does not explain things well. It only has the very basic concept, but if your code needs anything more it does not help. I went to tutoring at my school and they couldn't help either. I only have 5 chapters of java for everyone to look at. I just need it to work to pass the class. I need to loop the program if the date is invalid still. Someone helped me with the q to quit. Hopefully it works in my program. I'll post what I have. It is really long. when I go to compile it keeps not working. I've spent so long on this and it is due tonight, I just need the help.

import java.util.Scanner;

/*
	Date Validation Program.
	This program is designed to take user input of a date in DD/MM/YYYY format and validate this date. 
	It is to find out of this year is a leap year. It is then going to say if the date entered was valid 
	for that year. It will then write out the full month name with the date. It will also give the date
	number of the year. This program will show the knowledge of creating working methods for use by a 
	main method.
	
	Written by: Anna Javorka
**/

public class DateVal
{
	//This section is for the main method, which will later call the written methods.
	public static void main(String[] args)
	{
		//Declare Variables
		String Date;
		String Day;
		String Month;
		String Year;
		String Length;
		String Full;
		String Num;
		String Leap;
		int day = 0;
		int month = 0;
		int year = 0;
		
		
		//import scanner
		
		//Request User Input. 
		
		boolean quit = false;
		
		do 
		{
		 	Scanner in = new Scanner(System.in);
			System.out.println("Please insert a date in MM/DD/YYYY format or press Q to quit.");
			Date = input.nextLine();
			quit = date.equalsIgnoreCase("q");
		} 
		while (!quit && !isDate(Date));
               
		input.close();
		if (quit) System.exit(0);		
		
		//convert string to int for later comparison
		
		String Month = Date.substring(0, 1);
		int month = Integer.parseInt(Month);
		String Day = Date.substring(3, 4);
		int day = Integer.parseInt(Day);
		String Year = Date.substring(6, 9);
		int year = Integer.parseInt(Year);
		

		String Leap = LeapYear(Date);
		String Val = DateValidate(Date);
		
		if (VAL.equals(Invalid))
		{
			System.out.println("Error");
			//add system loop
		}
		String Full = FullDate(Date);
		String Num = DateNumber(Date);
		
		System.out.print("Input Date: " + Date);
		System.out.print("Full Date: " + Full);
		System.out.println("Date Number: " + Num);

		//Add loop back to top
	}
	/*
	Determines if input year is  Leap year by algorithim.
	Returns String of LeapYear or NotLeapYear
	**/
	
	public static String LeapYear()
	{
		Boolean isLeapYear = ((year/4 == 0) && (year/100 != 0) || (year/400 == 0));
	
		if (isLeapYear)
		{
			Return IsLeap;
		}
		else
		{
			Return NotLeap;
		}
	}
	
	/*
	This Method Validate's the user's input as a real date. 
	Month must be between 1-12. Day must be correct for it's month
	as well as take Leap Year into account.
	**/
	public static String DateValidate()
	{
		//validate the date if in a leap year
		if (Leap.equals(IsLeap))
		{
			if (month == 1)
			{
				if ((day >=1) && (day <=31))
				{
					Return Valid;
				}
				else
				{
					Return Invalid;
				}
			}
			//February has 29 days
			else if (month == 2)
			{
				if ((day >=1) && (day <=29))
				{
					Return Valid;
				}
				else
				{
					Return Invalid;
				}
			}
			else if (month == 3)
			{
				if ((day >=1)&& (day <=31))
				{
					Return Valid;
				}
				else
				{
					Return Invalid;
				}
			}
			else if (month == 4)
			{
				if ((day >=1) && (day <=30))
				{
					Return Valid;
				}
				else
				{
					Return Invalid;
				}
			}
			else if (month == 5)
			{
				if ((day >=1) && (day <=31))
				{
					Return Valid;
				}
				else
				{
					Return Invalid;
				}
			}
			else if (month == 6)
			{
				if ((day >=1) && (day <=30))
				{
					Return Valid;
				}
				else
				{
					Return Invalid;
				}
			}
			else if (month == 7)
			{
				if ((day >=1) && (day <=31))
				{
					Return Valid;
				}
				else
				{
					Return Invalid;
				}
			}
			else if (month == 8)
			{
				if ((day >=1) && (day <=31))
				{
					Return Valid;
				}
				else
				{
					Return Invalid;
				}
			}
			else if (month == 9)
			{
				if ((day >=1) && (day <=30))
				{
					Return Valid;
				}
				else
				{
					Return Invalid;
				}
			}
			else if (month == 10)
			{
				if ((day >=1) && (day <=31))
				{
					Return Valid;
				}
				else
				{
					Return Invalid;
				}
			}
			else if (month == 11)
			{
				if ((day >=1) && (day <=30))
				{
					Return Valid;
				}
				else
				{
					Return Invalid;
				}
			}
			else if (month == 12)
			{
				if ((day >=1) && (day <=31))
				{
					Return Valid;
				}
				else
				{
					Return Invalid;
				}
			}
			else
			{
				Return Invalid;
			}
		}
		//validate the date if not a leap year. 
		else
		{
			if (month == 1)
			{
				if ((day >=1) && (day <=31))
				{
					Return Valid;
				}
				else
				{
					Return Invalid;
				}
			}
			//February has 28 days
			else if (month == 2)
			{
				if ((day >=1) && (day <=28))
				{
					Return Valid;
				}
				else
				{
					Return Invalid;
				}
			}
			else if (month == 3)
			{
				if ((day >=1)&& (day <=31))
				{
					Return Valid;
				}
				else
				{
					Return Invalid;
				}
			}
			else if (month == 4)
			{
				if ((day >=1) && (day <=30))
				{
					Return Valid;
				}
				else
				{
					Return Invalid;
				}
			}
			else if (month == 5)
			{
				if ((day >=1) && (day <=31))
				{
					Return Valid;
				}
				else
				{
					Return Invalid;
				}
			}
			else if (month == 6)
			{
				if ((day >=1) && (day <=30))
				{
					Return Valid;
				}
				else
				{
					Return Invalid;
				}
			}
			else if (month == 7)
			{
				if ((day >=1) && (day <=31))
				{
					Return Valid;
				}
				else
				{
					Return Invalid;
				}
			}
			else if (month == 8)
			{
				if ((day >=1) && (day <=31))
				{
					Return Valid;
				}
				else
				{
					Return Invalid;
				}
			}
			else if (month == 9)
			{
				if ((day >=1) && (day <=30))
				{
					Return Valid;
				}
				else
				{
					Return Invalid;
				}
			}
			else if (month == 10)
			{
				if ((day >=1) && (day <=31))
				{
					Return Valid;
				}
				else
				{
					Return Invalid;
				}
			}
			else if (month == 11)
			{
				if ((day >=1) && (day <=30))
				{
					Return Valid;
				}
				else
				{
					Return Invalid;
				}
			}
			else if (month == 12)
			{
				if ((day >=1) && (day <=31))
				{
					Return Valid;
				}
				else
				{
					Return Invalid;
				}
			}
			else
			{
				Return Invalid;
			}
		}
	}
	
	/* 
	This method changes the user input date into the full month name.
	**/
	public static String FullDate()
	{
		if (month == 1)
		{
			Full = ("January " + day + ", " + year);
		}
		else if (month == 2)
		{
			Full = ("February " + day + ", " + year);
		}
		else if (month == 3)
		{
			Full = ("March " + day + ", " + year);
		}
		else if (month == 4)
		{
			Full = ("April " + day + ", " + year);
		}
		else if (month == 5)
		{
			Full = ("May " + day + ", " + year);
		}
		else if (month == 6)
		{
			Full = ("June " + day + ", " + year);
		}
		else if (month == 7)
		{
			Full = ("July " + day + ", " + year);
		}
		else if (month == 8)
		{
			Full = ("August " + day + ", " + year);
		}
		else if (month == 9)
		{
			Full = ("September " + day + ", " + year);
		}
		else if (month == 10)
		{
			Full = ("October " + day + ", " + year);
		}
		else if (month == 11)
		{
			Full = ("Novemeber " + day + ", " + year);
		}
		else if (month == 12)
		{
			Full = ("December " + day + ", " + year);
		}
		
		Return Full;
	}
	
	/* 
	This method gets the date number of the user input. 
	The date number will be changed for Leap Years. It
	adds the previous month's amount of days plus the current
	date in that month. 
	**/
	public static String DateNumber()
	{
		if (Leap.equals(IsLeap))
		{
			if (month == 1)
			{
				Num = day;
			}
			else if (month == 2)
			{
				Num = 31 + day;
			}
			else if (month == 3)
			{
				Num = 60 + day;
			}
			else if (month == 4)
			{
				Num = 91 + day;
			}
			else if (month == 5)
			{
				Num = 121 + day;
			}
			else if (month == 6)
			{
				Num = 152 + day;
			}
			else if (month == 7)
			{
				Num = 182 + day;
			}
			else if (month == 8)
			{
				Num = 213 + day;
			}
			else if (month == 9)
			{
				Num = 244 + day;
			}
			else if (month == 10)
			{
				Num = 274 + day;
			}
			else if (month == 11)
			{
				Num = 305 + day;
			}
			else if (month == 12)
			{
				Num = 335 + day;
			}
		else
		{
			if (month == 1)
			{
				Num = day;
			}
			else if (month == 2)
			{
				Num = 31 + day;
			}
			else if (month == 3)
			{
				Num = 59 + day;
			}
			else if (month == 4)
			{
				Num = 90 + day;
			}
			else if (month == 5)
			{
				Num = 120 + day;
			}
			else if (month == 6)
			{
				Num = 151 + day;
			}
			else if (month == 7)
			{
				Num = 181 + day;
			}
			else if (month == 8)
			{
				Num = 212 + day;
			}
			else if (month == 9)
			{
				Num = 245 + day;
			}
			else if (month == 10)
			{
				Num = 275 + day;
			}
			else if (month == 11)
			{
				Num = 305 + day;
			}
			else if (month == 12)
			{
				Num = 334 + day;
			}
		}
		Return Num;
	}
}
		
	



Was This Post Helpful? 0
  • +
  • -

Page 1 of 1