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

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

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

  • IN JAVA Write a class Store which includes the attributes: store name, city. Write another class...

    IN JAVA Write a class Store which includes the attributes: store name, city. Write another class encapsulating an Art Gallery, which inherits from Store. An Art Gallery has the following additional attributes: how many paintings are sold every year and the number of artists submitting artwork. Code the constructor, accessors, mutators, toString and equals method of the super class Store. Code the constructor, accessors and mutators for the subclass Art Gallery. In the Art Gallery class, also code a method...

  • Write a Java class that implements the concept of Coins, assuming the following attributes (variables): number...

    Write a Java class that implements the concept of Coins, assuming the following attributes (variables): number of quarters, number of dimes, number of nickels, and number of pennies. Include two constructors (non-argument constructor that assigns 1 to each coin, and another constructor that takes necessary arguments to create an object), needed getter and setter methods, method toString (to print coins objects in a meaningful way), and method equals (to compare two coins objects based on number of coins). Also include...

  • In JAVA, please In this module, you will combine your knowledge of class objects, inheritance and...

    In JAVA, please In this module, you will combine your knowledge of class objects, inheritance and linked lists. You will need to first create an object class encapsulating a Trivia Game which INHERITS from Game. Game is the parent class with the following attributes: 1. description - which is a string 2. write the constructor, accessor, mutator and toString methods. Trivia is the subclass of Game with the additional attributes: 1. trivia game id - integer 2. ultimate prize money...

  • I need the following written in Java please, thank you ' We want you to implement...

    I need the following written in Java please, thank you ' We want you to implement a java class that will show details on users and throw exceptions where needed. The following requirements specify what fields you are expected to implement in your User class: - A private String firstName that specifies the first name of the user - A private String lastName that specifies the last name of the user - A private int age that specifies user's age...

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

  • Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the...

    Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the concept of a college course, assuming that a course has following attributers: code (for instance COSC1337), a description, and a number of credits (for instance 3). Include a constructor, the accessors, mutators and methods ‘toString’, ‘equals’, and ‘finalize’. Write a client class to test the behavior of the class and its methods. The outline of the class is given as follows: public class Course...

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
ADVERTISEMENT