cover picture cover picture


Homework 3

CMP 167: Programming Methods I
Lehman College, City University of New York
Spring 2016


This Homework Is Due By 11:59 PM on Sunday October 16, 2016

Textbook Section 3.19 Homework 3-1

Write a program that computes the fare on Copenhagen Transit given two inputs from the user:

Your prompts to the user must be:

    Enter zone number :
    Enter adult or child :
  

The fare on Copenhagen Transit is specified as follows:

Your output must be of the format:

    The fare for adultOrChild to zone number zoneNumber is fare. 
  

Please note that your class should be named CopenhagenTransit.

Textbook Section 3.20 Homework 3-2

Write a program that asks the user for their age in days. The program will compute the person's age in years and then prints one of the following messages:

  1. If the user is 1 year old or younger, print: "You are an infant"
  2. If the user is over 1 year old and 3 years old or younger, print: "You are a toddler"
  3. If the user is over 3 years old and 12 years old or younger, print: "You are a child"
  4. If the user is over 12 years old and 19 years old or younger, print: "You are a teenager"
  5. If the user is over 19 years old and 21 years old or younger, print: "You are a young adult"
  6. If the user is over 21 years old and 50 years old or younger, print: "You are an adult"
  7. If the user is over 50 years old but 65 years old or younger, print: "You are middle aged"
  8. If the user is over 65 years, print: "You are a senior citizen"

For example:

    If the user entered: 350, your program would output: You are an infant
    If the user entered: 800, your program would output: You are a toddler
    If the user entered, 1825, your program would output: You are a child
    If the user entered, 5475, your program would output: You are a teenager
    If the user entered, 7300, your program would output: You are a young adult
    If the user entered, 10950, your program would output: You are an adult
    If the user entered, 23725, your program would output: You are middle aged
    If the user entered, 25550, your program would output: You are a senior citizen
  

Your prompt to the user to enter the number of days must be:

    Enter your age in number of days:
  

Please note that your class should be named AgeLabel.

Textbook Section 3.21 Homework 3-3

Write a Java program that asks the user for a date entered as 4 integers: dayNumber monthNumber date year. Where:

dayNumber
An integer from 1-7, where 1 = Sunday, 2 = Monday, ..., 7 = Saturday
monthNumber
An integer from 1-12, where 1 = January, 2 = February, ..., 12 = December
date
An integer from 1-31 representing the date.
year
An integer representing the year.

Your prompt to the user should be:

    Enter 4 integers representing dayNumber monthNumber date year:
  

You program will compute the proper dayName from the specified dayNumber and the proper monthName from the specified monthNumber. Your program will print out the specified date in the following format:

    dayNumber monthNumber date year is dayName monthName date, year
  

Please note your program will have to error check the input as follows:

Please note your program will have to error check the input as follows:

  1. First the dayNumber is checked for being in the range 1..7. If that is not the case, your program should print out the following message and terminate execution:
    	Invalid day number: dayNumber, please enter a number from 1..7.
          
  2. Then the monthNumber is checked for being in the range 1..12. If that is not the case, your program should print out the following message and terminate execution:
    	Invalid month number: monthNumber, please enter a number from 1..12.
          
  3. Then the date is checked for being in the range 1..31. If that is not the case, your program should print out the following message and terminate execution:
    	Invalid date number: date, please enter a number from 1..31.
          
  4. Then the date must be checked for validity. For example, there is no February 30, February 31, April 31, June 31, September 31, November 31. If the date is invalid, your program should print out the following message and terminate execution:
    	Invalid date: monthName, does not have date days, please enter a valid date.
          
  5. Lastly, if the monthNumber = 2, and date = 29 you must verify that the year entered is a leap year, which is the only time that February 29 exists. Please see Wikipidia's Page on Leap Year. If the year is not a leap year and the user entered a date = 29, your program should print out the following message and terminate execution:
    	Invalid date: year is not a leap year, February does not have date days, please enter a valid date.
          

Please note that your class should be named DateConverter.

Textbook Section 3.22 Homework 3-4

Write a Java program that asks the user for four words: word1 word2 word3 word4.

Your prompts to the user must be:

    Enter 4 words:
  

A sample runs would look like:

    Enter 4 words:
    she groans third downlow
    Word 1 = "she" Length = 3 position of 'h' = 1
    Word 2 = "groans" Length = 6 position of 'o' = 2
    Word 3 = "third" Length = 5 position of "hi" = 1
    Word 4 = "downlow" Length = 7 position of "low" = 4
    Word 1 is not equal to Word 2

    or

    Enter 4 words:
    shoe shoe chime glow
    Word 1 = "shoe" Length = 4 position of 'h' = 1
    Word 2 = "shoe" Length = 4 position of 'o' = 2
    Word 3 = "chime" Length = 5 position of "hi" = 1
    Word 4 = "glow" Length = 4 position of "low" = 1
    Word 1 is equal to Word 2
  

Please note that your class should be named Words.

Please submit the completed assignment in the corresponding section(s) in your textbook Lehman College, City University of New York CMP 167 Fakhouri Fall 2016: Programming in Java with zyLabs . No other forms of submission will be accepted.