Question
Write Java Program. Write a class named car that has the following fields: year. The year field is an int that holds the cars built
Write Java Program.
Write a class named car that has the following fields: year. The year field is an int that holds the cars built year make. The make field references a String object that holds the make of the car model. The model field references a String object that holds the model of the car speed. The speed field is an int that holds the cars current speed. milePerGallon In addition, the class should have the following constructor and other methods. No argument (default) constructor Constructor. The constructor should accept the cars year, make, model, and miles per gallon make as arguments. These values should be assigned to the objects year and make, model fields. The constructor should assign 0 to the speed field. Accessors (gets) . Appropriate accessor methods should get the values stored in an objects year, make , model, milePerGallon, and speed fields. Mutators (sets) . Appropriate mutator methods should set the values for objects year, make , model, milePerGallon, 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 gasUsage. The gasUsage method should calculate the gas usage for a 100 mile distance. Demonstrate the class in a program that creates a Car object, and calls the accelerate method five times. After each call to the accelerate method, get the current speed of the car and display it. Then call the brake method five times. After each call to the brake method, get the current speed of the car and display it. This demo class should also demo the change of year, make ,model, and milePerGallon, and speed fields as well as displaying the gas usage for 100 miles. Draw UML diagram
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started