Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write statements to create a class called Transport with: a public \"Mutator\" f

ID: 3830299 • Letter: W

Question

Write statements to create a class called Transport with: a public "Mutator" function (like: set ()) to change the private variable a public "Accessor' function (like: get ()) to access private variable The class has a private member variable wheels'. It has no constructor function. Define the above functions. Create two objects (car, bicycle) of this class. Set the wheels' to 4 and 2, respectively, for these objects using the "Mutator function. Then print the values of wheels' for both objects using the Accessor function.

Explanation / Answer

Transport.java

//Transport class
public class Transport {
   // private member variable wheels to represent number of wheels
   private int wheels;

   // Accessor function to get the value of wheels
   public int getWheels() {
       return wheels;
   }

   // Mutator function to set the value of wheels
   public void setWheels(int wheels) {
       this.wheels = wheels;
   }

}

TransportTest.java-To test Transport functions

//Test class with main method to test the Transport functionality
public class TransportTest {

   public static void main(String[] args) {
       // Create Transport object with name as car
       Transport car = new Transport();
       // set wheels as 4 using mutator function
       car.setWheels(4);

       // Create Transport object with name as car
       Transport bicycle = new Transport();
       // set Wheels as 2 using mutator function
       bicycle.setWheels(2);

       // Print wheels of car using Accessor Method
       System.out.println("Number of wheels for car: " + car.getWheels());

       // print wheels of bicycle using Accessor Method
       System.out.println("Number of wheels for bicycle: " + bicycle.getWheels());
   }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote