Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7801555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T00:59:08+00:00 2026-06-02T00:59:08+00:00

I am working on a homework assignment for my into to programming class that

  • 0

I am working on a homework assignment for my into to programming class that involves implementing interfaces. The problem here is that I really just flat out don’t understand interfaces or what they are used for (the professor was not very good about explaining it).

The assignment was to make a “Vehicle” superclass, and than three subclasses, something like “Truck” or “Jeep” that would each have a couple traits of their own. The “Vehicle” class must implement the comparable interface, which I think I have figured out (I have the compareTo() method written comparing the number of doors on vehicles), and one other class must also implement the “Hybrid” class (I have no idea what this means). We then have to implement the toString(), equals(Object o), and compareTo(Object o) methods.

I think I have the compareTo() down, but with the equals() I have no idea. The last thing we have to do is write a Main to test, this involves making an array of Vehicle objects, printing them out, sorting them, and then re printing them. It should also iterate through the array and print out the price premium for hybrids, and use the equals(Object o) method to compare 2 Vehicles.

Here is the code for my “Vehicle” superclass

package vehicle;

abstract public class Vehicle implements Comparable {
    private String color;
    private int numberOfDoors;

    // Constructor
    /**
     * Creates a vehicle with a color and number of doors
     * @param aColor The color of the vehicle
     * @param aNumberOfDoors The number of doors
     */
    public Vehicle(String aColor, int aNumberOfDoors) {
        this.color = aColor;
        this.numberOfDoors = aNumberOfDoors;
    }

    // Getters
    /**
     * Gets the color of the vehicle
     * @return The color of the vehicle
     */
    public String getColor() {return(this.color);}
    /**
     * Gets the number of doors the vehicle has
     * @return The number of doors the vehicle has
     */
    public int getNumberOfDoors() {return(this.numberOfDoors);}

    // Setters
    /**
     * Sets the color of the vehicle
     * @param colorSet The color of the vehicle
     */
    public void setColor(String colorSet) {this.color = colorSet;}
    /**
     * Sets the number of doors for the vehicle
     * @param numberOfDoorsSet The number of doors to be set to the vehicle
     */
    public void setNumberOfDoors(int numberOfDoorsSet) {this.numberOfDoors = numberOfDoorsSet;}

    @Override
    public int compareTo(Object o) {
        if (o instanceof Vehicle) {
            Vehicle v = (Vehicle)o;
            return this.numberOfDoors - v.getNumberOfDoors();
        }
        else {
            return 0;
        }
    }

    /**
     * Returns a short string describing the vehicle
     * @return a description of the vehicle
     */
    @Override
    public String toString() {
        String answer = "The car's color is "+this.color
                +". The number of doors is"+this.numberOfDoors;
        return answer;
    }
}

And I will also post one of my subclasses

package vehicle;

abstract public class Convertible extends Vehicle {
    private int topSpeed;

    // Constructor
    /**
     * Creates a convertible with a color, number of doors, and top speed
     * @param aColor The color of the convertible
     * @param aNumberOfDoors The number of doors of the convertible
     * @param aTopSpeed The top speed of the convertible
     */
    public Convertible (String aColor, int aNumberOfDoors, int aTopSpeed) {
        super(aColor, aNumberOfDoors);
        this.topSpeed = aTopSpeed;
    }

    // Getters
    /**
     * Gets the top speed of the convertible
     * @return The top speed of the convertible
     */
    public int getTopSpeed() {return(this.topSpeed);}

    // Setters
    /**
     * Sets the top speed of the convertible
     * @param topSpeedSet The top speed to set to the convertible
     */
    public void setTopSpeed(int topSpeedSet) {this.topSpeed = topSpeedSet;}

    /**
     * Returns a short description of the convertible
     * @return a short description of the convertible
     */
    @Override
    public String toString() {
        String answer = "The car's color is "+super.getColor()
                +", the number of doors is "+super.getNumberOfDoors()
                +", and the top speed is "+this.topSpeed+" mph.";
        return answer;
    }
}

I am definitely not asking anyone to do the work for me, but if someone could shed a bit more light on how interfaces work and what this homework is really asking that would be great.

All help is very much appreciated

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T00:59:10+00:00Added an answer on June 2, 2026 at 12:59 am

    Rather than doing your particular example I will run through why interfaces are useful and how to use them in a more general case.

    What is an Interface?

    When I first started programming I also found the concept of an interface to be confusing so I like to think of it as a standard “rule book”. Every class which implements this rule book has a list of “rules” it must follow. For example, consider the following interface:

    interface Bounceable{
      public void setBounce(int bounce);
      public int getBounce();
    }
    

    This above rule book declares an interface for something that bounces. It states that anything that is bounceable must set its bounce and also get its bounce. Any class which implements this interface must follow the rule book.

    Why would this rule book be useful?

    Well, what if you want to code up a playground, where kids play with all sorts of bouncy things. You might make the following types of bouncy things..

    public class FootBall implements Bounceable{
    private int bounce;
    
    public void setBounce(int bounce){
       this.bounce = bounce;
    }
    
    public int getBounce(){
      return this.bounce;
    }
    
    }
    
    public class BaseBall implements Bounceable{
    private int bounce;
    
    public void setBounce(int bounce){
       this.bounce = bounce;
    }
    
    public int getBounce(){
      return this.bounce;
    }
    
    }
    

    The above classes define a type of bouncy ball. You would then make your playground class and could define methods around the abstract Bounceable interface. For example, what if a basketball hoop was a method in your class? what if it could accept any bouncy thing as an argument? This would mean that you could pass any kind of ball as long as it implements bounceable. If you didn’t have interfaces or similar functionality you could see how messy your code would get the more balls you implement.

    EDIT: I’ve included a small practical example..

    A practical example of this would be..

    public void slamDunk(Bounceable bouncyThing){
      System.out.println("You scored three points!");
    }
    

    Both of the following calls to slamDunk are valid…

    slamDunk(new BaseBall());
    
    slamDunk(new FootBall());
    

    Now your slameDunk function can score points with any bounceable object.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on a homework assignment for a class. The problem statement says
I am working on a homework assignment on implementing interfaces, and am a little
I'm working on a homework assignment (a project), for which one criterion is that
For a homework assignment, I'm working with the following. It's an assigned class structure,
I'm working on a homework assignment and I ran into a little snag. I'm
I'm working on a homework assignment that asks me to create a calculator that
Some classmates and I are working on a homework assignment for Java that requires
I'm working on a homework assignment that basically asks us to parse a DNS
I'm working on a homework assignment that requires me to write a program that
I'm working on a homework assignment for my object oriented design class, and I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.