Software and Programming 1 Lab 8: Use of classes, static class variables and methods 1st March 2018 SP1-Lab8-2018.pdf Tobi Brodie (Tobi@dcs.bbk.ac.uk) 1
Lab 8 Objectives Understanding the encapsulation of objects. Use of the debugger to follow changes in objects. Understanding the difference between static (belongs to the class) and non-static (belongs to the object) variables and methods. Scan program descriptions and break them down into the correct code. Note: Marked exercises 6.1, 6.2 & 6.3 (Bicycle/BicycleTest) are a single marked exercise and you are required to complete them, and show the finished program, explaining how they work together, by the 15 th of March. Failure to complete ALL exercises by this date will mean you will not receive the full coursework marks. 2
Exercise 1: T09 To complete this exercise you will need to download the code from the following URL: http://www.dcs.bbk.ac.uk/~roman/sp1/extra/t09.java Once the code is downloaded, right click on the file and choose edit with notepad++ from the contextual menu. Copy this code into a newly created class T09 in BlueJ. Do not run the code! 3
Exercise 1: T09 (2) Do not run the code! First, analyse the code and write down below what you think will be printed out: Compile the code, and then choose breakpoints that you can use with the BlueJ debugger to see when values change within the objects/class. Now, run the main method and check the results. 4
Marked Exercise 6.1: Bicycle/BicycleTest Write a program that enters bicycles into a system designed in a similar way to Boris Bikes. When new bicycles are entered, they will automatically be assigned a unique ID, but will not be at a pick up point (docking station). You will have to assign a docking station ID to each new bicycle entered. Enter three bicycles into your program. Print out each bicycle s ID number and location. If the location is set at zero, it is presumed the bicycle is out for hire. Finally, print out the number of bicycles in the system. 5
Marked Exercise 6.1: Bicycle/BicycleTest (2) Break down the previous instructions! new bicycles are entered [new objects] automatically be assigned a unique ID [unique ID linked to a static class variable?] will not be at a pick up point [initialise dockingstationid with 0 in constructor]. assign a docking station ID to each new bicycle [an object method?]. three bicycles into your program [three objects] and so on 6
Marked Exercise 6.1: Bicycle/BicycleTest (3) The terminal window running the BicycleTest main method should look similar to the following: 7
Marked Exercise 6.1: Bicycle Class (1) The Bicycle class should have the following variables: Private data: Two instance variables ID to represent a unique ID of the bicycle. dockingstationid to represent an ID of the docking station if the bicycle is parked or 0 if it is in use. One class variable lastassignednumber to store the last number assigned to a newly created instance of Bicycle. 8
Marked Exercise 6.1: Bicycle Class (2) The Bicycle class should have the following four instance methods: 1. getid should return bicycle's ID. 2. pickup, with no parameters, should check whether the bicycle is parked and, if it is available for hire, mark it as being used and return true; otherwise, it should return false. 3. park, with a docking station ID as its parameter, should check whether the bicycle is currently hired and if so, mark it as docked at the provided station ID and return true; otherwise, it should return false. Note: This method will be used to assign a docking station for each new Bicycle object. 9
Marked Exercise 6.1: Bicycle Class (3) 4. getdockingstationno, with no parameters, should check the dockingstationid, and return a String with the value: " is in use " if the dockingstationid is 0 or " is at " + dockingstationid otherwise. The Bicycle class should also have the following class method: getnumberofbicycles with no parameters, which should return the number of bicycles in the system 10
Bicycle Class Skeleton public class Bicycle { /* To Do: declare private data, i.e. instance variables (ID and dockingstationid) and declare and initialise class variable (lastassignednumber) */ /* Instance methods (public interface) */ public int getid() { /* To Do */ } public boolean pickup() { /* To Do */ } public boolean park(int docksid) { /* To Do */ } public String getdockingstationno() { /* To Do */ } 11
Bicycle Class Skeleton (2) /* Class method */ public static int getnumberofbicycles() { /* To Do */ } /* Constructor */ public Bicycle() { } // Assign an ID to created instance // Initialise dockingstationid } // end of class Bicycle 12
BicycleTest Class Skeleton //Scanner import public class BicycleTest { public static void main(string[ ] args) { // create new Scanner object: in // declare int variable to use to assign values from in // create instance of Bicycle: bicycle1 //get user input for docking station //call park method to assign docking station // Repeat creation and assignment for 2 more objects 13
BicycleTest Class Skeleton (2) //print bicycle id and status for bicycle1 using instance //methods getid() & getdockingstationno() // repeat print code above for other objects //print number of bicycles in system using the method // getnumberofbicycles() } //end method } //end BicycleTest 14
Marked Exercise 6.2: BicycleTest continued Modify the BicycleTest class to include the other instance method implemented in the Bicycle Class: pickup() The pickup() method should check whether a bicycle is docked, and if it is, allow the bicycle to be removed from the dock to be used. Call this method, and the park() method at least twice in the BicycleTest class and add extra prints to the terminal to indicate the docked and undocked status. 15
Marked Exercise 6.2: Bicycle/BicycleTest The terminal window running the BicycleTest main method should now look similar to the following: 16
Marked Exercise 6.3: BicycleTest continued Create an additional reusable method getrandomnumber() that creates a random number that replaces the user input. Use this for the argument sent to the park() instance method after each instance of Bicycle is created. getrandomnumber() should have two parameters, int min and int max, and should return a random int value within the min/max range. For the purposes of the Bicycle program values between 0 and 10 will suffice. Hint: Consider searching on the Internet how one can generate random numbers with the Java libraries. 17
Marked Exercise 6.3: Bicycle/BicycleTest The terminal window running the BicycleTest main method should now look similar to the following: 18