0

Hey guys. I wanted to know if someone can tell me what's wrong with my code. I got it to work, to a point. It shows all the months, however, when a user inputs the month/number it ends. Which it needs to playback the month... Example: 2 = March

Here's the code

import java.util.Scanner;

public class MonthDemo
{
   public static void main(String[] args)
   {
      // Use the no-arg constructor.
      Month object1 = new Month();
      System.out.println("Month " + object1.getMonthNumber() +
                         " = " + object1);
	  Month object2 = new Month(2);
	  for (int i = 1; i <= 12; i++)
      {
         object2.setMonthNumber(i);
         System.out.println("Month " + object2.getMonthNumber() +
                         " name is " + object2);
      }
		
		String mName;
		Scanner keyboard = new Scanner(System.in);
		Month object5 = new Month("January");
		
		System.out.println("Enter a month name: ");
		mName = keyboard.nextLine();
		System.out.println(object5.mName());
   }
}

And the other part...

/**
   Month</strong> <strong class="highlight">Class</strong>
   Chapter</strong> <strong class="highlight">9</strong>, <strong class="highlight">Programming</strong> <strong class="highlight">Challenge</strong> 6
*/

public class Month
{
   private int monthNumber;

   /**
      The No-arg constructor initializes the object to
      <strong class="highlight">month</strong> #1.
   */

   public Month()
   {
      monthNumber = 1;
   }

   /**
      Constructor
      @param m The <strong class="highlight">month</strong> number to initialize the object to.
   */

   public Month(int m)
   {
      if (m < 1 || m > 12)
         monthNumber = 1;
      else
         monthNumber = m;
   }

   /**
      Constructor
      @param name The name of the <strong class="highlight">month</strong> to initialize the object to.
   */

   public Month(String name)
   {
      if (name.equalsIgnoreCase("january"))
         monthNumber = 1;
      else if (name.equalsIgnoreCase("february"))
         monthNumber = 2;
      else if (name.equalsIgnoreCase("march"))
         monthNumber = 3;
      else if (name.equalsIgnoreCase("april"))
         monthNumber = 4;
      else if (name.equalsIgnoreCase("may"))
         monthNumber = 5;
      else if (name.equalsIgnoreCase("june"))
         monthNumber = 6;
      else if (name.equalsIgnoreCase("july"))
         monthNumber = 7;
      else if (name.equalsIgnoreCase("august"))
         monthNumber = 8;
      else if (name.equalsIgnoreCase("september"))
         monthNumber = 9;
      else if (name.equalsIgnoreCase("october"))
         monthNumber = 10;
      else if (name.equalsIgnoreCase("november"))
         monthNumber = 11;
      else if (name.equalsIgnoreCase("december"))
         monthNumber = 12;
      else
         monthNumber = 1;
   }

   /**
      The setMonthNumber method sets the <strong class="highlight">month</strong>.
      @param m The number of the <strong class="highlight">month</strong>.
   */

   public void setMonthNumber(int m)
   {
      if (m < 1 || m > 12)
         monthNumber = 1;
      else
         monthNumber = m;
   }

   /**
      The getMonthNumber method gets the <strong class="highlight">month</strong> number.
      @return The number of the <strong class="highlight">month</strong>.
   */

   public int getMonthNumber()
   {
      return monthNumber;
   }

   /**
      The getMonthName method gets the name of the <strong class="highlight">month</strong>.
      @return The name of the <strong class="highlight">month</strong>.
   */

   public String getMonthName()
   {
      String name;
      
      switch (monthNumber)
      {
         case 1:  name = "January";
                  break;
         case 2:  name = "February";
                  break;
         case 3:  name = "March";
                  break;
         case 4:  name = "April";
                  break;
         case 5:  name = "May";
                  break;
         case 6:  name = "June";
                  break;
         case 7:  name = "July";
                  break;
         case 8:  name = "August";
                  break;
         case 9:  name = "September";
                  break;
         case 10: name = "October";
                  break;
         case 11: name = "November";
                  break;
         case 12: name = "December";
                  break;
         default: name = "Unknown";
      }
      
      return name;
   }

   /**
      toString method
      @return A reference to a String representation of
              the object.
   */
   
   public String toString()
   {
      return getMonthName();
   }
   
   /**
      equals method
      @param month2 Another <strong class="highlight">Month</strong> object.
      @return true if the two <strong class="highlight">Month</strong> objects contain
              the same <strong class="highlight">month</strong>, false otherwise.
   */
   public boolean equals(Month month2)
   {
      boolean status;
      
      if (month2.getMonthNumber() == monthNumber)
         status = true;
      else
         status = false;
      
      return status;
   }

   /**
      greaterThan method
      @param month2 Another <strong class="highlight">Month</strong> object.
      @return true if the calling objects is greater
              than the argument, false otherwise.
   */
   
   public boolean greaterThan(Month month2)
   {
      boolean status;
      
      if (monthNumber > month2.getMonthNumber())
         status = true;
      else
         status = false;
      
      return status;
   }
   
   /**
      lessThan method
      @param month2 Another <strong class="highlight">Month</strong> object.
      @return true if the calling objects is less
              than the argument, false otherwise.
   */
   
   public boolean lessThan(Month month2)
   {
      boolean status;
      
      if (monthNumber < month2.getMonthNumber())
         status = true;
      else
         status = false;
      
      return status;
   }
}
3
Contributors
4
Replies
12
Views
7 Years
Discussion Span
Last Post by moutanna
0

Line 25: System.out.println(object5.mName()); , I doubt that the Month class has a method mName() .
What errors do you get and what are you trying to do?

0

Line 25: System.out.println(object5.mName()); , I doubt that the Month class has a method mName() .
What errors do you get and what are you trying to do?

Trying to get it where the user inputs either a number, or the month. And it will show both the month and the number... I tried to take out the mName thing... but it only shows all the months and numbers... then lets the user input, then it cancels for some reason.

0

Your main should look like:

public static void main(String[] args) {
        // Use the no-arg constructor.
        Month object1 = new Month();
        System.out.println("Month " + object1.getMonthNumber()
                + " = " + object1);
        Month object2 = new Month(2);
        for (int i = 1; i <= 12; i++) {
            object2.setMonthNumber(i);
            System.out.println("Month " + object2.getMonthNumber()
                    + " name is " + object2);
        }

        System.out.println("Enter a month name/Number or Q to quitte: ");
        Month object5;

        while (true) {
            Scanner keyboard = new Scanner(System.in);
            int intMonth;
            String strMonth = keyboard.next();
            if (strMonth.equals("Q")) {
                break;
            }
            try {
                intMonth = Integer.parseInt(strMonth);
                object5 = new Month(intMonth);
                System.out.println(object5.getMonthName());
            } catch (Exception e) {
                object5 = new Month(strMonth);
                System.out.println(object5.getMonthNumber());
            }

        }

    }

This will retrives the month name if you type a number and the month number if you type a month name and will exit if you type "Q".
Hope it helps

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.