Creating Simple Classes. Outline of Class Account Class Account Account # Balance Holder name phone# Overdrawn (true/false) Data Members Open Credit Debit.

Slides:



Advertisements
Similar presentations
The Line Class Suppose you are involved in the development of a large mathematical application, and this application needs an object to represent a Line.
Advertisements

Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Introduction to Object-Oriented Programming CS 21a: Introduction to Computing I First Semester,
Chapter 3 – Implementing Classes. Chapter Goals To become familiar with the process of implementing classes To be able to implement simple methods To.
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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
CHAPTER 2 OBJECTS AND CLASSES Goals: To understand the concepts of classes and objects To realize the difference between objects and object references.
ECE122 L6: Problem Definition and Implementation February 15, 2007 ECE 122 Engineering Problem Solving with Java Lecture 6 Problem Definition and Implementation.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Classes with multiple methods Part 1. Review of classes & objects Early on, we learned that objects are the basic working units in object-oriented programs.
Object-oriented analysis (OOA) techniques are used to (1) study existing objects to see if they can be reused or adapted for new uses, and (2) define new.
Understanding class definitions Looking inside classes.
Lecture 3. 2 Introduction Java is a true OO language -the underlying structure of all Java programs is classes. Everything must be encapsulated in a class.
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Classes CS 21a: Introduction to Computing I First Semester,
ACO 101: Introduction to Computer Science Anatomy Part 2: Methods.
CS61B L03 Building Objects (1)Garcia / Yelick Fall 2003 © UCB  Dan Garcia ( Kathy Yelick  (
Encapsulation, Inheritance & Polymorphism. OOP Properties Encapsulation ­The process of creating programs so that information within a class is not accessible.
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
CSSE501 Object-Oriented Development. Chapter 4: Classes and Methods  Chapters 4 and 5 present two sides of OOP: Chapter 4 discusses the static, compile.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
Java - Classes JPatterson. What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you.
CSE 501N Fall ‘09 04: Introduction to Objects 08 September 2009 Nick Leidenfrost.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Classes. Student class We are tasked with creating a class for objects that store data about students. We first want to consider what is needed for the.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
IT108 Objects and Classes Part I George Mason University Revised 4/3/2012.
Java methods Methods break down large problems into smaller ones Your program may call the same method many times saves writing and maintaining same code.
Review TEST 2 Chapters 4,5,7. QUESTION For which type of operands does the == operator always work correctly: (a) int, (b) double, or (c) String?
SEEM Java – Basic Introduction, Classes and Objects.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
Object Oriented Programming and Data Abstraction Rowan University Earl Huff.
72 4/11/98 CSE 143 Abstract Data Types [Sections , ]
Non-Static Classes What is the Object of this lecture?
Methods.
Java: Variables and Methods By Joshua Li Created for the allAboutJavaClasses wikispace.
Topics Instance variables, set and get methods Encapsulation
Class Fundamentals BCIS 3680 Enterprise Programming.
Chapter 3 Implementing Classes
Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance.
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 4th Edition.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Computer Programming II Lecture 5. Introduction to Object Oriented Programming (OOP) - There are two common programming methods : procedural programming.
Topic: Classes and Objects
Creating Your Own Classes
Defining Classes and Methods
Classes & Objects: Examples
Classes, Encapsulation, Methods and Constructors (Continued)
More on Classes and Objects
Class Everything if Java is in a class. The class has a constructor that creates the object. public class ClassName private Field data (instance variables)
Recap Week 2 and 3.
JAVA CLASSES.
AN INTRODUCTION TO OBJECTS AND CLASSES
Classes CS 21a: Introduction to Computing I
Introduction to Object-Oriented Programming
CS 1054: Lecture 2, Chapter 1 Objects and Classes.
Presentation transcript:

Creating Simple Classes

Outline of Class Account Class Account Account # Balance Holder name phone# Overdrawn (true/false) Data Members Open Credit Debit Check_balance Print_account_information Operations

There are 3 categories of methods Constructors: –Creates an instance of a class –Name is always the same as the class itself: Account() –Class_name object_name = new Class_name(); Mutators –Changes the state of an object Accessors –Retrieves a value from an object and returns it, or displays values from an object. It does not change the state of an object.

Define the data type of each attribute Account #: Long, integer number, each unique Balance: Float Holder name: String phone#: String Overdrawn (true/false): Boolean, to be used later

Define in words the purpose of each operation OPEN: to open an account we would need to –Get Holder information including the name of both the holder and co-holder, and their address information & phone #. –Create an account # –Set an initial balance –This method is a constructor.. the name will be “Account” instead of open

Credit: –Add a specific amount to the account balance –This method is a mutator, but does not return a value Debit: –Deducts the amount from the account balance –This method is a mutator, and does not return a value

Print Account Information: –Displays all information about the account, including the holders name, phone number, the account number, and current balance. –This method is an Accessor, but there is no need to return a value CheckBalance: –Returns the current account balance upon request –This method is an Accessor and returns a value

A class definition consists of variables called data members or instance fields. These hold the state of an object. Variables which are class data members, only one instance of these variables are ever created. Constructors used to create objects of the class. Methods used to modify the state of the object, and to report on the state of the object.

Basic outline of a class definition Access_Specifier class Class_name{ …. Class members go here ….. // class variables // instance variables //constructors // class - static methods // instance methods }

Access specifiers Private: If a member of a class is private, you CANNOT access it outside the class. It can only be used inside the body of the class itself. Public: If a member of a class if public, it can be accessed outside the class. Static: The static modifier, can be combined with either of the previous modifiers. In terms of a method, it simply means that the method can be called independently of being applied to an individual object… These are static methods that we saw in class Math. When applied to a data member (instance variable), the variable is a CLASS variable, and only one instance of the variable will be created, and will belong to and be shared by all objects of the class. These variables are created and initialized, even if no objects of a class are declared.

To declare a variable in a class definition the syntax is: Access_specifier type variable_name; To declare a method in a class definition the syntax is: Access_specifier return_type Method_name(parameter_list) { executable code }

Outline of class Account public class Account { // put class variables here // put instance variables here // put constructors here // put static methods here // put instance methods here }

public class Account { // CLASS VARIABLES GO HERE private static int accountNumber = 1; // INSTANCE VARIABLES GO HERE private int account; private String holderName; private String holderPhoneNumber; private double balance; private boolean overdrawn = false; // CONSTRUCTORS GO HERE public Account(String holder, String phone, double newBalance) { account = accountNumber; accountNumber++; holderName = holder; holderPhoneNumber = phone; balance = newBalance; }

// STATIC METHODS GO HERE public static void ShowNextAccountNumber() { System.out.println("the next available account number is: "+ accountNumber); return; } // INSTANCE METHODS GO HERE public void credit (double amount) { balance = balance + amount; return; } public void debit (double amount) { balance = balance - amount; return; } public void printAccountInformation() { System.out.println(" The account number is : " + account); System.out.println(" The account holder is : " + holderName); System.out.println(" The account holder's phone number is : " + holderPhoneNumber); System.out.println(" The account balance is : " + balance); System.out.println(" The account is overdrawn : " + overdrawn); return; }

Testing Account public class testAccount { public static void main (String args[]) { Account myAccount; Account tomsAccount; myAccount = new Account("Camille Hayhurst", " ", ); tomsAccount = new Account("Tom Jones", " ", ); System.out.println("\n"); myAccount.printAccountInformation(); System.out.println("\n"); tomsAccount.printAccountInformation(); System.out.print("\n If we add another account "); Account.showNextAccountNumber(); myAccount.credit( ); System.out.println("\n After the credit my account balance is: " + myAccount.checkBalance()); tomsAccount.debit(1543.0); System.out.println("\n After the debit tom's account balance is: " + tomsAccount.checkBalance()); System.out.println("\n"); myAccount.printAccountInformation(); System.out.println("\n"); tomsAccount.printAccountInformation(); }

myAccount = new Account("Camille Hayhurst", " ", ); tomsAccount = new Account("Tom Jones", " ", ); account: 1 holderName: Camille Hayhurst holderPhoneNumber: balance: overdrawn = false; account: 2 holderName: Tom Jones holderPhoneNumber: balance: overdrawn = false; accountNumber = 1

System.out.println("\n"); myAccount.printAccountInformation(); System.out.println("\n"); tomsAccount.printAccountInformation(); The account number is : 1 The account holder is : Camille Hayhurst The account holder's phone number is : The account balance is : The account is overdrawn : false The account number is : 2 The account holder is : Tom Jones The account holder's phone number is : The account balance is : The account is overdrawn : false

System.out.print("\n If we add another account "); Account.showNextAccountNumber(); If we add another account the next available account number is: 3

myAccount.credit( ); System.out.println("\n After the credit my account balance is: " + myAccount.checkBalance()); tomsAccount.debit(1543.0); System.out.println("\n After the debit tom's account balance is: " + tomsAccount.checkBalance()); After the credit my account balance is: After the debit tom's account balance is:

System.out.println("\n"); myAccount.printAccountInformation(); System.out.println("\n"); tomsAccount.printAccountInformation(); The account number is : 1 The account holder is : Camille Hayhurst The account holder's phone number is : The account balance is : The account is overdrawn : false The account number is : 2 The account holder is : Tom Jones The account holder's phone number is : The account balance is : The account is overdrawn : false