1 / 134

Workshop on Java Programming

Workshop on Java Programming. September 29, October 13, October 20 YANPING CHEN ychen@site.uottawa.ca. Introduction to Object-Oriented Programming Concepts. Software Life Cycle. Steps in software development Problem specification Algorithm development Coding Testing Debugging

romeo
Download Presentation

Workshop on Java Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Workshop on Java Programming September 29, October 13, October 20 YANPING CHEN ychen@site.uottawa.ca

  2. Introduction to Object-Oriented Programming Concepts

  3. Software Life Cycle • Steps in software development • Problem specification • Algorithm development • Coding • Testing • Debugging • Maintenance • Documenting is an activity that occurs throughout the cycle

  4. Problem Specification • The goal – use computer to solve problems • Problems are usually stated in natural language • Software designer extracts the exact problem specification from the informal problem -- requirements analysis

  5. Understand the Problem • Clarify • What data is given (available for use when the problem-solving takes place) • What results are required • What assumptions are safe to make • What constraints must be obeyed

  6. Example 1 Informal statement John wants to know the total and average cost of the three books he just bought. Givens: descriptions and names of the values that are know A, B, C numbers representing the cost of each of John’s books Results: descriptions and names of the values to be computed from the givens • SUM, the sum of A, B and C • AVG, the average of A, B, C

  7. Example 2 Informal statement Write an algorithm that takes four scores (each out of 25) and computes their average (out of 100) Givens: descriptions and names of the values that are know A, B, C, D numbers representing the four scores Results: descriptions and names of the values to be computed from the givens • AVG, the average of A, B, C, D out of 100

  8. Algorithm • An algorithm is a solution for a problem • An algorithm’s HEADER specifies the name of the algorithm, an order for the givens and an order for the results • An algorithm’s BODY is a sequence of instructions which, when executed, computes the desired results from the givens. • Algorithms are written in “pseudocode”. A precise notation convenient for people but not executable by computers.

  9. Intermediate Variables • Often it is useful within an algorithm to use a variable that is neither a given nor a result to temporarily hold a value. • Intermediate variables’ values are not returned to the calling statement, nor are they remembered from one call to the next.

  10. Algorithm for Example1 • GIVENS: numbers A, B, C • RESULTS: • SUM, the sum of A, B and C • AVG, the average of A, B and C • HEADER Alg1(A, B, C) Return: (Sum, Avg) • BODY • Sum = A+B+C • Avg = Sum /3

  11. Algorithm for Example2 • GIVENS: numbers A, B, C, D • RESULTS: • AVG, the average of A, B, C and D out of 100 • HEADER Alg2(A, B, C, D) Return: (Avg) • BODY • Sum = A + B + C + D //an Intermediate Variable • Avg = Sum /4/25*100

  12. Tracing an Algorithm • To TRACE an algorithm is to execute it by hand, one statement at a time, keeping track of the value of each variable. • The aim is either to see what results the algorithm produces or to locate “bugs” in the algorithm.

  13. Tracing Example • Example 1 Avg Alg1 (18, 25, 20) • Example 2 Avg Alg2 (18, 25, 20, 19)

  14. Invoking an Algorithm • To invoke an algorithm you use a “call” statement which is identical to the header except for the names of the givens and results. (X,Y) Alg1(10, 7, -2) invokes algorithm Alg1 with givens A=10, B=7, C=-2 and returns the results in X(Sum) and Y(Avg) • Information is passed between the call statement and the algorithm based on the ORDER of the givens and results, not their names.

  15. Coding • Coding = translating an algorithm into a particular programming language so it can be executed on a computer • Be sure your algorithm is correct before coding it by tracing it on test data. • Coding is largely a mechanical process, not a creative one. • Both algorithm development and coding require very careful attention to detail

  16. Translating to Code • To program is to first develop and test algorithms in pseudocode and then TRANSLATE them into code in a programming language • Translating algorithms into code is very much a mechanical process, involving only a few decisions. • For each pseudocode block we will see one foolproof way of translating it to code. Algorithms are then translated block by block.

  17. Using Algorithms • When developing an algorithm, it is a good idea to make as much use as possible of existing algorithms (ones you have written or that are available in a library) • You can put a CALL statement to any existing algorithm wherever it is needed in the algorithm you are developing. Be sure you get the ORDER of the givens and results correctly. • To call an algorithm you need to know its header but not how it works – you must just trust that it works correctly.

  18. Information Passing • When calling an algorithm the call statement and the header must be identical except for the names of the givens and results. These are matched one-to-one in the order they appear. CALL: (T, AvgOutOf25) Alg1(X,Y,Z) HEADER: (Sum, Avg) Alg1(A,B,C) The arrow show how information is passed.

  19. Testing, Debugging and Maintenance • Testing = looking for errors (“bugs”) in an algorithm or program by executing it on test data (givens) and checking the correctness of the results. • Debugging = locating and correcting an error in an algorithm or program • Maintenance = changing an algorithm or program that is in use.

  20. What is an Object? An object is a software bundle of variables and related methods • Software objects are used to model real-world objects • Real-world objects share two characteristics: They all have state and behavior. • Software objects also have state and behavior. A software object maintains its state in one or more variables. And implements its behavior with methods. • A variable is an item of data named by an identifier. • A method is a function (subroutine) associated with an object

  21. Examples of Object • Bicycles: • States: current gear, current pedal cadence, two wheels, number of gears • Behaviors: braking, accelerating, slowing down, changing gears • Dogs: • States: name, color, breed, hungry • Behaviors: barking, fetching, wagging tail

  22. What is a Message? Software objects interact and communicate with each other using messages • A single object alone is generally not very useful. Instead, an object usually appears as a component of a larger program or application that contains many other objects. • Through the interaction of these objects, programmers achieve higher-order functionality and more complex behavior.

  23. What is a Message? (2) • Messages provide two important benefits. • An object's behavior is expressed through its methods, so message passing supports all possible interactions between objects. • Objects don't need to be in the same process or even on the same machine to send and receive messages back and forth to each other.

  24. Components of Message (1) • When object A wants object B to perform one of B's methods, object A sends a message to object B • Sometimes, the receiving object needs more information so that it knows exactly what to do • when you want to change gears on your bicycle, you have to indicate which gear you want. This information is passed along with the message as parameters

  25. Components of Message (2) • Three components that comprise a message: • The object to which the message is addressed (YourBicycle) • The name of the method to perform (changeGears) • Any parameters needed by the method (lowerGear)

  26. What is a Class? (1) A class is a blueprint, or prototype, that defines the variables and the methods common to all objects of a certain kind. • In the real world, you often have many objects of the same kind. For example, your bicycle is just one of many bicycles in the world. • Bicycles have some state and behavior in common. However, each bicycle's state is independent of and can be different from that of other bicycles. • Using o-o terminology, we say that your bicycle object is an instance of the class of objects known as bicycles.

  27. What is a Class? (2) • In object-oriented software, it's also possible to have many objects of the same kind that share characteristics: rectangles, employee records, video clips, and so on. Objects of the same kind are similar and you can create a blueprint for those objects.

  28. Class and Instance (1) • After create the bicycle class, you can create any number of bicycle objects from the class. • When create an instance of a class, the system allocates enough memory for the object and all its instance variables. • Each instance gets its own copy of all the instance variables defined in the class.

  29. Class and Instance (2) • Variables: • Class variable: contains information that is shared by all instances of the class • Instance variable: contains information for one specific instance • Methods • Class methods: invoked directly from the class. • Instance methods: invoked on a particular instance

  30. Example for Class Variables • Suppose that all bicycles had the same number of gears. • Defining an instance variable to hold the number of gears is inefficient: each instance would have its own copy of the variable, but the value would be the same for every instance. • Define a class variable that contains the number of gears. All instances share this variable. If one object changes the variable, it changes for all other objects of that type.

  31. Objects vs. Classes • The difference between classes and objects is confusing. • In software, the term "object" is sometimes used to refer to both classes and instances. • Class is not shaded. It represents a blueprint of an object rather than an object itself. A blueprint of a bicycle is not a bicycle. • An object is shaded, indicating that the object exists and that you can use it.

  32. What is Inheritance? A class inherits state and behavior from its superclass. • Inheritance provides a powerful and natural mechanism for organizing and structuring software programs. • You know a lot about an object by knowing its class. Even if you don't know what a penny-farthing is, if I told you it was a bicycle, you would know that it had two wheels, handle bars, and pedals. • Object-oriented systems allow classes to be defined in terms of other classes.

  33. Example for Inheritance • Mountain bikes, racing bikes, and tandems are all kinds of bicycles. In object-oriented terminology, they are all subclasses of the bicycle class. Similarly, the bicycle class is the superclass

  34. How does Inheritance work? (1) • Each subclass inherits state (in the form of variable declarations) and methods from the superclass. e.g. Mountain bikes, racing bikes, and tandems share some states: cadence, speed, and some behaviors: braking and changing pedaling speed • Subclasses are not limited to the state and behaviors provided to them by their superclass. Subclasses can add variables and methods to the ones they inherit from the superclass. e.g. Some mountain bikes have an extra set of gears with a lower gear ratio.

  35. How does Inheritance work? (2) • Subclasses can also override inherited methods and provide specialized implementations for those methods. e.g You had a mountain bike with an extra set of gears, you would override the "change gears" method so that the rider could use those new gears. • Not just one layer of inheritance: the inheritance tree, or class hierarchy, can be as deep as needed. -- The farther down in the hierarchy a class appears, the more specialized its behavior.

  36. How does Inheritance work? (3) • In JAVA, the Object class is at the top of class hierarchy, and each class is its descendant (directly or indirectly). Object provides behaviors that are required of all objects running in the Java Virtual Machine. e.g. All classes inherit Object's toString method, which returns a string representation of the object.

  37. Benefits of Inheritance • Reuse the code in the superclass: Subclasses provide specialized behaviors from the basis of common elements provided by the superclass. • Programmers can implement superclasses called abstract classes that define "generic" behaviors. The abstract superclass defines and may partially implement the behavior, but much of the class is undefined and unimplemented. Other programmers fill in the details with specialized subclasses.

  38. Why Abstract Class • Abstract class: The methods can not be specified at that moment. e.g. TextMessage, VoiceMessage, FaxMessage all have a method called play(), how can you implement play() in the superclass Message? Message TextMessage VoiceMessage FaxMessage

  39. What is an Interface? (1) An interface is a contract in the form of a collection of method and constant declarations. • An interface is probably most analogous to a protocol (an agreed on behavior). • When a class implements an interface, it promises to implement all of the methods declared in that interface

  40. What is an Interface? (2) • Interface defines a protocol of behavior that can be implemented by any class anywhere in the class hierarchy. • Capturing similarities among unrelated classes without artificially forcing a class relationship. • Declaring methods that one or more classes are expected to implement. • Revealing an object's programming interface without revealing its class.

  41. Example of Interface • An inventory program for bicycle doesn't care what class of items it manages as long as each item provides certain information, such as price and tracking number. • The inventory program sets up a protocol of communication. This protocol comes in the form of a set of constant and method definitions contained within an interface. • The inventory interface would define, but not implement, methods that set and get the retail price, assign a tracking number, and so on.

  42. Summary • Class is a prototype for objects • Objects are created from classes • An object's class is its type • How to create an object from a class • What constructors are • What class variables and methods are • What instance variables and methods are • How to find out what a class's superclass is • Interface is a protocol of behavior

  43. Exercises • Define classes based on the following scenario: (make reasonable assumption) • Your use an bank card to do some transaction from a bank machine. • There are three type of transaction: withdraw, deposit and pay bill. • You need input a 4 digits PIN before you can do any transaction • Bank machine will give you a record after each transaction

  44. First Cup of JAVA

  45. Concise History of Java • 1991: Group of Sun engineers design a small portable computer language for consumer devices • 1992: First product delivered by the group • 1995: Hotjava – a WWW browser written in Java with a built-in interpreter of intermediate bytecodes starts Java hype • 1996: First official version of Java is released • 1999: Professional-looking applications start to appear using the new Swing GUI API

  46. About Java • Java technology is both a programming language and a platform • The Java programming language is a high-level language: • Simple • Architecture neutral • Object oriented • Portable • Distributed • High performance • Interpreted • Multithreaded • Robust • Dynamic • Secure

  47. Overview of Java (1) • Object-Orientation (OO) • Programs describe interactions between a collection of objects which are designed to model some aspect of the real world. For example, an airline reservation system would involve Airplanes, Seats, Passengers, Tickets and so on. • Syntax is borrowed from C++, while object model from Smalltalk.

  48. Overview of Java (2) • Portability • As in some version of Pascal, source code is first compiled into platform-independent intermediate code – bytecode. Unlike Pascal, this intermediate compilation stage is explicit • Once in the form of bytecodes, programs can be executed by platform-dependent virtual machines. • Java is often advertised as an interpreter. In reality, Java source code is always compiled into bytecodes first. Bytecodes themselves are interpreted by Java virtual machine (Java VM)

  49. How Java works? • The Java programming language is unusual both compiled and interpreted. • With the compiler, first you translate a program into an intermediate language called Java bytecodes The platform-independent codes interpreted by the interpreter on the Java platform. The interpreter parses and runs each Java bytecode instruction on the computer. • Compilation happens just once; interpretation occurs each time the program is executed.

More Related