4. Write part of the code that removes the car from the dealer class that is sold.

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question
100%

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.
Transcribed Image Text: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.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY