DOC PREVIEW
UNF COP 2551 - Lecture Notes

This preview shows page 1-2-3-23-24-25-26-46-47-48 out of 48 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 48 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 48 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 48 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 48 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 48 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 48 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 48 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 48 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 48 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 48 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 48 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Chapter 4Writing ClassesOutlineSlide 4Classes and ObjectsClassesSlide 7Slide 8Slide 9Slide 10The Die ClassThe toString MethodConstructorsData ScopeInstance DataSlide 16UML Diagrams – Intro to Architectural DesignUML Class DiagramsSlide 19EncapsulationSlide 21Slide 22Slide 23Visibility ModifiersSlide 25Slide 26Slide 27Slide 28Accessors and MutatorsMutator RestrictionsSlide 31Method DeclarationsMethod Control FlowSlide 34Method HeaderMethod BodyThe return StatementParametersLocal DataBank Account ExampleSlide 41Slide 42Slide 43Driver ProgramsSlide 45Slide 46Slide 47Constructors RevisitedChapter 4Writing Classes© 2004 Pearson Addison-Wesley. All rights reserved 2/48Writing Classes•We've been using predefined classes. Now we will learn to write our own classes to define objects•Chapter 4 focuses on:class definitionsinstance dataencapsulation and Java modifiersmethod declaration and parameter passingconstructorsgraphical objectsevents and listenersbuttons and text fields© 2004 Pearson Addison-Wesley. All rights reserved 3/48OutlineAnatomy of a ClassEncapsulationAnatomy of a Method© 2004 Pearson Addison-Wesley. All rights reserved 4/48Writing Classes•The programs we’ve written in previous examples have used classes defined in the Java standard class library•Now we will begin to design programs that rely on classes that we write ourselves•The class that contains the main method is just the starting point of a program True object-oriented programming is based on defining classes that represent objects with well-defined characteristics and functionality© 2004 Pearson Addison-Wesley. All rights reserved 5/48Classes and Objects•Recall from our overview of objects in Chapter 1 that an object has state and behavior•Consider a six-sided die (singular of dice)Its state can be defined as which face is showingIts primary behavior is that it can be rolled•We can represent a die in software by designing a class called Die that models this state and behaviorThe class serves as the blueprint for a die object•We can then instantiate as many die objects as we need for any particular program. Each die might have a different ‘state’ (value of its attribute, face) but the same ‘behaviors’ (can be rolled – a method)© 2004 Pearson Addison-Wesley. All rights reserved 6/48Classes•A class can contain data declarations and method declarationsint size, weight;char category;Data declarationsMethod declarationsUsually call these ‘attributes’ or ‘properties’Each of these has a ‘state’ that is, its ‘value’ at any particular instance in time.These are the ‘behaviors’ or ‘methods’ or‘services’ objects of this class can provide.e.g. rollDie(); might be such a behavior.Also called the object’s responsibilities.All the ‘functionalities’, that is, the ‘actions’ that can take place on the attributes are included here and here alone.© 2004 Pearson Addison-Wesley. All rights reserved 7/48Classes•The values of the data define the state of an object created from the class•The functionality of the methods define the behaviors of the object•For our Die class, we might declare an integer that represents the current value showing on the face•One of the methods would “roll” the die by setting that value to a random number between one and six© 2004 Pearson Addison-Wesley. All rights reserved 8/48Classes•We’ll want to design the Die class with other data and methods to make it a versatile and reusable resource•Any given program will not necessarily use all aspects of a given class•Let’s look at RollingDice.java•And Die.java •OK. Here’s where we really start. Be certain to study and understand this stuff!!© 2004 Pearson Addison-Wesley. All rights reserved 9/48•//********************************************************************•// RollingDice.java Author: Lewis/Loftus•//•// Demonstrates the creation and use of a user-defined class.•//********************************************************************•public class RollingDice•{• //-----------------------------------------------------------------• // Creates two Die objects and rolls them several times.• //-----------------------------------------------------------------• public static void main (String[] args)• {• Die die1, die2; // declares to reference variables. • int sum;• die1 = new Die();// declares two objects!• die2 = new Die();// die1 and die2 point to them.• die1.roll(); // invoking the roll() ‘method’ as defined in Die (ahead)• die2.roll();• System.out.println ("Die One: " + die1 + ", Die Two: " + die2); // Whoa! What happens here???• die1.roll();• die2.setFaceValue(4);• System.out.println ("Die One: " + die1 + ", Die Two: " + die2);• sum = die1.getFaceValue() + die2.getFaceValue();• System.out.println ("Sum: " + sum);• sum = die1.roll() + die2.roll();• System.out.println ("Die One: " + die1 + ", Die Two: " + die2);• System.out.println ("New sum: " + sum);• }•}© 2004 Pearson Addison-Wesley. All rights reserved 10/48•// Die.java // Represents one die (singular of dice) with faces showing values // between 1 and 6.•// Document as in the book – not here. This was done for space considerations!!•public class Die •{• private final int MAX = 6; // maximum face value• private int faceValue; // current value showing on the die•// Constructor: Sets the initial face value• public Die(). // This is the Constructor.• {• faceValue = 1; // sets up an initial value when object was created.• }•public int roll() // Rolls the die and returns the result.• {• faceValue = (int)(Math.random() * MAX) + 1; // What does this do?? Find out! Why +1 ???• return faceValue;• }•public void setFaceValue (int value) // Face value mutator.• {• faceValue = value;• // get and set are standard.• public int getFaceValue() // Face value accessor• {• return faceValue;• }•public String toString() // Returns a string representation of this die.• {• String result = Integer.toString(faceValue);// Is this a Wrapper class? Check book!• return result;• }•} // end class Die.© 2004 Pearson Addison-Wesley. All rights reserved 11/48The Die Class•The Die class contains two data valuesa


View Full Document

UNF COP 2551 - Lecture Notes

Download Lecture Notes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture Notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture Notes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?