bartleby

Concept explainers

bartleby

Videos

Expert Solution & Answer
Book Icon
Chapter 9, Problem 10E

Explanation of Solution

Creating a class "RoomCounter.java":

  • Import required package.
  • Define the class "RoomCounter".
    • Declare a private variable "countPeople".
    • Define a default constructor and initialize the value.
    • Give function definition to "addPerson ()".
      • Increment the value of "countPeople".
    • Give function definition to "removePerson ()".
      • Check of the value of "countPeople" is less than or equal to 0.
        • Throw "NegativeCounterException" with a message.
          • Decrement the value of "countPeople".
    • Give function definition to "getcount ()".
      • Return the value of "countPeople".
    • Define the "main ()" method.
      • Create an object "rc" for the class "RoomCounter".
      • Get and print the value of count by using the function "rc.getCount ()".
      • Add three persons by calling the function "addPerson ()" three times.
      • Get and print the value of count by using the function "rc.getCount ()".
      • Inside "try" block,
        • Remove two persons by calling the function "removePerson ()" two times.
        • Get and print the value of count by using the function "rc.getCount ()".
        • Remove one person by calling the function "removePerson ()" one time.
        • Get and print the value of count by using the function "rc.getCount ()".
        • Again remove one person by calling the function "removePerson ()" one time.
        • Get and print the value of count by using the function "rc.getCount ()".
          • Catch "NegativeCounterException".
            • Get and print the message by using the function "getMessage ()".

Creating a class "NegativeCounterException.java":

  • Define the class "NegativeCounterException" that extends "Exception".
    • Define parameterized constructor.
      • Call the parent class’s method by using "super" keyword.

Program:

RoomCounter.java:

//Define a class

public class RoomCounter

{

//Declare a private variable

private int countPeople;

//Define a default constructor

public RoomCounter()

{

//Assign 0

countPeople = 0;

}

//Give function definition to add person

public void addPerson()

{

//Increment the variable

countPeople++;

}

//Function definition to remove person

public void removePerson()throws NegativeCounterException

{

//Check if count is less than or equal to 0

if(countPeople <= 0 )

//Throw an exception

throw new NegativeCounterException("Can not remove a person");

//Decrement the counter variable

countPeople--;

}

//Function to get count

public int getCount()

{

//Return count

return countPeople;

}

//Define the main method

public static void main(String[] args)

{

//Create an object for the class

RoomCounter rc = new RoomCounter();

//Get the count after invoking default constructor

System...

Blurred answer
Students have asked these similar questions
Draw out an example of 3 systems using Lamport’s logical clock and explain the steps in words.
"Systems have become very powerful and sophisticated, providing quality information fordecisions that enable the firm to coordinate both internally and externally."With reference to the above statement compare the operations of any three data gatheringsystems today’s organisations use to aid decision making.
labmas Course Home XDocument courses/13810469/menu/a2c41aca-b4d9-4809-ac2e-eef29897ce04 There are three ionizable groups (weak acids and/or bases) in glutamic acid. Label them on the structure below Drag the appropriate labels to their respective targets. OOH [] CH3N CH CH2 CH2 IC HO Reset Help

Chapter 9 Solutions

Java: An Introduction to Problem Solving and Programming (8th Edition)

Chapter 9.1, Problem 11STQ Chapter 9.1, Problem 12STQ Chapter 9.1, Problem 13STQ Chapter 9.1, Problem 14STQ Chapter 9.2, Problem 15STQ Chapter 9.2, Problem 16STQ Chapter 9.2, Problem 17STQ Chapter 9.2, Problem 18STQ Chapter 9.2, Problem 19STQ Chapter 9.2, Problem 20STQ Chapter 9.2, Problem 21STQ Chapter 9.2, Problem 22STQ Chapter 9.2, Problem 23STQ Chapter 9.3, Problem 24STQ Chapter 9.3, Problem 25STQ Chapter 9.3, Problem 26STQ Chapter 9.3, Problem 27STQ Chapter 9.3, Problem 28STQ Chapter 9.3, Problem 29STQ Chapter 9.3, Problem 30STQ Chapter 9.3, Problem 31STQ Chapter 9.3, Problem 32STQ Chapter 9.3, Problem 33STQ Chapter 9.3, Problem 34STQ Chapter 9.3, Problem 35STQ Chapter 9.4, Problem 36STQ Chapter 9.4, Problem 37STQ Chapter 9.4, Problem 38STQ Chapter 9, Problem 1E Chapter 9, Problem 2E Chapter 9, Problem 3E Chapter 9, Problem 4E Chapter 9, Problem 5E Chapter 9, Problem 6E Chapter 9, Problem 8E Chapter 9, Problem 9E Chapter 9, Problem 10E Chapter 9, Problem 11E Chapter 9, Problem 12E Chapter 9, Problem 13E Chapter 9, Problem 1P Chapter 9, Problem 2P Chapter 9, Problem 3P Chapter 9, Problem 2PP Chapter 9, Problem 3PP Chapter 9, Problem 7PP Chapter 9, Problem 10PP Chapter 9, Problem 11PP

Additional Engineering Textbook Solutions

Find more solutions based on key concepts
// Class declaration public class CheckPoint { // Main function public static void main(String[] args) { // Dec...

Starting Out with Java: From Control Structures through Objects (7th Edition) (What's New in Computer Science)

The force in members DC, HI, and JI of the truss and the state of members are in tension or compression.

INTERNATIONAL EDITION---Engineering Mechanics: Statics, 14th edition (SI unit)

One class is derived from another class. The subclass is the derived class, which means the subclass contains m...

Starting Out with Programming Logic and Design (5th Edition) (What's New in Computer Science)

Knowledge Booster
Background pattern image
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
    SEE MORE QUESTIONS
    Recommended textbooks for you
    Text book image
    EBK JAVA PROGRAMMING
    Computer Science
    ISBN:9781337671385
    Author:FARRELL
    Publisher:CENGAGE LEARNING - CONSIGNMENT
    Text book image
    EBK JAVA PROGRAMMING
    Computer Science
    ISBN:9781305480537
    Author:FARRELL
    Publisher:CENGAGE LEARNING - CONSIGNMENT
    Text book image
    Programming Logic & Design Comprehensive
    Computer Science
    ISBN:9781337669405
    Author:FARRELL
    Publisher:Cengage
    Text book image
    Microsoft Visual C#
    Computer Science
    ISBN:9781337102100
    Author:Joyce, Farrell.
    Publisher:Cengage Learning,
    Text book image
    Programming with Microsoft Visual Basic 2017
    Computer Science
    ISBN:9781337102124
    Author:Diane Zak
    Publisher:Cengage Learning
    Text book image
    Systems Architecture
    Computer Science
    ISBN:9781305080195
    Author:Stephen D. Burd
    Publisher:Cengage Learning
    Java Math Library; Author: Alex Lee;https://www.youtube.com/watch?v=ufegX5o8uc4; License: Standard YouTube License, CC-BY