title | type | duration | creator | ||||
---|---|---|---|---|---|---|---|
Creating Classes |
lab |
1:30 |
|
Note: This can be a pair programming activity or done independently.
In this lab you will be using your knowledge of classes to create two classes that will be used in the main method. To calculate
There is no starter code. Create a new project in IntelliJ for this lab
- Create a StopLight class with the following global variable: lightColor
- Create a constructor that sets the lightColor to a value of your choice.
- Create the following methods that accept no input:
isRed
,isYellow
,isGreen
. - Implement the methods to return true when the lightColor matches the color of the method. i.e; isRed() should return true if lightColor is red.
- Create a method
setLightColor
, that accepts a parameter to change the lightColor variable. Make sure to handle when the wrong input is given to the method.
- Create a Car class with the following global variables: color, brand, topSpeed
- Create a constructor that assigns values to the color, brand, and topSpeed variables.
- Implement
go
,slow
, andstop
methods that print something out. (You can print whatever is appropriate for the methods) - Create individual getter methods that print out the color, brand, and topSpeed of the car.
- Create individual setter methods that set the color, brand, and topSpeed of the car.
- Instantiate a
StopLight
object in the Main.java class using the constructor you created in theStopLight
class. - Instantiate a
Car
object in the Main.java class using the constructor you created in theCar
class. - In the Main.java class, create a loop(while, do while, for loop; your choice) that runs 30 times.
- Inside the loop; change the lightColor of the
StopLight
object in the correct sequence and have theCar
object react accordingly to theStopLight
lightColor using theCar
object's methods for each iteration. (Each time you loop back, change the lightColor)
Bonus:
- Have 20 cars react to the stop light.
- Randomly have a car run the red light.
Pull request from forked Project on GitHub that contains your implementation of the StopLight
class, the Car
class, and the main program that uses the classes you created.
- Oracle: Defining a class
- Oracle: Instantiating a class