Task 1- Create a computer program that will calculate the range for 3 different
ID: 3572321 • Letter: T
Question
Task 1- Create a computer program that will calculate the range for 3 different vehicles.
The program should create a “programmer created” class, where 3 int objects are created passengers, fuel capacity, mpg.
Create a void() method inside the “programmer created “ class to calculate vehicle range.
range = fuel capacity * miles per gallon.
Each Vehicle type should have unique values for number of passengers, fuel capacity, and miles per gallon.
Follow the sample below and print information on 3 vehicle types.
Sample Output: // Create similar output for 3 Vehicle Types
The minivan carries= 7
The minivan has a fuel capacity of = 16
The minivan mpg = 21
The minivan has a range of: 336 miles
Explanation / Answer
public class Vehicle {
int numberOfPassengers, fuelCapacity, milesPerGallon;
/**
* @param numberOfPassengers
* @param fuelCapacity
* @param milesPerGallon
*/
public Vehicle(int numberOfPassengers, int fuelCapacity, int milesPerGallon) {
this.numberOfPassengers = numberOfPassengers;
this.fuelCapacity = fuelCapacity;
this.milesPerGallon = milesPerGallon;
}
/**
* @return the numberOfPassengers
*/
public int getNumberOfPassengers() {
return numberOfPassengers;
}
/**
* @return the fuelCapacity
*/
public int getFuelCapacity() {
return fuelCapacity;
}
/**
* @return the milesPerGallon
*/
public int getMilesPerGallon() {
return milesPerGallon;
}
/**
* @param numberOfPassengers
* the numberOfPassengers to set
*/
public void setNumberOfPassengers(int numberOfPassengers) {
this.numberOfPassengers = numberOfPassengers;
}
/**
* @param fuelCapacity
* the fuelCapacity to set
*/
public void setFuelCapacity(int fuelCapacity) {
this.fuelCapacity = fuelCapacity;
}
/**
* @param milesPerGallon
* the milesPerGallon to set
*/
public void setMilesPerGallon(int milesPerGallon) {
this.milesPerGallon = milesPerGallon;
}
public void vehicleRange() {
int range = fuelCapacity * milesPerGallon;
System.out.println("The minivan has a range of: " + range + " miles");
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "The minivan carries= " + numberOfPassengers
+ " The minivan has a fuel capacity of = " + fuelCapacity
+ " The minivan mpg = " + milesPerGallon;
}
public static void main(String[] args) {
Vehicle vehicle1 = new Vehicle(7, 16, 21);
System.out.println(vehicle1);
vehicle1.vehicleRange();
}
}
OUTPUT:
The minivan carries= 7
The minivan has a fuel capacity of = 16
The minivan mpg = 21
The minivan has a range of: 336 miles
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.