Presentation is loading. Please wait.

Presentation is loading. Please wait.

Unit 1 Lab16.

Similar presentations


Presentation on theme: "Unit 1 Lab16."— Presentation transcript:

1 Unit 1 Lab16

2 Understanding Threads
Remember, when creating a Thread, you must use an object that implements the Runnable interface Thread Constructor header in Thread class: public Thread(Runnable arg) Using the thread constructor in Lab14 class: Swimmer karel = new Swimmer(2); //Swimmer class implements Runnable Thread t1 = new Thread(karel);

3 Understanding Threads
Because Thread was defined knowing that the class you use with it contains the run() method, the start() method from the Thread class invokes the run method…

4 public class Thread { public Thread(Runnable arg) /*Constructor definition not shown*/ } public void start() run(); public class Lab14 { public static void main(String[] args) { Swimmer weismuller = new Swimmer(2); Swimmer fraser = new Swimmer(4); Swimmer spitz = new Swimmer(6); Swimmer phelps = new Swimmer(8); Thread t1 = new Thread( weismuller ); Thread t2 = new Thread( fraser ); Thread t3 = new Thread( spitz ); Thread t4 = new Thread( phelps ); t1.start(); t2.start(); t3.start(); t4.start(); } public class Swimmer implements Runnable { //code not shown public void run() }

5 Implementing Interfaces
You can extend only one class You can implement numerous interfaces public class Shifter extends Robot implements Runnable, Workable

6 Visibility Modifiers public -can be accessed/used from anywhere (inside of any class) private -can only be accessed from inside the class in which it was created protected – default visibility modifier (when none is specified). Can be accessed by any class in the same package or by any subclass.

7 Visibility Modifiers contd
Constructors and methods that you want to be able to use in other classes are public. Example: Inside of the Athlete Class, the constructors and methods (turnRight() & turnAround() ) are public. Inside of Lab02, you can use these constructors and methods. If they were private, you could not. Attributes or data fields are usually declared private so that they cannot be directly changed from outside of the class.

8 Data Fields Recall that some resource classes have attributes (data fields) and behaviors (methods) What data fields are in the Robot class? Direction, Location, and # of Beepers

9 Creating Data Fields Also called instance data - each object made gets its own copy of the data field. They are declared inside of a class, above the constructors and methods. They are usually defined with the private visibility modifier They are usually initialized inside of the constructor.

10 Creating Data Fields public class Shifter extends Robot implements Runnable, Workable { private int myBeepers; //data field declared public Shifter(int y) //Call to super must be first command super(1, y, Display.EAST, 0); //data field initialized myBeepers = 0; }

11 Changing Data Fields public class Shifter extends Robot implements Runnable, Workable { NOTE: This is not the real code for workCorner!!! public void workCorner() { int x = 0; //not a data field while(nextToBeeper()) pickBeeper(); myBeepers++; //unique for each object x++; } Saves value Does not

12 Starting Lab16 Lab16 is already complete Open Shifter shell
Since Shifter implements Workable and Runnable, what methods do you have to define? public void moveOneBlock(); - just like Harvester/Carpenter public void turnToTheRight(); - just like Harvester/Carpenter public void turnToTheNorth(); - just like Harvester/Carpenter public void workCorner(); public void run();

13 Starting Lab16 public void workCorner(); public void run();
Create an integer and set it to zero. Pick up all the beepers in the pile you are standing on (you do not know how many), increase the integer you just created by one each time you pick up a beeper. Put down all the beepers from the previous pile. You know exactly how many to put down, that number is represented by the variable myBeepers Make myBeepers become equal to the integer you created so that it will save the value of how many beepers you just picked up. public void run(); How can each Shifter complete the entire task?


Download ppt "Unit 1 Lab16."

Similar presentations


Ads by Google