Question

In: Computer Science

What are the errors in this code? //1. //Filename: CarbonFootprintTest.java //The file tests the Car class...

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

Solutions

Expert Solution

//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:


Related Solutions

CODE BLOCK E import csv filename = "D:/python/Week8/files/green.csv" with open(filename) as file: data_from_file = csv.reader(file) header_row...
CODE BLOCK E import csv filename = "D:/python/Week8/files/green.csv" with open(filename) as file: data_from_file = csv.reader(file) header_row = next(data_from_file) for index,column_header in enumerate(header_row): print(index,column_header) How many COUMNS (not rows!) will be printed in the above code?
Create a Java class file for a Car class. In the File menu select New File......
Create a Java class file for a Car class. In the File menu select New File... Under Categories: make sure that Java is selected. Under File Types: make sure that Java Class is selected. Click Next. For Class Name: type Car. For Package: select csci2011.lab7. Click Finish. A text editor window should pop up with the following source code (except with your actual name): csci1011.lab7; /** * * @author Your Name */ public class Car { } Implement the Car...
1. Write a program that prompts the user for a filename, then reads that file in...
1. Write a program that prompts the user for a filename, then reads that file in and displays the contents backwards, line by line, and character-by character on each line. You can do this with scalars, but an array is much easier to work with. If the original file is: abcdef ghijkl the output will be: lkjihg fedcba Need Help with this be done in only PERL. Please use "reverse"
identify the syntax errors in the following code:             public class Hello {                    &
identify the syntax errors in the following code:             public class Hello {                         private static int main(String [] args) {                                     string Msg=”Hello, Wrld!;                                     Sytem.out.println(msg+ “Ken")
In python Complete the function get_Astring(filename) to read the file contents from filename (note that the...
In python Complete the function get_Astring(filename) to read the file contents from filename (note that the test will use the file data.txt and data2.txt provided in the second and third tabs), strip off the newline character at the end of each line and return the contents as a single string.
Write a Java program (single file/class) whose filename must be Numbers.java. The program reads a list...
Write a Java program (single file/class) whose filename must be Numbers.java. The program reads a list of integers from use input and has the following methods: 1. min - Finds the smallest integer in the list. 2. max- Finds the largest integer in the list. 3. average - Computes the average of all the numbers. 4. main - method prompts the user for the inputs and ends when the user enter Q or q and then neatly outputs the entire...
Write a Java program (single file/class) whose filename must be Numbers.java. The program reads a list...
Write a Java program (single file/class) whose filename must be Numbers.java. The program reads a list of integers from use input and has the following methods: 1. min - Finds the smallest integer in the list. 2. max- Finds the largest integer in the list. 3. average - Computes the average of all the numbers. 4. main - method prompts the user for the inputs and ends when the user enter Q or q and then neatly outputs the entire...
what is the cause of an increased risk for type 1 errors when T tests are...
what is the cause of an increased risk for type 1 errors when T tests are conducted and how might researchers eliminate the increased risk of a type 1 error in a study?
original code: filename = "CCL-mbox-tiny.txt" with open(filename, 'r') as f: for line in f.readlines(): print(line.strip()) 1....
original code: filename = "CCL-mbox-tiny.txt" with open(filename, 'r') as f: for line in f.readlines(): print(line.strip()) 1. Now modify the code above to print the number of lines in the file, instead of printing the lines themselves. You'll need to increment a variable each time through the loop and then print it out afterwards. There should be **332** lines. 2. Next, we'll focus on only getting lines addressing `X-DSPAM-Confidence:`. We do this by including an `if` statement inside the `for` loop....
There are two errors in this code. Identify the errors and give the correct code that...
There are two errors in this code. Identify the errors and give the correct code that will make the program to display the following output: Rectangle: height 2.0 width 4.0 Area of the Rectangle is 8.0 ----- public interface Shape { public double getArea(); } class Rectangle implements Shape { double height; double width; public Rectangle(double height, double width) { this.height=height; this.width=width; } public double getArea() { return height*width; } public String toString() { return "Rectangle: height "+height+" width "+width;...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT