Question

Need help with Exception coding in JAVA

The aims of this lab are: 

  1. Understand the purpose of exception handling

  2. How to create your own exception class.

  3. Use of assertion


1. Create a class TutorialSpace that used to enrol student in a tutorial class. It should have the following private attributes:

  • slots — the number of available slots in a tutorial class

  • activated — true if the tutorial class has been activated 


and the following methods:

  • TutorialSpace(n) — a constructor for a tutorial class with n slots. 

  • activate() — activates the tutorial class. Throws an exception if the tutorial class has already been activated. 

  • reserveSlot()—enrol a student into the tutorial class by decreasing the number of slots left for enrolling in the class. Throws an exception if slot is either completely used or the tutorial class is not active. 

  • slotsRemaining()—returns the number of slots remaining in the tutorial class.


To handle the two exceptions as described before, you will need to write two exception classes: NotActivatedException and EmptyException


2. Write a client program, registerClass, to test the use of the TutorialSpace class. Make sure you consider a testing strategy that can test all the methods and exceptions in the TutorialSpace class. 


0 0
Add a comment Improve this question Transcribed image text
Answer #1

public class TutorialSpace {

        

        // slots — the number of available slots in a tutorial class

        private int slots;

        // activated — true if the tutorial class has been activated

        private boolean activated;

        

        /**

        * TutorialSpace(n) — a constructor for a tutorial class with n slots.

        * 

        * @param n

        */

        public TutorialSpace(int n) {

                this.slots = n;

                this.activated = false;

        }

        

        /**

        * activate() — activates the tutorial class. Throws an exception if the

        * tutorial class has already been activated.

        * 

        * @throws NotActivatedException

        */

        

        public void activate() throws NotActivatedException {

                if (activated) {

                        throw new NotActivatedException("Already Activated");

                } else {

                        activated = true;

                }

        }

        

        /**

        * reserveSlot()—enrol a student into the tutorial class by decreasing the

        * number of slots left for enrolling in the class. Throws an exception if slot

        * is either completely used or the tutorial class is not active.

        * 

        * @throws EmptyException

        */

        public void reserveSlot() throws EmptyException {

                

                if (!activated || slots == 0) {

                        throw new EmptyException("Tutorial space is empty");

                } else {

                        slots--;

                }

        }

        

        /**

        * slotsRemaining()—returns the number of slots remaining in the tutorial class.

        */

        

        public int slotsRemaining() {

                return slots;

        }

        

}




public class NotActivatedException extends Exception {

        

        /**

        * 

        */

        private static final long serialVersionUID = 1L;

        

        public NotActivatedException(String msg) {

                super(msg);

        }

        

}




public class EmptyException extends Exception {

        

        /**

        * 

        */

        private static final long serialVersionUID = 1L;

        public EmptyException(String msg) {

                super(msg);

        }

        

        

}


answered by: codegates
Add a comment
Know the answer?
Add Answer to:
Need help with Exception coding in JAVA
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Need help with writing a Client test in JAVA

    public class TutorialSpace {                  // slots — the number of available slots in a tutorial class         private int slots;         // activated — true if the tutorial class has been activated         private boolean activated;                  /**         * TutorialSpace(n) — a constructor for a tutorial class with n slots.         *          * @param n         */         public TutorialSpace(int n) {                 this.slots = n;                 this.activated = false;         }                  /**         * activate() — activates the tutorial class. Throws an exception if the         * tutorial class has already been activated.         *          * @throws NotActivatedException         */                  public void activate() throws NotActivatedException {                 if (activated) {                         throw new NotActivatedException("Already Activated");                 } else {                         activated = true;                 }         }                  /**         * reserveSlot()—enrol a student into the tutorial class by decreasing the         * number of slots left for enrolling in the class. Throws an exception if slot         * is either completely used or the tutorial class is not active.         *          * @throws EmptyException         */         public void reserveSlot() throws EmptyException {                                  if (!activated || slots == 0) {                         throw new EmptyException("Tutorial space is empty");                 } else {                         slots--;                 }         }                  /**         * slotsRemaining()—returns the number of slots remaining in the tutorial class.         */                  public int slotsRemaining() {                 return slots;         }          } public class NotActivatedException extends Exception {                  /**         *          */         private static final long serialVersionUID = 1L;                  public NotActivatedException(String msg) {                 super(msg);         }          } public class EmptyException extends Exception {                  /**         *          */         private static final long serialVersionUID = 1L;         public EmptyException(String msg) {                 super(msg);         }                  ...

  • Advanced Object-Oriented Programming using Java Assignment 4: Exception Handling and Testing in Java Introduction -  This assignment...

    Advanced Object-Oriented Programming using Java Assignment 4: Exception Handling and Testing in Java Introduction -  This assignment is meant to introduce you to design and implementation of exceptions in an object-oriented language. It will also give you experience in testing an object-oriented support class. You will be designing and implementing a version of the game Nim. Specifically, you will design and implement a NimGame support class that stores all actual information about the state of the game, and detects and throws...

  • IN JAVA: Write a class, ZeroException, which is an Exception, and is used to signal that somethi...

    IN JAVA: Write a class, ZeroException, which is an Exception, and is used to signal that something is zero when it shouldn't be. Write the class ArrayManipulator which creates an array and provides several methods to manipulate values taken from an array. ZeroException Task: Exceptions are used to signal many types of problems in a program. We can write our own as well to describe specific exceptional conditions which may arise. The advantage of doing so is that when exceptions...

  • Needs Help In Java Programming language Exceptional Cars Purpose To review interfaces and exception usage. Directions...

    Needs Help In Java Programming language Exceptional Cars Purpose To review interfaces and exception usage. Directions Your task is to write a class called Car. Your class should have the following fields and methods: private int position private boolean headlightsOn public Car() - a constructor which initializes position to 0 and headlightsOn to false public Car(int position) - a constructor which initializes position to the passed in value and headlightsOn to false, and it should throw a NegativeNumberException with a...

  • Exception handling is a powerful tool used to help programmers understand exception errors. This tool separates...

    Exception handling is a powerful tool used to help programmers understand exception errors. This tool separates the error handling routine from the rest of the code. In this application, you practice handling exception errors. You use sample code that was purposefully designed to generate an exception error, and then you modify the code so that it handles the errors more gracefully. For this Assignment, submit the following program: The following code causes an exception error: import java.io.BufferedReader; import java.io.IOException; import...

  • Background: The purpose of this assignment is to practice dealing with exception handling and textual data....

    Background: The purpose of this assignment is to practice dealing with exception handling and textual data. Exception handling is a very important part of being an object-oriented programming. Rather returning some kind of int return value every time you tickle an object, C++ programmers expect methods to focus on their task at hand. If something bad happens, C++ programmers expect methods to throw exceptions. When caught, exceptions can be processed. When uncaught, they cause a program to terminate dead in...

  • JAVA Which of these statements does not match by using exception action in Java? 1. Exception...

    JAVA Which of these statements does not match by using exception action in Java? 1. Exception action makes it possible for the calling method to handle errors in called methods.    2. Exception action simplifies programming since incorrect reporting and handling can be located in catch blocks separately from the rest of the code.    3. Exception action increases the performance of programs. 4. Java separates exception action from common processes. Why create instances of File Class? - several alternatives...

  • Need some help on the following problem in java. I have to modify the code I...

    Need some help on the following problem in java. I have to modify the code I written so far(below) to do the following three things: a). Write a method called diagnostics that occasionally throws any one of the exceptions you have created depending on the values generated by a random-number generator. b). Write a method called display that calls diagnostics and provides exception handlers to display a message when an exception occurs. Otherwise, if an exception does not occur, method...

  • I ONLY need question 2 part C AND D answered...it is in bold. Please do not...

    I ONLY need question 2 part C AND D answered...it is in bold. Please do not use hash sets or tables. In java, please implement the classes below. Each one needs to be created. Each class must be in separate file. The expected output is also in bold. I have also attached the driver for part 4 to help. 1. The Student class should be in the assignment package. a. There should be class variable for first and last names....

  • Chapter 08 Python Assignment: Question 1-5 Please I need help in my python course. Question 1...

    Chapter 08 Python Assignment: Question 1-5 Please I need help in my python course. Question 1 The finally clause of a try statement a. can be used to recover from an exception b. is required c. can be used to display more information about an exception d. is executed whether or not an exception has been thrown 10 points Question 2 Which of the following is the correct way to code a try statement that catches any type of exception...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT