Java Review 3 - PowerPoint PPT Presentation

1 / 23
About This Presentation
Title:

Java Review 3

Description:

A Sleeper interface that classes wishing to use the service must implement. ... { new AlarmThread(sleeper, time).start(); The AlarmThread class. class AlarmThread ... – PowerPoint PPT presentation

Number of Views:20
Avg rating:3.0/5.0
Slides: 24
Provided by: csi59
Category:
Tags: java | review | sleeper

less

Transcript and Presenter's Notes

Title: Java Review 3


1
Java Review 3
  • Rem Collier

2
Java Wrapper Classes
  • In Java, the term wrapper class commonly refers
    to a set of Java classes that objectify the
    primitive Java types.
  • That is, for each primitive type, there is a
    corresponding Java Wrapper class that
    represents that type.
  • e.g. the wrapper for the int type is the Integer
    class.
  • Wrapper Classes are based upon the well-known
    software engineering design pattern called the
    Wrapper pattern.
  • A design pattern is a template solution to a
    common problem. It describes the problem and
    identifies the recommended solution(s) to that
    problem.
  • Because design patterns deal with common
    problems, they are often quite abstract!

3
Wrapper Design Pattern
  • Some code (the client) needs a class to use a
    certain interface (which it does not use).
  • The problem
  • e.g. we want to store an int in a Vector, but
    Vectors do not accept primitive types.
  • The Solution
  • We create another class that wraps the underlying
    class/type and provides an appropriate interface
    for the client.
  • e.g. we create an Integer class that subclasses
    Object (as all classes do), allowing us to store
    wrapped ints in Vectors.

4
Example that will not work
  • import java.util.Vector
  • public class MyApp
  • public static void main(String args)
  • int myValue 2
  • Vector myVector new Vector()
  • myVector.addElement(myValue)
  • for (int x0 x lt myVector.size() x)
  • System.out.println(Value
    myVector.get(x))

The compiler detects an error here
5
Example that will work
  • import java.util.Vector
  • public class MyApp
  • public static void main(String args)
  • int myValue 2
  • Vector myVector new Vector()
  • myVector.addElement(new Integer(myValue))
  • for (int x0 x lt myVector.size() x)
  • System.out.println(Value
    myVector.get(x))

Here we wrap the int in the Integer class
6
Java Wrapper Classes
Object
Boolean
Character
Void
Number
String
Short
Double
Integer
Long
Float
Byte
7
Some Useful Methods
  • The Java Wrapper Classes include various useful
    methods
  • Getting a value from a Stringe.g. int value
    Integer.parseInt(33)sets value to 33.
  • Converting a value to a Stringe.g. String str
    Double.toString(33.34)sets str to the String
    33.34.
  • Getting a wrapper class instance for a
    valuee.g. Integer obj Integer.getInteger(12
    )creates a new Integer object with value 12
    and makes obj refer to that object.

8
Some Useful Methods
  • Getting the maximum permitted int value e.g.
    int value Integer.MAX_VALUEsets value to
    2147483647.
  • Getting the minimum permitted int value e.g.
    int value Integer.MIN_VALUEsets value to
    -2147483647.
  • Getting the value in an Integer objecte.g. int
    value new Integer(545).intValue()sets value
    to 545.

9
Reading a Double
  • import java.io.
  • class MyProgram
  • public static void main(String args)
  • BufferedReader in new BufferedReader(
  • new InputStreamReader(System.in))
  • String line null
  • System.out.println("Input Something" )
  • try
  • line in.readLine()
  • catch (IOException ie)
  • System.out.println("Exception caught " ie)
  • try
  • double value Double.parseDouble(line)
  • System.out.println("Value " value)
  • catch (NumberFormatException nfe)

10
Interfaces in Java
  • In Object-Oriented terms, the interface of a
    class is the set of public methods and data
    members of that class.
  • These methods and data members describe how
    external code (I.e. other classes) can interact
    with the class.
  • Once a class is made publicly available for use,
    great care is required when changing that classes
    interface.
  • E.g. consider how annoyed you would be if Sun
    changed the interface of the System class from
    System.out to System.cout
  • Java supports the definition of interfaces
    independent of implementation of that interface.
  • A Java interface is a named collection of method
    definitions (without implementation). An
    interface can also include constant declarations.
  • In Java, we define an interface using the
    interface keyword.

11
Example Interface
  • Let us consider a general security service for
    web applications.
  • We dont always know how we wish to validate
    users, so we cannot implement a general user
    validation algorithm.
  • So, we create an interface that describes what a
    security service should do, and leave it to the
    developer of a specific application to implement
    a particular security service.
  • interface SecurityService
  • public boolean validateUser(String uid, String
    pwd)
  • public static final String ADMIN_USERNAME
    Admin

12
Interfaces in Java
  • The definition of the methods described in a Java
    interface is the responsibility of any class that
    implements that interface.
  • If a class implements an interface, then it must
    include definitions for all of the methods
    described in that interface.
  • If you do not include definitions for all the
    methods, the class will not compile!
  • A powerful feature of Java interfaces is that
    they can be implemented in more than one class!
  • For example, our SecurityService interface could
    be implemented as
  • A DatabaseSecurityService, which looks validates
    the username and password against a database
    table, or
  • A SimpleSecurityService, which validates the
    username and password against hardcoded values
    for the username and password.

13
Interface Example
  • interface SecurityService
  • public boolean validateUser(String uid,
    String pwd)
  • public static final String ADMIN_USERNAME
    Admin
  • class SimpleSecurityService implements
    SecurityService
  • public boolean validateUser(String uid,
    String pwd)
  • if (uid.equals(ADMIN_USERNAME)
  • pwd.equals(god))
  • return true
  • return false

14
Using Interfaces Implementations
  • Java interfaces can be used as types.
  • For example SecurityService service
  • Variables or attributes whose type is an
    interface can be used to reference any class the
    implements that interface.
  • For exampleSecurityService service new
    SimpleSecurityService()
  • When an object is cast as an interface, only
    those methods declared in the interface may be
    accessed.
  • For exampleboolean valid service.validateUser
    (rem, hi)

15
A simple login program
  • import java.io.
  • class MyProgram
  • public static String readUserInput()
  • BufferedReader in new BufferedReader(
  • new InputStreamReader(System.
    in))
  • try
  • return in.readLine()
  • catch (IOException ie)
  • System.out.println("Exception caught "
    ie)
  • return null

16
A simple login program
  • public static void main(String args)
  • System.out.print(Username)
  • String username readUserInput()
  • System.out.print(Password)
  • String password readUserInput()
  • SecurityService service new
    SimpleSecurityService()
  • if (service.validateUser(username,
    password))
  • System.out.println(Valid User)
  • else
  • System.out.println(Invalid User)

17
Some Comments
  • Another way in which we could have coded this
    example is through inheritance and abstract
    classes.
  • class SecurityService
  • public abstract boolean validateUser(String uid,
    String pwd)
  • public static final String ADMIN_USERNAME
    Admin
  • class SimpleSecurityService extends
    SecurityService
  • public boolean validateUser(String uid, String
    pwd)
  • if (uid.equals(ADMIN_USERNAME)
    pwd.equals(god))
  • return true
  • return false

18
So, what is the need for interfaces?
  • It is not always possible for us to use
    inheritance to deliver the required interface.
  • Remember Java supports only single inheritance.
    That is, a class has exactly one parent class.
  • Whilst a class can have only one parent class, it
    can implement many interfaces.
  • A class includes both private and public methods
    and attributes.
  • Java interfaces only define what methods must be
    implemented, not how to implement them!
  • So, we can write classes that subclass some
    parent class and implement one or more
    interfaces!
  • But when is this useful?

19
Another Example
  • Lets consider an alarm clock service.
  • This service notifies objects after a certain
    amount of time has passed.
  • It is implemented through two components
  • An AlarmClock class that implements the service,
    and
  • A Sleeper interface that classes wishing to use
    the service must implement.
  • Whenever an object wishes to sleep, it contacts
    an alarm clock instance requesting that it be
    allowed to sleep for a specified time.
  • Once that specified time has passed, the alarm
    clock tells the sleeping object to wake up.

20
The AlarmClock class
  • import java.util.ArrayList
  • class AlarmClock
  • public static void letMeSleepFor(Sleeper
    sleeper, long time)
  • new AlarmThread(sleeper, time).start()

21
The AlarmThread class
  • class AlarmThread extends Thread
  • private Sleeper sleeper
  • private long time
  • public AlarmThread(Sleeper sleeper, long
    time)
  • this.sleeper sleeper
  • this.time time
  • public void run()
  • try
  • sleep(time)
  • catch (Exception e)
  • System.out.println(Failed to
    sleep)
  • sleeper.wakeUp()

22
The Sleeper interface
  • interface Sleeper
  • public static final long ONE_MINUTE 60000
  • public static final long ONE_SECOND 1000
  • public void wakeUp()
  • public class TestSleeper implements Sleeper
  • private int iterations
  • public TestSleeper()
  • iterations 0
  • public void wakeUp()
  • Iterations
  • System.out.println("Woken up for the "
    iterations " time.")
  • AlarmClock.letMeSleepFor(this,
    ONE_SECOND)

23
The Applet POC
  • So, what happens when we want an Applet to use
    this service?
  • class ClockApplet extends Applet implements
    Sleeper
  • // . . .
  • public void wakeUp()
  • repaint()
  • AlarmClock.letMeSleepFor(ONE_MINUTE)
  • // . . .
  • Obviously, we could not use inheritance here
Write a Comment
User Comments (0)
About PowerShow.com