Question
public class Vehicle { // private fields / internal data private int year; // year the vehicle was manufactured private int miles; // miles on
public class Vehicle {
// private fields / internal data
private int year; // year the vehicle was manufactured
private int miles; // miles on the total odometer
private double mpg; // miles per gallon the vehicle gets
// public methods / operations
// constructor - used to create an object from the class
public Vehicle() {
// this one is empty; we'll see constructors with code soon!
}
// setters (mutators) and getters (accessors)
public int getYear() {
return year; // return THIS object's year value
}
public void setYear(int year) {
this.year = year; // set THIS object's year to argument (year) value
}
public int getMiles() {
return miles; // return THIS object's miles value
}
public void setMiles(int miles) {
this.miles = miles; // set THIS object's miles to argument (miles) value
}
public double getMpg() {
return mpg; // return THIS object's mpg value
}
public void setMpg(double mpg) {
this.mpg = mpg; // set THIS object's mpg to argument (mpg) value
}
// instance methods
public void drive(int miles) {
this.miles += miles; // add argument miles value to THIS object's miles
}
}
---------------------------------------------------
Create a new class in your project.
Create a main method in your new class.
Inside your main method:
- Declare a Vehicle reference variable.
- Instantiate a Vehicle object.
- Assign the Vehicle object to the Vehicle reference variable.
- Set the Vehicle object's year, miles, and mpg variables.
- Output to the screen the Vehicle's year, miles, and mpg values. Include some descriptive text so that we know what we're looking at.
- Prompt the user to input the number of miles they will be driving the Vehicle.
- Input from the keyboard the number of miles the Vehicle will be driven.
- Drive the Vehicle object by calling the drive() method and moving it the number of miles that the user indicated.
- Output to the screen the Vehicle's new miles value. Again, include descriptive text so that the output is sensible.
Complete the "Inside your main method" steps TWICE so that you are working with two separate Vehicle objects.
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