Arrays and Objects - PowerPoint PPT Presentation

1 / 45
About This Presentation
Title:

Arrays and Objects

Description:

Classes can inherit' the methods and attributes of another Class, saving you re ... Creating a class that has bits that need filling in inheriting classes. Interfaces. ... – PowerPoint PPT presentation

Number of Views:29
Avg rating:3.0/5.0
Slides: 46
Provided by: andrew58
Category:

less

Transcript and Presenter's Notes

Title: Arrays and Objects


1
Lecture 4
  • Arrays and Objects

2
Review
  • Variables
  • Type name new Type()
  • Methods
  • name.methodName(inputs)
  • variable name.methodName(inputs)
  • returnType methodName (Type inputVariable)
  • return variable

3
Review Structure
  • class ClassName
  • int instanceVariable
  • public static void main (String args)
  • ClassName a new ClassName()
  • a.methodOne(100)
  • void methodOne(int parameterVariable)
  • instanceVariable parameterVariable
  • methodTwo()
  • void methodTwo()
  • Type object new Type()
  • object.methodInObject()

4
This week
  • Arrays
  • Objects
  • Encapsulation
  • Polymorphism
  • Inheritance
  • The Art of Programming 4 Efficiency
  • Next week and the practical

5
Arrays
  • Arrays are variables with multiple examples of
    the same kind of object in them.
  • For example, you might have a line array
    variable, with 10 different Point class objects
    in it.
  • As usual, we start by making a label available
  • Type arrayName or Type arrayName
  • E.g., Point line
  • This has made an array label.

6
  • Once our label exists, we can now attach it to an
    object. However, first we need to assign the
    right space for it
  • arrayName new Typesize
  • E.g., line new Point 10
  • This has now made 10 object labels in our array
    set to null. We can now attach them to objects.
    The spaces are numbered 0 to 9.
  • line0 point1
  • line9 new Point()
  • To use them, we just reference the space.
  • line3.setX(200)

7
Arrays cont.
  • The size of an array can be found by using its
    length variable, e.g.,
  • if (line.length 10)
  • Arrays can be declared and assigned at the same
    time (usually are, in fact)
  • Point line new Point 12
  • Or, we can actually fill them with existing
    objects
  • Point line home, pub
  • where home and pub are objects of the class
    Point.

8
Multi-dimensional arrays
  • You dont just have to have one dimensional
    arrays, you can have as many dimensions as you
    like.
  • A map of population density for example may be a
    2D array
  • int popMap new int100100
  • You can think of the array as a table, with the
    first size as the row numbers and the second as
    the columns.
  • You refer to the position as, for example,
  • arrayName101 (object at the 11th row, 2nd
    column)

9
  • Alternatively you can think of them as arrays of
    arrays.
  • int array2D new int 44
  • array2D0 00, 01, 02, 03
  • array2D1 10, 11, 12, 13
  • array2D2 20, 21, 22, 23
  • array2D3 30, 31, 32, 33

10
  • You dont actually have to define anything but
    the leftmost size initially.
  • int array2D new int 4
  • This means we can have different sizes in our
    second dimension
  • array2D0 new int2 00, 01
  • array2D1 new int4 10, 11, 12,
    13
  • array2D2 new int3 20, 21, 22
  • array2D3 new int1 30
  • We can have as many dimensions as we like.
  • int array4D new int100

11
Looping through 2D arrays
  • for (int i 0 i lt array.length i)
  • for (int j 0 j lt arrayi.length j)
  • arrayij new SomeClass()
  • Note Use of array as an array of arrays, each
    with a length.
  • j will never reach arrayi.length this is good
    as positions start at zero.
  • i increases by one each time j goes from zero to
    is length minus one.
  • Very important code.

i
j
12
Variations
  • Looping through the same positions in two arrays
  • for (int i 0 i lt arrayA.length i)
  • for (int j 0 j lt arrayAi.length j)
  • arrayAij arrayBij

i
j
arrayA
arrayB
13
Variations
  • Looping through two arrays at positions relative
    to one array (note boundary problem)
  • for (int i 0 i lt arrayA.length i)
  • for (int j 0 j lt arrayAi.length j)
  • if ((i gt 0) (j gt 0)) arrayAij
    arrayBi-1j-1

i
j
14
  • Arrays
  • Objects
  • Encapsulation
  • Polymorphism
  • Inheritance
  • The Art of Programming 4 Efficiency
  • Next week and the practical

15
Objects, a quick refresher.
  • Classes are photocopy originals for objects.
  • Usually we make an object from a class.
  • The object contains all the code in the class.
  • We can therefore call methods inside the object.
  • pointObject.setX(200)
  • We pass a method certain arguments.
  • Can use objects variables directly but isnt
    usual.
  • pointObject.attribute top of the morning

16
Special methods constructors
  • Special method for setting up objects.
  • First method in any class. If theyre not there
    (we havent made any so far), the compiler makes
    them.
  • public class Point
  • String attribute null
  • public Point ()
  • attribute empty space
  • Return an object of that type and have no name
    (or have no name, and implicitly return that
    type)
  • Called when we make a new object.
  • Point pointObject new Point ()

17
  • Arrays
  • Objects
  • Encapsulation
  • Polymorphism
  • Inheritance
  • The Art of Programming 4 Efficiency
  • Next week and the practical

18
The Philosophy of Object Orientation
  • Encapsulation.
  • Data and the methods working on them are
    contained.
  • You dont need to know how something is done,
    just what does it. Indeed, you shouldnt be able
    to find out.
  • Polymorphism.
  • You can appear to have one method that does many
    things. E.g., Math.sqrt() takes in both ints and
    doubles.
  • Inheritance.
  • Classes can inherit the methods and attributes
    of another Class, saving you re-implementing
    stuff.
  • Can develop hierarchies simple and complex
    versions.


19
  • Arrays
  • Objects
  • Encapsulation
  • Polymorphism
  • Inheritance
  • The Art of Programming 4 Efficiency
  • Next week and the practical

20
How does Java implement encapsulation?
  • Classes and objects wrap around methods and
    data.
  • Methods - act on data passed in, and return
    values, you dont need to know how they work.
  • Variables in other Classes largely called via
    methods.
  • Access control.
  • static control.

21
Encapsulation method access control
  • Access to blocks and variables can be controlled.
  • Default
  • Code can be seen only in same directory
    (package).
  • protected
  • Can only be seen by the same type of class or a
    class that inherits it.
  • private
  • Only accessed by the class its in.
  • public
  • Accessed by any other code.

22
Encapsulation access control
  • For example
  • public class example
  • private int x
  • public void setX (int newX)
  • x newX
  • We should always try to make class variables
    private to encourage encapsulation.
  • Now we can see why main has to be public.

23
Encapsulation variable access control
  • If variables are declare final they cant be
    altered.
  • Such variables are known as constants in other
    languages.
  • Conventionally in uppercase and underscores.
  • E.g.,
  • // 10 year average per kilo
  • final int PRICE_OF_GOLD 10100
  • final int PRICE_OF_SILVER 202
  • final int PRICE_OF_BEER 4

24
Encapsulation static
  • Usually an object must exist before you can use
    the methods or variables in the class.
  • Methods and variables can be declared static
    they can then be called before an object of this
    type exists,
  • ClassType.methodName() or ClassType.variable
  • e.g. Math.sqrt() e.g. System.out
  • Static variables are used directly. There is one
    copy that can be used anywhere. Change them in
    one object, and the variable changes in all.
  • Static methods can only use other static
    variables and methods.

25
main
  • public static void main (String args)
  • public access.
  • static.
  • Returns nothing.
  • Called main
  • Takes in a String array and calls it args
    i.e., you can start the program like this
  • gt java programName hi little program 10
  • args0 hi args1 little
  • args2 program args3 -10

26
Dealing with main
  • You can see that main must be static, because we
    dont make an object with it in the compiler
    just calls it.
  • Because main is static, it can only use static
    objects and methods.
  • i.e. only objects made inside it or declared
    static.
  • Because of this, it is usual to use a constructor
    to do everything.
  • Well see later that this is also useful for
    building applets.

27
Getting round main
  • class OurClass
  • OurClass()
  • // do everything here.
  • public static void main (String args)
  • OurClass ourObject new OurClass()
  • In fact, as were not going to use ourObject, we
    can just do
  • new OurClass()

28
  • Arrays
  • Objects
  • Encapsulation
  • Polymorphism
  • Inheritance
  • The Art of Programming 4 Efficiency
  • Next week and the practical

29
How does Java implement polymorphism?
  • Overloading.
  • You can call methods that do similar things the
    same name. The JVM will decide which is to be
    used on the basis of what is passed in.
  • E.g.,
  • Math.sqrt(int) Math.sqrt(float)
  • Math.sqrt(double)
  • All called using Math.sqrt(number)
  • Note that if a method doesnt exist, the JVM will
    try an invisible cast. If this fails youll get
    an error.

30
Polymorphism constructors
  • The constructor can be overloaded (and typically
    is) to set different variables.
  • class Point
  • int x int y
  • Point ()
  • Point (int startX, int startY)
  • x startX
  • y startY
  • Called thus
  • Point point1 new Point(23, 42)

31
  • Arrays
  • Objects
  • Encapsulation
  • Polymorphism
  • Inheritance
  • The Art of Programming 4 Efficiency
  • Next week and the practical

32
How does Java implement inheritance?
  • Class inheritance with extends.
  • Creating a class that inherits another.
  • Abstract classes.
  • Creating a class that has bits that need filling
    in inheriting classes
  • Interfaces.
  • Creating classes that are guaranteed to fulfill
    some criteria when inherited.

33
Inheritance extends
  • Classes can be inherited inheriting classes
    have all the public variables and methods of the
    inherited class.
  • E.g., We may want to make a class called Land
    and Sea from our Point class, each with the
    same methods as Point, but with their own
    variables and methods as well.

34
Inheritance extends
  • class Land extends Point
  • String owner Bob
  • x 23
  • setY(42)
  • attribute Circle of sycamore trees
  • Only need to define new variables and methods.
  • The parent class is called the Superclass, the
    inheritor is called the Subclass.

35
Inheritance extends
  • Subclasses cant access the private variables of
    Superclasses. Therefore, one way to set these is
    to use the Superclasss constructor.
  • class Land extends Point
  • Land ()
  • super(23,42)
  • The call to super must be the first thing in the
    Subclasses constructor.

36
Inheritance overriding
  • What happens if you call a method in the Subclass
    the same thing as a method in the Superclass?
  • If they have different arguments it doesnt
    matter the JVM will call the one that works
    with them.
  • If the arguments are the same the Subclass will
    be used. The method is overridden.
  • We can also use super. to refer to the
    Superclass methods and variables in the same way
    as we use this. to refer to the present class.
  • If we declare the Superclass methods as final
    they cant be overridden by the Subclass. final
    Classes cant be inherited.

37
Inheritance Abstract Classes
  • Classes that can only be inherited cant make
    objects from them.
  • Include methods and variables.
  • Any Class that extends it must provide all the
    missing methods, or be declared abstract itself.

38
  • abstract class Eastender
  • void cheer ()
  • System.out.println(Lets have a good old
    knees-up)
  • abstract void misery ()
  • class Beale extends Eastender
  • void misery ()
  • System.out.println (Pass us the bottle, Pat)
  • Beale Objects can use the cheer method
    without defining it, but they must define
    misery because all Eastender inheritors must
    have misery.

39
Inheritance interfaces
  • Interfaces are the ultimate abstract class.
  • They are lists of methods that must be defined in
    classes that implement them. All variables in
    them are final.
  • Why the devil would you want to do that?
  • You can make classes that rely on finding certain
    methods wherever they are used. By forcing people
    to implement these methods you can guarantee they
    exist.

40
  • public interface Eastender
  • void cheer ()
  • final static String STAIRS ApplesNPears
  • final static String PIANO Joanna
  • class Mitchell implements Eastender
  • void cheer ()
  • System.out.println(ear, get round that
    Eastender.PIANO)

41
  • Arrays
  • Objects
  • Encapsulation
  • Polymorphism
  • Inheritance
  • The Art of Programming 4 Efficiency
  • Next week and the practical

42
The Art of Programming efficiency
  • Trying to speed up the program.
  • Knowing how the JVM and compiler work and making
    sure you program in the way that will make it
    fastest.
  • For example, using switch rather than if / else
    if / else ladders, as switches only need one
    decision to be made.
  • Hard to teach because it relies on understanding
    how computers work, not how to program them.
  • Hints on the sheet.

43
Summary
  • Arrays are groups of similar Objects.
  • Type arrayName new Objectsize
  • int arrayName new intsize
  • arrayName new Object() arrayName 6
  • Arrays have a length variable set by the JVM.
  • Object Orientation is based around the concepts
    of Encapsulation, Polymorphism and Inheritance.
  • Java Encapsulation
  • Classes enclose data and methods.
  • Access controls.
  • Java Polymorphism
  • Methods used depends on the arguments.

44
Summary
  • Java inheritance
  • extends, abstract, interface
  • protected, private and public control access.
  • Static
  • Static Methods and variables can be called
    without an Object, e.g. Math.abs()
  • Static variables are the same variable whichever
    other Class youre in.
  • Final
  • Makes variables UNCHANGEABLE_CONSTANTS.
  • Final methods cant be overridden.
  • Final Classes cant be inherited.

45
Next week
  • Packages and errors.
  • Practical
  • Bioagent Bombing.
Write a Comment
User Comments (0)
About PowerShow.com