Table of Contents

This is not a current assignment. If you are currently enrolled in SE1010, do not do this assignment expecting to receive credit for it.

Lab 7: Simple Class Design

Project Context

You are being asked to implement a class Car that simulates an automobile. When a new car is created, the fuel efficiency (measured in miles/gallon) for the car is specified and it is assumed that the fuel tank is empty. The first thing that a user must do after the creation of the car is to fill it up. The car is then ready for use. As the car is driven around, the amount of fuel in the tank is reduced based on the fuel efficiency of the car and the speed at which the user drives the car. The consumption of gas during a trip is governed by a formula described in the section “Driving Speed versus Fuel Efficiency.” Your implementation of the Car class should provide the user the ability to determine the current amount of gasoline in the fuel tank and to fill it up. It is important to note that the user is left stranded if he/she attempts to drive the car for a distance that consumes more gas than is available in the gas tank. The user would then have to fill up their tank before continuing on their journey. You can assume that the user has the ability to add gas exactly where he/she was stranded. You will have to make sure that your implementation works for the following scenarios.

Scenario I

The user gets a new car with a gas tank capacity of 12 gallons and a fuel efficiency of 32 miles per gallon. After travelling a distance of 13 miles at the average speed of 30 miles/hr and another 51 miles at 40 miles/hr, the user still has 10 gallons of gas left in the car.

Scenario II

The user then leaves for a weekend trip to a destination which is 380 miles away without filling up the tank. On the way to his destination, he is stranded after travelling 255.52 miles, because his car is out of gas. After filling up the tank, he proceeds and has 7.128 gallons of gas left in his car after arriving at his destination. He travels the entire distance at an average speed of 65 mph.

Scenario III

On the way back from his trip, because of construction detours and traffic, his average speed is only 55 miles/hr and he has to travel 400 miles instead of 380. He fills up the tank before starting on his return journey but is still stranded after travelling 356.25 miles. After filling up, he continues and has 10.52 gallons remaining when he gets back home.

Driving Speed versus Fuel Efficiency

Environmental Protection Agency (EPA) says that the fuel efficiency of a car is approximately 15 percent less for every 10 miles/hr above 50 miles/hr. So the formula that can be used is:

Efficiencyactual = Efficiencypublished * e(-0.15*s)

where:

In other words, s = 0 when the speed is 50 miles/hr or less, s = 1 when the speed is 60 miles/hr, and s = 1.5 when the speed is 65 miles/hr.

Assignment Deliverables

  1. Draw (on paper) the UML class diagram for the Car class which shows the attributes and methods, along with their visibility attributes, for the classes that you may choose to implement for this assignment. Please make sure that you have an initial version of the UML diagram reviewed by your instructor before you start any implementation. You should submit a final version of the UML class diagram at the time of the submission of the assignment.
  2. Implement the Car class based on the UML class diagram created in Step 1.
  3. You should also implement a class named CarTester which has the main method in it. You should create several instances of the class car and test all the public methods for the classes that you have implemented. At a minimum, you should test for the scenarios that have been provided to you. Please note that each of the classes that you implement should be in a separate .java file.
  4. Submit your Car.java and CarTester.java files along with the UML class diagram for the Car class to your instructor.

Steps for Creating UML Class Diagrams

You should follow these steps when creating your UML class diagram.

  1. Identify the classes from the problem statement. Classes tend to encapsulate entities that exist in the real world. (This has been done for you in the problem statement.)
  2. Identify the attributes (nouns) and behaviors (verbs) for each of the classes identified earlier.
  3. Identify the visibility for your attributes and methods.
  4. Identify parameters (if needed) for your methods.
  5. Use information from Steps 1-4 to create UML class diagrams.