Question

In: Computer Science

JAVA 1. Create an Interface Vehicle. Declare one public void method in it paint( ). 2....

JAVA

1. Create an Interface Vehicle. Declare one public void method in it paint( ).

2. Create a class Car. Implements Vehicle. It's paint() method prints "Car Painted". It's drive( ) method prints "Car Driven".

3. Create a class Bus. Implements Vehicle. It's paint() method prints "Bus Painted" . It's drive( ) method prints "Bus Driven".  

4. Create a method AutoWork which accepts an object of type Vehicle. The method makes a call to the vehicle's paint( ) and drive() methods.

5. In the main method of your Main class, create a reference and object of Car class. Pass it to the AutoWork method.

6. In the main method of your Main class, create a reference and object of Bus class. Pass it to the AutoWork method.

------

7. In the main method of your Main class, create a reference v of Vehicle interface and object of Car class. Pass it to the AutoWork method. (You may have to remove/comment the call to the drive() method in AutoWork)

8. Using the same reference v, create object of Bus. Pass it to the AutoWork method. (Again you may have to remove/comment the call to the drive() method in AutoWork)

Solutions

Expert Solution

CODE :

import java.util.*;

interface Vehicle
{
   public void paint();
}

class Car implements Vehicle
{
   public void paint()
   {
       System.out.println("Car painted");
   }
   public void drive()
   {
       System.out.println("Car Driven");
   }
}

class Bus implements Vehicle
{
   public void paint()
   {
       System.out.println("Bus painted");
   }
   public void drive()
   {
       System.out.println("Bus Driven");
   }
}

class JavProg
{
   public void AutoWork(Vehicle v)
   {
       v.paint();
       //v.drive();
   }

   public static void main(String args[])
   {
      
       JavProg tester =new JavProg();
       Vehicle v;//reference of vehicle interface

       Car c = new Car();
       v = c;//holding car object
      
       tester.AutoWork(v);//calling the auto work passing car object
      
       Bus b = new Bus();
       v = b;
       tester.AutoWork(v);
      
   }
}

OUTPUT :



Related Solutions

In Java Create an interface Create an interface Printing, which have a method getPageNumber () that...
In Java Create an interface Create an interface Printing, which have a method getPageNumber () that return page number of any printing. Create an abstract class named Novel that implements Printing. Include a String field for the novel's title and a double field for the novel price. Within the class, include a constructor that requires the novel title, and add two get methods--one that returns the title and one that returns the price. Include an abstract method named setPrice().. Create...
Consider the following interface: public interface Car{ public String getMake(); public void setMake(); public void honk();...
Consider the following interface: public interface Car{ public String getMake(); public void setMake(); public void honk(); public void crash(); public void drive(); } public interface Boat{ public String getMake (); public void setMake (); public void blast_horn(); public void sink(); public void move(); } 1. Create a concrete FamilyCar class from the Car interface.
java: Given the definitions: public interface ActionListener { public void actionPerformed(ActionEvent e); } public JTextField {...
java: Given the definitions: public interface ActionListener { public void actionPerformed(ActionEvent e); } public JTextField { public JTextField(){} public void setText(String text) {} public String getText() {} } Write the code to create a JButton on the South of the window and a JTextField on the North. The first time you click on the button, it should print out “hello!” on the JTextField. For the second time, should show “hello!hello!”. For the third time, should show “hello!hello!hello!”.
from Big Java Early Objects 7th edition Question Declare an interface Filter as follows: public interface...
from Big Java Early Objects 7th edition Question Declare an interface Filter as follows: public interface Filter { boolean accept(Object x); } Modify the implementation of the Data class in Section 10.4 to use both a Measurer and a Filter object. Only objects that the filter accepts should be processed. Demonstrate your modification by processing a collection of bank accounts, filtering out all accounts with balances less than $1,000. Solve Exercise •• P10.6, using a lambda expression for the filter....
in java. using the following template as a method, public static void shellSort(int[] array) { create...
in java. using the following template as a method, public static void shellSort(int[] array) { create a program that counts the number of comparisons and swaps of the sorting method does using int array1[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; // Best Case Test int array2[] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1}; // Worst Case Test int array3[] = {10, 4, 8, 2, 6, 1, 9, 3, 7, 5}; //...
For Java Let's get more practice testing! Declare a public class TestRotateString with a single void...
For Java Let's get more practice testing! Declare a public class TestRotateString with a single void static method named testRotateString. testRotateString receives an instance of RotateString as a parameter, which has a single method rotate. rotate takes a String and an int as arguments, and rotates the passed string by the passed amount. If the amount is positive, the rotation is to the right; if negative, to the left. If the String is null, rotate should return null. Your testRotateString...
JAVA please write this method public static void recursiveSelectionSort(int[] arr) { }
JAVA please write this method public static void recursiveSelectionSort(int[] arr) { }
JAVA please write this method public static void recursiveMergeSort(int[] arr) { }
JAVA please write this method public static void recursiveMergeSort(int[] arr) { }
JAVA Given the company and Employee interface, create a findLeastPaid() method (that will do the opposite...
JAVA Given the company and Employee interface, create a findLeastPaid() method (that will do the opposite of whats below). instead of finding the the best paid convert it find the lowest paid person. Given the Company and FullTimeEmployee classes and the Employee interface, write a Java program that reads in a file of full time employees with each line of input containing the name of the employee (String) and gross Salary (double) and stores them in an array list. Then...
JAVA Given the company and Employee interface, create a findLeastPaid() method (that will do the opposite...
JAVA Given the company and Employee interface, create a findLeastPaid() method (that will do the opposite of whats below). instead of finding the the best paid convert it find the lowest paid person. write a Java program that reads in a file of full time employees with each line of input containing the name of the employee (String) and gross Salary (double) and stores them in an array list. Then the arraylist of stored employees, find and print the less...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT