Car Class
Write a class named Car that has the following fields:
- yearModel. The yearModel field is an int that holds the car’s year model.
- make. The make field references a String object that holds the make of the car.
- speed. The speed field is an int that holds the car’s current speed.
In addition, the class should have the following constructor and other methods.
- Constructor. The constructor should accept the car’s year model and make as arguments. These values should be assigned to the object’s yearModel and make fields. The constructor should also assign 0 to the speed field.
- Accessors. Appropriate accessor methods should get the values stored in an object’s yearModel, make, and speed fields.
- accelerate. The accelerate method should add 5 to the speed field each time it is called.
- brake. The brake method should subtract 5 from the speed field each time it is called.
Demonstrate the class in a
Trending nowThis is a popular solution!
Learn your wayIncludes step-by-step video
Chapter 6 Solutions
Starting Out with Java: From Control Structures through Objects (7th Edition) (What's New in Computer Science)
Additional Engineering Textbook Solutions
Starting Out with Java: From Control Structures through Data Structures (3rd Edition)
Absolute Java (6th Edition)
Computer Science: An Overview (13th Edition) (What's New in Computer Science)
C++ How to Program (10th Edition)
Problem Solving with C++ (9th Edition)
- Create a class named Car that has the following properties: • Year—The Year property holds the car’s year model. • Make—The Make property holds the make of the car. • Speed—The Speed property holds the car’s current speed. In addition, the class should have the following constructor and other methods: • Constructor—The constructor should accept the car’s year and model and make them as arguments. These values should be assigned to the backing fields for the object’s Year and Make properties. The constructor should also assign 0 to the backing field for the Speed property. • Accelerate—The Accelerate method should add 5 to the Speed property’s backing field each time it is called. • Brake—The Brake method should subtract 5 from the Speed property’s backing field each time it is called. Demonstrate the class in an application that creates a Car object. The application’s form should have an Accelerate button that calls the Accelerate method and then displays the car’s current speed each…arrow_forwardJAVA -arrow_forwardjava Assignment Description: Write a class named Car that has the following fields: The yearModel field is an int that holds the car’s year model. The make field references a String object that holds the make of the car. The speed field is an int that holds the car’s current speed. In addition, the class should have the following constructor and other methods. The constructor should accept the car’s year and make as arguments. These values should be assigned to the object’s yearModel and make fields. The constructor should also assign 0 to the speed field. Constructor: The constructor should accept no arguments. Constructor: The constructor should accept the car’s year as argument.Assign the value to the object’s yearModel. No need to assign anything to the make and assign 0 to speed. Appropriate mutator methods should set the values in an object’s yearModel, make, and speed fields. Appropriate accessor methods should get the values stored in an object’s yearModel, make, and…arrow_forward
- Write a class named Car that has the following member variables: yearModel: An int that holds the car’s year model. name: A string that holds the name of the car. company: A string that holds the company of the car. speed: An int that holds the car’s current speed. In addition, the class should have the following member functions. Mutator: Appropriate accessor functions to set the values of object’s yearModel, name, company, and speed member variables. Accessor: Appropriate accessor functions to get the values stored in an object’s yearModel, company, 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, call the setter functions to set the values and then call the accelerate function five times. After each call to the accelerate function, get…arrow_forwardWrite a class named Aircraft that has the following member variables: xear - An variable that holds the aircraft's model year. make - A string object that holds the make of the aircraft Qwuer: A string object that holds the type of owner of the aircraft. speed - An int that holds the aircraft's current speed. altitude, An int that holds the aircraft's current altitude. engine type- a String value to hold the type of the engine the aircraft has In addition, the class should have the following member functions. Constructor - The constructor should accept the aircraft's owner and make arguments and assign these values to the object's owner and make member variables. The constructor should initialize the speed variable to 0 and the altitude variable to 0. Accessors - Appropriate accessor functions should be created to allow values to be retrieved from an object's year, make, owner, altitude, and speed member variables. Mutator - Appropriate mutator function should be created to allow the…arrow_forwardDesign a Ship class that the following members: A field for the name of the ship (a string). A field for the year that the ship was built (a string). A constructor and appropriate accessors and mutators. A toString method that displays the ship’s name and the year it was built. Design a CruiseShip class that extends the Ship class. The CruiseShip class should have the following members: A field for the maximum number of passengers (an int). A constructor and appropriate accessors and mutators. A toString method that overrides the toString method in the base class. The CruiseShip class’s toString method should display only the ship’s name and the maximum number of passengers. Design a CargoShip class that extends the Ship class. The CargoShip class should have the following members: A field for the cargo capacity in tonnage (an int). A constructor and appropriate accessors and mutators. A toString method that overrides the toString method in the base class. The…arrow_forward
- Write a class named Car that has the following member variables: Year: An int that holds the cars year model. Model: A string that holds the brand of the car. Speed: An int that holds the cars current speed. Color: A string that holds the color of the car. In addition, the class should have the following constructor and other member functions: Constructor: The constructor should accept the cars year, model ,speed and color as arguments. Accessor: Appropriate accessor functions to get the values stored in an object. Accelerate: The accelerate function should add 10 to the speed member variable If it’s value smaller than 50. Brake: The brake function should subtract 15 from the speed member variable If it’s value larger than 130. Count: The count function classifies the cars according to their model and their color. Afterward display the count of cars model= BMW and color=Red. Establish the class in a program that creates ten Car objects, and then calls the accelerate function ten…arrow_forwardAssignment 10: Chapter 10 Complete Programming Challenge 10 at the end of chapter 10 10. Ship, CruiseShip, and CargoShip Classes Design a Ship class that the following members: A field for the name of the ship (a string) - A field for the year that the ship was built (a string). A constructor and appropriate accessors and mutators. A toString method that displays the ship's name and the year it was built. Design a CruiseShip class that extends the Ship class. The CruiseShip class should have the following members: A field for the maximum number of passengers (an int). A constructor and appropriate accessors and mutators. A toString method that overrides the toString method in the base class. The CruiseShip class's toString method should display only the ship's name and the maximum number of passengers. Design a CargoShip class that extends the Ship class. The CargoShip class should have the following members: A field for the cargo capacity in tonnage (an int). A constructor and…arrow_forwardAssignment: 1- Create a class called Book with the following attributes: 1. title: The title of the book. 2. author: The author of the book. 3. pages: The number of pages in the book. Use a constructor (__init___) to initialize these attributes when creating an object from the class. Add a method inside the class calle description() which returns a description of the book Lec 3: Classes and Objects Object-oriented programming 2- Create a class called Phone with the following attributes: 1. brand: The brand of the phone. 2. price: The price of the phone. Use a constructor (__init__) to initialize these attributes when creating an object from the class. Add a method inside the class called show_info(), which prints the phone's detailsarrow_forward
- in C# languageDesign a Book class that holds the title, author’s name, and price of the book. Books’s constructor shouldinitialize all of these data members except the price which is set to 500/-. Create a display method thatdisplays all fields.All Books are priced at 500/- unless they are PopularBooks. The PopularBooks subclass replaces theBookprice and sets each Book’s price to 50,000/- through PopularBooks construcor. Override the displaymethod to display all fields.Write a Main () method that declares an array of five Book objects. Ask the user to enter the title andauthor for each of the 5 Books. Consider the Book to be a PopularBook if the author is one of the following:Khaled Hosseini, Oscar Wilde, or Rembrandt. Display the five Books’ details.arrow_forwardStatic & Not Final Field: Accessed by every object, Changing Non-Static & Final Field: Accessed by object itself, Non-Changing Static & Final: Accessed by every object, Non-Changing Non-Static & Not Final Field: Accessed by object itself, ChangingRead the following situation and decide how the variables should be defined. You have a class named HeartsPlayerA round of Hearts starts with every player having 13 cardsPlayers then choose 3 cards to “trade” with a player (1st you pass left, 2nd you pass right, 3rd you pass across, 4th you keep)Players then strategically play cards in order to have the lowest scoreAt the end of the round, points are cumulatively totaled for each player.If one player’s total is greater than 100, the game ends and the player with the lowest score wins. 1. How should the following data fields be defined (with respect to final and static)?(a) playerPosition (These have values of North, South, East, or West)(b) directionOfPassing(c) totalScore…arrow_forwardINVENTORY 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_forward
- EBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENTMicrosoft Visual C#Computer ScienceISBN:9781337102100Author:Joyce, Farrell.Publisher:Cengage Learning,