Java: An Introduction to Problem Solving and Programming (7th Edition)
Java: An Introduction to Problem Solving and Programming (7th Edition)
7th Edition
ISBN: 9780133766264
Author: Walter Savitch
Publisher: PEARSON
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
Need help with python code! How do I simplify my code for a beginner to understand, simple fixed format and centering? Such as:  print(f"As an int variable: {age_int:^7}") print(f"In numeric binary: {age_int:^7b}") My Code:name = input("Enter your name: ")print(f"In text name is: {' '.join(name)}")decimal_values = []binary_values = []for letter in name:   ascii_val = ord(letter)   binary_val = format(ascii_val, '08b')   decimal_values.append(str(ascii_val))   binary_values.append(binary_val)# Loop through each letter:print(f"In ASCII decimal: {' '.join(decimal_values)}")print(f"In ASCII binary: {' '.join(binary_values)}")# Ageage_str = input("Enter your age: ")age_int = int(age_str)print(f"As a string \"{age_str}\": {' '.join(age_str)}")age_decimal_values = []age_binary_values = []for digit in age_str:   ascii_val = ord(digit)   binary_val = format(ascii_val, '07b')   age_decimal_values.append(str(ascii_val))   age_binary_values.append(binary_val)print(f"In ASCII decimal: {'…
Don't use chatgpt or any other AI
Don't use chatgpt or any other AI

Chapter 9 Solutions

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

Additional Engineering Textbook Solutions

Find more solutions based on key concepts
Knowledge Booster
Background pattern image
Computer Science
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