Writing Classes We have talked about classes and objects.

Slides:



Advertisements
Similar presentations
Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view class.
Advertisements

© 2011 Pearson Education, publishing as Addison-Wesley Chapter 4: Writing Classes Presentation slides for Java Software Solutions for AP* Computer Science.
CSCI 1100/1202 April 3, Testing A program should be executed multiple times with various input in an attempt to find errors Debugging is the process.
© 2006 Pearson Education Chapter 4: Writing Classes Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis,
Chapter 4: Writing Classes
© 2006 Pearson Education Chapter 4: Writing Classes Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis,
CS 211 Inheritance AAA.
Chapter 4 Writing Classes. © 2004 Pearson Addison-Wesley. All rights reserved4-2 Writing Classes The programs we’ve written in previous examples have.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Chapter Day 7. © 2007 Pearson Addison-Wesley. All rights reserved4-2 Agenda Day 7 Questions from last Class?? Problem set 1 Corrected  Good results 3.
Chapter 4: Writing Classes Presentation slides for Java Software Solutions Foundations of Program Design Second Edition by John Lewis and William Loftus.
1 Classes, Encapsulation, Methods and Constructors Class definitions Scope of Data –Instance data –Local data The this Reference Encapsulation and Java.
Chapter 4: Writing Classes Presentation slides for Java Software Solutions Foundations of Program Design Third Edition by John Lewis and William Loftus.
Objects and Classes Objects and Classes objects defining classes method declaration object references and aliases instance variables encapsulation and.
Chapter 4: Writing Classes Presentation slides for Java Software Solutions for AP* Computer Science by John Lewis, William Loftus, and Cara Cocking Java.
1 (c) elsaddik CSI 1102 Introduction to Software Design Prof. Dr.-Ing. Abdulmotaleb El Saddik University of Ottawa (SITE.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 4: Writing Classes Presentation slides for Java Software Solutions for AP* Computer Science.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley John Lewis, Peter DePasquale, and Joseph Chase Chapter 5: Writing Classes.
Writing Classes (Chapter 4)
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Agenda Object Oriented Programming Reading: Chapter 14.
Introducing Objects and stuff GABY and MATT Definition: Object: “a class that is the root of the hierarchy tree for all classes in JAVA.” –An object.
© 2004 Pearson Addison-Wesley. All rights reserved September 12, 2007 Encapsulation ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Programming in Java (COP 2250) Lecture 9 Chengyong Yang Fall, 2005.
CSCI 1100/1202 April 1-3, Program Development The creation of software involves four basic activities: –establishing the requirements –creating.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Lecture 4 b Writing Classes b class declarations b method declarations b constructors b instance variables b encapsulation b method overloading b program.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 4: Writing Classes Presentation slides for Java Software Solutions for AP* Computer Science.
Chapter 4: Writing Classes. 2 b We've been using predefined classes. Now we will learn to write our own classes to define new objects b Chapter 4 focuses.
Programming in Java (COP 2250) Lecture 10 Chengyong Yang Fall, 2005.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 4: Writing Classes Presentation slides for Java Software Solutions for AP* Computer Science.
College Board Topics – A.P. Computer Science A Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
1 Chapter 4: Writing Classes  Chapter 4 focuses on: class definitions encapsulation and Java modifiers method declaration, invocation, and parameter passing.
© 2004 Pearson Addison-Wesley. All rights reserved3-1 Objects Declaration: String title;  title (object variable) of type String( Class )  title is just.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
CSCI 51 Introduction to Programming Dr. Joshua Stough February 26, 2009.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes : Review Java Software Solutions Foundations of Program Design Seventh Edition John.
Chapter 4: Writing Classes
Classes and OOP.
Chapter 4: Writing Classes
Writing Classes Chapter 4.
Classes A class is a blueprint of an object
Chapter 4: Writing Classes
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
Java LESSON 7 Objects, Part 1
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
Chapter 5 – Writing Classes
Defining Classes and Methods
Ch 4: Writing Classes Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: Classes and Objects.
Chapter 4: Writing classes
Outline Writing Classes Copyright © 2012 Pearson Education, Inc.
Classes, Encapsulation, Methods and Constructors (Continued)
Outline Boolean Expressions The if Statement Comparing Data
CHAPTER 6 GENERAL-PURPOSE METHODS
Writing Classes.
Chapter 4 Writing Classes.
Defining Classes and Methods
Outline Anatomy of a Class Encapsulation Anatomy of a Method
Object Oriented Programming Review
Encapsulation September 13, 2006 ComS 207: Programming I (in Java)
Defining Classes and Methods
Defining Classes and Methods
Conditionals and Loops
What to expect this week
Chapter 4: Writing Classes
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

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.

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.

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

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

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.

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.

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()

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;

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(); }

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);

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; }

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.

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;

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.

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.

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;

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(); }

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

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

Writing Classes Do now: Page 209 and 211-212 Banking account example

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

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

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); }

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); }

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;

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();

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

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

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

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

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

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

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 4.11. Notice the similarities in design to our much simpler SaveNumber class. 33