Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
Software and Programming 1
Lab 7:
Use of classes, 
static class variables 
and methods
1SP1-Lab7-2015-16.ppt
Tobi Brodie (Tobi@dcs.bbk.ac.uk)17 February2016
Lab 7 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: Exercise 2/3:  Cycle / CycleTest2 are a single checked 
exercise and you are required to complete them, and show the finished  
program, explaining how they work together, when requested. Make 
sure you have backed up your work on a memory stick or similar. 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 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
Exercise 2: Cycle/CycleTest
Write a program that enters Bicycles into a system 
designed in a similar way to ‘Boris Bikes’ .
When new cycles 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 cycle entered.
Enter three cycles into your program.
Print out each cycle’s ID number and location. If the 
location is set at zero, it is presumed the cycle is out 
for hire.
Finally print out the number of cycles in the system. 5
Exercise 2: Cycle/CycleTest (2)
Break down the previous instructions!
…new cycles 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 cycle 
[an object method?].
…three cycles into your program [three objects] 
..and so on…
6
Exercise 2: Cycle/CycleTest (3)
The terminal window running the CycleTest main 
method should look similar to the following:
7
Exercise 2: Cycle Class (1)
The Cycle class should have the variables:
Private data:
• Two instance variables
– ID which represents a unique ID of the cycle.
– dockingStationID which represents an ID of the docking 
station if the cycle is parked or 0 if it is in use. 
• One class variable
– lastAssignedNumber which stores the last number 
assigned to a newly created instance of Cycle.
8
Exercise 2: Cycle Class (3) 
The Cycle class should have the following four instance 
methods:
1. getID should return cycle's ID.
2. pickup, with no parameters, should check whether the cycle 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 cycle 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 Cycle object.
9
Exercise 2: Cycle Class (4) 
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 Cycle class should also have the following class 
method:
getNumberOfCycles with no parameters, which should 
return the number of cycles in the system
10
Cycle Class Skeleton
11
public class Cycle
{
/* 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 */ }
Cycle Class Skeleton (2)
12
/* Class method */
public int getNumberOfCycles()  { /* To Do */ }
/* Constructor */      
public Cycle()       
{       
// Assign an ID to created instance
// Initialise dockingStationID 
}
}  // end of class Cycle 
CycleTest Class Skeleton 
13
//Scanner import
public class CycleTest
{      
public static void main(String[ ] args)   
{
// create new scanner object: in
// declare int variable to use to assign values from in
// create instance of Cycle: cycle1
//get user input for docking station 
//call park method to assign docking station
// Repeat creation and assignment for 2 more objects
CycleTest Class Skeleton (2)
14
//print cycle id and status for cycle1 using instance         
methods   getID()   &    getDockingStationNo()
// repeat print code above for other objects
//print number of cycles in system using the method 
// getNumberOfCycles() 
}  //end method
}   //end CycleTest     
Exercise 3: CycleTest2
15
Modify the CycleTest class to include the other 
instance method implemented in the Cycle Class:
pickup()
Call this method, and the park() method at least 
twice in the CycleTest class and add in any extra 
prints to the terminal if necessary.
Exercise 4: CycleTest3
16
Create an additional reusable method 
getRandomNumber() in 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 Cycle is created. 
getRandomNumber() should have two 
parameters, int min and int max and should 
return an int value within the min/max range.
For the purposes of the Cycle program values 
between 0 and 10 will suffice.