Course Hero Logo
Question
Answered step-by-step

Programming Challenge 5 of Chapter 8 required you to write a Month...

Programming Challenge 5 of Chapter 8 required you to write a Month class that holds information about the month. Write exception classes for the following error conditions:

A number less than 1 or greater than 12 is given for the month number.

An invalid string is given for the name of the month.

Modify the Month class so that it throws the appropriate exception when either of these errors occurs. Demonstrate the classes in a program.

 

 

 

heres my program but it wont run please help

----------------------------------------------------------------

import java.io.*;

//Invalid string exception

class InvalidString extends Exception
{
  public InvalidString()
  {
   super("Erro:String cannont be empty");
  }
}

//Invalid Month exception

class InvalidNumber extends Exception
{
  public InvalidNumber()
  {
     super ("Error:Cannot be less than 1 and greater than 12");
  }
}
class Month
{
  private int monthNumber;
  /**
   * no-arg constructor
   */
  public Month ()
  {}
  
  /**
   * month number constructor
   * @param number
   */
  public Month (int number)throws InvalidNumber
  {
     setMonthNumber (number);
  }
  
  /**
   * month name constructor
   * @param month
   */
   public Month (String m) throws InvalidString, InvalidNumber
   {
      String upper = m.toUpperCase();
      if( upper.equals("JANUARY"))
           setMonthNumber(1);
      else if(upper.equals("FEBUARY"))
           setMonthNumber(2);
      else if( upper.equals("MARCH"))
           setMonthNumber(3);
      else if(upper.equals("APRIL"))
           setMonthNumber(4);
      else if(upper.equals("MAY"))
           setMonthNumber(5);
      else if( upper.equals("JUNE"))
           setMonthNumber(6);
      else if(upper.equals("JULY"))
           setMonthNumber(7);
      else if(upper.equals("AUGUST"))
           setMonthNumber(8);
      else if( upper.equals("SEPTEMBER"))
           setMonthNumber(9);
      else if(upper.equals("OCTOBER"))
           setMonthNumber(10);
      else if(upper.equals("NOVEMBER"))
           setMonthNumber(11);
      else if( upper.equals("DECEMBER"))
           setMonthNumber(12);
      else
         throw new InvalidString();
    }
    
    /**
     * monthNumber mutator
     * @param number
     */
    public void setMonthNumber (int number) throws InvalidNumber
    {
       // Input validation
       // Valid input 1 - 12 otherwise default to 1
       if ( (number < 1) || (number > 12))
            throw new InvalidNumber(number);
       else
             monthNumber = number;
    }
    
    /**
     * accessor for monthNumber
     * @return monthNumber
     */
     public int getMonthNumber()
     {
         return monthNumber;
     }
     
     /**
      * Month Name generator
      * @return monthName
      */
     public String getMonthName()
     {
         switch (monthNumber)
         {
            case 1 :
                 return "JANUARY";
            case 2 :
                 return "FEBUARY";
            case 3 :
                 return "MARCH";
            case 4 :
                 return "APRIL";
            case 5 :
                 return "MAY";
            case 6 :
                 return "JUNE";
            case 7 :
                 return "JULY";
            case 8 :
                 return "AUGUST";
            case 9 :
                 return "SEPTEMBER";
            case 10 :
                 return "OCTOBER";
            case 11 :
                 return "NOVEMBER";
            case 12 :
                 return "DECEMBER";
            default :
                 return "JANUARY";
         }
     }
     
     /**
      * toString Overload
      */
      public String toString()
      {
         return getMonthName();
      }
      
      /**
       * comparison of passed object with current object
       to determine equality
       * @param m
       * @return boolean
       */
       public boolean equals(Month m)
       {
         if (m.getMonthNumber() == this.getMonthNumber())
              return true;
         else
              return false;
       }
       
       /**
        * comparison of passed object with current object
        * to determine equality
        * @param m
        * @return boolean
        */
        public boolean greaterThan (Month m)
        {
             if (this.getMonthNumber() > m.getMonthNumber())
                  return true;
             else
                return false;
        }
        
        /**
         * comparison of passed object with current object
         * to determine equality
         * @param m 
         * @return boolean
         */
        public boolean lessThan (Month m)
        {
           if (this.getMonthNumber() < m.getMonthNumber())
                return true;
           else
                return false;
         }
   }

 

-------------------------------------------------------------

 

class MonthDemo
{
   public static void main(String [] args) throws Exception
   {
       try
       {
           //creating instance to class
           Month object1=new Month();
           Month object2=new Month("APRIL");
           //set function call
           object1.setMonthNumber(14);
           System.out.println("Month:"+object1.toString());
       }
       catch(InvalidNumber e)
       {
          System.out.println(e.getMessage());
       }
       catch(InvalidString e)
       {
          System.out.println(e.getMessage());
       }
       //to exit the program
       System.exit(0);
    }
}
 

Answer & Explanation
Verified Solved by verified expert
Rated Helpful

ipsum dolor sit amet, consectetur adipiscing elit. Nam lacinia pulvinar

trices ac magna. Fusce dui lectus, congue vel laoreet ac, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam lacinia pulvinar tortor nec facilisis. Pellentesque dapibus efficitur laoreet. Nam risus ante, dap

Unlock full access to Course Hero

Explore over 16 million step-by-step answers from our library

Subscribe to view answer
Step-by-step explanation

iscing elit. Nam lacinia

facilisis. Pellentesque dapibus eff

, dictum vitae odio. Donec aliquet. Lorem ip
molestie
, consec
facilisis. Pellentesque dapibus
ec facilisis.
o
fficitur laoreet. Nam ris
fficitur laoreet. Nam risus ante, dapibus a molestie consequat, ultrices ac magna. Fus
o
o
gue

ac, dictum

ec aliquet. Lore

at, ultrices ac ma
o
lestie consequat, ultri

onec aliquet.
o
sus ante, dapibu
o

usce dui lectus, co
o
inia pulvinar tortor nec fac
Donec aliquet. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam lacinia pulvinar tort
ipsu
Fusce dui lectu
o
, consec
ec aliquet. Lorem ipsum
o
sus ante, dapibus a molestie consequa
sus ante, dapibu
consectetur adipiscing elit. Nam lacinia pu
cing elit. Nam l
tesque dapibus efficitur laoreet. Nam r
entesque dapibu
dictum vitae odio. Donec aliquet. Lorem
ipiscing elit. N
dictum vitae odio. Donec aliquet. Lore
e vel laoreet a
, dictum vitae odio. Donec aliquet. Lo
ctum vitae odio
m ipsum dolor sit amet, consectetur adi
, dictum vitae
congue vel laoreet ac, dictum vitae odio
or nec facilisis
dictum vitae odio. Donec aliquet. Lorem ips
or nec facilisis
sum dolor sit amet, consectetur adipiscin
gue vel laoreet
, ultrices ac magna. Fusce dui lectus, cong
s ante, dapibus a
ac, dictum vitae odio. Donec aliquet. Lore
, ultrices ac ma
ipsu
rem ipsum dolor sit amet, consectetur adipiscing elit. Nam lacinia pulvinar tortor nec facilisis. Pellent
o
, consec
ctum vitae odio. Donec aliquet. L
o
ultrices ac magna.
sus ante, dapibu
ipsu
Fusce dui lectu
o

a molestie consequat, ultri
o
ng elit. Nam lacini
o
, consec
rem ipsum dolor sit amet, c
o
ac, dictum
, consec
rem ipsum dolor sit
o
consectetur adipiscing e
a mole
tesque dapibus efficitur
a mole
iscing elit. Nam lacin
a mole
inia pulvinar tortor ne
a mole
a. Fusce dui lectus,
a mole
dictum vitae odio. Don
a mole
inia pulvinar tortor
a mole
nec facilisis. Pellente
a mole
congue vel laoreet ac, di
a mole
or nec facilisis. Pellente
a mole
ia pulvinar tortor nec faci
a mole
usce dui lectus, congue vel
a mole
ctum vitae odio. D
o
, consec
lestie cons
o

, ultrices ac magna. Fus
o
m ipsum dolor sit amet
o


a. Fusce dui lectus, congue ve
o
pulvinar tor
, consec
or nec facilisis. Pellentesque dapibus
icitur laore
ipsu
a molestie co
, consec
s a molestie
o


m ipsum dolor sit amet, consectetur
o
pulvinar tor
, consec
sus ante, dapibus a molestie consequa
icitur laore
ipsu
a molestie co
, consec
s a molestie
o



itur laoreet. Nam risus ante, da
o
pulvinar tor
, consec
onec aliquet. Lorem ipsum dolor sit a
icitur laore
ipsu
a molestie co
, consec
s a molestie
o
o

Student review
100% (1 rating)
Recently Asked Questions
Explore recently asked questions from the same subject