1. Create a Java Project called DateViewer.
- Create a class called FormattedDate.
- Define three int data members for the class: month, day, year.
- Implement a constructor for the class that takes as input three int values representing a month, day, and year, and saves them in the data members month, day, and year.
- Implement a method isLeapYear that will take no input and will return true if the year value represents a leap year and false otherwise. See http://www.factmonster.com/spot/leapyear2.html for a description of the rules of determining a leap year:
- Most years that can be divided evenly by 4 are leap years. (For example, 2012 divided by 4 = 503: Leap year!)
- Exception: Century years are NOT leap years UNLESS they can be evenly divided by 400. (For example, 1700, 1800, and 1900 were not leap years, but 1600 and 2000, which are divisible by 400, were.) from FactMonster.com
- Implement a method isValid that will take no input and will return true if the month, day, and year refer to a valid day and false otherwise.
- Implement a method getDaysInMonth that will take no input and will return the number of days in the given month. Keep in mind that the month may be invalid (e.g., smaller than 1 or greater than 12) and the number of days in February changes based on whether it is a leap year.
- Implement a method getMonthString that will take no input and will return a string representation of the month, for example 1 will translate to January and 9 will translate to September. Return an empty string if the month is invalid.
- Implement a method toString that will take no input and will return a nicely formatted string representing the date.
- Create a class DateDriver that contains a main method that will create several FormattedDate objects and print the result of calling the methods of FormattedDate. The output of your program will look as follows (you may test other dates):
Info for 6, 23, 1983:
Is Valid? true
Is Leap Year? false
Days in Month: 30
Month String: Jun
Jun 23, 1983
Info for 2, 24, 2012:
Is Valid? true
Is Leap Year? true
Days in Month: 29
Month String: Feb
Feb 24, 2012
Info for 2, 24, 1700:
Is Valid? true
Is Leap Year? false
Days in Month: 28
Month String: Feb
Feb 24, 1700
Info for 2, 29, 1700:
Is Valid? false
Is Leap Year? false
Days in Month: 28
Month String: Feb
Invalid date
Info for 9, 31, 1700:
Is Valid? false
Is Leap Year? false
Days in Month: 30
Month String: Sep
Invalid date
Info for 14, 31, 1700:
Is Valid? false
Is Leap Year? false
Days in Month: -1
Month String:
Invalid date
Once you have completed problem 1, follow the submission instructions for uploading your code to SVN.
2. Create a Java Project called WordAnalyzer.
- Create a class called Word.
- Define one data member of type String named theWord.
- Implement a constructor for the class that takes as input a String and saves the value in the data member theWord.
- Implement a method countChar that takes as input a char and returns the number of times the character appears in the data member theWord. You must use a for loop in your implementation of this method. Use the Java API to find out more about methods of the String class you may use to access individual characters in a string.
- Implement a method matchingChars that takes as input a String and returns the number of characters that appear in the same location in the input string and theWord, for example 'cat' and 'cow' have one matching character and 'dog' and 'horse' also have one matching character. You must use a while loop in your implementation of this method.
- Implement a method isPalindrome that will take no input and will return true if theWord is the same forward and backward, and false otherwise. Keep in mind that upper/lowercase should be ignored.
- Create a class WordDriver that contains a main method that will create a Word object and display the result of calling its methods. The output of your program will look as follows (you may do additional tests):
Word: science
There is/are 0 occurrence(s) of the character 'a'.
There is/are 1 occurrence(s) of the character 'i'.
There is/are 1 occurrence(s) of the character 's'.
There is/are 2 occurrence(s) of the character 'e'.
There is/are 1 matching character(s) in computer.
There is/are 2 matching character(s) in score.
There is/are 0 matching character(s) in ample.
Is science a palindrome? false
Is Madam a palindrome? true
Once you have completed problem 2, follow the submission instructions for uploading your code to SVN.
Please submit your work in an SVN directory: https://www.cs.usfca.edu/svn/<username>/cs112/lab2
Submission Instructions