4. Write part of the code that removes the car from the dealer class that is sold.
do part 4
import java.util.*;
// Car class
class Car{
private String name; // Variable to hold car name
private String model; // Variable to hold car model
// Default constructor
Car(){
this.name = null;
this.model = null;
}
// Parametrised constructor
Car(String name, String model){
this.name = name;
this.model = model;
}
// Function to get car name
public String getName(){
return this.name;
}
}
// Dealer class
class Dealer{
private Car[] arr; // Array holding car objects for a dealer
private int count; // Variable to hold number of cars under a dealer
// Default constructor
Dealer(){
arr = new Car[50];
count=0;
}
// Function to add a car under a dealer
public void addCar(Car obj){
this.arr[this.count] = obj;
this.count++;
}
// Function to check if a car exists under a dealer or not
public boolean contains(String name){
int flag=0;
for(int i=0; i<this.count; i++){
if(arr[i].getName().equals(name)){
flag=1;
break;
}
}
if(flag==1){
return true;
}
return false;
}
}
// Driver Class
public class Main
{
public static void main(String[] args) {
Dealer obj = new Dealer(); // Creating dealer class object
Car c1 = new Car("Honda","Model1"); // Creating Honda car object
Car c2 = new Car("Toyota","Model1"); // Creating Toyota car object
System.out.println("Honda car added.");
obj.addCar(c1);
System.out.println("Toyota car added.");
obj.addCar(c2);
Scanner sc = new Scanner(System.in);
System.out.println();
System.out.print("Enter the car name to be searched : ");
String name = sc.nextLine();
boolean result = obj.contains(name);
if(result==true){
System.out.println("Car is present");
}
else{
System.out.println("Car is not present");
}
}
}
![Question 5:
1. Explain how the Dealer class can maintain record of multiple cars using minimal code.
2. Write an addCar() method that receives a Car object and adds it to the Dealer class.
3. Write a contains method that takes the name of a Car and determines whether the Dealer
has that Car in its collection.
4. Write part of the code that removes the car from the dealer class that is sold.](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F4eca6402-bf16-4ab3-b4a7-bffc058f8485%2Fb181999e-40aa-4ef8-b079-6ef6d34061e4%2Fg3evg6mc_processed.png&w=3840&q=75)
![](/static/compass_v2/shared-icons/check-mark.png)
Trending now
This is a popular solution!
Step by step
Solved in 2 steps
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Computer Networking: A Top-Down Approach (7th Edi…](https://www.bartleby.com/isbn_cover_images/9780133594140/9780133594140_smallCoverImage.gif)
![Computer Organization and Design MIPS Edition, Fi…](https://www.bartleby.com/isbn_cover_images/9780124077263/9780124077263_smallCoverImage.gif)
![Network+ Guide to Networks (MindTap Course List)](https://www.bartleby.com/isbn_cover_images/9781337569330/9781337569330_smallCoverImage.gif)
![Computer Networking: A Top-Down Approach (7th Edi…](https://www.bartleby.com/isbn_cover_images/9780133594140/9780133594140_smallCoverImage.gif)
![Computer Organization and Design MIPS Edition, Fi…](https://www.bartleby.com/isbn_cover_images/9780124077263/9780124077263_smallCoverImage.gif)
![Network+ Guide to Networks (MindTap Course List)](https://www.bartleby.com/isbn_cover_images/9781337569330/9781337569330_smallCoverImage.gif)
![Concepts of Database Management](https://www.bartleby.com/isbn_cover_images/9781337093422/9781337093422_smallCoverImage.gif)
![Prelude to Programming](https://www.bartleby.com/isbn_cover_images/9780133750423/9780133750423_smallCoverImage.jpg)
![Sc Business Data Communications and Networking, T…](https://www.bartleby.com/isbn_cover_images/9781119368830/9781119368830_smallCoverImage.gif)