Core Java Programs to improve your skill set
Problem Description: Print a multiplication table for the given number with appropriate allignment upto the given number.
Ex: Multiplication table upto number 5.
* | 1 2 3 4 5
-----------------------------
1 | 1 2 3 4 5
2 | 2 4 6 8 10
3 | 3 6 9 12 15
4 | 4 8 12 16 20
5 | 5 10 15 20 25
Source can be found here.
Problem Description: Print a calendar for the given month in a year. Use the below hints to generate the logic and don't use any built in functions.
1. Odd Days:
We are supposed to find the day of the week on a given date.
For this, we use the concept of 'odd days'.
In a given period, the number of days more than the complete weeks are called odd days.
2. Leap Year:
i. Every year divisible by 4 is a leap year, if it is not a century.
ii. Every 4th century is a leap year and no other century is a leap year.
Note: A leap year has 366 days.
Examples:
i. Each of the years 1948, 2004, 1676 etc. is a leap year.
ii. Each of the years 400, 800, 1200, 1600, 2000 etc. is a leap year.
iii. None of the years 2001, 2002, 2003, 2005, 1800, 2100 is a leap year.
3. Ordinary Year:
The year which is not a leap year is called an ordinary years. An ordinary year has 365 days.
4. Counting of Odd Days:
4.1. 1 ordinary year = 365 days = (52 weeks + 1 day.)
1 ordinary year has 1 odd day.
4.2. 1 leap year = 366 days = (52 weeks + 2 days)
1 leap year has 2 odd days.
4.3. 100 years = 76 ordinary years + 24 leap years
= (76 x 1 + 24 x 2) odd days = 124 odd days.
= (17 weeks + days) 5 odd days.
Number of odd days in 100 years = 5.
Number of odd days in 200 years = (5 x 2) 3 odd days.
Number of odd days in 300 years = (5 x 3) 1 odd day.
Number of odd days in 400 years = (5 x 4 + 1) 0 odd day.
Similarly, each one of 800 years, 1200 years, 1600 years, 2000 years etc. has 0 odd days.
5. Day of the Week Related to Odd Days:
No. of days: | 0 | 1 | 2 | 3 | 4 | 5 | 6 |
---|---|---|---|---|---|---|---|
Day: | Sun. | Mon. | Tues. | Wed. | Thurs. | Fri. | Sat. |
Solution can be found here.
Problem description: Write a Java program to accepet variable number of String paramters which are in the form of numbers and calculate the sum of all number. Handle the exception, when a String not able to cnvert as Number and exclude the same while sum calculation.
Solution can be found here.
Problem description: Throw Module specific exception i.e., InvalidCredentialsException to identify quickly related to Login functionality errors.
This user defined(Module Specific) exception should match with the following criteria.
- When user key in blank in username field:
- Throw exception along with the message to convey as username should not be blank.
- When user key in bank in password field:
- Throw exception along with the message to convey as password should not be blank.
- When user key in non-numeric string in username field:
- Throw exception along with the actual root cause i.e., NumberFormatException.
- When user name and password not matched with the criteria:
- Throw exception along with the message to convey as username and password are incorrect.
Solution can be found here.