Question

In: Computer Science

1. Create a class Car with data: Name, Price, Production and properly methods. 2. Create another...

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:

  • Add: add new item of T to a
  • Display: display all items of a
  • getSize: return the number item of a
  • checkEmpty: check and return whether a is empty or not
  • delete(int pos): remove the item at the position pos of a.

Write a program to use GenericCar as above menu.

Make your own main program to test all above methods.

Solutions

Expert Solution

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 :)


Related Solutions

Code in Java 1. Create a class Flower with data: Name, Price, Color and properly methods....
Code in Java 1. Create a class Flower with data: Name, Price, Color and properly methods. 2. Create another class named ListFlower. This class manages a collection of Flower (may be LinkedList) named a. Implementing some methods for ListFlower: Add: add new item of Flower to a Display: display all items of a sort(): sort as descending by Price and display all items of a search(Flower f): check and return whether f is exists in a or not. delete(int pos):...
1. Please create a New Class with the Class Name: Class17Ex Please add the ten methods:...
1. Please create a New Class with the Class Name: Class17Ex Please add the ten methods: 1. numberOfStudents 2. getName 3. getStudentID 4. getCredits 5. getLoginName 6. getTime 7. getValue 8. getDisplayValue 9. sum 10. max Show Class17Ex.java file with full working please. Let me know if you have any questions.
Create java Class with name Conversion. Instructions for Conversion class: The Conversion class will contain methods...
Create java Class with name Conversion. Instructions for Conversion class: The Conversion class will contain methods designed to perform simple conversions. Specifically, you will be writing methods to convert temperature between Fahrenheit and Celsius and length between meters and inches and practicing overloading methods. See the API document for the Conversion class for a list of the methods you will write. Also, because all of the methods of the Conversion class will be static, you should ensure that it is...
1: Create a class Circle 2: Create two instances of the Circle class 1: Based on...
1: Create a class Circle 2: Create two instances of the Circle class 1: Based on Program 13-1 (see attachment Pr13-1.cpp) in the textbook, create a class name “Circle” with the following declarations (hint: you can use PI=3.14): //Circle class declaration class Circle { private: double radius; public: void setRadius(double); double getRadius() const; double getArea() const; double getPerimeter() const; }; 2: Based on Program 13-2 (see attachment Pr13-2.cpp) in the textbook, create two instances of the Circle class, pizza1 and...
JAVA CODING PLEASE Create a class SportsCar that inherits from the Car class (CAR CLASS LISTED...
JAVA CODING PLEASE Create a class SportsCar that inherits from the Car class (CAR CLASS LISTED BELOW THIS QUESTION). Include the following: A variable Roof that holds the type of roof (Ex: Convertible, Hard-Top, Soft-Top) A variable Doors that holds the car’s number of doors (Ex: 2, 4) Implement the ChangeSpeed method to add 20 to the speed each time it is called. Add exception handling to the ChangeSpeed method to keep the speed under 65. Implement the sound method...
Create a class that holds data about a job applicant. Include a name, a phone number,...
Create a class that holds data about a job applicant. Include a name, a phone number, and four Boolean fields that represent whether the applicant is skilled in each of the following areas: word processing, spreadsheets, databases, and graphics: Include a constructor that accepts values for each of the fields. Also include a get method for each field. Create an application that instantiates several job applicant objects and pass each in turn to a Boolean method that determines whether each...
IN JAVA PLEASE Create a class called Child with an instance data values: name and age....
IN JAVA PLEASE Create a class called Child with an instance data values: name and age. a. Define a constructor to accept and initialize instance data b. include setter and getter methods for instance data c. include a toString method that returns a one line description of the child
Create a class named Horse that contains the following data fields: name - of type String...
Create a class named Horse that contains the following data fields: name - of type String color - of type String birthYear - of type int Include get and set methods for these fields. Next, create a subclass named RaceHorse, which contains an additional field, races (of type int), that holds the number of races in which the horse has competed and additional methods to get and set the new field. ------------------------------------ DemoHorses.java public class DemoHorses {     public static void...
Write a class Car that contains the following attributes: The name of car The direction of...
Write a class Car that contains the following attributes: The name of car The direction of car (E, W, N, S) The position of car (from imaginary zero point) The class has the following member functions: A constructor to initialize the attributes. Turn function to change the direction of car to one step right side (e.g. if the direction is to E,it should be changed to S and so on.) Overload the Turn function to change the direction to any...
THIS IS JAVA PROGRAMMING 1. Create a class named Name that contains the following: • A...
THIS IS JAVA PROGRAMMING 1. Create a class named Name that contains the following: • A private String to represent the first name. • A private String to represent the last name. • A public constructor that accepts two values and assigns them to the above properties. • Public methods named getProperty (e.g. getFirstName) to return the value of the property. • Public methods named setProperty ( e.g. setFirstName)to assign values to each property by using a single argument passed...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT