Concept explainers
Car Class
Write a class named Car that has the following member variables:
• year. An int that holds the car’s model year.
• make. A string object that holds the make of the car.
• speed. An int that holds the car’s current speed.
In addition, the class should have the following member functions.
• Constructor. The constructor should accept the car’s year and make as arguments and assign these values to the object’s year and make member variables. The constructor should initialize the speed member variable to 0.
• Accessors. Appropriate accessor functions should be created to allow values to be retrieved from an object’s year, make, and speed member variables.
• accelerate. The accelerate function should add 5 to the speed member variable each time it is called.
• brake. The brake function should subtract 5 from the speed member variable each time it is called.
Demonstrate the class in a program that creates a Car object and then calls the accelerate function five times. After each call to the accelerate function, get the current speed of the car and display it. Then, call the brake function five times. After each call to the brake function, get the current speed of the car and display it.
Trending nowThis is a popular solution!
Learn your wayIncludes step-by-step video
Chapter 7 Solutions
Starting Out with C++: Early Objects (9th Edition)
Additional Engineering Textbook Solutions
Database Concepts (8th Edition)
Modern Database Management
Electric Circuits. (11th Edition)
Starting Out with Programming Logic and Design (5th Edition) (What's New in Computer Science)
Java How to Program, Early Objects (11th Edition) (Deitel: How to Program)
Web Development and Design Foundations with HTML5 (8th Edition)
- 2. Car Class Write a class named car that has the following fields: ▪ yearModel. The year Model field is an int that holds the car's year model. ▪ make. The make field is a String object that holds the make of the car, such as "Ford", "Chevrolet", "Honda", etc. ▪ speed. The speed field is an int that holds the car's current speed. In addition, the class should have the following methods. ■ Constructor. The constructor should accept the car's year model and make as arguments. These values should be assigned to the object's year Model and make fields. The constructor should also assign 0 to the speed field.arrow_forward1. RetailItem Class Write a class named Retailltem that holds data about an item in a retail store. The class should have the following fields: • description. It is a String object that holds a brief description of the item. • unitsOnHand. It is an int variable that holds the number of units currently in inventory. • price. It is a double that holds the item's retail price. Write appropriate mutator methods that store values in these fields and accessor methods that return the values in these fields. Once you have written the class, write a separate program that creates three RetailItem objects and stores the following data in them. Units On Hold Description Designer Jeans Jacket Price 40 34.95 12 59.95 Shirt 20 24.95arrow_forwardCruise Recreational Activities Example Recreational activities include things like aerobics, shuffle board, and swimming. Each activity is identified by an activity code and includes other information such as description. Classes are offered for each activity. A class is uniquely identified by a combination of the activity code and the day/time at which it is held. It is assumed that a specific class will never be offered for the same activity at the same day and time, although it could be offered on a different day and/or time. Other information about a class includes the enrollment limit and the current enrollment count. A class will never include more than one activity. A passenger can sign up for a class as long as there is sufficient room in the class. Passengers are identified by a unique passenger number. Other information stored about passengers includes name, address, and age. Passengers have no limit on the number and type of classes they can sign up for. When they…arrow_forward
- True or FalseA base class reference variable can reference an object of any class that is derived from the base class.arrow_forwardT/F A method defined in a class can access the class' instance data without needing to pass them as parameters or declare them as local variables.arrow_forwardMonth Class Exceptions Write a class named Month. The class should have an int field named monthNumber that holds the number of the month. For example, Janauary would be 1, February would be 2, and so forth. In addition, provide the following methods: A no-arg constructor that sets the monthNumber field to 1. A constructor that accepts the number of the month as an argument. It should set the monthNumber value to the value passed as the argument. If a value less than 1 or greater than 12 is passed, the constructor should throw an InvalidMonthNumberException. A constructor that accepts the name of the month, such as “January” or “February”, as an argument. It should set the monthNumber field to the correct corresponding value. If an invalid name is passed, the constructor should throw an InvalidMonthNameException. A setMonthNumber method that accepts an int argument, which is assigned to the monthNumber field. If a value less than 1 or greater than 12 is passed, throw an…arrow_forward
- True or false: A class object can be passed to a function, but cannot be returned by a function.arrow_forwardJAVA -arrow_forwardRemaining Time: 35 minutes, 18 seconds, ¥ Question Completion Status: A class named Account is defined as the following. class Account } private: int id; double balance; public: //A constructor without parameter that creates a default account with id 0, balance 0 Account): /A constructor with the parameter that setting the id and balance Account(int, double), int getId); / return the ID double getBalance() //return the balance void withdraw(double amount) // the amount will be withdrawn void setlID(int), //set the ID void setBalance (double) //set the balance }: Assume that we will create a separate file of implementation of the function definition for the questions a) -b) a) Write a construction function definition with two parameters (ID and balance). b) Write a function definition of the function declaration (void withdraw(double amount):). c) Creates an Account object with an ID (1122) and balance ($20000). Then, write source codes to withdraw $2 MacBooarrow_forward
- INVENTORY CLASS You need to create an Inventory class containing the private data fields, as well as the methods for the Inventory class (object). Be sure your Inventory class defines the private data fields, at least one constructor, accessor and mutator methods, method overloading (to handle the data coming into the Inventory class as either a String and/or int/float), as well as all of the methods (methods to calculate) to manipulate the Inventory class (object). The data fields in the Inventory class include the inventory product number (int), inventory product description (String), inventory product price (float), inventory quantity on hand (int), inventory quantity on order (int), and inventory quantity sold (int). The Inventory class (java file) should also contain all of the static data fields, as well as static methods to handle the logic in counting all of the objects processed (counter), as well as totals (accumulators) for the total product inventory and the total product…arrow_forwardTrue or False Objects that are instances of a class are always passed by value.arrow_forwardPYTHON LANGUAGEarrow_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