In: Computer Science
1. Create a class Car with data: Name, Price, Production and properly methods.
2. Create another class named GenericCar with a parameter of the T type. This class manages a collection of object T (may be LinkedList) named a. Implementing some methods for GenericCar:
Write a program to use GenericCar as above menu.
Make your own main program to test all above methods.
TestCar.java
--------------------------------
package com.data.structure;
import com.faisal.checgg2.com.Car;
public class TestCar {
//starting point of the program
public static <T> void main(String[] args)
{
// creating the object of
GenericCar
GenericCar car=new
GenericCar();
// adding the Car item into the
list
car.add(new
Car("Ferari",1500000.0,"Red in color"));
car.add(new
Car("Alto",1700000.0,"white in colore"));
car.add(new
Car("Enova",1500000.0,"black in color"));
// Display all item from List
car.display();
// fetch the size of all item in
the List
int itemSize=car.getSize();
System.out.println("Number of item
in the List :"+itemSize);
// check the List is Empty or
Not
String
chkEmpty=car.checkEmpty();
System.out.println("List is Empty
or Not : "+chkEmpty);
// Delete a perticuler item from
List
car.delete(2);
System.out.println("---After
deletion of item 2---");
// Again Display all item from
List
car.display();
}
}
----------------------------
GenericCar.java
----------------------------
package com.data.structure;
import java.util.ArrayList;
import com.faisal.checgg2.com.Car;
public class GenericCar<T> {
ArrayList<Car> carList=new
ArrayList<Car>();
// below method used to add Car item in the List
public void add(Car car) {
carList.add(car);
}
// below method is used to display all item in List
public void display() {
for(Car car: carList) {
System.out.println("Name :"+car.getName()+", Price
:"+car.getPrice()+", Production :"+car.getProduction());
}
}
// below method is used to get the size of List
public int getSize() {
return carList.size();
}
// below method is used to check item List is Empty or
Not
public String checkEmpty() {
if(carList.size()>0) {
return "Not
Empty";
}
return "Empty";
}
//// below method is used to delete a item from List
public void delete(int n) {
if(carList.size()>n) {
carList.remove(n);
System.out.println("Item deleted from List At Index : "+n);
}
}
}
---------------------------
Car.java
---------------------------
package com.faisal.checgg2.com;
public class Car {
String name;
double price;
String production;
public Car(String name, double price, String
production) {
super();
this.name = name;
this.price = price;
this.production = production;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getProduction() {
return production;
}
public void setProduction(String production) {
this.production = production;
}
}
----------------------------
SAMPLE OUTPUT:
Name :Ferari, Price :1500000.0, Production :Red in color
Name :Alto, Price :1700000.0, Production :white in colore
Name :Enova, Price :1500000.0, Production :black in color
Number of item in the List :3
List is Empty or Not : Not Empty
Item deleted from List At Index : 2
---After deletion of item 2---
Name :Ferari, Price :1500000.0, Production :Red in color
Name :Alto, Price :1700000.0, Production :white in colore
-------------------------------
SUMMARY:
I have provided the solution as per your requirement, i hope
you're satisfied with the way i have approached. please dont
hesitate to give me a Like if you like it, i appreciate your like.
If you have any queries you can shoot them any time in comments
section. I will be glad to help you.
Thank you :)