Take Test: Midterm Exa ? C 0-Secure l https://blackboard uviedu/webapps/assessme
ID: 3910300 • Letter: T
Question
Take Test: Midterm Exa ? C 0-Secure l https://blackboard uviedu/webapps/assessment/take.., 40 points Save Answer QUESTION 6 Given the following class public class Vehicle protected String manufacturer, protected int year protected char fuelType; protected String VIN, Il vehicle manufacturer ll model year llG gas, E electric, D diesel, W wind, U unknown Il an String to hold the 17 characters of the vehicle's VIN number l no-arg constructor delined, as are all get/set methods. Write me: An overloaded constructor (takes the four data members as parameters): assume that a custom exception class called InvalidVINException exists. Your constructor will test to ensure that the first character of the VIN is a digit, if not through an exception. Will also test to ensure that the VIN is exactly 17 characters in length, if not through an exception. Both exceptions must have a custom message. a copy constructor, and . an equals method. .a toString method TTTArial Click Save and Submit to save and submit. Click Save All Ansvers to save all ansvers Save All Answers Save and SubmitExplanation / Answer
public class Vehicle {
protected String manufacturer;
protected int year;
protected char fuelType;
protected String VIN;
public Vehicle(String m,int y,char f,String v) throws InvalidVINException {
if(!Character.isDigit(v.charAt(0))) {
throw new InvalidVINException("Vehicle number should start with digit");
}
else if(v.length()!=17) {
throw new InvalidVINException("Vehicle number should have 17 character");
}
else {
manufacturer=m;
year=y;
fuelType=f;
VIN=v;
}
}
public Vehicle(Vehicle ob) {
manufacturer=ob.manufacturer;
year=ob.year;
fuelType=ob.fuelType;
VIN=ob.VIN;
}
@Override
public boolean equals(Object obj) {
// If the obj is itself return true
if (obj == this) {
return true;
}
/* if obj is not an instance of Vehicle returns false */
if (!(obj instanceof Vehicle)) {
return false;
}
// typecast obj to Vehicle
Vehicle v = (Vehicle) obj;
// if all data member match return true, false otherwise
if(v.manufacturer==manufacturer && v.fuelType==fuelType && v.year==year && v.VIN==VIN)
return true;
else
return false;
}
@Override
public String toString() {
//create a custom print string and return the value
String str="Manufacturer name : "+manufacturer;
str+=" Model year : "+year;
if(fuelType=='G')
str+=" Fuel Type : Gas";
else if(fuelType=='E')
str+=" Fuel Type : Electric";
else if(fuelType=='W')
str+=" Fuel Type : Wind";
else
str+=" Fuel Type : Unknown";
str+=" Vehicle Number : "+VIN;
return str;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.