JAVA Classes. Objectives Be able to define new classes Be able to define appropriate instance...

26
JAVA Classes

Transcript of JAVA Classes. Objectives Be able to define new classes Be able to define appropriate instance...

Page 1: JAVA Classes. Objectives Be able to define new classes Be able to define appropriate instance variables Be able to define the usual methods of a class.

JAVAClasses

Page 2: JAVA Classes. Objectives Be able to define new classes Be able to define appropriate instance variables Be able to define the usual methods of a class.

Objectives• Be able to define new classes• Be able to define appropriate instance variables • Be able to define the usual methods of a class

• Constructors• Accessors• Mutators• Other

• Be able to distinguish between static and instance variables and methods• Be able to create and call static methods• Be able to create and call instance methods

Page 3: JAVA Classes. Objectives Be able to define new classes Be able to define appropriate instance variables Be able to define the usual methods of a class.

3

Classes• Classes encapsulate object types.• In object-centered design we

• Reuse old classes where possible• Build new classes when necessary

• Each new class that we design and build should have a coherent set of:• Knowledge

• Responsibilities

Page 4: JAVA Classes. Objectives Be able to define new classes Be able to define appropriate instance variables Be able to define the usual methods of a class.

Ice Cream Orders• Suppose you were writing a program for use at an ice

cream shop. The program needs to help track what orders are given and when they have been fulfilled.

• What are the necessary…• Data?

• Number of scoops• Flavor• Order fulfilled (true or false)

• Operations?• Create new order• Change fulfilled status• Compute cost

Page 5: JAVA Classes. Objectives Be able to define new classes Be able to define appropriate instance variables Be able to define the usual methods of a class.

5

Classes and Objects• Classes describe sets of similar objects by specifying their:

• Attributes

• Behaviors

• Each object has its own copies of the attribute values.

Page 6: JAVA Classes. Objectives Be able to define new classes Be able to define appropriate instance variables Be able to define the usual methods of a class.

6

Using Classes• When you use a class object:

• The calling program doesn’t know:• How to initialize the object’s data • How to implement the object’s methods

• The object itself is responsible for these things.• The calling program accesses them by calling predefined

methods.

Page 7: JAVA Classes. Objectives Be able to define new classes Be able to define appropriate instance variables Be able to define the usual methods of a class.

7

/** DRIVER PROGRAM * ... appropriate documentation ... */package c09classes.icecream;

public class IceCreamConsole{//Create a default orderIceCreamOrder firstOrder = new IceCreamOrder();System.out.println(firstOrder);

//Change the number of scoopsfirstOrder.setScoops(3);System.out.println(firstOrder);

//Print out how much the order costsSystem.out.println(“Collect: “ +

firstOrder.getCost());}

Page 8: JAVA Classes. Objectives Be able to define new classes Be able to define appropriate instance variables Be able to define the usual methods of a class.

8

Designing Classes• When we want to work with objects not supported by

existing types, we must design a new class.• The key design issues here are:

• What classes do we need?

• What goes in them (and what doesn’t)?

Page 9: JAVA Classes. Objectives Be able to define new classes Be able to define appropriate instance variables Be able to define the usual methods of a class.

9

Information Hiding• When we design a class we distinguish:

• the external interface to a class;• the internal implementation of the class.

• The principle of information hiding dictates that a class designer:• provide public views of those things that a class user really needs

to know;• hide all other details by making them private.

Page 10: JAVA Classes. Objectives Be able to define new classes Be able to define appropriate instance variables Be able to define the usual methods of a class.

10

Design using Perspectives • Use an external perspective to specify the public interface

to a class.

• Use an internal perspective to specify the internals of the class design.

Page 11: JAVA Classes. Objectives Be able to define new classes Be able to define appropriate instance variables Be able to define the usual methods of a class.

11

Implementing Classes

• Implementing the class attributes• Implementing the class methods:

• Constructors• Default-value constructor• Explicit-value constructor

• Accessors

• Mutators

• Other Methods

• Copy Constructors

Page 12: JAVA Classes. Objectives Be able to define new classes Be able to define appropriate instance variables Be able to define the usual methods of a class.

12

Class Attributes

• IceCreamOrder objects will certainly have to encapsulate their own:• # of scoops;• flavor;• fulfilled status

• These will be stored as instance variables, which means that each IceCreamOrder object will have its own versions of these values.

Page 13: JAVA Classes. Objectives Be able to define new classes Be able to define appropriate instance variables Be able to define the usual methods of a class.

13

Implementing Class Attributesclass IceCreamOrder{

private int myScoops; // number of scoops private String myFlavor; // flavor of ice cream private boolean myStatus; // order completion // other class stuff here…

}

Page 14: JAVA Classes. Objectives Be able to define new classes Be able to define appropriate instance variables Be able to define the usual methods of a class.

14

Default-Value Constructor

External View (in the driver):IceCreamOrder order1 = new IceCreamOrder();

Internal View (in the class): /** * Construct a new IceCreamOrder with default values */ public IceCreamOrder() { myScoops = 1; myFlavor = “Vanilla”;

myStatus = false;}

Page 15: JAVA Classes. Objectives Be able to define new classes Be able to define appropriate instance variables Be able to define the usual methods of a class.

15

Constructors as Methods• Constructors are like methods except that:

• They have no return type• They are given the same name as the class• They are invoked with new:

IceCreamOrder order = new IceCreamOrder();

• Constructors initialize instance variables within the limits set by the invariants.

• Constructor methods are often overloaded.

Page 16: JAVA Classes. Objectives Be able to define new classes Be able to define appropriate instance variables Be able to define the usual methods of a class.

16

Class Invariants

• Objects must maintain the integrity of their internal data.

/** * Indicate whether or not the number of scoops is valid. * @param scoops the value to check * @return true if valid, false otherwise */ private boolean isValidScoops(int scoops) { if (scoops < 1) { return false; } return true; }

Page 17: JAVA Classes. Objectives Be able to define new classes Be able to define appropriate instance variables Be able to define the usual methods of a class.

17

Explicit-Value ConstructorExternal View:IceCreamOrder order1 = new IceCreamOrder(2, “Superman”, false);

Internal View:/** Construct a new IceCreamOrder * @param scoops the number of scoops * @param flavor the ice cream flavor * @param status the order status */public IceCreamOrder(int scoops, String flavor, boolean status){ if(isValidScoops (scoops)){

myScoops = scoops; } else{ System.err.println(“Invalid number of scoops: “ + scoops);

System.exit(-1); } myFlavor = flavor; myStatus = status;}

Page 18: JAVA Classes. Objectives Be able to define new classes Be able to define appropriate instance variables Be able to define the usual methods of a class.

18

Accessor Methods/** @return my number of scoops */public int getScoops() { return myScoops;}

/** @return my flavor */public String getFlavor() { return myFlavor;}

/** @return my order status */public boolean getStatus() { return myStatus;}

Page 19: JAVA Classes. Objectives Be able to define new classes Be able to define appropriate instance variables Be able to define the usual methods of a class.

19

/** Change my order completion status */ public void setStatus(boolean status) { myStatus = status; }

/** Set a new number of scoops for the order * @param scoops non-negative scoops value */ public void setScoops(int scoops) {

if (isValidScoops(scoops)){ myScoops = scoops;}else{ System.err.println(“Could not change number of

scoops to “ + scoops); System.exit(-1);

} }

Mutator Methods

Page 20: JAVA Classes. Objectives Be able to define new classes Be able to define appropriate instance variables Be able to define the usual methods of a class.

20

/** * Print out the value of the order */ public String toString() {

String result = myScoops + “ scoop(s) of “ + myFlavor + “ :”;

if (myStatus){result += “ Fulfilled”;

}else{

result += “ Unfulfilled”;}return result;

}

Other Methods

Page 21: JAVA Classes. Objectives Be able to define new classes Be able to define appropriate instance variables Be able to define the usual methods of a class.

21

Copy Constructors

● We can’t “copy” objects as follows:IceCreamOrder order1 = new IceCreamOrder();IceCreamOrder order2 = order1;

This only copies the reference.

● Thus, we must write copy constructors:

IceCreamOrder order1 = new IceCreamOrder();IceCreamOrder order2 = order1.copy();

Page 22: JAVA Classes. Objectives Be able to define new classes Be able to define appropriate instance variables Be able to define the usual methods of a class.

22

Copying Objects/** * @return a copy of myself */public IceCreamOrder copy() { return new IceCreamOrder(myScoops, myFlavor,

myStatus);}

Page 23: JAVA Classes. Objectives Be able to define new classes Be able to define appropriate instance variables Be able to define the usual methods of a class.

23

Instance versus Class Members• Java allows data and methods to be associated with

either:• A particular object, or• The class as a whole.

• Instance members are defined for each object of a class.• Class members are marked as static and are shared by

all objects of a class.• Static methods only reference static data.• Static methods can be called without creating an instance

of the class.

Page 24: JAVA Classes. Objectives Be able to define new classes Be able to define appropriate instance variables Be able to define the usual methods of a class.

24

Referencing Members• Instance members are referenced using the object

identifier.objectIdentifier.memberIdentifier

• Class members are referenced using the class identifier.ClassIdentifier.memberIdentifier

• Examples:• instance: keyboard.readDouble()• class: Math.PI

Page 25: JAVA Classes. Objectives Be able to define new classes Be able to define appropriate instance variables Be able to define the usual methods of a class.

Class Data• Instance data is unique for each object of a class• Class data is shared between all objects of a class

• Marked as static

private static final double PRICE_PER_SCOOP = 1.50;

public double getCost() {return myScoops * PRICE_PER_SCOOP;

}

Page 26: JAVA Classes. Objectives Be able to define new classes Be able to define appropriate instance variables Be able to define the usual methods of a class.

26

/** * Compares one order to another * @param other * @return whether or not the order is bigger than

* some other order */public boolean isBigger(IceCreamOrder other){

return myScoops > other.getScoops();}

Object Interaction● Sometimes objects must interact with each

other.