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.
Vehiclesdifferently. You should pass an array ofOptionnot an array ofString. So:public Vehicle(String manufacturerName, int miles, int price, int seats, Option options[])optionsand single stringoption. Also use camel case for class fields as well. It's very confusing to read.