Answer to Question #59130 in Java | JSP | JSF for Peter

Question #59130
The UML for an Instantiable class called Vehicle is shown below:

Write an application program that uses an array to create three Vehicle objects with appropriate data. Add code to display the contents of the array.



Vehicle

-regNo:String
-carMake:String

+Vehicle()
+Vehicle(String, String)
+setRegNo(String):void
+setCarMake(String):void
+getRegNo():String
+getCarMake():String
+toString():String
1
Expert's answer
2016-04-13T09:44:04-0400
package com.company;
public class VehicleApp {
public static void main(String[] args) {
Vehicle[] vehicles = new Vehicle[3];
vehicles[0] = new Vehicle();
vehicles[0].setRegNo("21313");
vehicles[0].setCarMake("Audi");
System.out.println(vehicles[0]);

vehicles[1] = new Vehicle();
vehicles[1].setRegNo("56363");
vehicles[1].setCarMake("Nissan");
System.out.println(vehicles[1]);

vehicles[2] = new Vehicle();
vehicles[2].setRegNo("57786");
vehicles[2].setCarMake("BMW");
System.out.println(vehicles[2]);


}
}



package com.company;
public class Vehicle {
private String regNo;
private String carMake;
/**
Constructs a Vehicle object
*/
public Vehicle() { }
/**
Constructs a Vehicle object
@param regNo the reg no of the vehicle
@param carMake the carMake of the vehicle
*/
public Vehicle(String regNo, String carMake) {
this.regNo = regNo;
this.carMake = carMake;
}
/**
Returns the reg no of the vehicle
@return regNo of the vehicle
*/
public String GetregNo (){
return regNo;
}
/**
Returns the car make of the vehicle
@return carMake of the vehicle
*/
public String getcarMake (){
return carMake;
}
/**
Sets the regno to the parameter value
@param newregNo the reg no of the vehicle
*/
public void setRegNo(String newregNo) {
regNo = newregNo;
}
/**
Sets the car make to the parameter value
@param newcarMake the car make of the vehicle
*/
public void setCarMake(String newcarMake) {
carMake = newcarMake;
}
@Override
public String toString (){
return "RegNo: " + this.regNo + ", car make: " + this.carMake;
}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS