Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Help with exception handling.

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with exception handling.

    Hello guys,

    I have this assignment for my homework,

    Write a class named Month. The class should have an int field named monthNumber which will hold the number of the month. For example, January would be 1, February would be 2, etc. In addition, provide the following methods:

    · A default constructor that sets the monthNumber field to 1.

    · A constructor that accepts the number of the month as an argument. It should set the monthNumber field to the value passed as the argument.

    · A constructor that accepts the name of the month, such as “January” , “February”, etc., as an argument. It should set the monthNumber field to the corresponding value for the month named.

    · A setMonthNumber method that accepts an int argument, which is assigned to the monthNumber field.

    · A getMonthNumber method that returns the value in the monthNumber field.

    · A toString method that returns the same value as the getMonthName method.

    Also 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.

    I've wrote the code already and it's compiling and working perfectly, but I can't do the exception handling and would appreciate any help....

    public class Month
    {
    	private int monthNumber;
     
    	public Month()
    	{
    		monthNumber = 1;
    	}
    	public Month(int number)
    	{
    		if (number < 1 || number > 12)
    		{
    			monthNumber = 1;
    		}
    		else
    		{
    			number = monthNumber;
    		}
    	}
     
    	public Month(String monthName)
    	{
    		if (monthName.equals("jan"))
    		{
    			monthNumber = 1;
    		}
    		else if (monthName.equals("feb"))
    		{
    			monthNumber = 2;
    		}
    		else if (monthName.equals("mar"))
    		{
    			monthNumber = 3;
    		}
    		else if (monthName.equals("apr"))
    		{
    			monthNumber = 4;
    		}
    		else if (monthName.equals("may"))
    		{
    			monthNumber = 5;
    		}
    		else if (monthName.equals("june"))
    		{
    			monthNumber = 6;
    		}
    		else if (monthName.equals("july"))
    		{
    			monthNumber = 7;
    		}
    		else if (monthName.equals("aug"))
    		{
    			monthNumber = 8;
    		}
    		else if (monthName.equals("sept"))
    		{
    			monthNumber = 9;
    		}
    		else if (monthName.equals("oct"))
    		{
    			monthNumber = 10;
    		}
    		else if (monthName.equals("nov"))
    		{
    			monthNumber = 11;
    		}
    		else if (monthName.equals("dec"))
    		{
    			monthNumber = 12;
    		}
    	}
    	public void setMonthNumber(int number)
    	{
    		monthNumber = num;
    	}
    	public int getMonthNumber()
    	{
    		return monthNumber;
    	}
    	public String getMonthName()
    	{
    		String monthName;
     
    		switch (monthNumber)
    		{
    			case 1: monthName = "january";
    			break;
    			case 2: monthName = "february";
    			break;
    			case 3: monthName = "march";
    			break;
    			case 4: monthName = "april";
    			break;
    			case 5: monthName = "may";
    			break;
    			case 6: monthName = "june";
    			break;
    			case 7: monthName = "july";
    			break;
    			case 8: monthName = "august";
    			break;
    			case 9: monthName = "september";
    			break;
    			case 10: monthName = "october";
    			break;
    			case 11: monthName = "november";
    			break;
    			default: monthName = "december";
    		}
    		return monthName;
    	}
    	public String toString()
    	{
    		return getMonthName();
    	}
    	public boolean equals(Month month)
    	{
    		boolean result;
     
    		if (month.getMonthNumber() == monthNumber)
    		{
    			result = true;
    		}
    		else
    		{
    			result = false;
    		}
    		return result;
    	}
    	public boolean greaterThan(Month month)
    	{
    		boolean result;
     
    		if (month.getMonthNumber() < monthNumber)
    		{
    			result = true;
    		}
    		else
    		{
    			result = false;
    		}
    		return result;
    	}
    	public boolean lessThan(Month month)
    	{
    		boolean result;
     
    		if (month.getMonthNumber() > monthNumber)
    		{
    			result = true;
    		}
    		else
    		{
    			result = false;
    		}
    		return result;
    	}
    }

    Thanks!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with exception handling.

    I can't do the exception handling
    Can you explain your problem?
    What exceptions are you trying to handle? By handle I assume you mean use try{}catch blocks.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with exception handling.

    Exception classes for if the month number entered is less than 1 or more than 12 and if an invalid string name was entered.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with exception handling.

    Have you read the tutorial:
    Lesson: Exceptions (The Java™ Tutorials > Essential Classes)

    Basically exceptions are classes that work with the throw statement. You can define your own and throw them as needed.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. I/O AND EXCEPTION HANDLING need help
    By awood in forum What's Wrong With My Code?
    Replies: 6
    Last Post: February 3rd, 2013, 04:22 PM
  2. [SOLVED] exception handling
    By Topflyt in forum Exceptions
    Replies: 5
    Last Post: December 22nd, 2011, 12:00 PM
  3. [SOLVED] Exception Handling
    By Melawe in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 21st, 2011, 07:39 PM
  4. Exception handling
    By JavaGirl9001 in forum Object Oriented Programming
    Replies: 1
    Last Post: November 26th, 2011, 08:45 PM
  5. Exception handling
    By AnithaBabu1 in forum Exceptions
    Replies: 6
    Last Post: August 27th, 2008, 09:37 AM