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...
Writing Classes I Write a Java program containing two classes: Dog and a driver class Kennel....
Writing Classes I Write a Java program containing two classes: Dog and a driver class Kennel. A dog consists of the following information: • An integer age. • A string name. If the given name contains non-alphabetic characters, initialize to Wolfy. • A string bark representing the vocalization the dog makes when they ‘speak’. • A boolean representing hair length; true indicates short hair. • A float weight representing the dog’s weight (in pounds). • An enumeration representing the type...
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;...
Write two classes, ToDoList and Driver ToDoList should implement the following UML class diagram: ToDoList -list:String[]...
Write two classes, ToDoList and Driver ToDoList should implement the following UML class diagram: ToDoList -list:String[] -size:int +ToDoList() +add(String item):void +remove(String item): boolean +toString():String add(String item) should add the item as the last element in the list, updating size. remove(String item) should remove the item and return true, or, if the item was not in the list, return false.   To remove the item, check every list element from 0 to size-1, and if that item is equal to the parameter...
Consider the following classes: public class Clock extends Bear { public void method3() { System.out.println("Clock 3");...
Consider the following classes: public class Clock extends Bear { public void method3() { System.out.println("Clock 3"); } } public class Lamp extends Can { public void method1() { System.out.println("Lamp 1"); } public void method3() { System.out.println("Lamp 3"); } } public class Bear extends Can { public void method1() { System.out.println("Bear 1"); } public void method3() { System.out.println("Bear 3"); super.method3(); } } public class Can { public void method2() { System.out.println("Can 2"); method3(); } public void method3() { System.out.println("Can 3"); }...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT