Presentation is loading. Please wait.

Presentation is loading. Please wait.

Writing Classes We have talked about classes and objects.

Similar presentations


Presentation on theme: "Writing Classes We have talked about classes and objects."— Presentation transcript:

1 Writing Classes We have talked about classes and objects.
We have used pre-defined classes like Scanner, Random and Math. Now we will write our own classes.

2 Writing Classes Let’s revisit objects:
Objects have states and behaviors. Example: A coin object state: heads or tails behavior: flip A behavior of an object may change its state. The state of an object might determine the exact actions of a particular behavior.

3 Writing Classes Let’s revisit classes:
Classes are like blueprints for objects. Multiple objects can be created from the same plan. The objects each have their own set of states. For instance we can create two objects: coin1 and coin2. Coin1 might have a state of heads, while coin2 has a state of tails. Changing the state of coin1 will not affect the state of coin2

4 Writing Classes Example:
We used the String class. We created String objects from the class. Each string had a state of containing specific characters. Each string could call methods (behaviors) such as ToUpperCase , length(), or equals. Notice that some methods implement the behavior of returning information about the object’s current state

5 Writing Classes The String, Random and Scanner classes are supplied by the Java library. We can write our own classes to define objects we need to implement a certain task. We recently wrote a program that involved flipping a coin. We could have written a Coin object to help us with this. Our Coin object will contain states and behaviors. We earlier pointed out that a Coin has a state of heads or tails and a behavior of flip.

6 Writing Classes An class that defines an object will typically look something like this: Data declarations used to keep up with states Methods to implement behaviors Most Classes have a toString method that returns some string and is called when you try to print out an object created by the class.

7 Writing Classes Our Coin object will look like this: Data:
face: indicates heads or tails Possible Methods: flip() //flip the coin isHeads() //is the coin heads? isTails() //is the coin tails? getFace() //return heads or tails toString() //returns a string indicating heads or tails. (could be same as getFace()

8 Writing Classes The book chose to go with four methods: a constructor, flip(), isHeads() and toString. First we set up the state variables. We use constants HEADS and TAILS, so we don’t have to keep up with 1’s and 0’s import java.util.Random; public class Coin { private final int HEADS = 0; private final int TAILS = 1; private int face;

9 Writing Classes Constructors: Called when object is instantiated.
Coin myCoin = new Coin(); The coin will be flipped when the coin is created to give it an initial value public Coin () { constructor flip(); }

10 Writing Classes isHeads is simple enough:
The flip behavior method is defined. We would probably define a random r and use r.nextInt(2) for face. This is another way to do it without the having to define the random object as a class variable. I assume the book took this approach to not address a non-state-oriented class variable at this time. public void flip () { face = (int) (Math.random() * 2); } isHeads is simple enough: public boolean isHeads () return (face == HEADS);

11 Writing Classes The toString method returns a string. It is good practice to always define a toString method in your class, so that when a program contains System.out.println(your object), something meaningful is printed. public String toString() { String faceName; if (face == HEADS) faceName = "Heads"; else faceName = "Tails"; return faceName; }

12 Writing Classes The Coin object and a program that uses the class are on pages 199 and 200; Type in this code and run it. Pay close attention to how the coin object is used in the program. There is an expectation that you understand every line of this program.

13 Writing Classes public class DoIT() { int cVar = 0; .
Scope: The scope of data is where it can be directly accessed. Data declared at the class level can be accessed by the entire class. Data declared within a method can only be accessed inside that method. Data declared inside a method may be called local data. public class DoIT() { int cVar = 0; . public int someMethod() int localVar = 0;

14 Writing Classes Instance data:
Instance data exists for each instance (object created by) the class. In the coin program, each coin instance has a face. In Alice, each penguin you created could have its own color. The color of each penguin was instance date because it exists for each instance of penguin. When we define a class, no data is reserved in memory. The space for the data is not reserved until we instantiate the an object from the class. Two instances of a class will share the same methods, but they will have separate data. You and I might drive mechanically identical cars, but mine is in a different place traveling at a different speed. Maybe it’s different color. The way the two cars respond in similar situations is the same, but the state of the cars are different.

15 Writing Classes Encapsulation:
We try to write our objects so that they are encapsulated with regard to the outside world. We do not want programs outside our object to know the inner workings of our object or access class data directly. We want our object to be a “black box”. We know what can go in and what can come out; We understand the general behaviors, but we do not concern ourselves with the inner workings after they are written and debugged. The way we build worlds in Alice is an analogy of this. We know the methods and states of objects, but are not privy to the inner workings of the objects.

16 Writing Classes Another way to approach this concept is to say that objects should be “self-governing”. If possible, any modification to the objects data should be done through the objects methods. If possible, the calling program should be denied direct access to the objects data. This is usually achieved by letting class data be private and class methods that are to be called be public. For example: If we wanted to directly change the face of our coin object, we could write a method called setFace and call it like this: coin1.setFace(“Heads”); By making the face variable in the Coin class private, we prevent someone from doing this: coin1.face = 0;

17 Writing Classes Public and private are modifiers that define characteristics of methods or data. When we are writing classes, we use these modifiers to encapsulate data and methods Let’s look at the first part of Coin: public class Coin { private final int HEADS = 0; private final int TAILS = 1;   private int face; // private we: can’t access outside of this class. // Sets up the coin by flipping it initially. public Coin () //public we want to be able to call this when we instantiate. flip(); }

18 Writing Classes Helper methods: Usually not public. Example: Palidrome
public boolean class Palidrome(String) { stripSpacePuncs(); //helper return testForPalidrome();

19 Writing Classes Driver programs:
Driver programs use a class or classes to actually do something. The flow of control is controlled by the driver program. In our coin example: Coinflips was a driver program using the Coin class

20 Writing Classes Do now: Page 209 and Banking account example

21 Writing Classes Method Declaration:
public double calcIt(int num1, int num2, int num3) Modifier return type method name parameters

22 Writing Classes When a method is called the flow of control moves from the main program to the method. When the method is finished the flow of control moves back to the main program. Main program… statement public boolean aMethod(int par1) Statement //some stuff aMethod //call method return flag statement

23 Writing Classes Coin myCoin = new Coin(); // instantiate the Coin object for (int count=1; count <= NUM_FLIPS; count++) { myCoin.flip(); if (myCoin.isHeads()) heads++; else tails++; } System.out.println ("The number flips: " + NUM_FLIPS); System.out.println ("The number of heads: " + heads); System.out.println ("The number of tails: " + tails); public void flip () { face = (int) (Math.random() * 2); }

24 Writing Classes Return statements:
Return a value. The value must be the same as the return type in the declaration. for (int count=1; count <= NUM_FLIPS; count++) { myCoin.flip(); if (myCoin.isHeads()) heads++; else tails++; } public boolean isHeads () { return (face == HEADS); }

25 Writing Classes On a method call, the actual parameters are passes into formal parameters public class AddEm { public static void main(String[] args) int x = 2; int y = 3; System.out.println(addTwoNums(x,y)); System.out.println(x); // x is still 2 } public static double addTwoNums(double x, double y) x = x+y; return x;

26 C) Card = new Card(mySuit, myValue);
Public Class Card { private String mySuit; private int myValue; public String Card(String Suit, int Value) { /*implementation*/ } public String getSuit() return mySuit; } public int getValue() return myValue; public String toString() …. Which of the following represents correct /* implementation */ for the constructor in the card class? A) mySuit = suit; myValue = value;  B) suit = mySuit; value = myValue; C) Card = new Card(mySuit, myValue); D) Card = new Card(suit, value); E) mySuit = getSuit(); myValue = getValue();

27 Writing Classes Accessor and Mutator Methods
Accessor methods access data. They do not change it. Mutator methods change the data. Example: Say we write a Bug Object: myBug.getColor(); //returns my bugs color myBug.setColor(Color.RED) //changes the bugs color Instance data should only be accessed or changed using accessor or mutator methods. Declare the class variables private and the accessor/mutator methods public to achieve this. 27

28 Writing Classes Method overloading
We overload a method when we use methods with the same name, but different signatures. The signatures differ in the parameters called. For instance, we could write a constructor called Die() that created a six-sided die by default, and another constructor called Die(int sides) that created a die with sides number of faces. Die myDie = new Die() //creates a six-sided die Die myDie = new Die(4) //creates a four sided die 28

29 Writing Classes 1) Look closely at listing 4.6 and 4.7 Notice the overloading of the die constructor Create a class called SaveNumber. SaveNumber should have: a single int value for class data a constructor that takes an integer as a parameter and stores that integer in value. another constructor that takes no parameters and stores zero in value. an accessor that retrieves the current value a mutator that changes the value a toString that returns a string of the value 29

30 Warm-up 1) Write a Driver/Tester program for the SaveNumber Class you wrote yesterday. Create two instances of SaveNumber, N1= 5 and N2 = 2. Add N1 + N2 and store the result in N2 Change the value of N1 to 3. Subtract N1 from N2 and store the value in N1. Print N1 and N2. 30

31 Writing Classes Preconditions and Postconditions:
Precondition: Should be true when a method is called. Postcondition: Should be true when the method returns For instance a pre-condition of the Math.sqrt(int r) method is r>=0; For AP: some problems will list preconditions and postconditions for methods you write. 31

32 Writing Classes Object relationships and aggregation:
Objects can have different kinds of relationships. In this chapter, we learn about has-a relationships. One object has another object within it. For example an automobile object may contain a wheel object. We will also study is-a relationships when we cover inheritance in chapter 7. In this kind of relationship, one object is derived from another. For example an automobile might be derived from a broader vehicle class. 32

33 Writing Classes 1) Type in the student program, listing 4.12, 4.13 and 4.14 in the book starting on page 235. Notice that the Student Object has-a Address object. 2) Look at listing 4.10 and Notice the similarities in design to our much simpler SaveNumber class. 33


Download ppt "Writing Classes We have talked about classes and objects."

Similar presentations


Ads by Google