0

In my intermediate Java programming class, we are working through JUnit Tests (I believe this to be the right terminology, forgive me because I'm still very, very green with all the proper terminology) that have us building a Vehicle class that allows us to create an object of type Vehicle with multiple values while also having an Options class extending the Vehicle class.

My first test is as follows:

   void testVehicle()
   {
      Vehicle vehicle = new Vehicle("GMC", 80000, 7995, 4, null);

      assertEquals("GMC", vehicle.getManufacturerName());
      assertEquals(80000, vehicle.getMilesOnVehicle());
      assertEquals(7995, vehicle.getPrice());
      assertEquals(4, vehicle.getNumberOfSeats());
   }

I have my classes set up as follows:

public class Vehicle
{
    private String ManufacturerName;
    private int MilesOnVehicle;
    private int Price;
    private int NumberOfSeats;
    protected String Options;
    protected String[] Option;
    
    // No arg constructor
    public Vehicle()
    {
        
    }
 
    public Vehicle(String manufacturerName, int miles, int price, int seats, String options[]) {
        ManufacturerName = manufacturerName;
        MilesOnVehicle = miles;
        Price = price;
        NumberOfSeats = seats;
        String Option[] = options;
    }
 
// Getters and setter methods for ManufacturerName, MilesOnVehicle, Price, and NumberOfSeats

}

Where as my Options class is as follows;

public class Option extends Vehicle
{

    // No arg constructor
    public Option()
    {
    // No arg constructor
    }
    
    public Option(String string)
    {
        Options = string;
    }
    
    /*
     * Method to set option's indexes with String variables
     * set to String variable Option extended from Vehicle class
     */
    public void setDetails(String option)
    {
        Options = option;
    }
    
    /*
     * Method to get option as String
     * after being set by previous method setOption
     */
    public String getDetails() {
        return Options;
    }
    
    public void setOptions(String[] option)
    {
        Option = option;
    }
    
    public Object[] getOptions()
    {
        return null;
    }
    
}

I'm able to pass the first tests, but the second test is where I'm having issues because I see that we're bringing in two String variables for Options, but I'm not sure how to go about implementing a method or a proper constructor that will take them in, let alone access them again.

   @Test
   void testOption()
   {
      Option moonroof = new Option("Moonroof");
       assertEquals("Moonroof", moonroof.getDetails());
      Option leather = new Option("Leather");
      assertEquals("Leather", leather.getDetails());
      Option[] options = { moonroof, leather };
      Vehicle vehicle = new Vehicle("BMW", 90000, 10995, 4, options);
      assertEquals("Moonroof", vehicle.getOptions()[0].getDetails());
      assertEquals("Leather", vehicle.getOptions()[1].getDetails());
   }

I don't want someone to do my homework for me, but rather some guidance and pointers as to what I should be doing to fix my errors and so that I understand how to pass String variables into the array and how to then access it through those two methods I have to create (getOptions and getDetails).

EDIT: I've now actually passed all the tests from these challenges and saw what my error was. After following the UML diagram that was provided for us more closely, I realized that I was implementing more methods than necessary and creating redundant code. As user "markspace" stated about not extending Vehicle into option, that was one of the errors I was making.

2
  • Given the code you have now, I think you should declare the ctor of Vehicles differently. You should pass an array of Option not an array of String. So: public Vehicle(String manufacturerName, int miles, int price, int seats, Option options[]) Commented Mar 14, 2021 at 5:16
  • Just a suggestion, call the array options and single string option. Also use camel case for class fields as well. It's very confusing to read. Commented Mar 14, 2021 at 5:20

1 Answer 1

1

A few pointers which might help, but omitting some details for you to research.

Option class should not extend Vehicle - it's not a Vehicle itself. As well as your current definition you might consider Option as an enum

enum Option{LEATHER, MOONROOF, ...};

or as abstract class Option so that you could add derived classes per Option type and have fields for their colours, sizes etc

// class LeatherOption extends Option
Option leather = new LeatherOption("red);

Instead of passing in array for the options, consider "..." then you can use variable arguments and omit the null parameter for a basic models:

public Vehicle(String manufacturerName, int miles, int price, int seats, Option ... options) // or String ... options
{ this.options = options; }
=> 
Vehicle vehicle = new Vehicle("GMC", 80000, 7995, 4); // no options specified
Vehicle vehicle = new Vehicle("BMW", 90000, 10995, 4, moonroof, leather);

If you later add more fields to Vehicle you may end up with complex constructor arg list, so it may be better to reduce the constructor to the minimum parameters and have setXYZ for extra features:

vehicle.setOptions(Option ... options)
vehicle.hasOption(Option option) => boolean
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.