Pages

Saturday, 20 August 2016

Chapter 6 Exercise 16, Introduction to Java Programming, Tenth Edition Y. Daniel LiangY.

*6.16 (Number of days in a year) Write a method that returns the number of days in a year using the following header: public static int numberOfDaysInAYear(int year) Write a test program that displays the number of days in year from 2000 to 2020.

public class ProgrammingExercise6_16 {
 
 public static void main(String[] args) {
  System.out.printf("%-10s%-10s\n","Year", "Number of Days");
  System.out.println(String.format("%25s", "").replace(' ', '-'));
  for (int i = 2000; i <= 2020; i++) {
   System.out.printf("%-10d%10d\n",i,numberOfDaysInAYear(i));
  }
 
 }
 
 public static int numberOfDaysInAYear(int year) {
 
  if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
   return 366;
  }
  return 365;
 }
}

No comments:

Post a Comment