Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Java, OO and IDEs ICW Lecture 2 Errol Thompson Room 134.

Similar presentations


Presentation on theme: "Introduction to Java, OO and IDEs ICW Lecture 2 Errol Thompson Room 134."— Presentation transcript:

1 Introduction to Java, OO and IDEs ICW Lecture 2 Errol Thompson Room 134

2 Write down answers to the following questions What is a computer program? What is an object-oriented program?

3 What is a computer program? Any program  Instructions to a computer  Data plus algorithm  Takes in input carries out a process and generates output  May be divided into smaller chunks (i.e. functions and modules) An object-oriented program  Made up of entities called objects  An object has  state (data or attributes or fields, relationships) and  behaviour (functions, methods)  An OO program achieves things through interactions between objects  One object calls another (passes a message)  Java supports object-oriented programming

4 How do we define the characteristics of an object? In your program, you define “Classes”. You use “Classes” to create “Objects”. You retain references to “Objects” You call “Methods” to perform computation. You use “Methods” to manipulate data held in “Fields” within an Object.

5 What does this class do? class Something { public Something() { value = 0} private int value; public void inc () { value = value +1; } public int getValue () { return Value; } What would be a brief description for the behaviour of this class What is a better name for this class What fields and values are accessible to users of objects created from this class? What is the initial value of the field?

6 Object use public void testInc() { System.out.println("inc"); Counter instance = new Counter(); instance.inc(); int expResult = ?; int result = instance.getValue(); assertEquals(expResult, result); instance = null; } Write a short piece of Java code that uses the Counter class from the previous slide. Your code should use the object so that the resulting value retrieved from the object is equal to 1.

7 Objects and Methods Always think about running methods on objects. e.g. given Number objects “x” and “y” x.add(y) is more OO than x = add(x, y) Why write methods against objects? How do you decide which class should contain a method?

8 Object Interaction You Person 1 Directory Service Person 2 Person 3 Which objects can interact? For an object to call a method on another it must have knowledge of that object (i.e. a reference)

9 Static Methods. But not all methods in Java are called on objects,... what’s going on here? Some times methods are required that don’t run against a specific object. Initial program method (“main”) Factory methods Methods that are not object specific Any methods or fields that are not related to a specific object are declared as “Static”. They are class methods or fields

10 Static A static field is one that is the same for all objects. E.g. static pi. A static method is one that is the same for all objects. Static methods can’t refer to none static fields. Why? Static methods don’t need to be called on a particular objects. (Classname.method())

11 The main method If we just have objects and methods that run on objects how do we start our program? A class may have a special static “main” method. This method is run when the program is started.

12 Types Java is a statically typed language – this means All fields are declared with a type All methods have a return type (void means a method returns nothing) The type determines the values that can be assigned to a field or returned by a method, and the operations that can be performed on it. Where the type is a class name then the field will hold a reference to objects created from that class or one of its subclasses.

13 Primative Types Provided as part of the language byte, short, int, long, float, double, boolean, char Can not be used where a class is required. 13Interenet Computing Workship

14 Programming Problem The user has entered a set of data. It is to be held in a collection that 1.Enables the user to retrieve the items as a sorted collection 2.Enables the user to retrieve all unique items What classes in Java could we use as the base for this exercise and how can we develop our own class so that it reuses the existing Java class?

15 Conclusion Hand out photo copy of exerises. Make sure you can do these. If you can’t then come and talk to me about switching to the conversion M.Sc.

16 Next Time: Crypto Next week we will look at the Java Crypto API. This libarary lets you encrypt, decrypt, hash, handle keys,... And you will be given the first exerice worth 20% of your total mark.


Download ppt "Introduction to Java, OO and IDEs ICW Lecture 2 Errol Thompson Room 134."

Similar presentations


Ads by Google