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...
Python code def plot_dataset(file_path): """ Read in a text file where the path and filename is...
Python code def plot_dataset(file_path): """ Read in a text file where the path and filename is given by the input parameter file_path There are 4 columns in the text dataset that are separated by colons ":". c1:c2:c3:c4 Plot 3 datasets. (x axis vs y axis) c1 vs c2 (Legend label "n=1") c1 vs c3 (Legend label "n=1") c1 vs c4 (Legend label "n=1") Make sure you have proper x and y labels and a title. The x label should be...
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?
Write the following assembly code file into C code. .globl main .text main:    # Tests...
Write the following assembly code file into C code. .globl main .text main:    # Tests simple looping behaviour    li t0, 60    li t1, 0 loop:    addi t1, t1, 5    addi t0, t0, -1    bne t1, t0, loop    bne t1, zero, success failure:    li a0, 0    li a7, 93 # a7 is what determines which system call we are calling and we what to call write (64)     ecall # actually issue...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT