210 likes | 527 Views
FP301 OBJECT ORIENTED PROGRAMMING. FP301 Object Oriented Programming. 2.0 FUNDAMENTALS OF JAVA PROGRAMMING LANGUAGE . 2.1 Understand Objects. FP301 OBJECT ORIENTED PROGRAMMING. Learning Outcome Subtopic 2.1. FP301 OBJECT ORIENTED PROGRAMMING. Modelling Concepts. Abstraction.
E N D
FP301 OBJECT ORIENTED PROGRAMMING FP301 Object Oriented Programming
2.0 FUNDAMENTALS OF JAVA PROGRAMMING LANGUAGE 2.1 Understand Objects
FP301 OBJECT ORIENTED PROGRAMMING Learning Outcome Subtopic 2.1
FP301 OBJECT ORIENTED PROGRAMMING • Modelling Concepts Abstraction • Abstraction is the process of exposing only the relevant details and ignoring (hiding) the irrelevant details (Don't get confused with "datahiding" of the "Encapsulation"). • Abstraction simplifies the understanding and using of any complex system.
FP301 OBJECT ORIENTED PROGRAMMING • Cont.. Encapsulation • Through encapsulation, you can control what parts of a program can access the members of a class. • How a member of a class can be accessed is determined by the access modifier that modifies its declaration. • Java’s access modifiers are: • public • private • protected
FP301 OBJECT ORIENTED PROGRAMMING • Cont.. Packages • Is a collection of classes that can be shared by Java programs. • Are classified into • in-built packages – system packages • user-defined packages - Created by the users for their requirements.
FP301 OBJECT ORIENTED PROGRAMMING • Cont.. • The classes are grouped together based on their functionality. • Some of the important packages of Java are: • lang • util • io • awt • net • applet
FP301 OBJECT ORIENTED PROGRAMMING • Analyze a Problem Using Object Oriented Analysis (OOA) • Analysis: • Precise abstraction of what the desired system must do, not how it will be done. • Object-Oriented Analysis is method which examines requirements from the perspective of the classes and objects. • OOA Process: Use Case Modeling – shows functions Class-based Modeling – Class diagram, CRC Behavioral Model – State Diagram, Sequence Diagram
FP301 OBJECT ORIENTED PROGRAMMING • Cont.. Example : Illustrate and explain object oriented and analysis by develop an information system to support all customer-related business in car rental company. The system should support management of customer data, reservations, vehicle rental and customer billing. Identify problem domain : ………….. Objects: …….. Attributes: ……. Operation: ……….
FP301 OBJECT ORIENTED PROGRAMMING • Create classes from objects using UML Class Diagram • Draw a class diagram based on scenario below:- • A customer makes order from retail catalog. Use Order as a central class. • Associated with the order are the Customer making the purchase and the Payment. • A Payment is one of three kinds; Cash, Cheque or Credit. • The order contains OrderDetails, each with its associated Item.
FP301 OBJECT ORIENTED PROGRAMMING • Cont.. Possible Objects, attributes and behavior :-
FP301 OBJECT ORIENTED PROGRAMMING • Create classes from objects using UML Class Diagram • Example: • Class : Bicycle • Attributes/Behaviourbrand, colour, wheels, no of gear • Method/Operations speed, speed, stop, changeGear Class • Notes: • Public + • Private - Attributes / behaviour Method / Operations
FP301 OBJECT ORIENTED PROGRAMMING COMPONENTS OF A CLASS Structuring Classes • The class declaration • Attribute variable declarations and initialization (optional) • Methods (optional) • Comments (optional)
FP301 OBJECT ORIENTED PROGRAMMING Cont.. Class Declaration • Syntax: [modifiers] class class_identifier • Example: public class Shirt
FP301 OBJECT ORIENTED PROGRAMMING Cont.. Variable Declaration and Assignment public int shirtID = 0; public String description = “-description required-”; public char colorCode = ‘U’; public double price = 0.0; public int quantityInStock = 0;
FP301 OBJECT ORIENTED PROGRAMMING Cont.. Comments: A comment is text that is not part of your code but serves as guide when trying to figure out what the lines of code of your program are. To comment the contents of one line, you start it with double forward slashes like this // Example : //Here is where you put the comments You can include many lines of comments in your program. To do that, comment each line with the double slashes. An alternative is to start the beginning of the commented line or paragraph with /* and end the commented section with */ Example : /* Here is a simple sentence that I want to display when the program starts. It doesn't do much. I am planning to do more stuff in the future. */
FP301 OBJECT ORIENTED PROGRAMMING Cont.. Methods • Syntax: [modifiers] return_type method_identifier ([arguments]){ method_code_block } • Example: public void displayInformation() { System.out.println("Shirt ID: " + shirtID); System.out.println("Shirt description:" + description); System.out.println("Color Code: " + colorCode); System.out.println("Shirt price: " + price); System.out.println("Quantity in stock: " + quantityInStock); } // end of display method
FP301 OBJECT ORIENTED PROGRAMMING Cont.. 1 public class Shirt { 2 3 public int shirtID = 0; // Default ID for the shirt 4 public String description = "-description required-"; // default 5 // The color codes are R=Red, B=Blue, G=Green, U=Unset 6 public char colorCode = ’U’; 7 public double price = 0.0; // Default price for all shirts 8 public int quantityInStock = 0; // Default quantity for all shirts 9 10// This method displays the values for an item 11 public void displayInformation() { 12System.out.println("Shirt ID: " + shirtID); 13System.out.println("Shirt description:" + description); 14System.out.println("Color Code: " + colorCode); 15System.out.println("Shirt price: " + price); 16System.out.println("Quantity in stock: " + quantityInStock); 17 18 } // end of display method 19 } // end of class) Class declaration Variable declaration and assignment Method displayInformation()
FP301 OBJECT ORIENTED PROGRAMMING Construct main method () using appropriate syntax • is a special method that the JVM recognizes as the starting point for every Java technology program that runs from a command line • • Syntax: • public static void main (String [] args)
FP301 OBJECT ORIENTED PROGRAMMING ESCAPE SEQUENCE FOR SPECIAL CHARACTERS
FP301 OBJECT ORIENTED PROGRAMMING Cont.. Output :- Welcome to Java!