MON-26-NOV-18: Needed a better object modeling exercise than Donuts and Animals. All I could get was the screen clips of the 8 cars from Back To The Future 1.
WED-28-NOV-18: Fleshing out exercises based on BTF premise.
Modeling the Cars of Back To The Future
Expand our concepts of object-oriented Java by modeling vehicles as seen in class 1985 film: Back To The Future I.
The module provides a skeleton of our Car class and the client CarLand class where we actually instantiate the Car objects, set their member variables, and test our work.
You can view the entire code set for this module on our course GitHub repository
Create object models to represent each of the cars in Back To The Future I:
You can reference this btf wiki that lists the make and models.
Cars 6 and 7
wb_incandescentSkeleton Car and CarLand
Step 1 - Create Classes: Create a Car class and a CarLand class in a package of your choosing.
Step 2 - Screen Setup: Drag the tabs in NetBeans to either side of your screen so that you can see both the Car class, which is our object blueprint, and CarLand, which is our client class, at the same time.
Step 3 - Visualize the class structures: Our core components of our Car class is its member variables and methods. This is the NetBeans navigator view of our completed Car class:
Note the symbols that show private and public member variables. The method listings show the input and output types. Inputs and their types are in the (parentheses) and output types come after the :
And CarLand's Structure only has two methods, our main, and a utility method that takes in any Car object and sends the values of its member variables to the console.
Step 4 - Code: Use the following screen clips to code the Car and CarLand skeletons. Note that the code is deliberately chopped up into different screen clips so you can practice not just copying from the screen.
Methods that start and stop the car
Methods that allow us to read the values of private member variables
Methods that speed up and slow down the car
As the client class to Car, the job of CarLand is to instantiate (or, we also say, construct) instances of the Car class and operate its gizmos and gadgets.
Create an instance of a Car object with new
Call methods on Car and test with calls to displayCarStats
Utility method for displaying the value of member variables on a given Car
Note that this method takes in a Car object as its input and then systematically sends the values of each of the Car's member variables to System.out.println(). This helps us avoid repitition of code.
Test your code!
You should now be able to compile and run both classes, with the following output as a guide to check your work:
You can also view the entire code set on our course GitHub repository
wb_incandescentMini-project 0: More cars!
The sample code modeled the DeLorean. Pick another 1 or 2 cars from the page called "The 8 Cars of Back To The Future" linked on the left navbar. Instantiate your objects with the new keyword, set its member variables and call its methods:
Modeling the DeLorean:
wb_incandescentMini-project 1: No negative speed
Notice that we have a modeling problem when we try to decelerate more miles per hour of speed than the car is currently traveling at that instant. Here's the sample output and code that generated it.
Jump back into the Car class's methods that control card speed. Create logic using our java control statement if that will cope with a request from the client to slow down more mph than the car is traveling.
Decide what happens:
You as the designer need to decide what happens when the user tries to slow down too much: does the car go to zero speed? Does the car not change speed at all and issue an error message instead?
Code the logic and test the results
Once you have coded the controls, retest your code and generate output that confirms that your car cannot have a negative speed.
wb_incandescentMini-project 2: Build the transmission
Cars these days have automatic transmissions. The most modern transmissions use computer sensors to determine when to shift. The DeLorean was obviously a manual transmission car.
Refactor your Car class to contain a member variable of type int called gear. This variable now must be managed by your accelerate and decelerate methods such that the car is in the optimal gear for the given speed.
Study this sample output:
Note that we're seeing console outputs that show the car shifting up at certain speed breakpoints. We use our same utility method for printing the car info, and now it contains a note of the Car's current' gear.
Your output doesn't have to be exactly like this, but use this as a guide. This can be tricky, so think about what additional member variables you might need to control the shifting breakpoints.
TIP: Start small
Start by solving the simple problem of the car knowing its optimal gear at a given speed. This can be done with chained if-else statements. Only when you can determine the correct gear should you tackle the process of automatically generating output to show the up and down shifting.
wb_incandescentMini-project 3: The Flux Capacitor makes time travel possible
Use this screen clip of Dr. Brown's time machine to adjust your car model to become a time machine! Allow the user to input a destination date/time and simulate the car knowing what year it's in.
Start small: Don't try to actually represent time using fancy Time objects in Java. Just start with year as an int-type variable. Add a fluxCapacity member variable, and some methods for controlling the destination year.
If you get restless, checkout the LocalDateTime class in java.time package. Use this class to actually represent a point in time.