In: Computer Science
The following classes along with the driver has been created and compiled.
public class Car { public void method1() { System.out.println("I am a car object"); } } class Point { public void method2() { System.out.println("I am a Point object"); } } class Animal { public void method3() { System.out.println("I am an animal object"); } } The following driver class has been created and all the lines of code inside the for loop is not compiling. Modify the code and write it in the answer area so that it compiles and display the output. To get the full credit you cannot change anything else in the given code. you cannot change the code inside the above classes class Driver { public static void main(String[] args) { Object[] things = {new Car(), new Animal(),new Point()};//do not change this line for(int i = 0; i < things.length; i++) { things[i].method1(); //compiler error things[i].method2(); //compiler error things[i].method3(); //compiler error } } }
class Car {
public void method1() {
System.out.println("I am a car object");
}
}
class Point {
public void method2() {
System.out.println("I am a Point object");
}
}
class Animal {
public void method3() {
System.out.println("I am an animal object");
}
}
public class Driver {
public static void main(String[] args) {
Object[] things = { new Car(), new Animal(), new Point() };// do not change this line
for (int i = 0; i < things.length; i++) {
if (i == 0)
((Car) things[i]).method1(); // compiler error
if (i == 1)
((Animal) things[i]).method3(); // compiler error
if (i == 2)
((Point) things[i]).method2(); // compiler error
}
}
}
NOTE : PLEASE COMMENT BELOW IF YOU HAVE CONCERNS.
I AM HERE TO HELP YOUIF YOU LIKE MY ANSWER PLEASE RATE AND HELP ME IT IS VERY IMP FOR ME