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

1 Classes, Encapsulation, Methods and Constructors (Continued) Class definitions Instance data Encapsulation and Java modifiers Method declaration and.
Chapter 4: Writing Classes
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.
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.
Classes, Encapsulation, Methods and Constructors
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 Presentation slides for Java Software Solutions Foundations of Program Design Third Edition by John Lewis and William Loftus.
Chapter 4 Writing Classes 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights reserved.
ELC 312 Day 6. © 2004 Pearson Addison-Wesley. All rights reserved4-2 Agenda Questions? Problem set two Posted in WebCT  Due Sept 30  On Page 153&154.
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.
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.
Chapter 4 -2 part Writing Classes 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All.
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:
© 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 – 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.
Outline Anatomy of a Class Encapsulation Anatomy of a Method Copyright © 2014 Pearson Education, Inc.
© 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.
Classes, Encapsulation, Methods and Constructors (Continued)
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)
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 Writing Classes – Part 3

Getting to know classes so far Predefined classes from the Java API. We have defined a few classes of our own: –Account –Die –Book –Stock CSC 1051 M.A. Papalaskari, Villanova University

Getting to know classes so far Predefined classes from the Java API. We have defined a few classes of our own: –Account –Die –Book –Stock CSC 1051 M.A. Papalaskari, Villanova University Driver classes: –Transactions, OnePercent (Lab 8) –RollingDice –Bookshelf (Project 8) –(HW exercises: Writing Classes)HW exercises: Writing Classes

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

Today Review what we learned so far Encapsulation UML diagrams 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 Transactions1 { // // 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); System.out.println (acct1); System.out.println (acct2); acct1.deposit (25.85); System.out.println (); System.out.println (acct1); System.out.println (acct2); } } Review driver classes

class declaration CSC 1051 M.A. Papalaskari, Villanova University Constructor deposit() withdraw() long acctNumber; double balance; String name; Data declarations Method declarations toString() Review

Method definition –parameters –return type –return statement char calc (int num1, int num2, String message) { int sum = num1 + num2; char result = message.charAt (sum); return result; } ch = obj.calc (25, count, "Hello"); CSC 1051 M.A. Papalaskari, Villanova University Review

CSC 1051 M.A. Papalaskari, Villanova University using methods… Review

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

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 amount) { balance = balance + amount; } Review

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 amount) { balance = balance + amount; } Review

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 amount) { balance = balance + amount; } Review

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 amount) { balance = balance + amount; } Review

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

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

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

acct acctNumber balance name "Ted Murphy" CSC 1051 M.A. Papalaskari, Villanova University Object creation: Constructors Account acct1 = new Account ("Ted Murphy", 72354, ); public Account (String owner, long account, double initial) { acctNumber = account; balance = initial; name = owner; } Review

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

CSC 1051 M.A. Papalaskari, Villanova University Invoking methods in same class paintSnowman(page); public void paintSnowman(Graphics page, int x, int y) { // snowman code goes here... } Review

CSC 1051 M.A. Papalaskari, Villanova University static methods: 1) In same class doSomething(); public static void doSomething() { System.out.println(" At your service. " ); } Review

CSC 1051 M.A. Papalaskari, Villanova University static methods: 2) In another class OtherClass.doSomething(); public static void doSomething() { System.out.println(" At your service. " ); } Review OtherClass

encapsulation and Java modifiers UML diagrams CSC 1051 M.A. Papalaskari, Villanova University What’s next?

Encapsulation An encapsulated object can be thought of as a black box -- its inner workings are hidden from the client The client invokes the interface methods and they manage the instance data Methods Data Client CSC 1051 M.A. Papalaskari, Villanova University

Violating Encapsulation It is possible for a class to access the instance data of another class directly Methods Data Client CSC 1051 M.A. Papalaskari, Villanova University

Violating Encapsulation - Example It is possible for a class to access the instance data of another class directly - but don’t do this! See Account.java (modified) Account.java See ImInUrClassMessingUrInstanceData.java ImInUrClassMessingUrInstanceData.java deposit() withdraw() addInterest() name acctNumber balance Transactions.java CSC 1051 M.A. Papalaskari, Villanova University acct1.name = “Joe”; Account.java

Visibility Modifiers In Java, we enforce encapsulation through the appropriate use of visibility modifiers: –public – can be referenced from other classes –private – can be referenced only within that class: –protected – involves inheritance (discussed later) Data declared without a visibility modifier have default visibility and can be referenced by any class in the same package An overview of all Java modifiers is presented in Appendix E CSC 1051 M.A. Papalaskari, Villanova University

public constants are ok - Example Account acct1 = new Account (”Sartre", 72354, ); System.out.println (acct1); System.out.println ("Interest rate = " + acct1.RATE); CSC 1051 M.A. Papalaskari, Villanova University

Visibility Modifiers – the RULES publicprivate Variables Methods Yes Yes, for support methods only Yes NO (but OK for public constants) CSC 1051 M.A. Papalaskari, Villanova University

Encapsulation – Accessing the data Indirect access through methods accessors and mutators (“getters” and “setters”) Usually named getX() and setX() deposit() withdraw() addInterest() name acctNumber balance Transactions.java CSC 1051 M.A. Papalaskari, Villanova University acct1.getBalance() Account.java Methods Data Example

Encapsulation – Accessing the data Mutators (setters) can restrict access to the data, as appropriate Example: public void setQuantity(int num) { if (num<0) { System.out.println("*Error in setQuantity()"); System.out.println("negative quantity."); System.out.println("quantity not changed."); } else quantity = num; } CSC 1051 M.A. Papalaskari, Villanova University

More Exercises More Exercises on writing classes: Encapsulation and UML diagrams 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); continue Example: RollingDice.java Example: RollingDice.java (Driver)

CSC 1051 M.A. Papalaskari, Villanova University continue 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); } Example: RollingDice.java Example: RollingDice.java (Driver)

CSC 1051 M.A. Papalaskari, Villanova University continue 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 Example: RollingDice.java Example: RollingDice.java (Driver)

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; } Example: Die.java 4 faceValue

CSC 1051 M.A. Papalaskari, Villanova University continue // // Rolls the die and returns the result. // public int roll() { faceValue = (int)(Math.random() * MAX) + 1; return faceValue; } // // Face value mutator. // public void setFaceValue (int value) { faceValue = value; } // // Face value accessor. // public int getFaceValue() { return faceValue; } continue Example: Die.java 4 faceValue

CSC 1051 M.A. Papalaskari, Villanova University continue // // Returns a string representation of this die. // public String toString() { String result = Integer.toString(faceValue); return result; } Example: Die.java 4 faceValue

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

UML Class Diagrams A UML class diagram for the RollingDice program: RollingDice main (args : String[]) : void Die faceValue : int roll() : int setFaceValue (int value) : void getFaceValue() : int toString() : String die1 4 faceValue

Homework Review Sections Review the handout exercises and do the extra exercise with the Dog class at the end handout exercises Review the example that violates encapsulation and make sure you understand it: –See Account.java (modified) Account.java –See ImInUrClassMessingUrInstanceData.java ImInUrClassMessingUrInstanceData.java Do Exercises EX 4.1, 4.4, 4.10 Some slides in this presentation are adapted from the slides accompanying Java Software Solutions by Lewis & Loftus CSC 1051 M.A. Papalaskari, Villanova University