140 likes | 278 Views
Chapter 3 Creating a window. classes. What does it mean to say a computer runs Java? the computer has a programme called the Java Virtual Machine How to solve a problem using Java? Find a class which does what you want it to For instance create a window
E N D
Chapter 3Creating a window A Window
classes What does it mean to say a computer runs Java? the computer has a programme called the Java Virtual Machine How to solve a problem using Java? • Find a class which does what you want it to For instance create a window • Tell the JVM to create an object from the instructions contained in the class How? • Send the object a series of messages telling it to do what you want it to. Known as methods JVM A Window
Finding a class How do you know if there is a class which does what you want? Java ships with many classes which provide a great deal of functionality. That is a good place to start. Java web site java.sun.com/javase/7/docs/ JVM A Window
Java docs Worth downloading the documentation A web page so normal browser A Window
Local docs Unzip somewhere suitable and start exploring Description of the java system A Window
Sun tutorials Link to sun tutorials (now Oracle) Excellent resource Description of the java system A Window
API top 3 sections package names package descriptions all classes A Window
API top We have created a window with buttons respond as expected resize visible/invisible without knowing anything about the system A Window
Lessons To create instantiate an object from a class new <className> e.g. new JFrame(); To refer to an object you need to define a pointer (normally on instantiation) e.g. JFramejf = new JFrame(); JFramejfAlt = jf; In this case jfAlt and jf point to the same object. To access a method of the object send it a message. <Returned object> <objectName>.<methodName>(<message>); public booleanwinState() public void title() void means nothing will be returned boolean a boolean variable create name reference A Window
Syntax What if there is no class which does what you want? Modify an existing one : Object Oriented Write a new class How? Normal procedural elements Variable assignment Conditional execution loops Object creation and manipulation. Inheritance Polymorphism Collections Error handling Basic computer literacy. Not covered a =27; if ... then ... else do while for OO specific covered. Semantics more than syntax A Window
Themes How to design Object Oriented Programmes Encapsulation everything about the implementation of a class is contained in the class (and is inaccessible to the outside world) Inheritance how to enhance the functionality of existing classes. Polymorphism objects are accessed by pointers of a suitable type. The object must be an example of the object type which is pointed to by the pointer. You may refer to this as “a oranutang”, “a great ape”, “a primate”, or “a mammal” Possibly “a librarian” but never “a monkey” See T. Pratchett “Lords & Ladies” A Window
Isolation Programmer Efficiency compromised by meetings1 Programmer code with minimal interaction with colleagues. Start – to agree division of labour in the application – to individuals and programme units. End – to verify correct operation. Code reuse supported by modularisation. Modularisation the ability of a piece of code to run independently of the rest of the application. Code maintenance and improvement supported by modularisation Code correctness supported by modularisation 1The Mythical Man Month: Fred Brooks Heat flow application needs to solve PDEs. Solver isolation allows reuse. Improvements involve small part of app. Code shown to be correct is always correct. A Window
Reuse Stand on the shoulders of Giants1 Don’t re-invent the wheel. Use existing classes. Use Patterns. Solving problems leads to common patterns of interactions between objects. Those patterns can be documented using UML Reused to produce more robust & reliable code. Ideas can be found in Design Patterns: Elements of Reusable Object-Oriented Software1 1Issac Newton 2Gamma, Helm, Johnson, Vlissides Brief introduction A Window 0-201 63361-2
summary The user tells the JVM to instantiate an object from a class. The users sends messages to methods of the object. In response to which the object performs the requested actions. Find classes at Java api java.sun.com/javase/7/docs/ Inheritance Encapsulation Polymorphism Isolation modularisation A Window