CS242 Advanced Programming Concepts in Java - PowerPoint PPT Presentation

1 / 51
About This Presentation
Title:

CS242 Advanced Programming Concepts in Java

Description:

Mutable vs Immutable Objects. Circle c1 = new Circle(1.5f, 0,0); The circle is 'mutable', that is, it can be changed (for example, with setRadius) ... – PowerPoint PPT presentation

Number of Views:61
Avg rating:3.0/5.0
Slides: 52
Provided by: janicets
Category:

less

Transcript and Presenter's Notes

Title: CS242 Advanced Programming Concepts in Java


1
CS242Advanced Programming Concepts in Java
  • 9/04/07
  • Java Basics

Prof. Searleman jets_at_clarkson.edu
2
Outline
  • Eclipse do the basic tutorial in the Java
    Development User Guide (HelpWelcome)
  • Java Basics
  • Primitives vs. Objects
  • this vs. this() overloading constructors
  • String immutable vs. mutable objects
  • OOP concepts
  • Composition, Inheritance, Use
  • Methods common to all classes
  • toString()
  • Read OODP, Ch. 2 Effective Java, Ch. 3
  • HW1 posted due Thursday, 9/6/07, in class

3
Primitive typesnumbers literals
  • Primitive types wrapper classes
  • Literals
  • important point the distinction between a
    primitive and an Object

4
float is not Float
  • Consider
  • float x
  • Float y
  • What is the difference between x y?

5
Primitives Wrapper Classes
  • float x
  • Float y
  • x 2.7f // ok
  • y 2.7f // ERROR, why?
  • y is a reference (currently NULL)
  • y can refer to a Float object
  • There are 3 constructors

6
Wrapper class
  • float x
  • Float y
  • y new Float(2.7f) // OK
  • y new Float(2.7) // OK
  • y new Float(2.7) // OK
  • A wrapper class provides conversion
    functionality useful in data structures, sorting
    objects, etc.

7
float is not Float
  • float x 2.7f
  • Float y new Float(2.7)
  • What is the difference between x y?
  • x is a primitive cant send a message to x
  • y refers to an object responds to any message
    appropriate for Float

8
Circle class Point class
  • files Circle.java
  • Point.java
  • 1 approach for testing include a main program
    in each class
  • another approach write a separate test program
  • TestCircle.java

9
  • public class Circle // This class is not
    completely formulated yet
  • / private implementation (instance
    variables) /
  • private float radius
  • private Point center
  • / constructor given radius and 2 ints/
  • public Circle(float aRadius, int x, int y) /
    implementation /
  • / accessor method /
  • public float getRadius() return radius
  • / mutator method /
  • public void setRadius(float newRadius) radius
    newRadius
  • / accessor method /
  • public Point getCenter() return center
  • / method which computes and returns the area
    of the circle /
  • public float computeArea() / implementation
    /

10
Circle class, revisited
  • public class Circle
  • private float radius
  • private Point center
  • / constructs a circle with given radius
    center /
  • public Circle(float radius, int x, int y)
  • this.radius radius
  • this.center new Point(x, y)

11
Relationships between classes
public class Circle private float
radius private Point center
A Circle has a Point
UML Class Diagram
FilledCircle
12
Relationships between classes
Object
A Circle is an Object
(inheritance)
IS-A
public class Circle / stuff / (everything
is an Object, except for a primitive value)
Circle
UML Class Diagram
13
Overloading methods
  • multiple versions of a method, distinguished by
    their signatures (name argument list)
  • it is very common to overload constructors
  • example
  • public Circle(int x, int y) // creates unit
    circle
  • public Circle(float radius, int x, inty)

14
Uses of this
  • this reference to current object
  • this.radius
  • this() way to call another
    constructor in the same class

15
Overloading constructors version 1
  • public Circle(float radius, int x, int y)
  • this.radius radius
  • this.center new Point(x,y)
  • public Circle(int x, int y)
  • this(1.0f, x, y) / reuses code /

advantage can change the implementation without
changing all the other constructors
16
Overloading constructors version 2
  • public Circle(int x, int y)
  • this.radius 1.0f
  • this.center new Point(x,y)
  • public Circle(float radius, int x, int y)
  • this(x, y)
  • this.radius radius

