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!”.
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...
Create a new Java file, containing this code public class DataStatsUser { public static void main...
Create a new Java file, containing this code public class DataStatsUser { public static void main (String[] args) { DataStats d = new DataStats(6); d.append(1.1); d.append(2.1); d.append(3.1); System.out.println("final so far is: " + d.mean()); d.append(4.1); d.append(5.1); d.append(6.1); System.out.println("final mean is: " + d.mean()); } } This code depends on a class called DataStats, with the following API: public class DataStats { public DataStats(int N) { } // set up an array (to accept up to N doubles) and other member...
Programming Language : JAVA Create an interface named Turner, with a single method called turn(). Then...
Programming Language : JAVA Create an interface named Turner, with a single method called turn(). Then create 4 classes: 1- Leaf: that implements turn(), which changes the color of the Leaf object and returns true. If for any reason it is unable to change color, it should return false (you can come up with a reason for failure). The new color can be determined at random. 2- Document: that implements turn(), which changes the page on the document to the...
(The Colorable interface) Design an interface named Colorable with a void method named howToColor(). Every class...
(The Colorable interface) Design an interface named Colorable with a void method named howToColor(). Every class of a colorable object must implement the Colorable interface. Design a class named Square that extends GeometricObject and implements Colorable. Design another class named Triangle that extends GeometricObject and implements Colorable. Implement howToColor in Square to display the message Color all four sides. Implement howToColor in Triangle to display the message Color all three sides. Draw a UML diagram that involves Colorable, Square, Triangle,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT