Download presentation
Presentation is loading. Please wait.
Published byGary McLaughlin Modified over 8 years ago
1
GP3, Martin Lillholm 1 Introductory Programming (GP) Spring 2006 Lecture 3 We start at 13:00 Slides are available from the course home page Feel free to print them now www.itu.dk/courses/GP/F2006 Martin Lillholm
2
GP3, Martin Lillholm 2 Mandatory Assignment Assignment sheets: –We’ll strive to make them public Thursday How did it go with last week’s mandatory assignment ? –Easy? –Hard? This week … a bit harder … more free
3
GP3, Martin Lillholm 3 Last week A bit more on compilation, runtime environment, classes, objects, methods Identifiers Variables Primitive types Operators Expressions Packages Graphics and applets
4
GP3, Martin Lillholm 4 Today … we have to go through a lot Classes, objects (instances) Attributes (instance variables) Methods and constructors A first look a static variables and variables (class variables) Encapsulation Object/reference variables – variable of non-primitive type The String, Random, Math og Scanner classes Formatted output
5
GP3, Martin Lillholm 5 Classes and Objects Classes – blue prints –Definitions of objects to come Attributes or instance variables Methods –Define a new non-primitive type Objects – instances –Realisations/instances of classes Instance variables Access to methods declared in “their” class Static variables and methods … later
6
GP3, Martin Lillholm 6 Attributes / Instance Variables Principally as (local) variables in methods but –Defines an object’s state – main OO point! –Declared outside methods –Visible throughout the class – all methods –“Lives” as long as the instance/object –Receives a standard value at object instantiation 0 (byte, char, short, int, long) 0.0 (float, double) false (boolean)
7
GP3, Martin Lillholm 7 Methods Methods are used to combine and name a collection of statements Parameters and return value Recycling Abstraction
8
GP3, Martin Lillholm 8 Constructors Special methods with the same name as the class they’re defined in Called/invoked at object creation/instantiation No return value Seldom called explicitly A class can have one or more constructors but doesn’t need any Always has an implicitly defined constructor that takes no arguments
9
GP3, Martin Lillholm 9 Classes and objects again Attrib. (def.) Constructors Methods Class 1 Attrib. (def.) Constructors Methods Class n Attrib. Class ref. Object m Attrib. Class ref Object 1 Attrib Class ref. Object 1 Attrib. Class ref. Object k ”Abstract” model/conceptRealisation – instance
10
GP3, Martin Lillholm 10 Dice Attributes Instance variables Constructor Methods New – non-primitive type
11
GP3, Martin Lillholm 11 How do We Instantiate/Create an Object? Using the new statement Typically by assignment and initialisation of object variables. operator
12
GP3, Martin Lillholm 12 What Actually Happens During Instantiation? A new object is created Memory is allocated for instance variables Possible initialisations of instance variables The statements of the “appropriate” constructor is executed Which constructor?
13
GP3, Martin Lillholm 13 Methods Again int methodName (int a, int b,...) { // Method body variabelErklæring1; kommando1; kommando2; variabelErklæring2;... kommando3; return... } int, double, char, boolean, String,... Return type Parameter names and types Note that parameters just are “exotic” local variables
14
GP3, Martin Lillholm 14 What Happens When We Call a Method? char calc (int num1, int num2, String message) { int sum = num1 + num2; char result = message.charAt (sum); return result; } ch = obj.calc (25, count, "Hello"); Formal and actual parameters
15
GP3, Martin Lillholm 15 What Happens When We Call a Method? myMethod(); myMethodcompute
16
GP3, Martin Lillholm 16 What Happens When We Call a Method? doIt helpMe helpMe(); obj.doIt(); main
17
GP3, Martin Lillholm 17 The Method Body char calc (int num1, int num2, String message) { int sum = num1 + num2; char result = message.charAt (sum); return result; } Following slides … otherwise as the main method sum and result are local – as are the parameters
18
GP3, Martin Lillholm 18 The return statement The return type indicates which type is returned A method that doesn’t return anything must have return type void A return statement return the value of an expression and terminates the method return Expression The expression must evaluate to a value that has the same type as the method’s return type
19
GP3, Martin Lillholm 19 Constructors Again Special methods with the same name as the class they’re defined in Called/invoked at object creations/instantiation No return value Seldom called explicitly A class can have one or more constructors but doesn’t need any Always has an implicitly defined constructor that takes no arguments
20
GP3, Martin Lillholm 20 More Dice... and BlueJ –RollingDice.java and Die.java i BlueJ (L&L page 157-159)
21
GP3, Martin Lillholm 21 Encapsulation (1) An object can be viewed from two angles: –Internally – all details: instance variables and the class’ methods –Externally – the services (the object’s interface) an object offers. All others details are encapsulated Another object (a client) can use the object’s services (methods) The client doesn’t know how the services work or any other private details. Only an object itself should be able to chance its state (instance variables) directly. Although other objects may cause it indirectly through services. Objects should be self governing.
22
GP3, Martin Lillholm 22 Encapsulation (2) Think of objects a black box Only possible access should be through services Why all this ? Methods Attributes/data Client
23
GP3, Martin Lillholm 23 Visibility Modifiers (1) Used for enforcing/implementing encapsulation Three types: private, public, protected We’ll wait with protected until we discuss inheritance Class members (methods and attributes) declared public can accessed and in the latter case changed by all clients Class members declared private are only available to the class itself Class members without a visibility modifier has default or package access
24
GP3, Martin Lillholm 24 Visibility Modifiers (2) publicprivate Variables Methods Provide services to clients Support other methods in the class Enforce encapsulation Violate encapsulation
25
GP3, Martin Lillholm 25 Accessors and Mutators To facilitate access to private attributes one can provide service methods – only if needed! An accessor method (e.g. getX ) makes it possible to read private attributes indirectly. A mutator method (e.g. setX ) makes it possible to change private attributes indirectly. Often with one or more limitations. Isn’t this exactly what we were striving to avoid?
26
GP3, Martin Lillholm 26 More Dice …
27
GP3, Martin Lillholm 27 Our First DIY Class … the Car Class A Car is defined by its: –Make –Top Speed A Car in BlueJ …
28
GP3, Martin Lillholm 28 Static variables and Methods Lives at class level and across instances One per class – despite the number if instances Can be accessed/used without instantiation Declared using the keyword static public static double sqrt (double num) {... } public static double PI; Using these examples from the Math class: –Math.PI*5 –int a = Math.sqrt(9); Why static methods and variables ?
29
GP3, Martin Lillholm 29 Static Methods and Variables Attrib. (def.) Constructors Methods Class 1 Attrib. (def.) Constructors Methods Class n Attrib. Class ref. Object m Attrib. Class ref Object 1 Attrib Class ref. Object 1 Attrib. Class ref. Object k ”Abstract” model/conceptRealisation – instance public static double sqrt (double num) {... } public static double PI;
30
GP3, Martin Lillholm 30 Hello.java again …
31
GP3, Martin Lillholm 31 Contains a reference to the object – not the object itself as is the case for variables of primitive type! Die number1 = new Die (); Die number2; number2 = new Die (); number2 = number1; // A reference is copied! number1 Die objekt1 number2 Die objekt2 Garbage collection. Object variables (Pointer fun with Binky)
32
GP3, Martin Lillholm 32 The String Class The type String is a class-type! Strings are objects/instances. String variables contains a reference. String title; title = ”Java Software Solutions”; (title = new String(”Java Software Solutions”)) This “trick” only works for Strings – syntactic sucker.
33
GP3, Martin Lillholm 33 The String Class Once a String object has been instantiated it can’t be changed – it’s immutable. But a variable that references it can be changed... unless of course it’s declared final Many of the String class’ methods return new instances and it looks (and works) as if one can change a String object. L&L page 119 (in BlueJ if time allows)
34
GP3, Martin Lillholm 34 The toString method Any class should have a toString() method! Should return a string representation of an object’s state Is called automatically using e.g. System.out.println and string concatenation. Die number1 = new Die (); System.out.println(number1); String message = ”Resultatet blev: ” + number1; System.out.println(message); toString can be as complex as desired – keep it simple...
35
GP3, Martin Lillholm 35 The Bank Account... –Transactions.java and Account.java in BlueJ. (L&L page 172-174)
36
GP3, Martin Lillholm 36 The Scanner Class (1)
37
GP3, Martin Lillholm 37 The Scanner Class (2)
38
GP3, Martin Lillholm 38 The Random Class
39
GP3, Martin Lillholm 39 The Math Class
40
GP3, Martin Lillholm 40 The DecimalFormat Class
41
GP3, Martin Lillholm 41 Questions... We’ve been through a lot … Ask away … This weeks mandatory assignment
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.