Programming with Java - Lake

Download Report

Transcript Programming with Java - Lake

COP 2800
Lake Sumter State College
Mark Wilson, Instructor
Java API
 3793
pre-written Java classes
 Organized into packages
 Package is part of the full name
• java.util.ArrayList
 Must
use the full name
• Type it out each time you use it
• Use an import statement
• import java.util;
 Option
one: get a book
• O’Reilly “Java in a Nutshell”
• Oracle “Java The Complete Reference”
 Option
two: online documentation
• http://docs.oracle.com/javase/7/docs/api/
 java.lang
– classes fundamental to the language
 java.util – utility classes
 javax.swing – Swing GUI classes
 java.awt – UI and graphics
 java.io – system input and output
 java.math – arbitrary precision arithmetic
 Found
in java.lang
 “Wrap" primitive values in an object
• primitives can be included in activities reserved for
objects
• such as being added to collections
• or returned from a method with an object return
value.
 Provide
an assortment of utility functions
• converting primitives to and from String objects,
• converting primitives and String objects to and from
different bases (or radix), such as binary, octal, and
hexadecimal.
Primitive
boolean
byte
char
int
float
double
long
short
Wrapper Class
Boolean
Byte
Character
Integer
Float
Double
Long
Short
Constructor Argument
boolean or String
byte or String
char
int or String
float, double or String
double or String
long or String
short or String
Object
Byte
Short
Number
Character
Boolean
Integer
Long
Float
Double











parseInt(s) - returns a signed decimal integer value equivalent to string s
toString(i) - returns a new String object representing the integer i
byteValue() - returns the value of this Integer as a byte
doubleValue() - returns the value of this Integer as a double
floatValue() - returns the value of this Integer as a float
intValue() - returns the value of this Integer as an int
shortValue() - returns the value of this Integer as a short
longValue() - returns the value of this Integer as a long
int compareTo(int i) - Compares the numerical value of the invoking
object with that of i. Returns 0 if the values are equal. Returns a negative
value if the invoking object has a lower value. Returns a positive value if
the invoking object has a greater value.
static int compare(int num1, int num2) - Compares the values of num1 and
num2. Returns 0 if the values are equal. Returns a negative value if num1 is
less than num2. Returns a positive value if num1 is greater than num2.
boolean equals(Object intObj) - Returns true if the invoking Integer
object is equivalent to intObj. Otherwise, it returns false.
Inheritance
 Encapsulation
• Data (instance variables, attributes) is kept private
• Access is through public methods
 Accessors or ‘getters’
 Mutators or ‘setters’
• Helper methods are kept private
 Inheritance
 Polymorphism
 Abstraction

Derive a new class from an existing one

Existing class is called the parent class, superclass,
or base class

Derived class is called the child class or subclass.

Child inherits characteristics of the parent

Child class inherits the methods and data defined
for the parent class

Can add new variables or methods, or can modify
the inherited ones
 All
variables and methods of a parent class,
even private members, are inherited by its
children
 Private members cannot be referenced by
name in the child class
 Private members inherited by child classes
exist and can be referenced indirectly
 Because the parent can refer to the private
member, the child can reference it indirectly
using its parent's methods
 The super reference can be used to refer to
the parent class, even if no object of the parent
class exists
 Inheritance
should create an is-a relationship
 Child is a more specific version of the parent
 Subclass extends the superclass
 Subclass can add methods and instance
variables
 Subclass can override methods from the
superclass
 Subclass can only extend (inherit from) a single
superclass
 Subclass inherits from all superclasses in the
hierarchy tree
public class Dog extends Animal {
. . .
}
A
subclass can override (redefine) the methods
of the superclass
• Objects of the subclass type will use the new method
• Objects of the superclass type will use the original
 Applies
to methods, not instance variables
 Changes the definition of a method
 Doesn’t change the parameter list
 Doesn’t change the type
 Can’t be less accessible
 Can incorporate the superclass via super
public class Pekinese extends Dog{
void makeNoise () {
super.makeNoise();
addPanting();
}
Polymorphism




When a program invokes a method through a superclass
variable the correct subclass version of the method is called,
based on the type of the reference stored in the superclass
variable
The same method name and signature can cause different
actions to occur depending on the type of object on which
the method is invoked
Polymorphism enables programmers to deal in generalities
and let the execution-time environment handle the specifics.
Objects to behave in manners appropriate to those objects
as long as the objects belong to the same inheritance
hierarchy.




A (public) method defined in a class is inherited
by all descendants of that class
When a message is sent to an object to use method
m(), any messages that m() sends will also be sent
to the same object
If the object receiving a message does not have a
definition of the method requested, an inherited
definition is invoked
If the object receiving a message has a definition
of the requested method, that definition is invoked
Shape
+area() : double
+perimeter() : double
Circle
-radius : double
+area() : double
+perimeter() : double
Rectangle
-height : double
-width : double
+area() : double
+perimeter() : double
Square
-side : double
+area() : double
+perimeter() : double
public class Shape { . . .}
public class Rectangle extends Shape { . . .}
public class Square extends Rectangle { . . .}
public class Circle extends Shape { . . .}
public class ShapeShifter {
public static void main (String [ ] args) {
Shape [ ] shapeList = new Shape[5];
shapeList[0] = new Circle(3.0);
shapeList[1] = new Rectangle(3.0, 4.0);
shapeList[2] = new Rectangle(2.5, 7.5);
shapeList[3] = new Circle(2.5);
shapeList[4] = new Square(5.0);
for (int i = 0; i < shapeList.length; i++) {
System.out.print (shapeList[i].toString( ) + “
”);
System.out.print (shapeList[i].area( ) + “
”);
System.out.println (shapeList[i].perimeter( ));
}
}
}
class Vet {
public void giveShot (Animal a){
// . . . //
a.makeNoise();
}
}
class PetOwner {
public void start {
Vet v = new Vet();
Dog d = new Dog();
Cat c = new Cat();
v.giveShot (d);
v.giveShot (c);
}
}
 Overload
≠ override
 Two methods with the same name, different
argument lists
 Not polymorphic
 Argument lists can be different
 Return types can be different
• But only if the argument list is changed
 Access
levels can change in any direction
class Calculator {
// instance variables go here
public void add(int i, int j) {
System.out.println("Integer addition");
}
public void add(int i, double j) {
System.out.println("Integer and double addition");
}
public void add(double i, double j) {
System.out.println("Double addition");
}
}
 Create
an instance of a class
 No constructor implies default constructor
 Access modifiers: public, protected, private, or
none
 No return type including void
 Exact same name as the class
 Can be overloaded
 The
compiler automatically supplies
• a no-argument constructor if no constructor is
explicitly declared and
• a no-argument super call when a constructor has no
explicit call to super.
public class Example {}
is functionally equivalent to
public class Example {
Example() {
super;
}
}