In: Computer Science
Write a Java application, and an additional class to represent some real-world entity such as a technology item, an animal, a person, a vehicle, etc. Keep in mind that a class is a model in code of something real or imagined, which has attributes (member variables) and behaviors (member methods).
The class will:
Create a total of 5 member variables for the class, selecting the appropriate data types for each field. For example, a class to represent a lamp might include color, price, height, numBulbs, batteryOperated. Each of these 5 variables need a data type.
Include at least three different constructor methods, in addition to the default constructor (0 argument constructor). The constructor is a function (method) which allocates memory and initialized the member variables specified in (a.).
Include getters/setters for to serve as as mutators and accessors for each variable. Name these appropriately such as setColor & getColor, setPrice & getPrice, setHeight & getHeight, setNumBulbs and getNumBulbs, and setBatteryOperated & getBatteryOperated.
Create a member function showValues() to display the values of an object in a formatted manner.
Create at least 2 other member functions (methods) for the class that will perform some operation on the data (i.e. calculations or additional report/display of output). For example, turnLampOn, changeBulb, etc.
The Java application class (with a main method) will:
Instantiate at least three objects (using each of the constructors at least once) with your program.
Note: Set the remaining data with the mutator methods.
Store the data from the individual objects into an ArrayList (or some other dynamic data structure of objects.
Utilize the showValues() method and each of the two methods on each of your objects to demonstrate their use.
Required Output: Generate output samples demonstrating all of your member functions, adequately testing them and showing their functionality (i.e. inputting values, outputting values (displaying them), performing calculations, etc.).
Vehicle.java
public class Vehicle {
// INSTANCE VRIABLES
private String make, model, color;
private int year;
private int distanceTravelled;
// DEFAULT CONSTRUCTOR
public Vehicle()
{
this.make = "";
this.model = "";
this.color = "";
this.year = 0;
this.distanceTravelled = 0;
}
// OVERLOADED CONSTRUCTOR
public Vehicle(String make, String model, String color, int
year)
{
this.make = make;
this.model = model;
this.color = color;
this.year = year;
this.distanceTravelled = 0;
}
// GETTER METHODS
public String getMake() {
return make;
}
public String getModel() {
return model;
}
public String getColor() {
return color;
}
public int getYear() {
return year;
}
public int getDistanceTravelled() {
return distanceTravelled;
}
// SETTER METHODS
public void setMake(String make) {
this.make = make;
}
public void setModel(String model) {
this.model = model;
}
public void setColor(String color) {
this.color = color;
}
public void setYear(int year) {
this.year = year;
}
public void setDistanceTravelled(int distanceTravelled) {
this.distanceTravelled = distanceTravelled;
}
public void moveForward(int distance)
{
if(distance < 0)
System.out.println("Please enter a positive distance for moving
forward.");
else
{
System.out.println("Vehicle moved " + distance + " km
forward.");
this.distanceTravelled += distance;
}
}
public void moveBackward(int distance)
{
if(distance > 0)
System.out.println("Please enter a negative distance for moving
backward.");
else
{
System.out.println("Vehicle moved " + (distance * -1) + " km
backward.");
this.distanceTravelled -= distance;
}
}
// METHOD TO PRINT OBJECT DETAILS
public void showValues()
{
System.out.println("Make: " + this.make + ", Model: " + this.model
+ ", Color: "
+ this.color + ", Year built-in: " + this.year + ", Distance
travelled: " + this.distanceTravelled);
}
}
DriverApp.java (Driver class)
import java.util.ArrayList;
public class DriverApp {
public static void main(String[] args)
{
ArrayList<Vehicle> vehicles = new ArrayList<>();
// adding Vehcile instance using it's constrtuctor
vehicles.add(new Vehicle("Cadillac", "Series 61", "Red",
1957));
// adding Vehicle instance using Setter methods
Vehicle v1 = new Vehicle();
v1.setMake("BMW");
v1.setModel("5-Series GT");
v1.setColor("White");
v1.setYear(2016);
vehicles.add(v1);
// adding Vehcile instance using it's constrtuctor
vehicles.add(new Vehicle("Kia", "Bongo Frontier", "Black",
2001));
// display all the vehicle instance details
System.out.println("ALL VEHICLE DETAILS:");
for(Vehicle v : vehicles)
{
v.showValues();
}
System.out.println("\nMOVING VEHICLE1 FORWARD, AND VEHICLE2 &
VEHICLE3 BACKWARD:");
vehicles.get(0).moveForward(15);
vehicles.get(1).moveBackward(-7);
vehicles.get(2).moveBackward(-22);
// display all the vehicle instance details
System.out.println("\nALL VEHICLE DETAILS:");
for(Vehicle v : vehicles)
{
v.showValues();
}
}
}
***************************************************************** SCREENSHOT **********************************************************