Chapter 4: Writing Classes

Slides:



Advertisements
Similar presentations
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 4: Writing Classes Presentation slides for Java Software Solutions for AP* Computer Science.
Advertisements

1 Classes, Encapsulation, Methods and Constructors (Continued) Class definitions Instance data Encapsulation and Java modifiers Method declaration and.
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.
Chapter 4: Writing Classes. 2 Objects  An object has: state - descriptive characteristics behaviors - what it can do (or what can be done to it)  For.
Chapter 4: Writing Classes
INF 523Q Chapter 4: Writing Classes (Examples). Coin.java b //****************************************************************** b // Coin.java Author:
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,
Chapter 3, More on Classes & Methods Clark Savage Turner, J.D., Ph.D. Copyright 2003 CSTurner, from notes and text.
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.
Foundations of Program Design Writing Classes. 2 Objects b An object has: state - descriptive characteristicsstate - descriptive characteristics behaviors.
Chapter 4: Writing Classes Presentation slides for Java Software Solutions Foundations of Program Design Second Edition by John Lewis and William Loftus.
Basic Concepts of OO An object has state: defined by the set of fields or attributes. behavior: defined by the set of methods (operations that can be applied.
Chapter 4: Writing Classes Presentation slides for Java Software Solutions Foundations of Program Design Second Edition by John Lewis and William Loftus.
Classes, Encapsulation, Methods and Constructors
Aalborg Media Lab 26-Jun-15 Software Design Lecture 5 “ Writing Classes”
Chapter 4: Writing Classes Presentation slides for Java Software Solutions Foundations of Program Design Second Edition by John Lewis and William Loftus.
Chapter 4: Writing Classes Presentation slides for Java Software Solutions Foundations of Program Design Third Edition by John Lewis and William Loftus.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes Java Software Solutions Foundations of Program Design Seventh Edition John Lewis William.
Methods in Java Selim Aksoy Bilkent University Department of Computer Engineering
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.
Chapter 4 Writing Classes. © 2004 Pearson Addison-Wesley. All rights reserved2/48 Writing Classes We've been using predefined classes. Now we will learn.
Writing Classes (Chapter 4)
© 2006 Pearson Education Chapter 4: Writing Classes Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis,
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Java Software Solutions Lewis and Loftus Chapter 4 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects and Classes -- Introduction.
Chapter 4 -2 part Writing Classes 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All.
Chapter 4 Writing Classes Part 2. © 2004 Pearson Addison-Wesley. All rights reserved4-2 Classes A class can contain data declarations and method declarations.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSCI 1100/1202 April 1-3, Program Development The creation of software involves four basic activities: –establishing the requirements –creating.
© 2004 Pearson Addison-Wesley. All rights reserved September 14, 2007 Anatomy of a Method ComS 207: Programming I (in Java) Iowa State University, FALL.
Wednesday –POD –I have updated grades in powerschool. If you have a zero for a lab grade, it probably means you didn’t DropItToMe. Please do so. –Slides.
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.
INF 523Q 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.
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.
Chapter 4 Writing Classes. © 2004 Pearson Addison-Wesley. All rights reserved4-2 Writing Classes We've been using predefined classes. Now we will learn.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes Java Software Solutions Foundations of Program Design Seventh Edition John Lewis William.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 4: Writing Classes Presentation slides for Java Software Solutions for AP* Computer Science.
Outline Anatomy of a Class Encapsulation Anatomy of a Method Graphical Objects Graphical User Interfaces Buttons and Text Fields Copyright © 2012 Pearson.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes : Review Java Software Solutions Foundations of Program Design Seventh Edition John.
Chapter 4: Writing Classes
Chapter 4: Writing Classes
Writing Classes Chapter 4.
Writing Classes We've been using predefined classes from the Java API. Now we will learn to write our own classes. Chapter 4 focuses on: class definitions.
Classes A class is a blueprint of an object
Chapter 4: Writing Classes
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
CSCI 1100/1202 April 10,
Classes, Encapsulation, Methods and Constructors (Continued)
Chapter 4 Writing Classes
Anatomy of a Method.
Writing Classes.
Chapter 4 Writing Classes.
Outline Anatomy of a Class Encapsulation Anatomy of a Method
Chapter 4: Writing Classes
Presentation transcript:

Chapter 4: Writing Classes

Writing Classes We've been using predefined classes. Now we will learn to write our own classes to define new objects Chapter 4 focuses on: class declarations method declarations instance variables encapsulation method overloading graphics-based objects

Objects An object has: For example, a particular bank account state - descriptive characteristics behaviors or actions - what it can do (or be done to it) For example, a particular bank account has an account number has a current balance can be deposited into can be withdrawn from

Classes A class is a blueprint of an object Instantiation For example A model or pattern from which objects are created A class defines the methods and types of data associated with an object Instantiation Creating an object from a class is called instantiation; an object is an instance of a particular class For example Account class could describe many bank accounts, toms_savings is a particular bank account with a particular balance

Classes The String class was provided for us by the Java standard class library But we can also write our own classes that define specific objects that we need For example, suppose we wanted to write a program that simulates the flipping of a coin We could write a Coin class to represent a coin object

Classes A class contains data declarations and method declarations int x, y; char ch; Data declarations Method declarations

Defining Classes The syntax for defining a class is: class class-name { declarations constructors methods } The variables, constructors, and methods of a class are generically called members of the class

Defining Classes class Account { int account_number; double balance; Account (int account, double initial) { account_number = account; balance = initial; } // constructor Account void deposit (double amount) { balance = balance + amount; } // method deposit } // class Account

Data Scope The scope of data the area in a program in which that data can be used (referenced) Data declared at the class level can be used by all methods in that class Local data or variable Data declared within a method can only be used in that method

Constructors A constructor: is a special method that is used to set up a newly created object often sets the initial values of variables has the same name as the class does not return a value has no return type, not even void The programmer does not have to define a constructor for a class

Constructors Constructs can take parameters often used to initialize some variables in the object For example, the Account constructor could be set up to take a parameter specifying its initial balance: Account toms_savings = new Account (1,125.89);

Classes and Objects A class defines the data types for an object, but a class does not store data values Each object has its own unique data space(memory) instance variables The variables defined in a class because each instance of the class has its own Methods are shared among all objects of a class All methods in a class have access to all instance variables of the class

Classes and Objects Objects Class account_number balance 2908371 573.21 Class int account_number double balance account_number balance 4113787 9211.84

Object References The declaration of the object reference variable and the creation of the object can be separate activities: Account toms_savings; toms_savings = new Account (1, 125.89); Once an object exists, its methods can be invoked using the dot operator: toms_savings.deposit (35.00);

References An object reference holds the memory address of an object Chess_Piece bishop1 = new Chess_Piece(); All interaction with an object occurs through a reference variable References have an effect on actions such as assignment bishop1

Assignment The act of assignment takes a copy of a value and stores it in a variable For primitive types: num2 = num1; Before num1 5 num2 12 After num1 5 num2

Reference Assignment For object references, the value of the memory location is copied: bishop2 = bishop1; Before bishop1 bishop2 After bishop1 bishop2

Writing Methods A method declaration specifies the code that will be executed when the method is invoked (or called) When a method is invoked, the flow of control jumps to the method and executes its code When complete, the flow returns to the place where the method was called and continues The invocation may or may not return a value, depending on how the method was defined

Method Control Flow The called method could be within the same class, in which case only the method name is needed myMethod(); myMethod compute

Method Control Flow The called method could be part of another class or object obj.doIt(); main doIt helpMe helpMe();

Method Declarations A method declaration begins with a method header char calc (int num1, int num2, String message) method name parameter list The parameter list specifies the type and name of each parameter The name of a parameter in the method declaration is called a formal argument return type

Method Declarations The method header is followed by the method body char calc (int num1, int num2, String message) { int sum = num1 + num2; char result = message.charAt (sum); return result; } sum and result are local data They are created each time the method is called, and are destroyed when it finishes executing The return expression must be consistent with the return type

The return Statement The return type of a method indicates the type of value that the method sends back to the calling location A method that does not return a value has a void return type The return statement specifies the value that will be returned Its expression must conform to the return type

Parameters Each time a method is called, the actual arguments in the invocation are copied into the formal arguments ch = obj.calc (25, count, "Hello"); char calc (int num1, int num2, String message) { int sum = num1 + num2; char result = message.charAt (sum); return result; }

Encapsulation You can take one of two views of an object: internal - the structure of its data, the algorithms used by its methods external - the interaction of the object with other objects in the program From the external view, an object is an encapsulated entity, providing a set of specific services These services define the interface to the object

Encapsulation An encapsulated object can be thought of as a black box Its inner workings are hidden to the client, which only invokes the interface methods Methods Client Data

Encapsulation An object should be self-governing; any changes to the object's state (its variables) should be accomplished by that object's methods We should make it difficult, if not impossible, for another object to "reach in" and alter an object's state The user, or client, of an object can request its services, but it should not have to be aware of how those services are accomplished

Visibility Modifiers We accomplish encapsulation through the appropriate use of visibility modifiers A modifier a Java reserved word that specifies particular characteristics of a programming construct We've used the modifier final to define a constant Java has three visibility modifiers: public, private, and protected We will discuss the protected modifier later

Visibility Modifiers public visibility private visibility Members of a class that are declared with public can be accessed from anywhere private visibility Members of a class that are declared with private can only be accessed from inside the class default visibility Members declared without a visibility modifier can be accessed by any class in the same package Java modifiers are discussed in detail in Appendix F

Visibility Modifiers As a general rule, no object's data should be declared with public visibility Methods that provide the object's services usually declared with public visibility so that they can be invoked by clients Public methods are also called service methods Other methods, called support methods, can be defined that assist the service methods; they should not be declared with public visibility

Writing Classes See BankAccounts.java (page 188) See Account.java (page 189) An aggregate object is an object that contains references to other objects An aggregate object represents a has-a relationship An Account object is an aggregate object it contains a reference to a String object (that holds the owner's name) A bank account has a name

BankAccouts.java import Account; public class BankAccounts { public static void main (String[] args) { Account acct1 = new Account ("Ted Murphy", 72354, 102.56); Account acct2 = new Account ("Jane Smith", 69713, 40.00); Account acct3 = new Account ("Edward Demsey", 93757, 759.32); acct1.deposit (25.85); double smithBalance = acct2.deposit (500.00); System.out.println ("Smith balance after deposit: " + smithBalance); System.out.println ("Smith balance after withdrawal: " + acct2.withdraw (430.75, 1.50)); acct3.withdraw (800.00, 0.0); // exceeds balance acct1.addInterest(); acct2.addInterest(); acct3.addInterest(); System.out.println (); System.out.println (acct1); System.out.println (acct2); System.out.println (acct3); }

Account.java import java.text.NumberFormat; public class Account { private NumberFormat fmt= NumberFormat.getCurrencyInstance(); private final double RATE = 0.045; // interest rate of 4.5% private long acctNumber; private double balance; private String name; public Account (String owner, long account, double initial) { name = owner; acctNumber = account; balance = initial; } public double deposit (double amount) { if (amount < 0) { // deposit value is negative System.out.println (); System.out.println ("Error: Deposit amount is invalid."); System.out.println (acctNumber + " " + fmt.format(amount)); else balance = balance + amount; return balance;

Account.java public double withdraw (double amount, double fee) { amount += fee; if (amount < 0) { // withdraw value is negative System.out.println (); System.out.println ("Error: Withdraw amount is invalid."); System.out.println ("Account: " + acctNumber); System.out.println ("Requested: " + fmt.format(amount)); } else if (amount > balance) { // withdraw value exceeds balance System.out.println ("Error: Insufficient funds."); System.out.println ("Available: " + fmt.format(balance)); else balance = balance - amount; return balance;

Account.java public double addInterest () { balance += (balance * RATE); return balance; } public double getBalance () { public long getAccountNumber () return acctNumber; public String toString () return (acctNumber + "\t" + name + "\t" + fmt.format(balance));

Writing Classes Sometimes an object has to interact with other objects of the same type For example, we might add two Rational number objects together as follows: r3 = r1.add(r2); One object (r1) is executing the method and another (r2) is passed as a parameter See RationalNumbers.java (page 196) See Rational.java (page 197)

Overloaded Methods Method overloading Signature the process of using the same method name for multiple methods Signature The signature of each overloaded method must be unique The signature is based on the number, type, and order of the parameters The compiler must be able to determine which version of the method is being invoked by analyzing the parameters The return type of the method is not part of the signature

Overloading Methods float tryMe (int x) { return x + .375; } Version 1 float tryMe (int x, float y) { return x*y; } Version 2 result = tryMe (25, 4.32) Invocation

Overloaded Methods The println method is overloaded: etc. The lines println (String s) println (int i) println (double d) etc. The lines System.out.println ("The total is:"); System.out.println (total); invoke different versions of the println method

Overloaded Methods Constructors are often overloaded to provide multiple ways to set up a new object Account (int account) { account_number = account; balance = 0.0; } // constructor Account Account (int account, double initial) { balance = initial; } // constructor Account

SnakeEyes.java import Die; public class SnakeEyes { public static void main (String[] args) { final int ROLLS = 500; int snakeEyes = 0, num1, num2; Die die1 = new Die(); // creates a six-sided die Die die2 = new Die(20); // creates a twenty-sided die for (int roll = 1; roll <= ROLLS; roll++) { num1 = die1.roll(); num2 = die2.roll(); if (num1 == 1 && num2 == 1) // check for snake eyes snakeEyes++; } System.out.println ("Number of rolls: " + ROLLS); System.out.println ("Number of snake eyes: " + snakeEyes); System.out.println ("Ratio: " + (float)snakeEyes/ROLLS);

Die.java public class Die { private final int MIN_FACES = 4; private int numFaces; // number of sides on the die private int faceValue; // current value showing on the die public Die () { numFaces = 6; faceValue = 1; } public Die (int faces) { if (faces < MIN_FACES) else numFaces = faces;

Die.java public int roll () { faceValue = (int) (Math.random() * numFaces) + 1; return faceValue; } public int getFaceValue ()

The StringTokenizer Class The next example makes use of the StringTokenizer class, which is defined in the java.util package A StringTokenizer object separates a string into smaller substrings (tokens) By default, the tokenizer separates the string at white space The StringTokenizer constructor takes the original string to be separated as a parameter Each call to the nextToken method returns the next token in the string

Method Decomposition A method should be relatively small, so that it can be readily understood as a single entity A potentially large method should be decomposed into several smaller methods as needed for clarity Therefore, a service method of an object may call one or more support methods to accomplish its goal See PigLatin.java (page 207) See PigLatinTranslator.java (page 208)

PigLatin.java import PigLatinTranslator; import cs1.Keyboard; public class PigLatin { public static void main (String[] args) { String sentence, result, another; PigLatinTranslator translator = new PigLatinTranslator(); do { System.out.println (); System.out.println ("Enter a sentence (no punctuation):"); sentence = Keyboard.readString(); result = translator.translate (sentence); System.out.println ("That sentence in Pig Latin is:"); System.out.println (result); System.out.print ("Translate another sentence (y/n)? "); another = Keyboard.readString(); } while (another.equalsIgnoreCase("y")); }

PigLatinTranslator.java import java.util.StringTokenizer; public class PigLatinTranslator { public String translate (String sentence) { String result = ""; sentence = sentence.toLowerCase(); StringTokenizer tokenizer = new StringTokenizer (sentence); while (tokenizer.hasMoreTokens()) { result += translateWord (tokenizer.nextToken()); result += " "; } return result; private String translateWord (String word) { if (beginsWithVowel(word)) result = word + "yay"; else if (beginsWithPrefix(word)) result = word.substring(2) + word.substring(0,2) + "ay"; else result = word.substring(1) + word.charAt(0) + "ay";

PigLatinTranslator.java private boolean beginsWithVowel (String word) { String vowels = "aeiouAEIOU"; char letter = word.charAt(0); return (vowels.indexOf(letter) != -1); } private boolean beginsWithPrefix (String str) { return ( str.startsWith ("bl") || str.startsWith ("pl") || str.startsWith ("br") || str.startsWith ("pr") || str.startsWith ("ch") || str.startsWith ("sh") || str.startsWith ("cl") || str.startsWith ("sl") || str.startsWith ("cr") || str.startsWith ("sp") || str.startsWith ("dr") || str.startsWith ("sr") || str.startsWith ("fl") || str.startsWith ("st") || str.startsWith ("fr") || str.startsWith ("th") || str.startsWith ("gl") || str.startsWith ("tr") || str.startsWith ("gr") || str.startsWith ("wh") || str.startsWith ("kl") || str.startsWith ("wr") || str.startsWith ("ph") );

Applet Methods In previous examples we've used the paint method of the Applet class to draw on an applet The Applet class has several methods that are invoked automatically at certain points in an applet's life The init method, for instance, is executed only once when the applet is initially loaded The Applet class also contains other methods that generally assist in applet processing

Graphical Objects Any object we define by writing a class can have graphical elements The object must simply obtain a graphics context (a Graphics object) in which to draw An applet can pass its graphics context to another object just as it can any other parameter See LineUp.java (page 212) See StickFigure.java (page 215)

LineUp.java import StickFigure; import java.applet.Applet; import java.awt.*; public class LineUp extends Applet { private final int APPLET_WIDTH = 400; private final int APPLET_HEIGHT = 150; private final int HEIGHT_MIN = 100; private final int VARIANCE = 30; private StickFigure figure1, figure2, figure3, figure4; public void init () { int h1, h2, h3, h4; // heights of stick figures h1 = (int) (Math.random() * VARIANCE) + HEIGHT_MIN; h2 = (int) (Math.random() * VARIANCE) + HEIGHT_MIN; h3 = (int) (Math.random() * VARIANCE) + HEIGHT_MIN; h4 = (int) (Math.random() * VARIANCE) + HEIGHT_MIN; figure1 = new StickFigure (100, 150, Color.red, h1); figure2 = new StickFigure (150, 150, Color.cyan, h2); figure3 = new StickFigure (200, 150, Color.green, h3); figure4 = new StickFigure (250, 150, Color.yellow, h4); setBackground (Color.black); setSize (APPLET_WIDTH, APPLET_HEIGHT); }

LineUp.java public void paint (Graphics page) { figure1.draw (page); }

StickFigure.java import java.awt.*; public class StickFigure { private int baseX; // center of figure private int baseY; // floor (bottom of feet) private Color color; // color of stick figure private int height; // height of stick figure public StickFigure (int center, int bottom, Color shade, int size) baseX = center; baseY = bottom; color = shade; height = size; }

StickFigure.java public void draw (Graphics page) { int top = baseY - height; // top of head page.setColor (color); page.drawOval (baseX-10, top, 20, 20); // head page.drawLine (baseX, top+20, baseX, baseY-30); // trunk page.drawLine (baseX, baseY-30, baseX-15, baseY); // legs page.drawLine (baseX, baseY-30, baseX+15, baseY); page.drawLine (baseX, baseY-70, baseX-25, baseY-70); // arms page.drawLine (baseX, baseY-70, baseX+20, baseY-85); }