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. when you create a class by making it inherit from another class, the new class...
1. when you create a class by making it inherit from another class, the new class automatically, contains the data fields and _____ of the original class a. fonts b. methods c. class names d. arrays 2. if a programming language does not support ________, the language is not considered object oriented a. syntax b. applets c. loops d. polymorphism 3. when you create a class and do not provide a ______, java automatically supplies you with a default one...
java Objective: Create a class. Create objects. Use methods of a class. Create a class BankAccount...
java Objective: Create a class. Create objects. Use methods of a class. Create a class BankAccount to represent a bank account according to the following requirements: A bank account has three attributes: accountnumber, balance and customer name. Add a constructor without parameters. In the initialization of the attributes, set the number and the balance to zero and the customer name to an empty string. Add a constructor with three parameters to initialize all the attributes by specific values. Add a...
Create a Class with Data Fields and Methods in Java. Provide a Java coding solution for...
Create a Class with Data Fields and Methods in Java. Provide a Java coding solution for the following: 1. Name the class SalonServices 2. Add private data fields: a. salonServiceDescription – This field is a String type b. price - This field is a double type 3. Create two methods that will set the field values. a. The first method setSalonServiceDescription() accepts a String parameter defined as service and assigns it to the salonServiceDescription. The method is not static b....
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...
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...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT