Question

In: Computer Science

1. Implement the Vehicle class.  Add the following private instance variables to the Accoun class:...

1. Implement the Vehicle class.

 Add the following private instance variables to the Accoun class:

• An instance variable called wheelsCount of type int

• An instance variable called vType of type String

• An instance variable called isTruck of type boolean .

 Add getters and setters for the three instance variables

 Add the following methods to the Account class:

• A void method called initialize that takes a parameter of type int, a String,and one double and uses those arguments to set the values of the WheelsCount VType,and isTruck instance variables.

• A void method called display that displays the vehicle information

• A boolean method called equals with one parameter of type Vehicle.This method must return true if all instance variables of the given objects equal to the instance variables of the current object (this object).

 In the main method of your main class, create two Vehicle objects and perform the following:

• Initialize the first and second objects with your own parameter values.

• Display both vehicles • Use the == to compare the two vehicle objects and display the result.

• Use the equals method for the first object, compare it with the second vehicle as the parameter to the method and display the result.

Run the main program to see if the tests were successful.

• Here is a sample output of the program.

First Vehicle:

Vehicle Type: Ford F150

Wheels Count: 4

is Truck: true

Second Vehicle:

Vehicle Type: Ford F150

Wheels Count: 4

is Truck: true

v1 is NOT equal to v2 using ==

v1 is equal to v2 using equals method

Solutions

Expert Solution

Note:
If you are a beginner I will help you in executing the code.
Could you plz go through this code and let me know if u need any changes in this.Thank You
=================================

// Vehicle.java

public class Vehicle {
   // Declaring instance variables
   private int wheelsCount;
   private String vType;
   private boolean isTruck;

   /**
   * @return the wheelsCount
   */
   public int getWheelsCount() {
       return wheelsCount;
   }

   /**
   * @param wheelsCount
   * the wheelsCount to set
   */
   public void setWheelsCount(int wheelsCount) {
       this.wheelsCount = wheelsCount;
   }

   /**
   * @return the vType
   */
   public String getvType() {
       return vType;
   }

   /**
   * @param vType
   * the vType to set
   */
   public void setvType(String vType) {
       this.vType = vType;
   }

   /**
   * @return the isTruck
   */
   public boolean isTruck() {
       return isTruck;
   }

   /**
   * @param isTruck
   * the isTruck to set
   */
   public void setTruck(boolean isTruck) {
       this.isTruck = isTruck;
   }

   public void initialize(int noOfWheels, String vType, boolean isTruck) {
       this.wheelsCount = noOfWheels;
       this.vType = vType;
       this.isTruck = isTruck;
   }

   public void display() {
       System.out.println("Vehicle Type :" + vType);
       System.out.println("Wheels Count :" + wheelsCount);
       System.out.println("is Truck :" + isTruck);
   }

   // This method will check whether the two vehicle class objects are equal or not
   public boolean equals(Vehicle v) {
       if (vType.equalsIgnoreCase(v.getvType())
               && wheelsCount == v.getWheelsCount() && isTruck == v.isTruck()) {
           return true;
       } else {
           return false;
       }
   }
}

====================================

====================================

// Tester.java

public class Tester {

   public static void main(String[] args) {

       Vehicle v1 = new Vehicle();
       v1.initialize(4, "Ford F150", true);

       Vehicle v2 = new Vehicle();
       v2.initialize(4, "Ford F150", true);
       System.out.println("First Vehicle:");

       v1.display();
       System.out.println("Second Vehicle:");
v2.display();

       if (v1 == v2) {
           System.out.println("v1 is equal to v2 using ==");
       } else {
           System.out.println("v1 is NOT equal to v2 using ==");
       }

       if (v1.equals(v2)) {
           System.out.println("v1 is equal to v2 using equals method");
       } else {
           System.out.println("v1 is NOT equal to v2 using equals method");
       }

   }

}

====================================

====================================

Output:

=====================Could you plz rate me well.Thank You


Related Solutions

