Java If/Else
Java If/Else
Lets take another look at the program which converts pounds to kilograms.
Since a weight cannot be a negative number, our program should not accept a
negative number as a valid weight.
Lets rewrite the program to print an error message if the weight entered in
pounds is negative.
6. Write a Java program which reads a year (integer) from the user and decides whether
that year is a leap year. A year is a leap year (and so contains a February 29) if it is
divisible by 4. But if the year is also divisible by 100 then it is not a leap year, unless it is
divisible by 400. This means that years such as 1992, 1996 are leap years because they
are divisible by 4 and are not affected by the rest of the rule which applies to century
years such as 1900 and 2000. Century years are not leap years except where they are a
multiple of 400. Hence, the years 1700, 1800 and 1900 were not leap years and did not
contain a February 29. But the year 2000 was a leap year, the first such century leap year
since 1600.
Challenge 1: If you finish the exercises early, try to solve this problem. Write a
Java program that reads a date from the user in numeric form. For example,
February 17, 2003 would be entered as the three integers 2, 17, and 2003. Your
program must then determine if the date is a valid date. Use the following
information to determine if the date is valid: January, March, May, July, August,
October, and December all have 31 days. April, June, September, and November
all have 30 days. February has 28 days in a non-leap year and 29 days in a leap
year. Echo the input and print either valid date or invalid date as output.