Please help me with this code. Thank you
Implement the following Java class: Vehicle
Class should contain next instance variables:
Supply your class with:
Your class will implement following methods:
* - this method will expand array productionYears by 1 element and place argument int as the last subscript
** - this method will compare two objects by checking whether or not the manufacturer variables are the same
*** - this method will output all the information with a neat formatting
Write a driver program which will creates at least 2 object of this Class and tests all the methods
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
// Vehicle.java
import java.util.Arrays;
public class Vehicle {
// attributes
private int numberOfWheels;
private double engineCapacity;
private boolean isElectric;
private String manufacturer;
private int[] productionYears;
// default constructor
public Vehicle() {
numberOfWheels = 0;
engineCapacity = 0;
isElectric = false;
manufacturer = "";
productionYears = new int[0];
}
// constructor taking initial values for all elements
public Vehicle(int numberOfWheels, double engineCapacity,
boolean isElectric, String manufacturer, int[] productionYears) {
this.numberOfWheels = numberOfWheels;
this.isElectric = isElectric;
// validating and setting engine capacity
if(!isElectric){
setEngineCapacity(engineCapacity);
}
this.manufacturer = manufacturer;
this.productionYears = productionYears;
}
// getters and setters
public int getNumberOfWheels() {
return numberOfWheels;
}
public void setNumberOfWheels(int numberOfWheels) {
this.numberOfWheels = numberOfWheels;
}
public double getEngineCapacity() {
return engineCapacity;
}
public void setEngineCapacity(double engineCapacity) {
// checking if vehicle is electric
if (isElectric) {
// error
System.out.println("Error: can't set engine capacity"
+ " for an electric vehicle");
engineCapacity = 0;
} else {
// setting the value
this.engineCapacity = engineCapacity;
}
}
public boolean isElectric() {
return isElectric;
}
public void setElectric(boolean isElectric) {
this.isElectric = isElectric;
}
public String getManufacturer() {
return manufacturer;
}
public void setManufacturer(String manufacturer) {
this.manufacturer = manufacturer;
}
public int[] getProductionYears() {
return productionYears;
}
public void setProductionYears(int[] productionYears) {
this.productionYears = productionYears;
}
// method to add production year to the array
public void addProductionYear(int year) {
// creating a new array with one size more than productionYears
int[] newArray = new int[productionYears.length + 1];
// copying all elements from productionYears to newArray
for (int i = 0; i < productionYears.length; i++) {
newArray[i] = productionYears[i];
}
// adding year at the end of newArray
newArray[productionYears.length] = year;
// replacing productionYears with newArray
productionYears = newArray;
}
// returns true if two vehicles have same manufacturer
public boolean isSameManufacturer(Vehicle v) {
return this.manufacturer.equalsIgnoreCase(v.manufacturer);
}
@Override
public String toString() {
// creating and returning a properly formatted String containing all
// info
String data = "Number of wheels: " + numberOfWheels
+ ", Engine capacity: " + engineCapacity + "\n";
data += "Is electric: " + isElectric + "\n";
data += "Manufacturer: " + manufacturer + "\n";
data += "Production years: " + Arrays.toString(productionYears);
return data;
}
}
// Test.java
public class Test {
public static void main(String[] args) {
// Testing Vehicle class and methods
Vehicle v1 = new Vehicle();
System.out.println("V1\n" + v1);
v1.setManufacturer("Ford");
v1.setNumberOfWheels(4);
v1.setEngineCapacity(200);
v1.addProductionYear(1994);
v1.addProductionYear(1996);
v1.addProductionYear(1997);
v1.addProductionYear(1999);
System.out.println("V1 updated: \n" + v1);
Vehicle v2 = new Vehicle(4, 0, true, "Tesla", new int[] { 2016, 2017 });
System.out.println("V2\n" + v2);
v2.setEngineCapacity(299);
System.out.println("V2\n" + v2);
System.out.println("V1 and V2 have same manufacturers? "+v1.isSameManufacturer(v2));
v1.setManufacturer("Tesla");
System.out.println("now V1 and V2 have same manufacturers? "+v1.isSameManufacturer(v2));
}
}
/*OUTPUT*/
V1
Number of wheels: 0, Engine capacity: 0.0
Is electric: false
Manufacturer:
Production years: []
V1 updated:
Number of wheels: 4, Engine capacity: 200.0
Is electric: false
Manufacturer: Ford
Production years: [1994, 1996, 1997, 1999]
V2
Number of wheels: 4, Engine capacity: 0.0
Is electric: true
Manufacturer: Tesla
Production years: [2016, 2017]
Error: can't set engine capacity for an electric vehicle
V2
Number of wheels: 4, Engine capacity: 0.0
Is electric: true
Manufacturer: Tesla
Production years: [2016, 2017]
V1 and V2 have same manufacturers? false
now V1 and V2 have same manufacturers? true
Please help me with this code. Thank you Implement the following Java class: Vehicle Class should...
Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the concept of a college course, assuming that a course has following attributers: code (for instance COSC1337), a description, and a number of credits (for instance 3). Include a constructor, the accessors, mutators and methods ‘toString’, ‘equals’, and ‘finalize’. Write a client class to test the behavior of the class and its methods. The outline of the class is given as follows: public class Course...
In Java* Please implement a class called "MyPet". It is designed as shown in the following class diagram. Four private instance variables: name (of the type String), color (of the type String), gender (of the type char) and weight(of the type double). Three overloaded constructors: a default constructor with no argument a constructor which takes a string argument for name, and a constructor with take two strings, a char and a double for name, color, gender and weight respectively. public...
You are to create a class Appointment.java that will have the following: 5 Instance variables: private String month private int day private int year private int hour private int minute 1 default constructor public Appointment() 1 non-default constructor that accepts arguments for all instance variables, your constructor must call the setter methods below to set the values of the instance variables public Appointment(String monthPassed, int dayPassed, int yearPassed, int hourPassed, int minutePassed) 5 setter methods (one for each instance variable)...
C++ computer science Given the partial class HardDrive implementation below, implement all of the following: 1: default constructor that initialized the attributes with proper default data of your choice. 2: destructor() method, that releases all the dynamically allocated memory. 3: copy constructor() method: That properly manages the dynamic array when the object is passed by value to a function as a parameter. 4: overload the assignment operator: To properly handle copying the dynamic array when one object is assigned to...
Need help to create general class Classes Data Element - Ticket Create an abstract class called Ticket with: two abstract methods called calculateTicketPrice which returns a double and getld0 which returns an int instance variables (one of them is an object of the enumerated type Format) which are common to all the subclasses of Ticket toString method, getters and setters and at least 2 constructors, one of the constructors must be the default (no-arg) constructor. . Data Element - subclasses...
Given the following class: public class Vehicle { protected String manufacturer; // vehicle manufacturer protected int year; // model year protected char fuelType; // G = gas, E = electric, D = diesel, W = wind, U = unknown protected String VIN; // an String to hold the 17 characters of the vehicle's VIN number. ... // no-arg constructor defined, as are all get/set methods. } Write me: An overloaded constructor (takes the four data members as parameters): assume that...
I need the following written in Java please, thank you ' We want you to implement a java class that will show details on users and throw exceptions where needed. The following requirements specify what fields you are expected to implement in your User class: - A private String firstName that specifies the first name of the user - A private String lastName that specifies the last name of the user - A private int age that specifies user's age...
Exercise 1 Adjacency Matrix In this part, you will implement the data model to represent a graph. Implement the following classes Node.java: This class represents a vertex in the graph. It has only a single instance variable of type int which is set in the constructor. Implement hashCode() and equals(..) methods which are both based on the number instance variable Node - int number +Node(int number); +int getNumberO; +int hashCode() +boolean equals(Object o) +String toString0) Edge.java: This class represents a...
Writing 3 Java Classes for Student registration /** * A class which maintains basic information about an academic course. */ public class Course { /** * Attributes. */ private String code; private String title; private String dept; // name of department offering the course private int credits; /** * Constructor. */ public Course(String code, String title, int credits) { // TODO : initialize instance variables, use the static method defined in // Registrar to initialize the dept name variable...
no buffered reader. no try catch statements. java code please. And using this super class: Medialtemjava a Create the "middle" of the diagram, the DVD and ForeignDVD classes as below: The DVD class will have the following attributes and behaviors: Attributes (in addition to those inherited from Medialtem): • director:String • rating: String • runtime: int (this will represent the number of minutes) Behaviors (methods) • constructors (at least 3): the default, the "overloaded" as we know it, passing in...