17
this vs. this()
  • this.
  • this(

18
String
  • NOT an array of char
  • immutable (i.e. cannot be changed)
  • string literal enclosed in double quotes
  • methods length(), equals(), toUpperCase(),
    compareTo(), indexOf(), substring(), toString(),
    trim(), replace(),
  • use the StringBuffer class if you want to modify
    characters in the buffer

19
  • String myName Fred
  • String yourName Wilma
  • String s Hello, yourName
  • s myName
  • s s.trim()
  • Effective Java, Chapter 2, p. 13
  • Item 4 avoid creating duplicate objects
  • String s new String(silly) // bad idea
  • String s silly // much better

20
Mutable vs Immutable Objects
  • Circle c1 new Circle(1.5f, 0,0)
  • The circle is mutable, that is, it can be
    changed (for example, with setRadius)
  • String s1 Hello
  • The string cannot be changed (can change the
    reference s1, but not the string)
  • s1 s1.substring(3,5) // s1 refers to a new
    String
  • StringBuffer sBuf1 Hello
  • String buffers can be changed
  • sBuf1 sBuf1.setCharAt(0, j) // now jello

21
String operations
  • Parsing strings is an important operation that we
    will look at later (StringTokenizer)
  • Converting to a string is another important
    operation

22
Converting to a String toString() method
  • System.out.println( Answer 42 )
  • So the int value 42 is converted to a string
  • with the method Integer.toString()

String
23
Methods common to all objects
  • cf. Effective Java, Chapter 3
  • superclass java.lang.Object
  • methods toString(), equals(), hashcode(),
    finalize(), clone(), getClass(), wait(),
    notify(), notifyAll()

24
toString() method
  • c1 new Circle(1.5f, 0, 0)
  • System.out.println( c1 )

Prints Circle_at_1afa3 !!
25
Item 9 Always override toString()
  • Effective Java, Chapter 3, p. 42
  • When practical, the toString method should
    return all of the interesting info contained in
    the object.
  • Note that toString should never print anything

26
  • public class Circle
  • // other stuff
  • public String toString() // 1st version
  • return \n \t center
  • center.getX() ,
  • center.getY()
  • \n \t radius radius

27
  • public class Circle
  • public String toString()
  • return \t center center
  • \n \t radius radius
  • // alternative implementation
  • // other methods

28
  • Circle c1 new Circle(1.5f, 10, 20)
  • System.out.println( c1 )

prints center lt10,20gt radius 1.5
29
toString() called automatically
  • System.out.println( Answer 42 )
  • System.out.println( d1 )
  • System.out.println( d1.topFace() )
  • System.out.println( d1.toString() )

30
Overriding toString()
  • It is recommended that you specify the format of
    the return value for classes associated with a
    value, and to document your intentions.
  • examples
  • phone number format (XXX) YYY-ZZZZ
  • social-security number XXX-XX-XXXX

31
Methods common to all objects
  • cf. Effective Java, Chapter 3
  • superclass java.lang.Object
  • methods toString(), equals(), hashcode(),
    finalize(), clone(), getClass(), wait(),
    notify(), notifyAll()
  • Item 9 Always override toString()

32
is not equals()
  • tests for equivalence (exactly same object)
  • compare primitives using and !
  • equals() tests whether 2 objects are equal in
    value
  • compare objects using equals(), unless
  • you intend to compare for equivalence

33
  • Integer n1 new Integer(42)
  • Integer n2 new Integer(42)
  • System.out.println( n1 n2 )
  • prints false
  • System.out.println( n1.equals(n2) )
  • prints true
  • n2 n1
  • System.out.println( n1 n2 )
  • now prints true

34
Comparing things
  • ! means not equivalent
  • compare primitives using and !
  • compare objects using equals()
  • This is especially true when comparing two
    strings!
  • unless, of course, you want to test for
    equivalence

35
  • cat cat true
  • String s1 cat
  • String s2 c
  • String s3 s2 at
  • s1 s3 false
  • s1.equals(s3) true

36
OOP
  • composition has-a relationship
  • inheritance is-a relationship
  • overloading methods
  • overriding methods
  • polymorphism many-shaped

37
Polymorphism
  • x1.draw()
  • x2.draw()
  • x3.draw()
  • what happens depends on what type of object x is

Circle Square FilledCircle
38
Relationships between classes
public class Circle / stuff / public class
FilledCircle extends Circle
Circle
(inheritance)
IS-A
A FilledCircle is a Circle
FilledCircle
UML Class Diagram
39
  • //// file Circle.java
  • public class Circle
  • private float radius
  • private Point center
  • / constructors, getRadius(), setRadius(),
  • getCenter(), computeArea() /
  • / want to create a subclass of Circle, namely
    FilledCircle /

40
Recall Uses of this
  • this reference to current object
  • this.radius
  • this() way to call another
    constructor in the same class

41
Uses of super
  • super reference to the superclass
  • super.radius // caution inaccessible
  • super() way to call a constructor for the
    superclass
  • public FilledCircle(float radius, int x, int y)
  • super(radius, x, y)

42
  • //// file FilledCircle.java
  • import java.awt.Color
  • public class FilledCircle extends Circle
  • private Color fillColor Color.black
  • public FilledColor(float radius, int x, int y)
  • super(radius, x, y)
  • public FilledColor(
  • float radius, int x, int y, Color
    newFillColor)
  • super(radius, x, y)
  • fillColor newFillColor

43
UML class diagram
IS-A
IS-A
1
HAS-A
(composition)
IS-A
(inheritance)
44
Inheritance
  • A subclass inherits all instance variables and
    all methods from its superclasses
  • e.g. FilledCircle inherits radius and center from
    Circle along with the methods getRadius(),
    setRadius(), computeArea(), etc.

45
Packages
  • default package
  • access modifiers
  • - public
  • - private
  • - protected
  • - none (defaults to package access)

46
  • So FilledCircle cant directly manipulate radius
    or center (only Circle can)
  • public everyone can use it
  • private only within the class
  • protected subclasses have access
  • package everyone in same package

47
  • Overloading
  • multiple versions of a method, distinguished by
    their signatures
  • (name and argument list)
  • Overriding
  • when a subclass completely replaces a method that
    it inherits from its superclass (same signature)

48
IS-A
IS-A
HAS-A
1
IS-A
49
methods toString(), equals(), getClass(),
methods constructors, get/setCenter(),
get/setRadius(), computeArea() toString()
methods constructors, get/setColor() toString()
50
  • public class FilledCircle
  • extends Circle
  • private Color fillColor Color.black
  • public String toString()
  • return super.toString()
  • \n \t color fillColor
  • // other methods for the class

51
  • Circle c1 new Circle(1.5f, 10, 20)
  • FilledCircle fc1
  • new FilledCircle(2.0f, 1, 1, Color.cyan)
  • System.out.println( c1 )
  • System.out.println( fc1 )

center lt10,20gt radius 1.5
center lt1,1gt radius 2.0 color
java.awt.Colorr0,g255,b255
Write a Comment
User Comments (0)
About PowerShow.com