Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please create the JUnit Test Class: CarTester Introduction: In this lab, we will practice writing JUnit tests for a custom class. Specifically, we will utilize

Please create the JUnit Test Class: CarTester

Introduction: In this lab, we will practice writing JUnit tests for a custom class. Specifically, we will utilize the Car.java class and create JUnit tests to effectively test it.

Lab Instructions: 1. Create a project in Eclipse named Lab5.

2. Add the Car.java class to your project workspace in the src folder. You can copy and paste or drag and drop it into the folder.

3. Right click on the src folder and select New JUnit Test Case to create a new Java JUnit test class. Make sure you have the New JUnit 4 test radio button selected at the top of the window that appears. Next, in the name text box, follow the naming convention discussed in the lecture and name this class CarTester. That is, the name of the class we are testing, followed by the word Tester. Finally, at the bottom of the window, there is a text box labeled Class under test:. Here, type the name of the class, Car, that we are testing. While this is not necessary for the tests to execute, it is good practice to include this. Now, click Next to see a list of methods provided in your Car.java class. You can select which methods you want to create a test for by selecting the checkboxes in the window (see which methods you need to include below). This provides a set of test method stubs for you to fill in. Or, you can skip this, click finish, and make the methods yourself. Once you click Finish, it will ask you to add a JUnit library to your project. Click OK to add it to the project.

4. Your CarTester class should now be opened in your Eclipse IDE. If you selected checkboxes for methods to test, you will have a list of method stubs. If not, you will need to make the method stubs yourself. Below is a list of constructors and methods that are required for you to test.

@Test

a. public Car() b. public Car(String owner, String make, String model, int yearModel) c. public Car(String owner, String make, String model, int yearModel, double fuelLevel) d. public Car(String owner, String make, String model, int yearModel, double fuelLevel, int speed, boolean start) e. public Car(Car anotherCar) f. public boolean accelerate() g. public boolean brake() h. public boolean isGasTankEmpty() i. public boolean sameOwner(Car anotherCar) j. public boolean equals(Object o) k. public String toString() l. public void setYearModel(int yearModel) m. public void setFuelLevel(double fuelLevel) n. public void setSpeed(int speed)

5. When writing your test cases, make sure you not only test the acceptable cases, but also the unacceptable cases as well. For example, when you test the accelerate() method, you need to think about the normal scenario as well as the edge cases (not enough fuel, speed is already at the maximum, engine is off, etc.).

6. When testing your setters that have validation checks, be sure to include the correct expected exception (either IllegalArgumentException or InvalidParameterException).

Rubric:

Task Grade
Test Constructors (3 points each) 15
Test setters with validation checking (5 points each) 15
Test accelerate() method 10
Test brake() method 10
Test isGasTankEmpty() method 10
Test sameOwner() method 10
Test equals(Object o) method 10
Test toString() method 10
Adequate JavaDoc included in the CarTester.java class 10
Total 100

Class Car:

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

1 import java.security.InvalidParameterException; 2 3 public class Car { 4 === Properties 5 private String owner, make, model; 6 private int yearModel, speed; 7 private double fuelLevel; 8 private boolean start; 9 10 Constructors 11e /** 12 * The default (empty) constructor. This constructor initializes the owner, make, and model 13 as "", the yearModel as 2021, the speed as 0, the fuellevel as 1.0d, and start as false. 14 15 public Car() { 16 this("", , 2021, 1.0d, e, false); 17 } 18 190 20 * A partial constructor that takes the owner, make, model and yearModel instance properties 21 * to initialize an object. 22 23 @param owner A String owner value which represents the name of the owner of the car. 24 @param make A String make value which represents the make of the car. 25 @param model A String model value which represents the model of the car. 26 @param yearModel An int yearModel value which represents the year the car was manufactured. The 27 yearModel cannot be less than 1885 or greater than 2022. 28 */ 290 public Car(String owner, String make, String model, int yearModel) { 30 this(owner, make, model, yearModel, 1.0d, e, false); 31 } 32 330 34 A partial constructor that takes the owner, make, model, year and fuellevel instance 35 properties to initialize an object. 36 37 @param owner A String owner value which represents the name of the owner of the car. 38 @param make A String make value which represents the make of the car. 39 @param model A String model value which represents the model of the car. 40 @param yearModel An int yearModel value which represents the year the car was manufactured. The 41 yearModel cannot be less than 1885 or greater than 2022. 42 @param fuelLevel A double fuelLevel value which represents the fuel of the car. The fuellevel cannot 43 * be less than 0.0 or greater than 1.0. 44 */ 45e public Car(String owner, String make, String model, int yearModel, double fuellevel) { 46 this (owner, make, model, yearModel, fuellevel, @, false); 47 } 48 490 50 * A workhorse constructor that receives values for all instance properties 51 * and initializes the object. 52 53 @param owner A String owner value which represents the name of the owner of the car. 54 @param make A String make value which represents the make of the car. 55 @param model A String model value which represents the model of the car. 56 @param yearModel An int yearModel value which represents the year the car was manufactured. The yearModel cannot be less than 1885 or greater than 2022. 58 @param fuellevel A double fuellevel value which represents the fuel of the car. The fuellevel cannot 59 * be less than 0.0 or greater than 1.0. 3 60 61 62 63 640 65 66 67 68 69 70 71 @param speed An inte speed value which represents the speed of the car. The speed cannot be greater than 250 mph or less than murah- @param start A boolean value which is true if the engine is on and false, otherwise. */ public Car (String owner, String make, String model, int yearModel, double fuellevel, int speed, boolean start) { this.owner = owner; this.make = make; this.model = model; setYearModel(yearModel); setFuelLevel(fuellevel); setSpeed(speed); setStart(start); } /** * The copy constructor that copies the instance properties of another car object. @param anotherCar An existing car to create a copy of. */ public Car(Car anotherCar) { this(anotherCar.owner, anotherCar.make, anotherCar.model, anotherCar.yearModel, anotherCar.fuellevel, anotherCar.speed, anotherCar.start); } 73 740 75 76 77 78 79 80 81 82 83 84 85 86e 87 88 89 90 91 92 93 94 ==EEEEE ======= Methods * Increases the speed by 4 miles/hour. It can increase the speed by * that amount only if the engine is on, there is enough fuel, and it is running at * less than 250 miles/hour. No matter what, it burns 0.05 amount of fuel if the engine is on. @return True if it can increase the speed by some amount or false, otherwise. public boolean accelerate() { if(!getStart()) { return false; } if(isGasTankEmpty() { return false; } setFuellevel(fuellevel - 0.05); if(speed 250) { return false; } 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 1176 118 if(speed + 4 > 250) { setSpeed (250); } else { setSpeed (speed + 4); } return true; } * Reduces the need hv 3 miles/hour T+ cannot reduce the need * Reduces the speed by 3 miles/hour. It cannot reduce the speed * if the speed is zero or the engine is turned off. @return True if it can reduce the speed by some amount or false, otherwise. */ public boolean brake() { if(!getStart()) { return false; } if(getSpeed() == ) { return false; } if(speed > 3) { setSpeed(speed - 3); } else { setSpeed(); } return true; } *** * Checks whether the tank is empty or not. * @return True if the tank is empty or false, otherwise. 118 119 120 121 122 123e 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 1410 142 143 144 145 146@ 147 148 149 150 151 152 153 1540 155 156 157 158 159 1600 161 162 163 164 165 166 167 1680 169 170 171 172 173 174 1750 A176 public boolean isGasTankEmpty() { if(fuelLevel 2022) { throw new InvalidParameterException ("The year model is not valid."); } else { this.yearModel = yearModel; } * Gets the fuellevel of the car. @return the double fuellevel of the car. public double getFuelLevel() { return fuellevel; } Sets the fuellevel of the car. @param fuellevel The double fuellevel of the car to set. @exception InvalidParameterException if the value is not within the valid range of greater than or less than 1.0. public void setFuelLevel(double fuellevel) { if(fuelLevel 1.0) { throw new InvalidParameterException("The fuel level is not valid."); } else { this.fuelLevel = } fuelLevel; /** Gets the speed of the car. @return the inte speed of the car. public int getSpeed() { return speed; } /** * Sets the speed of the car. @param speed The inte speed of the car to set. @exception InvalidParameterException if the value is not within the valid range of * greater than 6 or less than 250. 295 296 297 298 299 300e 301 302 303 304 3050 306 307 308 3090 310 311 312 313 314 315 3160 317 318 319 320 321 322 323 324e 325 326 327 328 3290 330 331 332 3330 334 335 336 337 3380 339 340 341 342 } 343 public void setSpeed(int speed) { if(speed 250) { throw new InvalidParameterException("The speed cannot be negative or more than 250 mph."); } else { this.speed = speed; } } *** Gets whether the engine is on (true) or off (false). @return the boolean engine status of the car. public boolean getStart() { return start; } Sets the engine status of the car (true = on, false = off). @param start the boolean start status of the car to set. public void setStart(boolean start) { this.start = start; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Databases 28th British National Conference On Databases Bncod 28 Manchester Uk July 2011 Revised Selected Papers Lncs 7051

Authors: Alvaro A.A. Fernandes ,Alasdair J.G. Gray ,Khalid Belhajjame

2011th Edition

3642245765, 978-3642245763

More Books

Students also viewed these Databases questions

Question

Describe the linkages between HRM and strategy formulation. page 74

Answered: 1 week ago

Question

Identify approaches to improving retention rates.

Answered: 1 week ago