Unit 1 Lab16.

Slides:



Advertisements
Similar presentations
Based on Java Software Development, 5th Ed. By Lewis &Loftus
Advertisements

INHERITANCE BASICS Reusability is achieved by INHERITANCE
C++ Classes & Data Abstraction
CSCI 1100/1202 April 3, Testing A program should be executed multiple times with various input in an attempt to find errors Debugging is the process.
Road Map Introduction to object oriented programming. Classes
Access to Names Namespaces, Scopes, Access privileges.
1 Classes Overview l Classes as Types l Declaring Instance Variables l Implementing Methods l Constructors l Accessor and Mutator Methods.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
19-Jun-15 Access to Names Namespaces, Scopes, Access privileges.
COMP 110 Introduction to Programming Mr. Joshua Stough October 8, 2007.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
28-Jun-15 Access to Names Namespaces, Scopes, Access privileges.
State,identity and behavior of objects Sem III K.I.R.A.S.
Extending the Robot Programming Language In the Robot world 1 mile = 8 blocks Suppose we want a robot to run a marathon (26+ miles)? Does our program have.
Inheritance using Java
Intro to OOP with Java, C. Thomas Wu
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Karel J Robot An introduction to BlueJ and Object- Oriented Programming.
CSC 212 Object-Oriented Programming and Java Part 1.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
Question of the Day  On a game show you’re given the choice of three doors: Behind one door is a car; behind the others, goats. After you pick a door,
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
CSC1401 Classes - 1. Learning Goals Computing concepts Identifying objects and classes Declaring a class Declaring fields Default field values.
CSCI 1100/1202 April 1-3, Program Development The creation of software involves four basic activities: –establishing the requirements –creating.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
08 Encapsulation and Abstraction. 2 Contents Defining Abstraction Levels of Abstraction Class as Abstraction Defining a Java Class Instantiating a Class.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
1 Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science A classwww.apComputerScience.com Day 4.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
Classes, Interfaces and Packages
Programming in java Packages Access Protection Importing packages Java program structure Interfaces Why interface Defining interface Accessing impln thru.
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Java – Methods Lecture Notes 5. Methods All objects, including software objects, have state and behavior. example, a student as an object has name, address,
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Topics Instance variables, set and get methods Encapsulation
Advanced Java class Nested Classes & Interfaces. Types of Nested Classes & Interfaces top-level nested –classes –interfaces inner classes –member –local.
Review – Primitive Data What is primitive data? What are 3 examples of primitive data types? How is a piece of primitive data different from an object?
Georgia Institute of Technology More on Creating Classes Barb Ericson Georgia Institute of Technology June 2006.
Modern Programming Tools And Techniques-I
Topic: Classes and Objects
OOP: Encapsulation &Abstraction
Reuse Separate classes could be developed for all the different objects that are needed in each program. However that would be wasteful. Often many functionalities.
University of Central Florida COP 3330 Object Oriented Programming
Polymorphism and Observers
Namespaces, Scopes, Access privileges
Encapsulation.
CSC 113 Tutorial QUIZ I.
Corresponds with Chapter 7
Outline Writing Classes Copyright © 2012 Pearson Education, Inc.
Group Status Project Status.
CLASS DEFINITION (FIELDS)
Advanced Java Programming
Object Oriented Programming
Unit 1 Test 1 Redo Friday at 8am here or Friday 4th BLOCK in Math Lab
Namespaces, Scopes, Access privileges
Unit 1 Lab14 & Lab15.
Outline Anatomy of a Class Encapsulation Anatomy of a Method
More on Creating Classes
Dr. R Z Khan Handout-3 Classes
Chapter 11 Inheritance and Encapsulation and Polymorphism
ITE “A” GROUP 2 ENCAPSULATION.
CSG2H3 Object Oriented Programming
Presentation transcript:

Unit 1 Lab16

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);

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…

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() }

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

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.

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.

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

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.

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; }

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

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();

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?