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

Hello, I\'ve started an assignment working with interfaces, and its the introduc

ID: 3803604 • Letter: H

Question

Hello, I've started an assignment working with interfaces, and its the introduction to interfaces. Needless to say, I've gotten stuck. I'm not quite sure how I'm supposed to use them. I've watched how-to videos, I've spoken with my professor, and I've referenced the book/slideshows/notes/etc, and nothing seems to help me understand. Would someone mind helping me out and explaining the logic/details on why they wrote the code how they did and/or how it works? Either as short //comments within the code, or just explaining it in regular language in the answer works as well. It would be appreciated! I'll provide the directions I was given, and all the code I have up to this point. I tried putting the common methods into the interface, but I don't really understand how to make those methods work back within my classes. Thank you in advance.

Directions:

1. The program works as before.

2. The Driver class is simpler: it only has to know about an interface called Drivable which will have all the methods that Chevy and Ford have in common that Driver will use. Driver does not need to know which specific classes it will use, only the interface.

3. The Chevy and Ford classes should implement Drivable and the method(s) that Drivable declares.

4. Add a third class, Dodge which gets 40 miles per gallon and implements Drivable. Have the Main class create a new Driver object that uses the Dodge object to drive 200 miles and prints a trip report.

(Side note: Solution should have a Driver class that does not refer to a Ford,Chevy, or Dodge object anywhere in the code inthe Driver.java file.)

Code:

//Main.java

package drivers;

public class Main {

   public static void main(String[] args) {
       Driver[] drivers = new Driver[5];

       drivers[0] = new Driver(new Chevy());
       drivers[0].drive(100);
       System.out.println(drivers[0]);

       drivers[1] = new Driver(new Ford());
       drivers[1].drive(300);
       System.out.println(drivers[1]);

      
       drivers[2] = new Driver( new Dodge() );
       drivers[2].drive( 200 );
       System.out.println( drivers[2] );   

   }
}

//Driver.java

package drivers;

public class Driver implements Drivable {
   private Ford ford;
   private Chevy chevy;

   private float gallons = 0;

   public Driver(Ford f) {
       ford = f;
   }

   public Driver(Chevy c) {
       chevy = c;
   }

   public void drive(float m) {
       if (ford != null)
           gallons = ford.drive(m);
       if (chevy != null)
           gallons = chevy.driveSomeMiles(m);
   }

   @Override
   public String toString() {
       if (ford != null)
           return "Drove a " + ford + " using " + gallons + " gallons.";
       if (chevy != null)
           return "Drove a " + chevy + " using " + gallons + " gallons.";
       return null;
   }

   @Override
   public float driveSomeDistance(float miles) {
       // TODO Auto-generated method stub
       return 0;
   }

   @Override
   public float getMiles() {
       // TODO Auto-generated method stub
       return 0;
   }
}

//Dodge.java

package drivers;

public class Dodge implements Drivable {
   private static final float milesPerGallon = 40.0f;
   private float odometerMiles;

   public Dodge() {
       odometerMiles = 0;
   }

   @Override
   public float driveSomeDistance(float miles) {
       odometerMiles = odometerMiles + miles;
       return miles / milesPerGallon;
   }

   @Override
   public float getMiles() {
       return odometerMiles;
   }

   @Override
   public String toString() {
       return "Dodge " + getMiles() + " miles";
   }
}

//Chevy.java

package drivers;

public class Chevy implements Drivable {
   private static final float milesPerGallon = 20.0f;
   private float odometerMiles;

   public Chevy() {
       odometerMiles = 0;
   }

   public float driveSomeMiles(float miles) {
       odometerMiles = odometerMiles + miles;
       return miles / milesPerGallon;
   }

   @Override
   public float getMiles() {
       return odometerMiles;
   }

   @Override
   public String toString() {
       return "Chevy " + getMiles() + " miles";
   }

   @Override
   public float driveSomeDistance(float miles) {
       odometerMiles = odometerMiles + miles;
       return miles / milesPerGallon;
   }
}

//Ford.java

package drivers;

public class Ford implements Drivable {
   private static final float milesPerGallon = 30.0f;
   private float odometerMiles;

   public Ford() {
       odometerMiles = 0;
   }

   public float drive(float miles) {
       odometerMiles = odometerMiles + miles;
       return miles / milesPerGallon;
   }

   @Override
   public float getMiles() {
       return odometerMiles;
   }

   @Override
   public String toString() {
       return "Ford " + getMiles() + " miles";
   }

   @Override
   public float driveSomeDistance(float miles) {
       // TODO Auto-generated method stub
       return 0;
   }
}

//Next file is Drivable.java, which is the interface file

//Drivable.java

package drivers;

public interface Drivable {

   float driveSomeDistance(float miles);

   float getMiles();

}

Explanation / Answer

package drivers;

Drove a Chevy 100.0 miles using 5.0
Drove a Ford 300.0 miles using 10.0
Drove a Dodge 200.0 miles using 5.0

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote