Lecture structure - PowerPoint PPT Presentation

1 / 41
About This Presentation
Title:

Lecture structure

Description:

These braces enclose the code for the method putHatOn() These braces enclose the class definition ... Top level class enclosing class ... – PowerPoint PPT presentation

Number of Views:25
Avg rating:3.0/5.0
Slides: 42
Provided by: Zab1
Category:

less

Transcript and Presenter's Notes

Title: Lecture structure


1
Lecture 7
2
Lecture structure
  • Project 2
  • Objects and classes
  • Nested Classes

3
Extend line to infinity
y
? angle
tolerance
0,0
x
4
Number of lines with ? in the interval (1,18)
deg Histogram
? i
5
10
10 intervals
0 18 36
180
Histogram should be stored in the public array
distribution i.e public double distribution
new double HistogramSize
5
Information about the segments Number of
segments Number of lines passing through
0 Number of intersecting segments Max
length . Min length . Max angle
. Min angle . Angle (in deg)
distribution 0.0 , 18.0 . 18.0 , 36.0
. 36.0 , 54.0 . 54.0 , 72.0
. .. ..
Divide each count by the total number of entered
segments (angles)
6
How many pairs of segments intersect ?
Intersecting segments
Not-intersecting
7
Three Classes
  • Class Point
  • Class LineSegment
  • Class Project 2
  • Templates you need to complete
  • Follow instructions exactly on template and
    project sheet

8
  • Where do I start?
  • Start Point Class
  • Line segment

9
READDATA.java
  • A sample code which demonstrates how to read data
    from an ASCII file using the methods of the class
    MaInput.
  • The code demonstrates the procedure you should
    use to read data from the file Project2.data

10
Java programs consist of files
Project2.java point.java linesegment.java
Class Project2
Class point
Class linesegment
The complete program consists of 3 files
Need to be in same directory
11
Revision
  • What defines a class ? You someone else may
    define a class differently according to larger or
    smaller set of parameters they want as class
    definition e.g
  • Class car 4 wheels , steering wheel
  • myCar an instance of a class is a technical
    term for an existing object of that class
  • So myCar is an instance of car class
  • Once you have a class defined you can come up
    with objects or instances of that class
  • the operations you can perform on an instance of
    a class are those defined within a class, so the
    usefulness and flexibility of a class depends on
    how you defined the class so think of the
    operations you want to do
  • Lets consider a hat class

12
  • class Cowboy
  • private String owner // Name of current owner
  • private int size // Stores the hat size
  • private boolean hatOnfalse // Records whether
    a hat is on or off
  • //Constructor to create a CowboyHat object
  • public CowboyHat(String person, int theSize)
  • size theSize //Set the hat size
  • owner person // Set the hat owner
  • //Method to put the hat on
  • public void putHatOn()
  • hatOn true record hat status as on
  • //Method to take the hat off
  • public void takeHatOff()
  • hatOn false //record hat status as off

These specify the attributes for the class
These braces enclose the class definition
A special method that creates Hat objects
These braces enclose the code for the method
putHatOn()
These are the other class methods

13
  • class Cowboy
  • Private String owner // Name of current owner
  • Private int size // Stores the hat size
  • Private boolean hatOnfalse // Records whether
    a hat is on or off
  • //Constructor to create a CowboyHat object
  • Public CowboyHat(String person, int theSize)
  • size theSize //Set the hat size
  • owner person // Set the hat owner
  • //Method to put the hat on
  • Public void putHatOn()
  • hatOn true record hat status as on
  • //Method to take the hat off
  • Public void takeHatOff()

Class has 3 instance variables, owner, size,
hatOn and last variable is always initialised to
false
Each object that is created according to this
class will have its own independent copy of these
variables, So each object will have its own
unique values for the owner, the hat size and
whether the hat is on or off
Fred 7 false
Jane 5 false
Mark 2 false

14
  • class Cowboy
  • private String owner // Name of current owner
  • private int size // Stores the hat size
  • private boolean hatOnfalse // Records whether
    a hat is on or off
  • //Constructor to create a CowboyHat object
  • Public CowboyHat(String person, int theSize)
  • size theSize //Set the hat size
  • owner person // Set the hat owner
  • //Method to put the hat on
  • Public void putHatOn()
  • hatOn true record hat status as on
  • //Method to take the hat off
  • Public void takeHatOff()
  • Encapsulation
  • Refers to hiding if items of data and methods
    within an object
  • achieved by private the instance variables
    within the Cowboy hat are all encapsulated .
  • They are accessible only through the methods
    defined by the class
  • therefore the only way to alter the values they
    contain is to call a method that does that
  • this is important for the security and integrity
    of the objects


15
Class variables or Static fields Shared between
all objects Sphere.PI
Class Sphere Definition --------------------------
------ Public class Sphere // class
variable Static double PI3.14 //instance
variables double xCenter double yCenter double
zCenter double radius
globe xCenter yCenter zCenter radius
3.14
Sphere Objects Object is an instance or an
occurrence of a class
Each object gets its own copy of instance
variables
  • Ball
  • xCenter
  • yCenter
  • aCenter
  • radius

16
  • class Sphere
  • static final double PI 3.14 // Class
    variable static - a fixed value
  • static int count 0 // Class
    variable to count objects
  • All objects of Sphere class will have access to
    and share the one copy of count variable and one
    copy of PI
  • // Instance variables
  • Each object of the class will have its own
    separate set of these variables
  • double radius // Radius of a
    sphere
  • double xCenter // 3D
    coordinates
  • double yCenter // of the center
  • double zCenter // of a sphere

17
  • class Sphere
  • static final double PI 3.14 // Class
    variable that has a fixed value
  • static int count 0 // Class
    variable to count objects
  • // Instance variables
  • double radius // Radius of a
    sphere
  • double xCenter // 3D
    coordinates
  • double yCenter // of the center
  • double zCenter // of a sphere
  • // Class constructor
  • Sphere(double theRadius, double x, double y,
    double z)
  • radius theRadius // Set the radius
  • // Set the coordinates of the center
  • xCenter x
  • yCenter y
  • zCenter z

Class methods have static keyword Returns the
value in static variable count You cannot
directly refer to any of the instance variables
in the class within a static method. Because a
static method can be executed when no objects of
the class have been created And therefore no
instance variables exist
18
this
  • Void changeRadius(double radius)
  • // change the instance variable to the argument
    value
  • this.radius radius
  • this,radius refers to instance variable
  • radius by itself refers to parameter

19
constructors
  • Constructor never returns a value , do not
    specify return type not even void
  • A constructor always has the same name as the
    class

20
  • class Cowboy
  • private String owner // Name of current owner
  • private int size // Stores the hat size
  • private boolean hatOnfalse // Records whether
    a hat is on or off
  • //Constructor to create a CowboyHat object
  • public CowboyHat(String person, int theSize)
  • size theSize //Set the hat size
  • owner person // Set the hat owner

These specify the attributes for the class
These braces enclose the class definition
A special method that creates Hat objects
These braces enclose the code for the method
putHatOn()
These are the other class methods

21
Creating objects of a class
  • Sphere ball declare a variable
  • No constructor is called because no object is
    created
  • To create an object of class you must use keyword
    new
  • ball new Sphere(10.0, 1.0, 1.0, 1.0) //
    create a sphere
  • Followed by a call to constructor

22
How arguments are passed to a Methodprimitive
type
  • Argument values are passed by pass-by-value
  • For each argument value that is passed to a
    method a copy of the value is made it is the
    copy that is passed to the method and referenced
    through the parameter name, not original name
  • You can modify the value passed as much as you
    like in the method but it wont affect the
    original value
  • Thus the method cannot modify the value of the
    parameter in the calling program

23
(No Transcript)
24
Passing objects to a Method
  • Pass-by-reference
  • When an object is passed as an argument to a
    method
  • A copy of the reference contained in the variable
    is transferred to the method, not a copy of the
    object itself
  • You can actually change the object itself
  • Because a variable of class type contains a
    reference to an object, not the object itself
  • As a copy of the reference still refers to the
    same object, the parameter name used in the body
    of a method will refer to the original object
    that was passed as the argument

25
(No Transcript)
26
Method Overloading
  • Is - Java allows you to define several methods
    in a class with the same name.
  • Each method needs - a unique set of parameters
  • Useful in many circumstances
  • e,.g the class Math contains 2 versions of
    round() , one accepts arguments for float other
    accepts arguments for double

27
Multiple Constructors
Multiple constructors are methods that can be
overloaded - just like any other method in
class You may want to generate objects for a
class from different sets of initial defining
data
Calling a constructor from a constructor this is
used by one constructor to explicitly invoke
another constructor in its same class Notice
first two constructors use this keyword to
explicitly invoke the third constructor
  • Class circle
  • double x
  • double y
  • double radius
  • Circle(double x)
  • this (x,0,1)
  • Circle(double x, double y)
  • this (x,y,1)
  • Circle (double x, double y, double radius)
    class member parameter
  • this.x x this.x x
  • this.yy
  • this.radius radius

28
Nested classes
  • All the classes you have defined so far have been
    separate from each other each stored away in
    its own source file.
  • You can have nested classes
  • Put the definition of one class inside the
    definition of another class
  • Inside class is called a nested class
  • Even a nested class can have another class nested
    inside it if needed

29
  • Implementing nested classes
  • Java lets you define a class as a member of
    another class. Such a class is called a nested
    class and is illustrated here
  • Codeclass EnclosingClass     . . .    class
    ANestedClass         . . .            A
    nested class is a class that is a member of
    another class.
  • nested classes reflect and enforce the
    relationship between two classes.
  • When would you use a nested class?
  • You should define a class within another class
    when
  • the nested class makes sense only in the context
    of its enclosing class
  • or when it relies on the enclosing class for its
    function. For example, a text cursor makes sense
    only in the context of a particular text
    component.

30
  • As a member of its enclosing class, a nested
    class has a special privilege
  • It has unlimited access to its enclosing class's
    members, even if they are declared private.
  • However, this special privilege isn't really
    special at all.
  • It is fully consistent with the meaning of
    private and the other access specifiers.
  • The access specifiers restrict access to members
    for classes outside of the enclosing class.
  • The nested class is inside of its enclosing class
    so that it has access to its enclosing class's
    members.

31
  • Like other members, a nested class can be
    declared static (or not). A static nested class
    is called just that a static nested class. A
    non-static nested class is called an inner class.
  • These are illustrated in the following code
  • Codeclass EnclosingClass     . . .    static
    class AStaticNestedClass         . . .     
      class InnerClass         . . .   

32
  • As with static methods and variables (normally
    called class methods and variables), a static
    nested class is associated with its enclosing
    class.
  • And like class methods, a static nested class
    cannot refer directly to instance variables or
    methods defined in its enclosing class-it can use
    them only through an object reference.
  • As with instance methods and variables, an inner
    class is associated with an instance of its
    enclosing class and has direct access to that
    object's instance variables and methods.
  • Also, because an inner class is associated with
    an instance, it cannot define any static members
    itself.

33
  • To help differentiate the terms nested class and
    inner class further
  • Think about them in the following way
  • The term "nested class" reflects the syntactic
    relationship between two classes
  • that is, syntactically, the code for one class
    appears within the code of another. In contrast,
    the term "inner class" reflects the relationship
    between instances of the two classes. Consider
    the following classes
  • Codeclass EnclosingClass     . . .    class
    InnerClass         . . .   

34
  • The interesting feature about the relationship
    between these two classes is not that InnerClass
    is syntactically defined within EnclosingClass.
  • Rather, it's that an instance of InnerClass can
    exist only within an instance of EnclosingClass
    and that it has direct access to instance
    variables and methods of its enclosing instance.
    The following diagram illustrates this idea.
    Instance of
  • Enclosing class instance of inner class

35
?
  • An inner class is a nested class whose instance
    exists within an instance of its enclosing class
    and has direct access to the instance members of
    its enclosing instance.
  • Other facts about nested classes
  • Also, the access specifiers -- private, public,
    protected, -- may be used to restrict access to
    nested classes just as they do to other class
    members.
  • Any nested class, can be declared in any block of
    code. A nested class declared within a method or
    other smaller block of code has access to any
    final, local variables in scope.

36
Nested classes
  • Public class Outside
  • //Nested class
  • Public class Inside
  • // Details of inside class..
  • //More members of Outside class

37
Nested classes
  • Inside class is declared public member of Outside
    , so it is accessible from outside class
  • Top level class enclosing class
  • In this example the inner class is not declared
    as a static member of the class outside
  • Until an object of the outside class is created
    you cant create any inside objects
  • However if you declare an object of the outside
    class it does not necessarily mean objects of the
    nested class are created.

38
Nested classes
  • To create an object
  • Outside outer new Outside()
  • No objects of the nested class inside are created
  • To create an object of nested class
  • Outside.Inside inner outer.new Inside()
  • // define a nested class object

39
Nested class
  • To make objects of a nested class type
    independent of objects of the enclosing class
    type, declare the nested class as static
  • Public class outside outer class
  • Public static class Skinside nested class
  • //Details of Skinside
  • Nested class
  • Public class Inside
  • //details of inside class
  • //More members of Outside class..

40
Nested classes
  • Now with skinside inside outside declared as
    static objects of the nested class can be
    declared independently from any objects of type
    outside.
  • Regardless of whether you have created any
    Outside objects or not.

41
  • Class Outside
  • static members

A non-static nested class can access any members
of the top-level class. A non-static nested
class can access static members of any static
nested classes within the same top level class
Static class skinside Static
members non-static members Class
Inside non-static members
non-static members
Members of a static nested class can access
static members of the top-level class
A non-static nested class cannot have static
members
Write a Comment
User Comments (0)
About PowerShow.com