Classes All Java code must be inside classes Class types – Utility classes Used to store constants, utility methods, etc. – Driver classes – Abstract Data.

Slides:



Advertisements
Similar presentations
Chapter 8: Designing Classes
Advertisements

IMPLEMENTING CLASSES Chapter 3. Black Box  Something that magically does its thing!  You know what it does but not how.  You really don’t care how.
Chapter 3 – Implementing Classes. Chapter Goals To become familiar with the process of implementing classes To be able to implement simple methods To.
Our BankAccount class should define three methods  deposit  withdraw  getBalance How a programmer will carry out these operations?  We assume that.
Chapter 3 Implementing Classes. Instance Variables Instance variables store the data of an object; the fields of an object. Instance of a class: an object.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
Chapter 2 – An Introduction to Objects and Classes Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Copyright © 2013 by John Wiley & Sons. All rights reserved. OBJECTS AND CLASSES CHAPTER Slides by Donald W. Smith TechNeTrain.com Final Draft 10/30/2011.
Chapter Goals To learn how to choose appropriate classes to implement
Classes. Object-Oriented Design Method for designing computer programs Consider “objects” interacting in the program –Example: a zoo, a gradebook.
Road Map Introduction to object oriented programming. Classes
The Java Programming Language  Simple – but abstract  Safe  Platform-independent ("write once, run anywhere")  Has a Rich growing library  Designed.
1 Classes Overview l Classes as Types l Declaring Instance Variables l Implementing Methods l Constructors l Accessor and Mutator Methods.
CHAPTER 2 OBJECTS AND CLASSES Goals: To understand the concepts of classes and objects To realize the difference between objects and object references.
Classes. Object-Oriented Design Method for designing computer programs Consider “objects” interacting in the program –Example: a zoo, a gradebook.
Object Oriented Programming CSC 171 FALL 2001 LECTURE 11.
Class Design CSC 171 FALL 2004 LECTURE 11. READING Read Chapter 7 It’s abstract But it should help with project #1.
1 Classes and Objects Overview l Classes and Objects l Constructors l Implicit Constructors l Overloading methods this keyword l public, private and protected.
Datalogi A 2: 15/9. Java Slides based on Horstmann chapter 2&3 Objects and classes Import, methods, references Implementing a class.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Chapter 3 Implementing Classes. Chapter Goals To become familiar with the process of implementing classes To be able to implement simple methods To understand.
Using Objects Object: an entity in your program that you can manipulate Attributes Methods Method: consists of a sequence of instructions that can access.
Chapter 3  Implementing Classes 1 Chapter 3 Implementing Classes.
Chapter 3 Implementing Classes. Chapter Goals To become familiar with the process of implementing classes To be able to implement simple methods To understand.
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 1 Chapter 7: More about Methods 1 Chapter 7 More about Methods.
Arrays and Objects OO basics. Topics Basic array syntax/use OO Vocabulary review Simple OO example Instance and Static methods Static vs. instance vs.
Writing Classes (Chapter 4)
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
ACO 101: Introduction to Computer Science Anatomy Part 2: Methods.
Chapter 3 Implementing Classes. Assignment Read 3.1 – 3.5 and take notes complete Self Check Exercises 1-10; Due September 24 th Read 3.6 – 3.8 and take.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 8 – Designing Classes.
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
ICOM 4015: Advanced Programming Lecture 3 Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Reading: Chapter Three:
Java - Classes JPatterson. What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Chapter 3 Introduction to Classes and Objects Definitions Examples.
ACM/JETT Workshop - August 4-5, : Defining Classes in Java.
CLASSES AND OBJECTS Object-Oriented Basics. Vocabulary Review – C++ Class Object Instance this new Constructor Default constructor Access (private/public/protected)
Encapsulation ◦ Blackbox concept Data and method(s) Hidden details InterfaceEffect(s) methods called class.
Bank Account Example public class BankAccount { private double balance; public static int totalAccounts = 0; public BankAccount() { balance = 0; totalAccounts++;
Object Oriented Programming and Data Abstraction Rowan University Earl Huff.
Classes CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Topics Instance variables, set and get methods Encapsulation
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
Class Everything in Java is in a class. The class has a constructor that creates the object. If you do not supply a constructor Java will create a default.
CSH Intro. to Java. The Big Ideas in Computer Science Beyond programming Solving tough problems Creating extensible solutions Teams of “Computational.
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 3: An Introduction to Classes 1 Chapter 3 An Introduction to Classes.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
Chapter 3 Implementing Classes
Comp1004: Building Better Objects II Encapsulation and Constructors.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Java 5 Class Anatomy. User Defined Classes To this point we’ve been using classes that have been defined in the Java standard class library. Creating.
A final variable is a constant
Lecture 3 John Woodward.
Chapter 3 – Implementing Classes
Java OOP Overview Classes and Objects, Members and Class Definition, Access Modifier, Encapsulation Java OOP Overview SoftUni Team Technical Trainers.
Implementing Classes Yonglei Tao.
7 CHAPTER OBJECTS AND CLASSES.
Chapter Goals To become familiar with the process of implementing classes To be able to implement and test simple methods To understand the purpose and.
Chapter Three - Implementing Classes
Chapter 3 Implementing Classes
د.سناء الصايغ الفصل الأول البرمجة الشيئية
Encapsulation and Constructors
More on Classes and Objects
Basics of OOP A class is the blueprint of an object.
JAVA CLASSES.
AN INTRODUCTION TO OBJECTS AND CLASSES
Classes.
Presentation transcript:

Classes All Java code must be inside classes Class types – Utility classes Used to store constants, utility methods, etc. – Driver classes – Abstract Data Types (ADTs) “blue-prints” to build objects

Defining ADT classes class attributes are normally encapsulated (i.e. private) class methods are normally public avoid using input/output inside the methods of a class static attributes and methods class constants

class BankAccount { private double balance; private int accountNumber; private static int lastAssignedNumber; //automatically initialized with zero public static final double OVERDRAFT_FEE=5; // constant /*overloaded constructors */ public BankAccount() { balance=0; lastAssignedNumber++; accountNumber= lastAssignedNumber; } public BankAccount(double initialBalance) { balance= initialBalance; lastAssignedNumber++; accountNumber= lastAssignedNumber; } public static int showLastAccountNumber(){ return lastAssignedNumber; } public void deposit(double amount) { double newBalance=balance+amount;//newBalance and amount are local variables balance=newBalance; } public void withdraw(double amount) {// a mutator method double newBalance=balance-amount; balance=newBalance; } Case Study

public double getBalance() { // an accessor method return balance; } public void transfer (double amount, BankAccount other) { // a side effect balance=balance-amount; other.balance=other.balance+amount; } } /* A class to test the BankAccount class */ public class BankAccountTest { public static void main (String[] args) { BankAccount harrysChecking=new BankAccount(); int a=100; BankAccount tomsChecking=new BankAccount(a); a=2000; harrysChecking.deposit(a); harrysChecking.withdraw(500); harrysChecking.transfer(500, tomsChecking); System.out.println(harrysChecking.getBalance()); System.out.println(tomsChecking.getBalance()); System.out.println(BankAccount. showLastAccountNumber()); System.out.println(BankAccount. OVERDRAFT_FEE); } }

Scope of variables Instance attributes/variables Local variables Parameter variables Static variables and constants

Parameter Passing Pass by value – Variables of primitive data types a=2000; harrysChecking.deposit(a); Pass by reference – Objects/arrays Checking.transfer(500, tomsChecking);

Other things to consider… Constructors – Default constructor Used only to initialize the encapsulated variables to the default values (0, false, null) Accessors and mutators – “Getters” and “setters” Static methods – Utility methods or – Used to handle static variables

public class CashRegister{ public CashRegister() { purchase = 0; payment = 0; } public void recordPurchase(double amount) { double total = purchase + amount; purchase = total; } public void enterPayment(double amount) { payment = amount; } public double giveChange() { double change = payment - purchase; purchase = 0; payment = 0; return change; } private double purchase; private double payment; } Case Study Cash Register

public class CashRegisterTester{ public static void main(String[] args) { CashRegister register = new CashRegister(); register.recordPurchase(29.50); register.recordPurchase(9.25); register.enterPayment(50); double change = register.giveChange(); System.out.println(change); } }