Download presentation
Presentation is loading. Please wait.
Published byDora Chandler Modified over 9 years ago
1
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/ CSC 1051 M.A. Papalaskari, Villanova University Designing Classes – part 2
2
Getting to know classes so far Predefined classes from the Java API. Defining classes of our own … CSC 1051 M.A. Papalaskari, Villanova University Driver classes: –Transactions –RollingDice –YouVeGotShoes (Project 5) –PeopleBeingPeople (Lab 8) Our classes: –Account –Die –Shoe –Person
3
Next: Review what we learned so far Focus on method definition Encapsulation CSC 1051 M.A. Papalaskari, Villanova University
4
class declaration CSC 1051 M.A. Papalaskari, Villanova University Constructor deposit() withdraw() long acctNumber; double balance; String name; Data declarations Method declarations toString() Review
5
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, 102.56); 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
6
acct1 72354 acctNumber 102.56 balance name "Ted Murphy" CSC 1051 M.A. Papalaskari, Villanova University Object creation: Constructors Account acct1 = new Account ("Ted Murphy", 72354, 102.56); public Account (String owner, long account, double initial) { acctNumber = account; balance = initial; name = owner; } Review
7
Account class acct1 72354 acctNumber 128.41 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
8
acct1 72354 acctNumber 102.56 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
9
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
10
myMethod(); myMethodcompute Method Control Flow If the called method is in the same class, only the method name is needed CSC 1051 M.A. Papalaskari, Villanova University
11
doIt helpMe helpMe(); obj.doIt(); main Method Control Flow The called method is often part of another class or object CSC 1051 M.A. Papalaskari, Villanova University
12
More Method Examples: Write a method that has one int parameter num, and prints “Happy Birthday” num times CSC 1051 M.A. Papalaskari, Villanova University
13
More Method Examples: Write a method with two double paramenters a and b that computes and returns the sum of squares: a 2 + b 2 of its two int parameters CSC 1051 M.A. Papalaskari, Villanova University
14
More Method Examples: Write a method that has one int parameter num, and returns the String “Happy Birthday” num times CSC 1051 M.A. Papalaskari, Villanova University
15
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
16
Violating Encapsulation experiment Revisit your solution for the Account Class ExerciseAccount Class Exercise Add some code to the OnePercent.java class to modify the value of an instance variable, eg: Now modify Account.java – insert the word private in front of that variable declaration: Re-compile the Account class and run your program again. Note the error you get. CSC 1051 M.A. Papalaskari, Villanova University acct1.name = “Joe”; private String name;
17
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
18
Violating Encapsulation - Example It is possible for a class to access the instance data of another class directly – but it’s not a good idea! 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
19
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
20
public constants are ok - Example Add some code to OnePercent.java to print out the interest rate used for the accounts: System.out.println ("Interest rate = " + acct1.RATE); CSC 1051 M.A. Papalaskari, Villanova University
21
public constants are ok - Example Add some code to OnePercent.java to print out the interest rate used for the accounts: System.out.println ("Interest rate = " + acct1.RATE); Normally, constants are declared as static. If RATE had been declared as follows: final static double RATE = 0.03; Then the last statement above would have been: System.out.println ("Interest rate = " + Account.RATE); CSC 1051 M.A. Papalaskari, Villanova University
22
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
23
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
24
Encapsulation – Mutators (setters) can restrict access to the data, as appropriate Example: Say a class has an instance variable: private int quantity; The mutator may also work to ensure that quantity, does not become negative: 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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.