Please help to fix this code is wil not run without errors. import javax.swing.*
ID: 3864937 • Letter: P
Question
Please help to fix this code is wil not run without errors. import javax.swing.*; class Vehicle { private int numberOfWheels; private int milePerGallon; public int getNunberOfWheels(){ return numberOfWheels; } public int getMilesPerGallon(){ return milePerGallon; } void setMilesPerGallon(int mpg){ milePerGallon = mpg; } void setNumberOfWheels(int n){ numberOfWheels = n; } public Vehicle(int noOfWheels, int mpg){ numberOfWheels = noOfWheels; milePerGallon = mpg; } } class PassengerCar extends Vehicle{ private int fuelTank; void setFuelTank(int ft){ fuelTank = ft; } int getFuelTank(){ return fuelTank; } public PassengerCar(int mpg, int fuelTank) { super(4, mpg); this.fuelTank = fuelTank; } public double maxDistanceDriven(){ return fuelTank * super.getMilesPerGallon(); } } class Truck extends Vehicle{ private double cargoLoad; double getCargoLoad(){ return cargoLoad; } void setCargoLoad(double cL){ cargoLoad = cL; } public Truck(int mpg, double cargoLoad) { super(8, mpg); this.cargoLoad = cargoLoad; } } public class UseVehicles{ public static void main(String[] args) { int numberOfWheels; numberOfWheels = Integer.parseInt(JOptionPane.showInputDialog("Enter number of wheels")); int milesPerGallon; milesPerGallon = Integer.parseInt(JOptionPane.showInputDialog("Enter miles per gallon")); int fuelTank; fuelTank = Integer.parseInt(JOptionPane.showInputDialog("Enter fuel tank capacity")); double cargoLoad; cargoLoad = Integer.parseInt(JOptionPane.showInputDialog("Enter cargo load")); Vehicle v = new Vehicle(numberOfWheels,milesPerGallon); PassengerCar p1 = new PassengerCar(milesPerGallon, fuelTank); Truck t1 = new Truck(milesPerGallon, cargoLoad); String vehicleOutput = "Vehicle information: Number of wheels: " + v.getNunberOfWheels() + " Average miles per gallon: " + v.getMilesPerGallon(); String carOutput = "Number of wheels: " + p1.getNunberOfWheels() + " Average miles per gallon: " + p1.getMilesPerGallon() + " Fuel Tank Capacity: " +p1.getFuelTank() + " Maximum distance driven: " + p1.maxDistanceDriven() ; String truckOutput = "Number of wheels: " + t1.getNunberOfWheels() + " Average miles per gallon: " + t1.getMilesPerGallon() + " Cargo Capacity: " + t1.getCargoLoad(); JOptionPane.showMessageDialog(null , vehicleOutput, "Vehicle Information", 1 ); JOptionPane.showMessageDialog(null , carOutput, "Passenger Car Information", 1); JOptionPane.showMessageDialog(null , truckOutput, "Truck Information", 1); } }
Explanation / Answer
Now the code is working with the following change , the change is i underlined in below line
i only changed one line i.e public static void main(String[] args) throws Exception
modified program :
import javax.swing.*;
class Vehicle {
private int numberOfWheels;
private int milePerGallon;
public int getNunberOfWheels(){
return numberOfWheels;
}
public int getMilesPerGallon(){
return milePerGallon;
}
void setMilesPerGallon(int mpg){
milePerGallon = mpg;
}
void setNumberOfWheels(int n){
numberOfWheels = n;
}
public Vehicle(int noOfWheels, int mpg){
numberOfWheels = noOfWheels;
milePerGallon = mpg;
}
}
class PassengerCar extends Vehicle{
private int fuelTank;
void setFuelTank(int ft){
fuelTank = ft;
}
int getFuelTank(){
return fuelTank;
}
public PassengerCar(int mpg, int fuelTank) {
super(4, mpg);
this.fuelTank = fuelTank;
}
public double maxDistanceDriven(){
return fuelTank * super.getMilesPerGallon();
}
}
class Truck extends Vehicle{
private double cargoLoad;
double getCargoLoad(){
return cargoLoad;
}
void setCargoLoad(double cL){
cargoLoad = cL;
}
public Truck(int mpg, double cargoLoad) {
super(8, mpg);
this.cargoLoad = cargoLoad;
}
}
public class UseVehicles{
public static void main(String[] args) throws Exception {
int numberOfWheels;
numberOfWheels = Integer.parseInt(JOptionPane.showInputDialog("Enter number of wheels"));
int milesPerGallon;
milesPerGallon = Integer.parseInt(JOptionPane.showInputDialog("Enter miles per gallon"));
int fuelTank;
fuelTank = Integer.parseInt(JOptionPane.showInputDialog("Enter fuel tank capacity"));
double cargoLoad;
cargoLoad = Integer.parseInt(JOptionPane.showInputDialog("Enter cargo load"));
Vehicle v = new Vehicle(numberOfWheels,milesPerGallon);
PassengerCar p1 = new PassengerCar(milesPerGallon, fuelTank);
Truck t1 = new Truck(milesPerGallon, cargoLoad);
String vehicleOutput = "Vehicle information: Number of wheels: " + v.getNunberOfWheels() + " Average miles per gallon: " + v.getMilesPerGallon();
String carOutput = "Number of wheels: " + p1.getNunberOfWheels() + " Average miles per gallon: " + p1.getMilesPerGallon()
+ " Fuel Tank Capacity: " +p1.getFuelTank() + " Maximum distance driven: " + p1.maxDistanceDriven() ;
String truckOutput = "Number of wheels: " + t1.getNunberOfWheels() + " Average miles per gallon: " + t1.getMilesPerGallon()
+ " Cargo Capacity: " + t1.getCargoLoad();
JOptionPane.showMessageDialog(null , vehicleOutput, "Vehicle Information", 1 );
JOptionPane.showMessageDialog(null , carOutput, "Passenger Car Information", 1);
JOptionPane.showMessageDialog(null , truckOutput, "Truck Information", 1);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.