What Is Programming Solution 3

From CompSciWiki
Jump to: navigation, search

COMP 1010 Home > What_Is_Programming_Questions


Introduction

An exercise to determine how many seconds are in various longer units of time.

   

{{{Body}}}

What do you need to know?

  • Standard conversion units per length of time, including some tricky ones, like how many leap years are there in a century.
  • How to compose an output sentence from parts.
  • How to do simple calculation of units.
  • How big an int can be, and when you need to change to a long.

Inputs

  • No inputs from the user are required.
  • Use well-known constants, like the number of seconds in a minute, minutes in an hour, hours in a day, etc.

Outputs

  • The number of seconds in a minute, an hour, a day, a week, a 30-day month, a standard year, a leap-year, a century, a millenium.
  • For example,
There are 60 seconds in a minute
There are 3600 seconds in an hour
There are 86400 seconds in a day
There are 604800 seconds in a week
There are 2592000 seconds in a 30-day month
There are 31536000 seconds in a standard year
There are 31622400 seconds in a leap year
There are 3155673600 seconds in a century
There are 31556822400 seconds in a millenium


Transformation

Starting from well-known constants, figure out all the required outputs.

Solution

/**...........................................................HowManySeconds
  * Calculate the number of seconds in various longer units of time.
  * COMP1010 Exercise (What Is Programming?)
  * @author Terry Andres 
  * @version 2009-Sep-15
  */
public class HowManySeconds {   
  
  /**...................................................................main
   * Required method.
   */
  public static void main( String[] args ) {
    final int SEC_PER_MIN = 60 ;
    final int MIN_PER_HOUR = 60 ;
    final int HOUR_PER_DAY = 24 ;
    final int DAY_PER_WEEK = 7 ;
    final int DAY_PER_MONTH = 30 ;
    final int DAY_PER_YEAR = 365 ;
    final int DAY_PER_LEAP_YEAR = 366 ;
    final long LEAP_PER_CENTURY = 24 ;
    final long LEAP_PER_MILLENIUM = 241 ;
    final String THERE_ARE = "There are " ;
    final String SEC_IN = " seconds in a" ;
    final int SEC_PER_HOUR, SEC_PER_DAY, SEC_PER_YEAR, 
      SEC_PER_LEAP_YEAR ;
    final long SEC_PER_CENTURY, SEC_PER_MILLENIUM ;

    System.out.println(THERE_ARE + SEC_PER_MIN + SEC_IN + " minute") ;

    SEC_PER_HOUR = SEC_PER_MIN * MIN_PER_HOUR ;
    System.out.println(THERE_ARE + SEC_PER_HOUR + SEC_IN + "n hour") ;
    
    SEC_PER_DAY = SEC_PER_HOUR * HOUR_PER_DAY ;
    System.out.println(THERE_ARE + SEC_PER_DAY + SEC_IN + " day") ;
    
    System.out.println(THERE_ARE + (SEC_PER_DAY * DAY_PER_WEEK)
                       + SEC_IN + " week") ;
    System.out.println(THERE_ARE + (SEC_PER_DAY * DAY_PER_MONTH)
                       + SEC_IN + " " + DAY_PER_MONTH + "-day month") ;
    
    SEC_PER_YEAR = SEC_PER_DAY * DAY_PER_YEAR ;
    System.out.println(THERE_ARE + SEC_PER_YEAR + SEC_IN 
                       + " standard year") ;
    
    SEC_PER_LEAP_YEAR = SEC_PER_DAY * DAY_PER_LEAP_YEAR ;
    System.out.println(THERE_ARE + SEC_PER_LEAP_YEAR + SEC_IN 
                       + " leap year") ;
    
    SEC_PER_CENTURY = SEC_PER_LEAP_YEAR * LEAP_PER_CENTURY
      + SEC_PER_YEAR * (100-LEAP_PER_CENTURY) ;
    System.out.println(THERE_ARE + SEC_PER_CENTURY + SEC_IN 
                       + " century") ;
    
    SEC_PER_MILLENIUM = SEC_PER_LEAP_YEAR * LEAP_PER_MILLENIUM
      + SEC_PER_YEAR * (1000-LEAP_PER_MILLENIUM) ;
    System.out.println(THERE_ARE + SEC_PER_MILLENIUM + SEC_IN 
                       + " millenium") ;
       
  }
}

Template loop detected: Template loop detected: