Results 1 to 5 of 5
  1. #1
    starplayerrob is offline Member
    Join Date
    Apr 2011
    Posts
    37
    Rep Power
    0

    Question WOW need help with Exceptions

    these are my instructions of what I have to do:

    CISM 2230: Advanced JAVA - Program 7: Exception Handling

    Create a new package in Eclipse called MonthExceptionPkg. In this package you will create 3 different Java files that demonstrate the use of Exception handling.

    MonthNumberException
    Create a custom class called MonthNumberException. This class will be used to display an error message back to the user if they do not correctly enter a number between 1 and 12. The custom class will have:
    • a null constructor with an error message that says “Error! You must enter a number between 1 and 12”.
    • a toString method with an error message that says “Error! MonthNumberException”

    Month Class
    Write a class name Month. The class should have a int field name monthNumber that holds the number of the month. In addition, provide the following methods:
    • 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.
    • If a value less than 1 or greater than 12 is passed, the constructor should THROW a custom MonthNumberException instance.
    • A setMonthNumber method
    • A getMonthNumber method
    • A getMonthName method that returns the name of the month, using an If/Else
    • A toString that returns the same value as the getMonthName method.

    MonthDemo
    Write a demo program with a main method that will test out the Month Class and use Try/Class blocks to handle exceptions. The program should accomplish the following tasks:
    • Prompt the user to enter a month
    • Use the response to create an instance of the Month class
    • Display the month name back to the user (calling either the getMonthName method or the toString)
    • Set up multiple CATCH blocks to handle any thrown exceptions such as MonthNumberException, NumberFormatException, and Exception, (use all three techniques for displaying error messages – getMessage, toString, and hard coded)

    When finished:
    o Submit a soft copy of your completed program thru the Program 7 link on WebCT
    o turn in a hardcopy of your program source code with complete documentation
    o screen shots of your JOP error/Exception messages
    o UML diagram of Month

    and i have started it and have been working on it all day but still can't seem to understand the try/catch and exception parts. Im really lost and am just trying to get where i understand this fully. if anyone could help it would be great, i will post what i have but i dont think it is right. and its not finished. im stuck and need help terribly. thank you in advance.

    month class:
    Java Code:
    package MonthExceptionPkg;
    import java.text.DateFormatSymbols;
    import java.util.Scanner;
    
    public class Month {
    int monthNumber;
    int monthName;
    	public Month(int monthNumber){
    		new Month(monthNumber);}
    		public Month() throws IOException {
    			if (monthNumber < 1 || monthNumber > 12){
    				throw new IOException();
    				public int getMonthNumber() {
    					return monthNumber;}
    				public void setMonthNumber(int monthNumber) {
    					this.monthNumber = monthNumber;}
    				public String getMonth(int month) {
    				    return new DateFormatSymbols().getMonths()[month-1];
    		        if (monthNumber == 1)
    		              System.out.println("January");
    		        else if (monthNumber == 2)
    		              System.out.println("February");
    		        else if (monthNumber == 3)
    		              System.out.println("March");
    		        else if (monthNumber == 4)
    		              System.out.println("April");
    		        else if (monthNumber == 5)
    		              System.out.println("May");
    		        else if (monthNumber == 6)
    		              System.out.println("June");
    		        else if (monthNumber == 7)
    		              System.out.println("July");
    		        else if (monthNumber == 8)
    		              System.out.println("August");
    		        else if (monthNumber == 9)
    		              System.out.println("September");
    		        else if (monthNumber == 10)
    		              System.out.println("October");
    		        else if (monthNumber == 11)
    		              System.out.println("November");
    		        else if (monthNumber == 12)
    		              System.out.println("December");
    		        else
    		              System.out.println("Invalid month.");}
    		        public void setMonthName(int monthName) {
    					this.monthName = monthName;}		
    				}
    month number exception class:
    Java Code:
    package MonthExceptionPkg;
    import javax.swing.JOptionPane;
    public class MonthNumberException {
    
    	public MonthNumberException() {
    		JOptionPane.showMessageDialog(null, "You must enter a number between 1 and 12",
    		        "ERROR!", JOptionPane.ERROR_MESSAGE);
    	}
    	public void setMonthNumber(int newMonthNumber){
    		monthNumber = newMonthNumber;
    	}
    
    }
    month demo class:
    Java Code:
    package MonthExceptionPkg;
    import java.util.Scanner;
    
    import javax.swing.JOptionPane;
    
    
    public class MonthDemo {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		Scanner in = new Scanner(System.in);
            System.out.print("Enter month's number: ");
            monthNumber = in.nextInt();
            try{
            	anObject.MonthNumber();
            } catch (MonthNumberException ex){
            	JOptionPane.showMessageDialog(null, "You must enter a number between 1 and 12",
        		        "ERROR!", JOptionPane.ERROR_MESSAGE);
            }
            }
    	}

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: WOW need help with Exceptions

    Are you sure that you want to display a JOptionPane from within the MonthNumberException class? And why would that class have a setMonthNumber method? I'm no expert on exceptions, but it would seem to me that an Exception class's only job is to notify that an Exception has occurred, usually by calling the super constructor and passing a message into it. The classes that handle the exception will then decide what to do with the information once the exception notifies them, and their behaviors might include showing the JOptionPane -- or not as they choose, but that's not the job of the Exception class itself.

  3. #3
    starplayerrob is offline Member
    Join Date
    Apr 2011
    Posts
    37
    Rep Power
    0

    Default Re: WOW need help with Exceptions

    your right, sorry that setMonthNumber is not supposed to be there. and the joptionane is needed in order to send out the error message, right? and im lost on the try catch methods. never had to do them before. thank you for the quick responce

  4. #4
    starplayerrob is offline Member
    Join Date
    Apr 2011
    Posts
    37
    Rep Power
    0

    Default Re: WOW need help with Exceptions

    also having problems with lines 8- 12 in month class. not sure if i did that part right. thats where i "threw" the exception.

  5. #5
    2by4 is offline Banned
    Join Date
    Dec 2011
    Posts
    143
    Rep Power
    0

    Default Re: WOW need help with Exceptions

    Exceptions help you to separate erroneous scenarios from normal scenarios within your code. Normal scenarios will be dealt with in the main part of your code and erroneous scenarios will be dealt with in catch blocks (exception handlers). It makes your code easier to read.

    So, the first question you may wish to consider is, "what is the normal scenario and what is the erroneous scenario and where does it first occur"?

    HINT: Something can be out of range. What is it, and where is it first set? That code must be in a try block and a part of that code must throw an exception. It will be caught in the catch block following the try block.

    A method (or constructor) should not normally exit with its object having an invalid state. One way to ensure this is for the method or constructor to validate the input and then deal with valid input in the main body of its scope, or erroneous input in a catch block. So input (method or constructor argument) validation is often a good thing to have at the beginning of the method or constructor, before any changes are made or objects are instantiated.

    There are now two options. The catch block can deal with the exception itself and return normally with a return statement, or it can throw an exception itself. If it throws an exception, the method or constructor returns to its caller with an exception just as if you had put a throw clause where the call was. The caller can then deal with exception. It too can return normally to its caller or throw the exception up the call chain. As you can see, the erroneous scenario is dealt with in catch statements somewhere in the call chain and the code can get back a recovery point easily.

    Without catch blocks the method or constructor would have to pass specific error codes back to the caller to say something unusual has happened, and normal code would be interspersed with error handling code. With catch blocks, if the caller is 6 calls up the chain, the last 5 would simply clean up any half changes in their object and throw the exception up the chain. The top method could display a UI message for the user. Imagine doing that without try-catch blocks? You would have to return error status. What if your method already returns something else useful?

Similar Threads

  1. Exceptions?
    By linc186 in forum New To Java
    Replies: 3
    Last Post: 03-07-2011, 09:03 AM
  2. Exceptions
    By Nerijus in forum New To Java
    Replies: 8
    Last Post: 05-18-2010, 02:44 PM
  3. Exceptions & More
    By besweeet in forum New To Java
    Replies: 12
    Last Post: 04-29-2010, 10:06 PM
  4. Exceptions
    By hedonist in forum New To Java
    Replies: 10
    Last Post: 09-08-2009, 09:38 AM
  5. Need Help With Exceptions
    By maggie_2 in forum New To Java
    Replies: 5
    Last Post: 12-15-2008, 08:12 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •