Introductory Programming GP Spring 2006 Lecture 6 We start at 13:00 Slides are available from the co - PowerPoint PPT Presentation

1 / 23
About This Presentation
Title:

Introductory Programming GP Spring 2006 Lecture 6 We start at 13:00 Slides are available from the co

Description:

Slides are available from the course home page. Feel free to print them now ... UML: Booch, Jacobsen and Rumbaugh (The Three Amigos) ... – PowerPoint PPT presentation

Number of Views:20
Avg rating:3.0/5.0
Slides: 24
Provided by: itu75
Category:

less

Transcript and Presenter's Notes

Title: Introductory Programming GP Spring 2006 Lecture 6 We start at 13:00 Slides are available from the co


1
Introductory Programming (GP)Spring
2006Lecture 6We start at 1300 Slides are
available from the course home pageFeel free to
print them nowwww.itu.dk/courses/GP/F2006
  • Martin Lillholm

2
Mandatory Assignment
  • How did it go with last weeks mandatory
    assignment ?
  • Easy?
  • Hard?

3
Last Week
  • Arrays
  • Arrays with more than one dimension
  • Arrays containing objects
  • Command-line arguments
  • The class ArrayList
  • Reading from a text file

4
Initialisation Lists from Last Week
  • int a 5
  • a 7
  • int nums 1,2,3
  • nums 4,5,6,7
  • nums new int4,5,6,7

5
This Week
  • A few words on software development
  • UML class diagrams
  • Classes
  • Attributes
  • Methods and Constructors
  • Dependencies
  • Aggregations
  • More about methods, parameters, and scope (scope)
  • More about static variable (class variables) and
    methods
  • Wrapper classes

6
Software Development
  • Typical (although simplified) phases in Software
    Development
  • Assignment
  • Requirements specification
  • Analysis and design
  • Implementation often several
    iterations
  • Testing
  • OO-design simplified
  • Classes (nouns can be good candidates)
  • Attributes
  • Responsibilities (verbs can be good candidates)
  • Walkthroughs

7
UML Class Diagrams
  • UML is much more than just class diagrams!
  • UML Booch, Jacobsen and Rumbaugh (The Three
    Amigos)
  • Class diagrams is used for describing classes and
    static connections between classes
  • Typically contains
  • Classes
  • Atributes
  • Methods and constructors
  • Dependencies
  • Aggregations
  • More information Martin Fowler, UML Distilled
    or Links on the course home page

8
UML Class Diagrams(Static overview)
TestPerson
Math
Bank Account
Person
Employee
Student
9
UML Class Diagrams(Class Details)
Person
identifier (zero or more types)
Person(String, int, int)
How detailed should the individual class
descriptions be? What is it good for anyway?
10
Methods Job Abstraction
  • Abstraction part of any good program design
  • The real work is done by methods
  • Bulk of code is written in methods
  • OO-design typically starts with class and
    attribute identification
  • Methods can be thought of as responsibilities
  • A responsibility/method (naturally) belongs to
    one class sometimes a tool box class
  • Algorithms and pseudo code can help write methods
  • Keep it simple! Rather have too many methods than
    too few!

11
Methods Parameters
  • Parameters are ultimately fancy local
    variables. Theyre initialised during method
    invocation by the actual parameter values call
    by value.
  • The difference between parameters of primitive
    type and reference type
  • Primitive type The variable value is copied
  • Reference type The reference is copied (alias)
  • ParameterTester.java LL page 327-331 in BlueJ

ch obj.calc (25, count, "Hello")
12
Scope revisited
  • A variables scope defines where it can be used
    and/or how long it stays alive.
  • A variables scope is from declaration until the
    end of the innermost block () in which its
    declared.
  • Attributes (instance and static), as a natural
    consequence, have the entire class as scope
    although instance variables only becomes live
    after instantiation.
  • The scope of parameters is the entire
    method/constructor.
  • A loop control variable declared in the header of
    a for statement has the header and body of the
    for statement as scope.
  • A variable or a parameter cannot be re-declared
    within its scope.
  • An attribute can be shadowed by local variables
    and parameters.

13
Scope Examples
(Sestoft, 2002)
14
Methods Overloading
  • Overloading enables multiple definitions for a
    single method name within one class.
  • The system cannot determine which version to used
    based solely on the method name
  • Several methods with the same are differentiated
    by their signature
  • A signature is defined as the number, type, and
    order of the parameters.
  • Overloading need unique signatures to work.
  • Weve earlier seen overloading for constructors
    but it can be used for any method

15
Overloading
  • The compiler determines which version of the
    method to invoke based on the actual parameters

float tryMe(int x) return x .375
float tryMe(int x, float y) return xy
16
Overloading
  • Why do we like overloading ?
  • Constructors
  • Methods example
  • println (String s)
  • println (int i)
  • println (double d)
  • System.out.println ("The total is")
  • System.out.println (total)
  • Please note that the return type isnt part of
    the signature.
  • Why would that be a problem?
  • Overloading examples in BlueJ

17
Static Variables and Methods
  • Declared using the keyword static
  • private static int count 0
  • ...
  • public static int getCount ()
  • return count
  • Static variables (class variables) and static
    methods are useable without prior instantiation.
    Static variables are allocated and possibly
    initialises the first time a given class name is
    encountered.
  • How does static variables actually work ?
  • Examples from the math class
  • Math.PI
  • Math.Pow (2,3)
  • Example SloganCounter.java LL page 294 i BlueJ
  • UML and static variables and methods.

18
main method
  • public static void main (String args)
  • You now know what its all about

19
Static
  • Why is it that you often write static methods at
    the exercises?
  • Questions static ?

20
The this reference
  • The this reference enables and object to
    reference itself.
  • When using this within a method you get a
    reference to the object containing the method
    just like any other object reference.
  • Can be used to overpower e.g. shadowing
  • class A
  • private x5
  • public void testMethod(int x)
  • if (this.x x)
  • System.out.println(Attribute and
    parameter are identical)
  • This and static contexts

21
The this reference
  • this and constructor parameters

class Account String name long
acctNumber double balance public Account
(String name, long acctNumber, double balance)
this.name name this.acctNumber
acctNumber this.balance balance
class Account public Account (String
newName, long newAcctNumber, double newBalance)
name newName acctNumber
newAcctNumber balance newBalance
22
Wrapper classes
  • All primitive types (byte, short, int, long,
    float, double, char, boolean, void)have wrapper
    classes
  • Enables us to store primitive types a objects.
  • Integer countObj new Integer (10)
  • Why wrapper classes?
  • Remove the distinction
  • ArrayList
  • Polymorphism
  • Collections of methods (LL appendix M)

23
Autoboxing og unboxing
  • Automatic conversion (wrapping/unwrapping)
    between primitive types and the corresponding
    wrapper class
  • Integer count
  • int number 42
  • count number // Autoboxing
  • Integer i new Integer (32)
  • int j
  • j i // Unboxing
Write a Comment
User Comments (0)
About PowerShow.com