Question
Q: Based on the code given in java apply the following: 1- inheritance and polymorphism 2- create abstract and interface class class Car : import
Q: Based on the code given in java apply the following:
1- inheritance and polymorphism
2- create abstract and interface class
class Car :
import java.util.Scanner;
public class Car { String carType; String numberPlate; Car() { } Car(String type, String numberPlate) { this.carType = type; this.numberPlate = numberPlate; } public String getCarType() { return carType; } public String getNumberPlate() { return numberPlate; } ArrayList cars = new ArrayList(); public void addCar() { Scanner sc = new Scanner(System.in); String s; int i=0; do { System.out.print("Enter car type: "); String name = sc.nextLine(); System.out.print("Enter the car plate number"); String num = sc.nextLine(); cars.add(new Car(name, num)); i++; System.out.print("Enter another Car details ? press y/n: "); s = sc.nextLine().toLowerCase(); }while( s.equals("y")); } public boolean isRegistered() { for(Car c : cars) { if(this.numberPlate.equals(c.numberPlate)) { return true; } } return false; } public String toString() { String str = ""; for(Car c: cars) { str = str +" Car Type: "+c.getCarType() + " Number plate: "+c.getNumberPlate(); } return str; } }
---------------------------------------------------------------------------------------------------------------------------------------
2- class Customer:
import java.util.ArrayList; import java.util.Scanner; public class Customer { String customerName; String mobileNum; String email; Customer() { } Customer(String name, String number, String email) { this.customerName = name; this.mobileNum = number; this.email = email; } public String getCustomerName() { return customerName; } public String getEmail() { return email; } public String getMobileNum() { return mobileNum; } public Customer addCusInfo() { Scanner sc = new Scanner(System.in); System.out.println("Enter customer name, contact no. and email address"); String name = sc.nextLine(); String contact = sc.nextLine(); String mail = sc.nextLine(); return new Customer(name, contact, mail); } public String toString() { return "Customer Name: "+getCustomerName() +" Mobile number: "+getMobileNum() +" Email: "+getEmail(); } }
-------------------------------------------------------------------------------------------------------------
3- class Employee:
public class Employee { String employeeName; String employeeId; Employee(String name, String id) { this.employeeName = name; this.employeeId = id; } public String toString() { return "Employee name: "+this.employeeName + " EmployeeId: "+this.employeeId; } }
-------------------------------------------------------------------------------------------------------------------------------------------------------
4- Test class (for main) :
Customer cus1 = new Customer(); cus1 = cus1.addCusInfo(); Car car1 = new Car(); car1.addCar(); Customer cus2 = new Customer(); cus2 = cus2.addCusInfo(); Car car2 = new Car(); car2.addCar(); Employee emp1 = new Employee("john","20Abcdef"); Employee emp2 = new Employee("michael","ahdj2345"); System.out.println(emp1); System.out.println(car1); System.out.println(cus1); System.out.println(emp2); System.out.println(car2); System.out.println(cus2);
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started