Concept explainers
Suppose that you are going to create an object used to count the number of people in a room. We know that the number of people in the room can never be negative. Create a RoomCounter class having three public methods:
- addPerson—adds one person to the room
- removePerson—removes one person from the room
- getCount—returns the number of people in the room
If removePerson would make the number of people less than zero, throw a NegativeCounterException.
Want to see the full answer?
Check out a sample textbook solutionChapter 9 Solutions
Java: An Introduction to Problem Solving and Programming (8th Edition)
Additional Engineering Textbook Solutions
Web Development and Design Foundations with HTML5 (9th Edition) (What's New in Computer Science)
Starting Out with C++ from Control Structures to Objects (9th Edition)
Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)
Database Concepts (8th Edition)
Starting Out With Visual Basic (8th Edition)
Web Development and Design Foundations with HTML5 (8th Edition)
- In java language Write a Rectangle class. A Rectangle has properties of width and length. You construct a Rectangle object by providing the width and length in that order. If no width and length are provided to the constructor, construct a Rectangle with width 0.0 and length 0.0. We want to be able to get and set both the width and the length independently. We also want to be able to ask for the area of the rectangle and the perimeter of the rectangle. What will the object need to remember? width and length - those are the instance variables. Rectangle class has these constructors : public Rectangle() - Constructs a new rectangle with width and length of 0.0. public Rectangle(double width, double length) - Constructs a new rectangle with the given width and length. Remember that the job of the constructor is to initialize the instance variables. It has these methods. public double getWidth() - Gets the width of this Rectangle public double getLength() - Gets the length of this…arrow_forwardIn java language Write a Rectangle class. A Rectangle has properties of width and length. You construct a Rectangle object by providing the width and length in that order. If no width and length are provided to the constructor, construct a Rectangle with width 0.0 and length 0.0. We want to be able to get and set both the width and the length independently. We also want to be able to ask for the area of the rectangle and the perimeter of the rectangle. What will the object need to remember? width and length - those are the instance variables. Rectangle class has these constructors : public Rectangle() - Constructs a new rectangle with width and length of 0.0. public Rectangle(double width, double length) - Constructs a new rectangle with the given width and length. Remember that the job of the constructor is to initialize the instance variables. It has these methods. public double getWidth() - Gets the width of this Rectangle public double getLength() - Gets the length of this…arrow_forwardCreate a class called SquareTester that will create a Square object. Print out the toString method with the object as your receiver. Print out the perimeter using the getArea method and the object as your receiver. public class SquareTester { public static void main(String{}args) { //create your Square object with any value as your argument //print the toString method //print the area of the square on a new line -ex: “The area is: 64” } }arrow_forward
- Exercise 1 - Number Guessing Game Make a new Java Project called Lab2. Within it, add a new class with the main method called NumberGuessGame. Inside the main method, implement a number guessing game, which will work by generating a pseudorandom number in the range of 0 to 100 (inclusive, so both 0 and 100 should be possible), then asking the user to enter a number that will be their guess. If the two match, display an appropriate message. Otherwise, display either "greater" or "lesser", depending on whether the pseudorandom number is greater than or less than the number entered.arrow_forwardPythonarrow_forwardScreen shot pleasearrow_forward
- Write a Rectangle class. A Rectangle has properties of width and length. You construct a Rectangle object by providing the width and length in that order. If no width and length are provided to the constructor, construct a Rectangle with width 0.0 and length 0.0. We want to be able to get and set both the width and the length independently. We also want to be able to ask for the area of the rectangle and the perimeter of the rectangle. What will the object need to remember? width and length - those are the instance variables. Rectangle class has these constructors : public Rectangle() - Constructs a new rectangle with width and length of 0.0. public Rectangle(double width, double length) - Constructs a new rectangle with the given width and length. Remember that the job of the constructor is to initialize the instance variables. It has these methods. public double getWidth() - Gets the width of this Rectangle public double getLength() - Gets the length of this Rectangle public void…arrow_forwardImplement a class called Planearrow_forwardWrite a compareTo method that compares two Song objects based on the artist, and title within artist. For example, given the following Songobjects: "Glass Animals""Heat Waves" "Elton John, Dua Lipa""Cold Heart" "Adele""Easy on Me" "Doja Cat""Kiss Me More" "Adele""My Little Love" They will be put in the following order: "Adele""Easy on Me" "Adele""My Little Love" "Doja Cat""Kiss Me More" "Elton John, Dua Lipa""Cold Heart" "Glass Animals""Heat Waves" In which class does the compareTo method belong?arrow_forward
- The browser window object has many members, two of which are window.prompt() that allows us to send a message to the user and solicit a value at the same time, and window.alert() that displays a message box to the user. let val = prompt("An instruction here: "); alert ("A message here."); The Math object has a pow(number, exponent) method that will return number to the power of exponent: > "12 to the 4th power is: " + Math.pow(12, 4); Button Text Button Text Note that an HTML5 button element has no built-in functionality: if we want the button to do something we have to write a JavaScript function that provides whatever functionality we require. Create an HTML document and link a JavaScript file to the document in the document header. Write a function getAndShow() in the JavaScript file that will use a prompt dialog to ask the user for any number. Use the number returned by the prompt to compute the fifth power of that number and display it in a message box with a suitable message.…arrow_forwardmaximum hours (inclusive). Make sure to cast the maximum range to an int before using it as parameter invoke the method to start the engine invoke the fly method and use as the input parameter the random number generated on the previous step invoke the method to land the plane and stop the engine invoke the addGas method. Invoke the calcGasNeededToFillTank () method to figure out the fuel needed to fill the tank and pass this value as input parameter to the addGas method.arrow_forwardSetup Instructions: Create a new class named Lab4 in the edu.ben.labs.lab4 package of your CMSC200 Java Project. Problem Set: Let's place a marker on the circle numbered 1 in the grid below. Then we will toss a coin. If the coin toss resulted in a 'heads', we would move the marker along the path labeled 'H' to the circle numbered 5. If the coin toss resulted in a 'tails', we would move the marker along the path labeled T' to the circle numbered 4. Now we would toss the coin again and move the marker accordingly. This is repeated for any number of coin tosses. The purpose of this problem is to simulate this automata and to report 2 3 the number of the circle on which the marker ends after a series of coin tosses. The marker always begins on the circle numbered 1. 7 Iпрut: Your program should accept a string consisting only of the characters 'h' or t, where each character represents the outcome of a coin toss ('h' - heads, 't - tails). The string is terminated by a #' character. Each…arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education