public class OperationsBetween { // You must define the following: // 1.) Two private instance variables,...
public class OperationsBetween { // You must define the following: // 1.) Two private instance variables, min and max   // 2.) A constructor which takes initial values for // min and max // 3.) An instance method named sum, which sums the // values between min and max and returns the // result. For example, if min = 3 and max = 5, // then this should return 12 (3 + 4 + 5). If // min is greater than...
public class OperationsBetween { // You must define the following: // 1.) Two private instance variables,...
public class OperationsBetween { // You must define the following: // 1.) Two private instance variables, min and max // // 2.) A constructor which takes initial values for // min and max // // 3.) An instance method named sum, which sums the // values between min and max and returns the // result. For example, if min = 3 and max = 5, // then this should return 12 (3 + 4 + 5). If // min is...
Write a class called Pen that contains the following information: Private instance variables for the price...
Write a class called Pen that contains the following information: Private instance variables for the price of the pen (float) and color of the pen (String). A two-argument constructor to set each of the instance variables above. If the price is negative, throw an IllegalArgumentException stating the argument that is not correct. Get and Set methods for each instance variable with the same error detection as the constructor. public class Pen {
In java Implement the class Book. It has the following instance variables: name, subject, year, maximumLoanPeriod,...
In java Implement the class Book. It has the following instance variables: name, subject, year, maximumLoanPeriod, and loanPeoriod. The following methods should be included: • Constructor(s), Accessors and Mutators as needed. • public double computeFine() => calculates the fine due on this item The fine is calculated as follows: • If the loanPeriod <= maximumLoanPeriod, there is no fine on the book. • If loanPeriod > maximumLoanPeriod o If the subject of the book is "CS" the fine is 10.00...
Complete the required methods: public class SongList { // instance variables private Song m_last; private int...
Complete the required methods: public class SongList { // instance variables private Song m_last; private int m_numElements; // constructor // Do not make any changes to this method! public SongList() { m_last = null; m_numElements = 0; } // check whether the list is empty // Do not make any changes to this method! boolean isEmpty() { if (m_last == null) return true; else return false; } // return the size of the list (# of Song nodes) // Do...
Create a class called Vehicle that includes four instance variables:      name, type,     tank size and...
Create a class called Vehicle that includes four instance variables:      name, type,     tank size and average petrol consumption. Provide 2 constructors, the first takes name and type as parameter, the second takes four parameters for the four instance variables. (2 pt) Provide also a method called distancePerTank that calculate the average distance that a vehicle can travel if the tank is full (multiplies the tank size by the average petrol consumption), then returns the value. (2 pt) Provide a...
The Hole Class Description: For the Hole class, there are three instance variables: the par for...
The Hole Class Description: For the Hole class, there are three instance variables: the par for the hole, the length of the hole in yards, and a Boolean valued indicator of whether the hole is one in which the players’ full drives will be measured for distance. John clarifies: The length of the hole in yards determines par for the hole. The current standard is: No more than 250 yards Par 3 251 to 470 yards Par 4 471 to...
c++ Add exception handling to the MyStack class (e.g. an instance of the class should throw...
c++ Add exception handling to the MyStack class (e.g. an instance of the class should throw an exception if an attempt is made to remove a value from an empty stack) and use the MyStack class to measure the execution cost of throwing an exception. Create an empty stack and, within a loop, repeatedly execute the following try block: try { i n t s t a c k . pop ( ) ; } catch ( e x c...
#Write a class called "Burrito". A Burrito should have the #following attributes (instance variables): # #...
#Write a class called "Burrito". A Burrito should have the #following attributes (instance variables): # # - meat # - to_go # - rice # - beans # - extra_meat (default: False) # - guacamole (default: False) # - cheese (default: False) # - pico (default: False) # - corn (default: False) # #The constructor should let any of these attributes be #changed when the object is instantiated. The attributes #with a default value should be optional. Both positional #and...
Programming Exercise Implement the following class design: class Tune { private:    string title; public:   ...
Programming Exercise Implement the following class design: class Tune { private:    string title; public:    Tune();    Tune( const string &n );      const string & get_title() const; }; class Music_collection { private: int number; // the number of tunes actually in the collection int max; // the number of tunes the collection will ever be able to hold Tune *collection; // a dynamic array of Tunes: "Music_collection has-many Tunes" public: // default value of max is a conservative...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT