Write Java code, saved as a file called Car.java, which conforms to the behavior
ID: 3890491 • Letter: W
Question
Write Java code, saved as a file called Car.java, which conforms to the behavior in the driver class below.
import java.util.Arrays;
public class Driver2 {
public static void main(String[] args){
//setting the drivers that can drive a car
String[] drivers = {"jdoe1", "samsmith", "georgemason", "redcar7", "jane"};
Car.setDrivers(drivers);
//creating a car with five valid colors and a model
String[] colors = {"red", "green", "blue", "yellow", "pink"};
Car sedan = new Car(colors,"Honda");
//any registered driver (from line 9) can drive thiscar in any valid color (line 13)
sedan.drive("green","jdoe1",88);
sedan.drive("green","samsmith",45);
sedan.drive("blue","jdoe1",89);
sedan.drive("green","georgemason",5);
sedan.drive("pink","redcar7",66);
sedan.drive("red","redcar7",23);
sedan.drive("pink","jane",7);
//invalid drivers or color options (when pre-specified) both return false
System.out.println(sedan.drive("pink","jane",65) == true);
System.out.println(sedan.drive("pink","BADUSER",65) == false);
System.out.println(sedan.drive("BADCOLOR","jane",65) == false);
//a car can be created without having to specify the valid colors; any color would be valid
Car minivan = new Car("Dodge");
//prints out the number of miles driven for each car
System.out.println(sedan.statistics());
System.out.println(minivan.statistics());
//need getters for these attributes
System.out.println(Arrays.toString(Car.getDrivers()));
System.out.println(Arrays.toString(sedan.getColors()));
System.out.println(Arrays.toString(sedan.getTrips()));
}
}
Here is the test case:
import static org.junit.Assert.*;
import org.junit.Test;
public class CarTest {
private static int passed = 0;
@Test
public void testStatic() {
String[] drivers = {"John", "Jane"};
Car.setDrivers(drivers);
assertEquals("John",Car.getDrivers()[0]);
assertEquals("Jane",Car.getDrivers()[1]);
assertNotEquals(0,++passed);
}
@Test
public void testCtor1() {
Car sedan = new Car("Honda");
assertEquals("0",sedan.statistics());
assertEquals(null,sedan.getTrips()[0]);
assertEquals(1000,sedan.getMAX_TRIPS());
assertEquals(1000,sedan.getTrips().length);
assertNotEquals(0,++passed);
}
@Test
public void testCtor2() {
String[] colors = {"red", "green", "blue"};
Car sedan = new Car(colors,"Honda");
assertEquals("0",sedan.statistics());
assertEquals("red",sedan.getColors()[0]);
assertEquals("green",sedan.getColors()[1]);
assertEquals("blue",sedan.getColors()[2]);
assertEquals(null,sedan.getTrips()[0]);
assertNotEquals(0,++passed);
}
@Test
public void testTrip1() {
String[] drivers = {"jdoe1", "samsmith", "georgemason", "redcar7", "jane"};
Car.setDrivers(drivers);
String[] colors = {"red", "green", "blue", "yellow", "pink"};
Car sedan = new Car(colors,"Honda");
sedan.drive("green","jdoe1",77);
sedan.drive("green","samsmith",21);
sedan.drive("blue","jdoe1",45);
sedan.drive("green","georgemason",76);
sedan.drive("pink","redcar7",55);
sedan.drive("red","redcar7",22);
assertEquals(true,sedan.drive("pink","jane",8));
assertEquals("304",sedan.statistics());
assertEquals("jdoe1",sedan.getTrips()[0]);
assertEquals("samsmith",sedan.getTrips()[1]);
assertEquals("jdoe1",sedan.getTrips()[2]);
assertEquals("georgemason",sedan.getTrips()[3]);
assertEquals("redcar7",sedan.getTrips()[4]);
assertEquals("redcar7",sedan.getTrips()[5]);
assertEquals("jane",sedan.getTrips()[6]);
assertNotEquals(0,++passed);
}
@Test
public void testTrip2() {
String[] drivers = {"jdoe1", "samsmith", "georgemason", "redcar7", "jane"};
Car.setDrivers(drivers);
String[] colors = {"red", "green", "blue", "yellow", "pink"};
Car sedan = new Car(colors,"Honda");
assertEquals(false,sedan.drive("red","bad",77));
assertEquals(true,sedan.drive("pink","jane",8));
assertEquals("8",sedan.statistics());
assertEquals("jane",sedan.getTrips()[0]);
assertNotEquals(0,++passed);
}
@Test
public void testTrip3() {
String[] drivers = {"jdoe1", "samsmith", "georgemason", "redcar7", "jane"};
Car.setDrivers(drivers);
String[] colors = {"red", "green", "blue", "yellow", "pink"};
Car sedan = new Car(colors,"Honda");
assertEquals(false,sedan.drive("NOPE","samsmith",77));
assertEquals(true,sedan.drive("pink","jane",8));
assertEquals("8",sedan.statistics());
assertEquals("jane",sedan.getTrips()[0]);
assertNotEquals(0,++passed);
}
@Test
public void testTrip4() {
String[] drivers = {"jdoe1", "samsmith", "georgemason", "redcar7", "jane"};
Car.setDrivers(drivers);
String[] colors = {"red", "green", "blue", "yellow", "pink"};
Car sedan = new Car("Honda");
for (int i = 0; i < 1010; i++)
sedan.drive("pink","jane",8);
assertEquals("8000",sedan.statistics());
assertEquals("jane",sedan.getTrips()[0]);
assertNotEquals(0,++passed);
}
}
Explanation / Answer
**Here is the code for the car.java class that satisfies all the method calls and its arguments. This solves all the issues or errors raised. Just implement the method based on your requirement. As the functionality of the method is not explained I am leaving so methods blank as you can implement them.
**CODE:
public class Car {
private static String[] myDrivers;
private String[] colors;
private String name;
public Car(String name) {
// TODO Auto-generated constructor stub
this.name = name;
}
public Car(String[] colors, String name) {
// TODO Auto-generated constructor stub
this.colors = colors;
this.name = name;
}
public static void setDrivers(String[] drivers) {
myDrivers = drivers;
}
public static Object[] getDrivers() {
// TODO Auto-generated method stub
return myDrivers;
}
public Object statistics() {
// TODO Auto-generated method stub
return null;
}
public Object[] getTrips() {
// TODO Auto-generated method stub
return null;
}
public Object getMAX_TRIPS() {
// TODO Auto-generated method stub
return null;
}
public Object[] getColors() {
// TODO Auto-generated method stub
return colors;
}
public void drive(String string, String string2, int i) {
// TODO Auto-generated method stub
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.