Write a class called Dealer that has two fields of type Vehi

Write a class called Dealer that has two fields of type Vehicle and Customer. The field for Vehicle is a class that has fields: make, model, and VIN. The field Customer is a class that has fields: Name and account number. The field called Name is also an existing class with fields last_name and first_name; and the field account number is a unique identifier of each customer. Also, the class Customer has an accessor method called getName(). that returns the reference to a Name object; and the class Name has an accessor method that returns a string representing the last name of an individual. From the above narrative do the following. Define the class Dealer so that you can create a Dealer object either by a customer object, or by a customer object and a vehicle object. Provide accessor methods for each type of fields in the class Dealer Also, provide a mutator method that changes a vehicle that replaces a current vehicle for another one Suppose the variable d is a reference to a Dealer object, write appropriate Java statement(s) that will extract the last name of an individual customer.

Solution

1 Ans:-

//Created a Dealer Class
public class Dealer {
  
   //Vehicle and Customer objects
   public Vehicle myVehicle;
   public Customer myCustomer;

   //Constructor for creating Dealer object by using a Customer object
   public Dealer(Customer myCustomer){
       this.myCustomer = myCustomer;
   }
  
   //Constructor for creating Dealer object by using Customer and Vehicle objects.
   public Dealer(Customer myCustomer, Vehicle myVehicle){
       this.myCustomer = myCustomer;
       this.myVehicle = myVehicle;
   }
  
   //Accessor Method to get vehicle
   public Vehicle getVehicle(){
       return this.myVehicle;
   }
  
   //Accessor Method to get Customer
   public Customer getCustomer(){
       return this.myCustomer;
   }
  
   //set vehicle to a new vehicle
   public void setVehicle(Vehicle myVehicle){
       this.myVehicle = myVehicle;
   }
}

//Vehicle Class
class Vehicle{
   public String make;
   public String model;
   public String VIN;
  
}

//Customer Class
class Customer{
   public Name myNmae;
   public String accountNumber;
  
   //Accessor Method
   public Name getName(){
       return this.myName;
   }
}

//Name Class
class Name{
   public String last_name;
   public String first_name;
  
   //Accessor Method to get Last_name
   public String getLastName(){
       return this.last_name;
   }
}

2) Given d is a reference to a Dealer Object.We have to write the java statement which will get the last_name of an individual customer

     d.getCustomer().getName().getLastName();

see the ANs :- 1 above for the Customer class and Name Class Definitions. Dealer class contains getCustomer method . Customer Class contains the getName() method and the Name class contains the getLastName() method for retrieving the last name of the customer of the Dealer d object.

 Write a class called Dealer that has two fields of type Vehicle and Customer. The field for Vehicle is a class that has fields: make, model, and VIN. The field
 Write a class called Dealer that has two fields of type Vehicle and Customer. The field for Vehicle is a class that has fields: make, model, and VIN. The field

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site