In: Computer Science
What are the errors in this code?
//1.
//Filename: CarbonFootprintTest.java
//The file tests the Car class and CarbonFootprint class
public class CarbonFootprintTest {
public static void main (String [] args) {
CarbonFootprint[] obj = new
CarbonFootprint[2];
obj[0] = new CarbonFootprint(20);
obj[1] = new Car(30);
System.out.println("Carbon Foot Print for
each item (lbs):\n");
//additional info for to give general idea of
program
for (CarbonFootprint test: obj)
test.getCarbonFootprint();
}//end main method
}//end class
//2. Filename: CarbonFootprint.java It only include
s one abstract method
public class CarbonFootprint {
//returns the carbon footprint of an
object
public void GetCarbonFootprint();
}//end interface
//3. Filename: Car.java
public class Car extends CarbonFootprint {
private double gallons;
public Car( double g ){
gallons = g;
} // end Car constructor
// one gallon of gas yields 20 pounds of CO2
public abstract void
GetCarbonFootprint(){
System.out.printf( "Car that has
used %.2f gallons of gas: %.2f\n",
gallons,
gallons * 20 );
} // end function GetCarbonFootprint
} // end class Car
//1.
//Filename: CarbonFootprintTest.java
//The file tests the Car class and CarbonFootprint class
public class CarbonFootprintTest {
public static void main (String [] args) {
CarbonFootprint[] obj = new CarbonFootprint[2];
obj[0] = new CarbonFootprint(20);//here you cannot object for
abstract class
obj[1] = new Car(30);
System.out.println("Carbon Foot Print for each item (lbs):
");
//additional info for to give general idea of program
for (CarbonFootprint test: obj)
test.getCarbonFootprint();//here the method name is wrong
}//end main method
}//end class
2)
public class CarbonFootprint {
//returns the carbon footprint of an object
public void GetCarbonFootprint();
}
in this class one abstract method is there so this class shoud be declred as abstract. Method also must be declared a sabstract
3)
public class Car extends CarbonFootprint {
private double gallons;
public Car( double g ){
gallons = g;
} // end Car constructor
// one gallon of gas yields 20 pounds of CO2
public abstract void GetCarbonFootprint(){
System.out.printf( "Car that has used %.2f gallons of gas: %.2f
",
gallons, gallons * 20 );
} // end function GetCarbonFootprint
} // end class Car
As you extended CarbonFootprint class here you get abstact
method GetCarbonFootprint() in herited to your class and you need
to implement that. But in your
code you have implemented and also added abstract keyword. If
method has bosy then it will ot be abstract
Find the corrected programs below:
CarbonFootprintTest.java
//1.
//Filename: CarbonFootprintTest.java
//The file tests the Car class and CarbonFootprint class
public class CarbonFootprintTest {
public static void main (String [] args) {
CarbonFootprint[] obj = new CarbonFootprint[2];
//obj[0] = new CarbonFootprint(20);//I am replcaing this with
obj[0] = new Car(20);
obj[1] = new Car(30);
System.out.println("Carbon Foot Print for each item (lbs):
");
//additional info for to give general idea of program
for (CarbonFootprint test: obj)
test.GetCarbonFootprint();
}//end main method
}//end class
Car.java
public class Car extends CarbonFootprint {
private double gallons;
public Car( double g ){
gallons = g;
} // end Car constructor
// one gallon of gas yields 20 pounds of CO2
public void GetCarbonFootprint(){
System.out.printf( "Car that has used %.2f gallons of gas: %.2f
",
gallons, gallons * 20 );
} // end function GetCarbonFootprint
} // end class Car
CarbonFootPrint.java
public abstract class CarbonFootprint {
//returns the carbon footprint of an object
public abstract void GetCarbonFootprint();
}//end interface
Output: