Question

In: Computer Science

Instructions Using the installed software for this course create a generic class called VehicleRental which accepts...

Instructions

Using the installed software for this course create a generic class called VehicleRental which accepts any generic type of Vehicle ( create an instance of Car, Van and MotorCycle classes)

Each type of Vehicle object has methods called drive, start and stop ( add simple print statement)

The VehicleRental class has a method called rent which accept a generic type of Vehicle object, this method will call drive method of passed Vehicle object

The solution will produce the following:

  1. VehicleRental class
  2. Vehicle interface from which following classes will implement
  3. Car, Van and MotorCycle classes
  4. VehicleRentalTest class with main method

Solutions

Expert Solution

Since you have not mentioned the language of your preference, I am providing the code in Java.

CODE

interface Vehicle {

void drive();

void start();

void stop();

}

class Van implements Vehicle {

@Override

public void drive() {

System.out.println("Driving the van...");

}

@Override

public void start() {

System.out.println("Starting the van...");

}

@Override

public void stop() {

System.out.println("Stopping the van...");

}

}

class Car implements Vehicle {

@Override

public void drive() {

System.out.println("Driving the car...");

}

@Override

public void start() {

System.out.println("Starting the car...");

}

@Override

public void stop() {

System.out.println("Stopping the car...");

}

}

class MotorCycle implements Vehicle {

@Override

public void drive() {

System.out.println("Driving the motorcycle...");

}

@Override

public void start() {

System.out.println("Starting the motorcycle...");

}

@Override

public void stop() {

System.out.println("Stopping the motorcycle...");

}

}

class VehicleRental<T extends Vehicle> {

public void rent(Vehicle v) {

v.drive();

}

}

public class Main {

public static void main(String[] args) {

VehicleRental<Car> vr1 = new VehicleRental<>();

vr1.rent(new Car());

VehicleRental<Van> vr2 = new VehicleRental<>();

vr2.rent(new Van());

VehicleRental<MotorCycle> vr3 = new VehicleRental<>();

vr3.rent(new MotorCycle());

}

}

OUTPUT


Related Solutions

Generic types A class or interface that declares one or more generic variables is called a...
Generic types A class or interface that declares one or more generic variables is called a generic type. In this portion of the activity, you will make a class generic. Open GenericsC.java in jGRASP then compile it. At this point you should be familiar with the two type-safety warnings given by the compiler. You should be able to understand the source of the error: the use of the raw types List and Collection. Since the List being declared (al) is...
Write a python program using the following requirements: Create a class called Sentence which has a...
Write a python program using the following requirements: Create a class called Sentence which has a constructor that takes a sentence string as input. The default value for the constructor should be an empty string The sentence must be a private attribute in the class contains the following class methods: get_all_words — Returns all the words in the sentence as a list get_word — Returns only the word at a particular index in the sentence Arguments: index set_word — Changes...
***Given a class called Student and a class called Course that contains an ArrayList of Student....
***Given a class called Student and a class called Course that contains an ArrayList of Student. Write a method called dropStudent() as described below. Refer to Student.java below to learn what methods are available.*** Course.java import java.util.*; import java.io.*; /****************************************************** * A list of students in a course *****************************************************/ public class Course{ /** collection of Students */ private ArrayList<Student> roster; /***************************************************** Constructor for objects of class Course *****************************************************/ public Course(){ roster = new ArrayList<Student>(); } /***************************************************** Remove student with the...
Please solve using jupyter notebook . 10.10- (Invoice Class) Create a class called Invoice that a...
Please solve using jupyter notebook . 10.10- (Invoice Class) Create a class called Invoice that a hardware store might use to represent an invoice for an item sold at the store. An Invoice should include four pieces of information as data attributes—a part number (a string), a part description (a string), a quantity of the item being purchased (an int) and a price per item (a Decimal). Your class should have an __init__ method that initializes the four data attributes....
Create a class called Student which stores • the name of the student • the grade...
Create a class called Student which stores • the name of the student • the grade of the student • Write a main method that asks the user for the name of the input file and the name of the output file. Main should open the input file for reading . It should read in the first and last name of each student into the Student’s name field. It should read the grade into the grade field. • Calculate the...
Using tkinter, create a GUI interface which accepts input of annual income. Using the table below,...
Using tkinter, create a GUI interface which accepts input of annual income. Using the table below, determine and output the income tax for that income. Tax Rate Income 10% $0 to $9,875.00;  12% $9,876.01 to $40,125.00; 22% $40,126 to $85,525.00; 24% $85,526.01 to $163,300.00. Test with the following income data: 163,300.00 9,876.01 85,525.00
Python Using tkinter, create a GUI interface which accepts input of a GPA. If the GPA...
Python Using tkinter, create a GUI interface which accepts input of a GPA. If the GPA is >= 3.5, display 'Dean’s List' If the GPA is < 2.0, display 'Probation’ If the GPA is < 3.5 and >= 2.0, display 'Regular Standing' Run one test case for each case above and save the output.
Create a java class with name Cat. Instructions for Cat class: This class is modeled after...
Create a java class with name Cat. Instructions for Cat class: This class is modeled after a Cat. You should have instance variables as follows: The Cat’s name The number of mice caught by the Cat. Whether or not the Cat is secretly plotting to kill you Note that you will need to choose both good types and meaningful identifiers for each of these instance variables. You may also assume that the Cat is not automatically always secretly plotting to...
Needed in C++ In this assignment, you are asked to create a class called Account, which...
Needed in C++ In this assignment, you are asked to create a class called Account, which models a bank account. The requirement of the account class is as follows (1) It contains two data members: accountNumber and balance, which maintains the current account name and balance, respectively. (1) It contains three functions: functions credit() and debit(), which adds or subtracts the given amount from the balance, respectively. The debit() function shall print ”amount withdrawn exceeds the current balance!” if the...
Implement the Shape hierarchy -- create an abstract class called Shape, which will be the parent...
Implement the Shape hierarchy -- create an abstract class called Shape, which will be the parent class to TwoDimensionalShape and ThreeDimensionalShape. The classes Circle, Square, and Triangle should inherit from TwoDimensionalShape, while Sphere, Cube, and Tetrahedron should inherit from ThreeDimensionalShape. Each TwoDimensionalShape should have the methods getArea() and getPerimeter(), which calculate the area and perimeter of the shape, respectively. Every ThreeDimensionalShape should have the methods getArea() and getVolume(), which respectively calculate the surface area and volume of the shape. Every...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT