CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: www.csc.villanova.edu/~map/1051/

Slides:



Advertisements
Similar presentations
Based on Java Software Development, 5th Ed. By Lewis &Loftus
Advertisements

INF 523Q Chapter 4: Writing Classes (Examples). Coin.java b //****************************************************************** b // Coin.java Author:
Chapter 4 Writing Classes. © 2004 Pearson Addison-Wesley. All rights reserved4-2 Writing Classes We've been using predefined classes. Now we will learn.
Chapter 4 Writing Classes. © 2004 Pearson Addison-Wesley. All rights reserved4-2 Writing Classes The programs we’ve written in previous examples have.
Writing Classes in Java
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.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes Java Software Solutions Foundations of Program Design Seventh Edition John Lewis William.
ECE122 L6: Problem Definition and Implementation February 15, 2007 ECE 122 Engineering Problem Solving with Java Lecture 6 Problem Definition and Implementation.
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. We've been using predefined classes. Now we will learn to write our own classes to define objects Chapter 4 focuses on:  class.
Chapter 4 Writing Classes 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights reserved.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes Java Software Solutions Foundations of Program Design Seventh Edition John Lewis William.
© 2004 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4: Writing classes Intermediate Java Programming Summer 2007.
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)
Objects and Classes. Problem Solving The key to designing a solution is breaking it down into manageable pieces When writing software, we design separate.
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.
Chapter 4 Writing Classes 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights reserved.
CSE 1201 Object Oriented Programming Writing Classes.
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:
© 2004 Pearson Addison-Wesley. All rights reserved September 12, 2007 Encapsulation ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Chapter 4 Writing Classes. © 2004 Pearson Addison-Wesley. All rights reserved4-2 Writing Classes We've been using predefined classes. Now we will learn.
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:
Programming in Java (COP 2250) Lecture 9 Chengyong Yang Fall, 2005.
© 2004 Pearson Addison-Wesley. All rights reserved September 14, 2007 Anatomy of a Method ComS 207: Programming I (in Java) Iowa State University, FALL.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Copyright © 2014 Pearson Education, Inc. Chapter 4 Writing Classes Java Software Solutions Foundations of Program Design 8 th Edition John Lewis William.
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Chapter 4 Writing Classes 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights reserved.
SEEM Java – Basic Introduction, Classes and Objects.
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.
Programming in Java (COP 2250) Lecture 10 Chengyong Yang Fall, 2005.
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. © 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.
Chapter 4 Writing Classes. © 2004 Pearson Addison-Wesley. All rights reserved4-2 Writing Classes We've been using predefined classes. Now we will learn.
Chapter 4 Writing Classes 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights reserved.
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.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes : Review Java Software Solutions Foundations of Program Design Seventh Edition John.
CSC 1051 – Data Structures and Algorithms I
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.
Chapter 4: Writing Classes
Chapter 4 Writing Classes.
Chapter 5 – Writing Classes
Chapter 4 Writing Classes.
Chapter 5 – 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
Chapter 4 Writing Classes.
Outline Writing Classes Copyright © 2012 Pearson Education, Inc.
Chapter 4 Writing Classes
Chapter 4 Writing Classes
Anatomy of a Method.
Writing Classes.
Chapter 4 Writing Classes.
Chapter 4 Writing Classes
Outline Anatomy of a Class Encapsulation Anatomy of a Method
Encapsulation September 13, 2006 ComS 207: Programming I (in Java)
Chapter 4 Writing Classes
4 Writing Classes Software Solutions Lewis & Loftus java 5TH EDITION
What to expect this week
Presentation transcript:

CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: CSC 1051 M.A. Papalaskari, Villanova University Designing Classes

Where do objects come from? CSC 1051 M.A. Papalaskari, Villanova University

Where do objects come from? Good question! Defining our own classes will make it possible to design our own objects We need to learn: 1.What does it mean to define our own classes? 2.How do we define what happens when an object is instantiated? 3.How do we define methods that can be invoked through our objects? CSC 1051 M.A. Papalaskari, Villanova University

1. Defining our own classes Example: Defining the Account Class –A class to represent a generic bank account –A blueprint for an Account object Data declarations Method declarations CSC 1051 M.A. Papalaskari, Villanova University Constructor deposit() withdraw() getBalance() toString() double balance; int acctNumber; String name;

CSC 1051 M.A. Papalaskari, Villanova University acctNumber balance name "Ted Murphy" Constructor deposit() withdraw() getBalance() toString() double balance; int acctNumber; String name; Account class Account object The object: –is like the house built from the blueprint –is an instance of the class – has its own data space –shares the methods defined by the class The class is the blueprint Classes define DATA and METHODS 1. Defining our own classes

Creating Objects – old example: We have already seen something like this: This invokes the Scanner constructor, which is a special method that sets up the object CSC 1051 M.A. Papalaskari, Villanova University 2. What happens when an object is instantiated? Scanner scan = new Scanner (System.in);

Account acct1 = new Account ("Ted Murphy", 72354, ); Creating Objects – our newly defined Account class: Invokes the Account constructor, which is a special method that sets up the object 2. What happens when an object is instantiated? Constructor deposit() withdraw() getBalance() toString() double balance; int acctNumber; String name;

acct acctNumber balance name "Ted Murphy" CSC 1051 M.A. Papalaskari, Villanova University Account acct1 = new Account ("Ted Murphy", 72354, ); Creating Objects – our newly defined Account class: A new Account object is created! 2. What happens when an object is instantiated?

As we have seen, once an object has been created, we can use the dot operator to invoke its methods: ans = scan.nextLine(); numChars = title.length(); CSC 1051 M.A. Papalaskari, Villanova University 3. What happens when an object’s method is invoked?

acct acctNumber balance name "Ted Murphy" CSC 1051 M.A. Papalaskari, Villanova University Me acct1.deposit (25.85); Method invocation Transactions class 3. What happens when an object’s method is invoked?

acct acctNumber balance name "Ted Murphy" CSC 1051 M.A. Papalaskari, Villanova University Me acct1.deposit (25.85); // // Deposits the specified amount into the account. // public void deposit (double x) { balance = balance + x; } Method invocation Method definition Account class Transactions class 3. What happens when an object’s method is invoked?

Chapter 4: Writing Classes We've been using predefined classes from the Java API. Now we will learn to write our own classes. –class definitions –instance data –encapsulation and Java modifiers –method declaration and parameter passing –constructors –graphical objects –events and listeners –buttons and text fields CSC 1051 M.A. Papalaskari, Villanova University

Account : Example of newly defined class acct acctNumber balance name "Ted Murphy" acct acctNumber balance name "Jane Smith" CSC 1051 M.A. Papalaskari, Villanova University

//******************************************************************** // Account.java Author: Lewis/Loftus // Simplified code by MA Papalaskari // Represents a bank account with methods deposit and withdraw. //******************************************************************** import java.text.NumberFormat; public class Account { int acctNumber; double balance; String name; // // Sets up the account by defining its owner's name, account // number, and initial balance. // public Account (String x, int y, double z) { name = x; acctNumber = y; balance = z; } // // Deposits the specified amount x into the account. // public void deposit (double x) { balance = balance + x; } continue Constructor deposit() withdraw() getBalance() toString() double balance; int acctNumber; String name;

continue // // Withdraws the specified amount from the account and applies // the fee. // public void withdraw (double x, double fee) { balance = balance - x - fee; } // // Returns the current balance of the account. // public double getBalance () { return balance; } // // Returns a one-line description of the account as a string. // public String toString () { NumberFormat fmt = NumberFormat.getCurrencyInstance(); return (acctNumber + "\t" + name + "\t" + fmt.format(balance)); } } CSC 1051 M.A. Papalaskari, Villanova University Constructor deposit() withdraw() getBalance() toString() double balance; int acctNumber; String name;

CSC 1051 M.A. Papalaskari, Villanova University //******************************************************************** // Transactions.java Author: MA Papalaskari // (based on Lewis/Loftus example) // Demonstrates the creation and use of multiple Account objects. //******************************************************************** public class Transactions { // // Creates some bank accounts and requests various services. // public static void main (String[] args) { Account acct1 = new Account ("Ted Murphy", 72354, ); Account acct2 = new Account ("Jane Smith", 69713, 40.00); Account acct3 = new Account ("Edward Demsey", 93757, ); System.out.println (acct1); System.out.println (acct2); System.out.println (acct3); acct1.deposit (25.85); acct1.withdraw (60, 2.50); System.out.println (); System.out.println (acct1); System.out.println (acct2); System.out.println (acct3); } }

CSC 1051 M.A. Papalaskari, Villanova University Account acct1 = new Account ("Ted Murphy", 72354, ); Transactions class: Creating Account objects Transactions class

CSC 1051 M.A. Papalaskari, Villanova University Account acct1 = new Account ("Ted Murphy", 72354, ); Transactions class: Creating Account objects public Account (String x, int y, double z) { name = x; acctNumber = y; balance = z; } Constructor method Transactions class Account class Constructor deposit() withdraw() getBalance() toString() double balance; int acctNumber; String name;

CSC 1051 M.A. Papalaskari, Villanova University Account acct1 = new Account ("Ted Murphy", 72354, ); Transactions class: Creating Account objects public Account (String x, int y, double z) { name = x; acctNumber = y; balance = z; } Constructor method Transactions class Account class Constructor deposit() withdraw() getBalance() toString() double balance; int acctNumber; String name; acct acctNumber balance name "Ted Murphy"

acct acctNumber balance name "Ted Murphy" acct acctNumber balance name "Jane Smith" CSC 1051 M.A. Papalaskari, Villanova University Account acct2 = new Account ("Jane Smith", 69713, 40.00); Account acct1 = new Account ("Ted Murphy", 72354, ); Transactions class: Creating more Account objects Transactions class

Account class: Using methods acct acctNumber balance name "Ted Murphy" CSC 1051 M.A. Papalaskari, Villanova University acct1.deposit (25.85);

Account class: Using methods acct acctNumber balance name "Ted Murphy" CSC 1051 M.A. Papalaskari, Villanova University acct1.deposit (25.85); // // Deposits the specified amount into the account. // public void deposit (double x) { balance = balance + x; }

Account class: Using methods acct acctNumber balance name "Ted Murphy" CSC 1051 M.A. Papalaskari, Villanova University acct1.deposit (25.85); // // Deposits the specified amount into the account. // public void deposit (double x) { balance = balance + x; }

Account class: Using methods acct acctNumber balance name "Ted Murphy" CSC 1051 M.A. Papalaskari, Villanova University acct1.deposit (25.85); // // Deposits the specified amount into the account. // public void deposit (double x) { balance = balance + x; }

Account class: Using methods acct acctNumber balance name "Ted Murphy" CSC 1051 M.A. Papalaskari, Villanova University acct1.deposit (25.85); // // Deposits the specified amount into the account. // public void deposit (double x) { balance = balance + x; }

Account class: Another Example acct acctNumber balance name "Ted Murphy" CSC 1051 M.A. Papalaskari, Villanova University acct1.withdraw (60, 2.50);

Account class: Another Example acct acctNumber balance name "Ted Murphy" CSC 1051 M.A. Papalaskari, Villanova University acct1.withdraw (60, 2.50); // // Withdraws the specified amount from the // account and applies the fee. // public void withdraw (double x, double fee) { balance = balance - x - fee; }

Account class: Another Example acct acctNumber balance name "Ted Murphy" CSC 1051 M.A. Papalaskari, Villanova University // // Withdraws the specified amount from the account // and applies the fee. // public void withdraw (double x, double fee) { balance = balance - x - fee; } acct1.withdraw (60, 2.50);

Class definitions A class can contain data declarations and method declarations Data declarations (also called fields) Method declarations (note: the constructor is also a method) CSC 1051 M.A. Papalaskari, Villanova University Constructor deposit() withdraw() getBalance() toString() double balance; int acctNumber; String name;

Bank Account Example There are some improvements that can be made to the Account class The design of some methods could also be more robust, such as verifying that the amount parameter to the withdraw() method is positive Some of these improvements are in the book examples Account.java, Transactions.java (simplified versions)Account.javaTransactions.java Account.java, Transactions.java (book versions)Account.java Transactions.java CSC 1051 M.A. Papalaskari, Villanova University

//******************************************************************** // Transactions.java Author: MA Papalaskari // (based on Lewis/Loftus example) // Demonstrates the creation and use of multiple Account objects. //******************************************************************** public class Transactions { // // Creates some bank accounts and requests various services. // public static void main (String[] args) { Account acct1 = new Account ("Ted Murphy", 72354, ); Account acct2 = new Account ("Jane Smith", 69713, 40.00); Account acct3 = new Account ("Edward Demsey", 93757, ); System.out.println (acct1); System.out.println (acct2); System.out.println (acct3); acct1.deposit (25.85); acct1.withdraw (60, 2.50); System.out.println (); System.out.println (acct1.toString()); System.out.println (acct2.toString()); System.out.println (acct3.toString()); } }

CSC 1051 M.A. Papalaskari, Villanova University //******************************************************************** // Transactions.java Author: MA Papalaskari // (based on Lewis/Loftus example) // Demonstrates the creation and use of multiple Account objects. //******************************************************************** public class Transactions { // // Creates some bank accounts and requests various services. // public static void main (String[] args) { Account acct1 = new Account ("Ted Murphy", 72354, ); Account acct2 = new Account ("Jane Smith", 69713, 40.00); Account acct3 = new Account ("Edward Demsey", 93757, ); System.out.println (acct1); System.out.println (acct2); System.out.println (acct3); acct1.deposit (25.85); acct1.withdraw (60, 2.50); System.out.println (); System.out.println (acct1); System.out.println (acct2); System.out.println (acct3); } } Sample Run Ted Murphy $ Jane Smith $ Edward Demsey $ Ted Murphy $ Jane Smith $ Edward Demsey $759.32

acct acctNumber balance name "Ted Murphy" CSC 1051 M.A. Papalaskari, Villanova University toString() method System.out.println(acct1.toString()); public String toString () { NumberFormat fmt = NumberFormat.getCurrencyInstance(); return (acctNumber +"\t”+ name +"\t”+ fmt.format(balance)); }

acct acctNumber balance name "Ted Murphy" CSC 1051 M.A. Papalaskari, Villanova University toString() method System.out.println(acct1.toString()); public String toString () { NumberFormat fmt = NumberFormat.getCurrencyInstance(); return (acctNumber +"\t”+ name +"\t”+ fmt.format(balance)); } can be omitted!

Examples of Classes CSC 1051 M.A. Papalaskari, Villanova University

Another example: The Die Class See RollingDice.java RollingDice.java See Die.java Die.java CSC 1051 M.A. Papalaskari, Villanova University

UML Class Diagrams UML = Unified Modelling Language Example: A UML class diagram for the RollingDice program: RollingDice main (args : String[]) : void Die faceValue : int roll() : int setFaceValue (value : int) : void getFaceValue() : int toString() : String

CSC 1051 M.A. Papalaskari, Villanova University //******************************************************************** // 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; int sum; die1 = new Die(); die2 = new Die(); die1.roll(); die2.roll(); System.out.println ("Die One: " + die1 + ", Die Two: " + die2); 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); } Sample Run Die One: 5, Die Two: 2 Die One: 1, Die Two: 4 Sum: 5 Die One: 4, Die Two: 2 New sum: 6

CSC 1051 M.A. Papalaskari, Villanova University //******************************************************************** // Die.java Author: Lewis/Loftus // // Represents one die (singular of dice) with faces showing values // between 1 and 6. //******************************************************************** 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() { faceValue = 1; } // // Rolls the die and returns the result. // public int roll() { faceValue = (int)(Math.random() * MAX) + 1; return faceValue; } continue

CSC 1051 M.A. Papalaskari, Villanova University continue // // Face value mutator. // public void setFaceValue (int value) { faceValue = value; } // // Face value accessor. // public int getFaceValue() { return faceValue; } // // Returns a string representation of this die. // public String toString() { String result = Integer.toString(faceValue); return result; }