Download presentation
Presentation is loading. Please wait.
1
Programming in Java CSE301 Half Lecture Harry Erwin, PhD University of Sunderland
2
Object-Orientation and Complexity As an experienced software architect (who could earn £90,000+ per year if I were willing to die before I am 60), I will give you my perspective on object-orientation. No person can understand the simultaneous interactions involving even a small number of objects. For example, 16 objects have 240 potential interfaces, if all are active. Balancing that many interactions is beyond your skill.
3
Managing Complexity The novice programmer will often generate enormous class and object diagrams with potentially hundreds of interactions. Don’t go there. Use packages (subsystems) rather than classes in your top-level diagram. Between 5 and 15 subsystems and a few more interfaces. Then drill down to produce class/object/package diagrams at the next level. Iterate this process until you can define the major classes. Document class interfaces and inheritance on separate UML diagrams.
4
Complexity and Encapsulation Finding a system design is not a simple task. This year, we will learn a number of principles you can use. Encapsulation is the first such principle. Strive for a design that consists of tightly bound subsystems that connect via a small number of narrow interfaces—APIs. GUI design is not a good model for this, so forget what you’ve learned about using visual tools until you understand how your design choices affect the way your GUI interfaces to your software.
5
Using Packages To specify the package a class belongs to, use the ‘package’ keyword. Note that classes belong to an unnamed package by default. This can be very convenient, but is also bad form. Eclipse will help you here. To load a class or interface Name from a package, P, use the following directive: –import P.Name; Note that the standard java class libraries live in a number of packages like: –java.lang –java.io –java.util –javax.swing
6
Writing a Java File A Java file consists of: –An optional ‘package’ directive –Zero or more ‘import’ directives –One or more class or interface definitions. (At most one is allowed to be ‘public’.) –Interspersed with comments When ClassName is a public class defined in the file, the name of the file must be ClassName.java. Eclipse will help you here.
7
Defining a Java Program A Java program has to have a class with a method with the following signature: –‘public static void main(String[] args);’ Assuming a public class exists with class name (e.g., CN) containing a method with the appropriate signature, it can be run by the following command: –‘java CN [list of arguments]’. Eclipse allows you to set the arguments as run properties.
8
Using Libraries Java has thousands of utility classes organized into libraries. We will be using some of them, for example: –java.io.* for input/output –java.lang.* for standard features and threads –java.text.* for string processing –java.util.* for utilities and collection classes –java.awt.* for the abstract windowing toolkit –javax.swing.* for the swing classes. –etc…
9
Class or Instance Methods? Instance methods require an instance. If you find yourself frequently creating instances as temporary variables for use with some method, you should define a class (static) method to do the same thing. Similarly, you might find class methods useful for doing comparisons among individual instances, since the class method can access the private fields of the instances passed to it as arguments.
10
Chaining Constructors It’s often convenient to chain constructors, calling one from another using the ‘this(argument list);’ syntax. This is especially frequent in the input/output library. The BankingDataReader gives some good examples of this.
11
Memory Leaks Memory leaks can occur in Java if a valid but unused reference to an object is left in scope: –int big_array[] = new int[100000]; –int result = foo(big_array) –// big_array is no longer needed but remains in –// scope. –big_array = null; // dumps it to avoid a leak –for(;;)handle_input(); // loops forever Hash tables can also be a source of memory leaks.
12
Interfaces Interface Dig_up { Object find(String name); } Any class with a find function of the correct signature can claim to implement the Dig_up interface. Note that you can do this in C++ as well, but it’s much rarer there.
13
Constructors Assume Foo is a class with a default constructor (Foo()) and an int member field, val. The following is good Java coding style for a second constructor: Foo(int val) { this(); // default constructor this.val = val; // Works! // sets the member field to val }
14
Role of ‘this’ 1.Used as a reference to the current object, much as in C++. 2.this.bar is a reference to the bar field of the current object. Ditto for this.bar() 3.this() is used in initialization, much like super().
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.