Programming Question

Discussion in 'OT Technology' started by morgue, Oct 31, 2006.

  1. morgue

    morgue New Member

    Joined:
    Jul 10, 2004
    Messages:
    8,278
    Likes Received:
    0
    I have this question on my assigment but I don't fully understand what they want me to do, it says:

    Use inheritance to create an exception superclass (called ExceptionA) and exception subclasses ExceptionB and ExceptionC, where ExceptionB inherits from ExceptionA and ExceptionC inherits from ExceptionB. Write a program to demonstrate that catch block for type ExceptionA catches exceptions of types ExceptionB and ExeptionC.

    So say I have class ExceptionA
    Code:
    import java.util.Scanner;
    
    public class ExceptionA 
    {
        public ExceptionA ( )
        {
            Scanner scanner = new Scanner( System.in );
            boolean continueLoop = true;
            
            do
                try
                {
                    System.out.print("Integer 10 divided by (Type an integer number a press Enter): ");
                    int denominator = scanner.nextInt();
                    System.out.printf("Result: %d\n", 10/denominator);
                    continueLoop = false;            
                } // end try
                catch ( ArithmeticException arithmeticException )
                {
                    System.err.printf( "\nException: %s\n", arithmeticException );
                    System.out.println("Zero is an invalid denominator. Please try again.\n" );
                }
            while (continueLoop);
        }
    }
    
    Now I'd need
    class ExceptionB extends ExceptionA
    class ExceptionC extends ExceptionB
    and class ExteptionTest (with the main)

    I don't understand... are they asking me to create a method or what?
     
  2. simbo855

    simbo855 New Member

    Joined:
    Aug 17, 2006
    Messages:
    224
    Likes Received:
    0
    so glad i stopped programming
     
  3. GOGZILLA

    GOGZILLA Double-Uranium Member

    Joined:
    Jan 16, 2003
    Messages:
    10,760
    Likes Received:
    3
    Location:
    Plantation, FL
    They want your main program to throw exceptions of type B and C and catch them using only ExceptionA
     
  4. morgue

    morgue New Member

    Joined:
    Jul 10, 2004
    Messages:
    8,278
    Likes Received:
    0
    more info?
    :sadwavey:
     
  5. samm

    samm Next in Line

    Joined:
    Dec 22, 2000
    Messages:
    2,630
    Likes Received:
    0
    Location:
    San Jose, CA
    what do you not understand? GOGZILLA explained pretty much everything.
     
  6. morgue

    morgue New Member

    Joined:
    Jul 10, 2004
    Messages:
    8,278
    Likes Received:
    0
    How do I do that? :(

    This is what I've done so far and I think I'm on the right track
    Code:
    
    public class ExceptionTest {
        
        public static int division(int denom)
            throws ExceptionA
        {
            return 10/denom
        }
        
        public static void main(String args[]) {
            try
            {
                System.out.print("Integer 10 divided by (Type an integer number a press Enter): ");
                int denominator = scanner.nextInt();
                System.out.printf("Result: %d\n", division(denominator));
                continueLoop = false;            
            } // end try
            catch ( ExceptionA eA )
            {
                System.out.println("Exception A" );
            }
            catch ( ExceptionB exceptionB )
            {
                System.out.println("Exception A" );
            }
            catch ( ExceptionC exceptionC )
            {
                System.out.println("Exception A" );
            }
        }
    }
     
  7. samm

    samm Next in Line

    Joined:
    Dec 22, 2000
    Messages:
    2,630
    Likes Received:
    0
    Location:
    San Jose, CA
    You're on the right track, but according to your assignment you should only have one catch block where you catch exceptions of type ExceptionA.
     
  8. morgue

    morgue New Member

    Joined:
    Jul 10, 2004
    Messages:
    8,278
    Likes Received:
    0
    Ok so something like this then
    Code:
    
    public class ExceptionTest {
        
        public static int division(int denom)
            throws ExceptionA
        {
            return 10/denom;
        }
        
        public static void main(String args[]) {
            try
            {
                System.out.print("Integer 10 divided by (Type an integer number a press Enter): ");
                int denominator = scanner.nextInt();
                System.out.printf("Result: %d\n", division(denominator));
                //continueLoop = false;            
            } // end try
            catch ( ExceptionA exceptionA )
            {
                System.out.println("Exception A" );
            }
        }
    }
     
    Last edited: Oct 31, 2006
  9. morgue

    morgue New Member

    Joined:
    Jul 10, 2004
    Messages:
    8,278
    Likes Received:
    0
    Ok here's this, now what? :(

    Code:
    import java.util.Scanner;
    
    public class ExceptionTest {
        
        public static int division(int denom)
            throws ExceptionA
        {
            return 10/denom;
        }
        
        public static void main(String args[]) {
            Scanner scanner = new Scanner( System.in );
            try
            {
                
                System.out.print("Integer 10 divided by (Type an integer number a press Enter): ");
                int denominator = scanner.nextInt();
                System.out.printf("Result: %d\n", division(denominator));
                //continueLoop = false;            
            } // end try
            catch ( ExceptionA exceptionA )
            {
                System.out.println("Exception A" );
            }
        }
    }
     
  10. morgue

    morgue New Member

    Joined:
    Jul 10, 2004
    Messages:
    8,278
    Likes Received:
    0
    I think this is it, isn't it?

    Code:
    import java.util.Scanner;
    
    public class ExceptionTest {
        
        
        public static int division(int denom) 
        {
            return 10/denom;
        }
        
        
        public static void main(String args[]) throws ExceptionA
        {
            Scanner scanner = new Scanner( System.in );
            boolean continueLoop = true;
            
            do{
                try
                {
                    
                    System.out.println("Chosse exception:");
                    System.out.printf("1. ExceptionA\n2. ExceptionB\n3. ExceptionC\n4. Exit Program\n");
                    int option = scanner.nextInt();
                    if (option == 1)
                        throw new ExceptionA();
                    else if (option == 2)
                        throw new ExceptionB();
                    else if (option == 3)
                        throw new ExceptionC();
                    else if (option == 4)
                        continueLoop = false;        
                } // end try
                catch ( ExceptionA eA )
                {
                    System.out.println("Exception occured" );
                }
                /*
                catch (ExceptionB eB )
                {
                    System.out.println("Exception B occured" );
                    throw new ExceptionC();
                }
                catch (ExceptionC eC )
                {
                    System.out.println("Exception C occured");
                }
                */
            } while( continueLoop );
        } //end main
    } //end class ExceptionTest
     
  11. GOGZILLA

    GOGZILLA Double-Uranium Member

    Joined:
    Jan 16, 2003
    Messages:
    10,760
    Likes Received:
    3
    Location:
    Plantation, FL
    b&c need to be derived from exception A. the point of this assignment is to show polymorphism by using a base class generalized pointer to be able to look up derived class exceptions based on type.
     

Share This Page