Question

In: Computer Science

The following classes along with the driver has been created and compiled. public class Car {...

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
     } 
   }
}

Solutions

Expert Solution

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


Related Solutions

IN JAVA Question 1 The following classes along with the driver has been created and compiled....
IN JAVA Question 1 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...
There has been a car accident and the driver if the car was brought to PRU....
There has been a car accident and the driver if the car was brought to PRU. She has been stabilized in the emergency department; however, transferring her to another facility 40 miles to ICU away would not be appropriate in her health status for her injuries would not sustain life. The patient has to be placed in ICU in the next 1-2 hours. The night supervisory gains composure and describes the status of patients occupying the 4 ICU beds. Patient...
Scenario # 7 There has been a car accident and the driver if the car was...
Scenario # 7 There has been a car accident and the driver if the car was brought to PRU. She has been stabilized in the emergency department; however, transferring her to another facility 40 miles to ICU away would not be appropriate in her health status for her injuries would not sustain life. The patient has to be placed in ICU in the next 1-2 hours. The night supervisory gains composure and describes the status of patients occupying the 4...
Case 7 There has been a car accident and the driver if the car was brought...
Case 7 There has been a car accident and the driver if the car was brought to PRU. She has been stabilized in the emergency department; however, transferring her to another facility 40 miles to ICU away would not be appropriate in her health status for her injuries would not sustain life. The patient has to be placed in ICU in the next 1-2 hours. The night supervisory gains composure and describes the status of patients occupying the 4 ICU...
Your Task: Starting with the following generic classes: public class LinkedListNode<E> { public E element; public...
Your Task: Starting with the following generic classes: public class LinkedListNode<E> { public E element; public LinkedListNode<E> next; } public class LinkedListBox<E> { public LinkedListNode<E> head; public int count = 0; } Create a LINKED LIST of random integers between 1 and 20. The program must prompt the user for the number of random integers to create before creating the list (i.e: if user asks for 40 integers then the program will link 40 integers between 1 and 20 generated...
In the previous lab you created a Car class and a Dealership class. Now, in this...
In the previous lab you created a Car class and a Dealership class. Now, in this activity change the design of the Dealership class, in which the list of the cars inside a dealership will be stored in an ArrayList. Then, provide required getter and setter methods to keep the records of all its cars. In your tester class, test your class and also printout the list of all cars of a given dealership object. // Java program import java.util.ArrayList;...
Assume you have created the following data definition class: public class Book {    private String title;...
Assume you have created the following data definition class: public class Book {    private String title;    private double cost;       public String getTitle() { return this.title; }    public double getCost() { return this.cost; }       public void setTitle(String title) {       this.title = title;    } // Mutator to return true/false instead of using exception handling public boolean setCost(double cost) {       if (cost >= 0 && cost < 100) {          this.cost = cost;          return true;       }       else {          return false;       }...
Assume you have created the following data definition class: public abstract class Organization { private String...
Assume you have created the following data definition class: public abstract class Organization { private String name;    private int numEmployees;    public static final double TAX_RATE = 0.01;    public String getName() { return this.name; }    public int getNumEmployees() { return this.numEmployees; }         public abstract double calculateTax() {       return this.numEmployees * TAX_RATE;    } } In your own words, briefly (1-2 sentences) explain why the data definition class will not compile. Then, write modified code that...
Use inheritance to implement the following classes: A: A Car that is a Vehicle and has...
Use inheritance to implement the following classes: A: A Car that is a Vehicle and has a name, a max_speed value and an instance variable called the number_of_cylinders in its engine. Add public methods to set and get the values of these variables. When a car is printed (using the toString method), its name, max_speed and number_of_cylinders are shown. B: An Airplane that is also a vehicle and has a name, a max_speed value and an instance variable called the...
Study the following class definition: class Car { public: Car(double speed); void start(); void accelerate(double speed);...
Study the following class definition: class Car { public: Car(double speed); void start(); void accelerate(double speed); void stop(); double get_speed() const; private: double speed; }; Which of the following options would make logical sense in the definition of the void accelerate(double speed)function? Group of answer choices this->speed = this->speed; this->speed = speed; this.speed = speed; speed1 = this->speed; Flag this Question Question 131 pts The Point class has a public function called display_point(). What is the correct way of calling...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT