ACM/JETT Workshop - August 4-5, 2005 1 06: Defining Classes in Java.

Slides:



Advertisements
Similar presentations
Classes and Objects. What is Design? The parts of the software including – what information each part holds – what things each part can do – how the various.
Advertisements

Class Relationships Part 1: Composition and Association CS 21a: Introduction to Computing I First Semester,
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.
Introduction to Object-Oriented Programming CS 21a: Introduction to Computing I First Semester,
ACM/JETT Workshop - August 4-5, :Inheritance and Interfaces.
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.
1 Using Classes and Working With Class Interfaces November 20, 2002 CSE103 - Penn State University Prepared by Doug Hogan.
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.
The Java Programming Language  Simple – but abstract  Safe  Platform-independent ("write once, run anywhere")  Has a Rich growing library  Designed.
CSM-Java Programming-I Spring,2005 Class Design Lesson - 4.
Inheritance Part III. Lecture Objectives To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke.
CHAPTER 2 OBJECTS AND CLASSES Goals: To understand the concepts of classes and objects To realize the difference between objects and object references.
1 Classes and Objects Overview l Classes and Objects l Constructors l Implicit Constructors l Overloading methods this keyword l public, private and protected.
Object Oriented Concepts in Java Objects Inheritance Encapsulation Polymorphism.
Chapter 3 Implementing Classes. Chapter Goals To become familiar with the process of implementing classes To be able to implement simple methods To understand.
Unit 081 Introduction to Nested Classes Nested classes are classes defined within other classes The class that includes the nested class is called the.
Using Objects Object: an entity in your program that you can manipulate Attributes Methods Method: consists of a sequence of instructions that can access.
© The McGraw-Hill Companies, 2006 Chapter 7 Implementing classes.
ACM/JETT Workshop - August 4-5, Object-Oriented Basics & Design.
ACM/JETT Workshop - August 4-5, 2005 UML Modeling using MagicDraw UML for Java Programmers.
Programming Languages and Paradigms Object-Oriented Programming.
CS 106 Introduction to Computer Science I 04 / 13 / 2007 Friday the 13 th Instructor: Michael Eckmann.
ACM/JETT Workshop - August 4-5, ExceptionHandling and User Interfaces (Event Delegation, Inner classes) using Swing.
CMP-MX21: Lecture 6 Objects & Methods 1 Steve Hordley.
Writing Classes (Chapter 4)
CSE 1301 Lecture 11 Object Oriented Programming Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Introduction to Object-Oriented Programming
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Java programs are built from classes. There is nothing else, but classes.
CSE 1302 Lecture 7 Object Oriented Programming Review Richard Gesick.
ACO 101: Introduction to Computer Science Anatomy Part 2: Methods.
1 21 COP 3540 Data Structures with OOP Overview: Chapter 1.
CSE 143 Lecture 2 More ArrayList ; classes and objects reading: 10.1; slides created by Marty Stepp and Hélène Martin
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Lecture 101 CS110 Lecture 10 Thursday, February Announcements –hw4 due tonight –Exam next Tuesday (sample posted) Agenda –questions –what’s on.
RECITATION 4. Classes public class Student { } Data Members public class Student { private String name; public String id; }
ACO 101: Intro to Computer Science Anatomy Part 3: The Constructor.
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
Teach.NET Workshop Series Track 4: AP Computer Science with.NET and J#
Problem 1 Bank.  Manage customers’ bank account using the following operations: Create a new account given a customer’s name and initial account. Deposit.
Programming Languages and Paradigms Activation Records in Java.
Bank Account Example public class BankAccount { private double balance; public static int totalAccounts = 0; public BankAccount() { balance = 0; totalAccounts++;
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
Classes - Intermediate
Classes CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Monday, Jan 27, 2003Kate Gregory with material from Deitel and Deitel Week 4 Questions from Last Week Hand in Lab 2 Classes.
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.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 6 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 3: An Introduction to Classes 1 Chapter 3 An Introduction to Classes.
Outline Anatomy of a Class Encapsulation Anatomy of a Method Graphical Objects Graphical User Interfaces Buttons and Text Fields Copyright © 2012 Pearson.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
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.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
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.
Lecture 3 John Woodward.
OBJECT ORIENTED PROGRAMMING II LECTURE 7 GEORGE KOUTSOGIANNAKIS
Implementing Classes Yonglei Tao.
suggested reading: Java Ch. 6
Chapter Three - Implementing Classes
You can work in groups on this program.
Anatomy of a Method.
JAVA Constructors.
Object Oriented Programming Review
JAVA CLASSES.
AN INTRODUCTION TO OBJECTS AND CLASSES
Introduction to Object-Oriented Programming
Java 1/31/2017 Back to Objects.
Previous Lecture: Today’s Lecture: Reading (JV):
Presentation transcript:

ACM/JETT Workshop - August 4-5, : Defining Classes in Java

ACM/JETT Workshop - August 4-5, Topics In this Lecture, we will examine the details of a Java class definition: –Constructors –Access methods –Using Containment

ACM/JETT Workshop - August 4-5, What to expect After this lecture, you should be able to use the given code in BankAccount.java file and do the following: –Add a new method to class BankAccount. –Add an object instance as a data member into class BankAccount. –Use the methods defined in BankAccount –Compile and test your code.

ACM/JETT Workshop - August 4-5, What are constructors Constructors are methods that initialize the instance variables of an object. Constructors have the same name as the class name and they have no return type. public BankAccount(String acctId, double balance){ // initialise instance variables this.acctId = acctId; this.balance = balance; }

ACM/JETT Workshop - August 4-5, What are constructors A class can have multiple constructors (method overloading). A default constructor is a parameter-less constructor. public BankAccount(){ this.acctId = “Not set”; this.balance = 0.0; } public BankAccount(){ // Call the two parameter constructor // that is defined this (“Not set”,0.0); }

ACM/JETT Workshop - August 4-5, What is “this”? this” is a Java keyword that refers to the object being manipulated by a constructor or instance method.

ACM/JETT Workshop - August 4-5, When is a constructor invoked? public static void main(String [] args){ BankAccount a1 = new BankAccount (); BankAccount a2 = new BankAccount (“A12”,200.00) } Show class BankAccount Example1_6 Show class BankAccount Example1_6

ACM/JETT Workshop - August 4-5, More methods We create an instance of BankAccount and store the reference in a2 as below: BankAccount a2 = new BankAccount (“A12”,200.00); What if we want to change the acct id to “A13” or What if we want to change the balance to “100”? a2.acctId = “A13” a2.balance = ; Will this work?

ACM/JETT Workshop - August 4-5, More methods We need to add some public access methods to set and get the values of private data members. Once the set and get methods are available in the class, we can change the balance using the set method a2.setBalance(100.00); Show class BankAccount With set and get methods Example2_6 Show class BankAccount With set and get methods Example2_6

ACM/JETT Workshop - August 4-5, More methods Let us complete the definitions for the methods, deposit () and withdraw() in BankAccount. The deposit method should add an amount to the balance of a BankAccount object. The amount is passed in as a parameter. The withdraw method should return the requested amount, if the balance >= requested amount; the amount then should be subtracted from the balance. Show class BankAccount With deposit () and withdraw() Show class BankAccount With deposit () and withdraw()

ACM/JETT Workshop - August 4-5, Using an instance of BankAccount Let us now see how we can create an instance of BankAccount, deposit and withdraw money from it. BankAccount ba1 = new BankAccount("A123",250.00); ba1.deposit (100.00); double amt = ba1.withdraw(50.00); BankAccount ba2 = new BankAccount(“B222",amt);

ACM/JETT Workshop - August 4-5, Add more meaning to class BankAccount Bank Accounts generally belong to specific banks. How do we add this meaning to our class BankAccount? Firstly, we need a class called Bank. A Bank has a branch name and city. In addition, a Bank maintains a list of BankAccounts.

ACM/JETT Workshop - August 4-5, Class Bank We will add the following data members to class Bank. branchName and city (strings) accounts, an array to hold a number of BankAccounts.

ACM/JETT Workshop - August 4-5, Class Bank We will add a constructor to initialize the new data member, parentBank. How do we add a bank account to the list of accounts in Bank? We will define a method called add (BankAccount) in the class Bank. Show class Bank

ACM/JETT Workshop - August 4-5, Add more meaning to BankAccount Now, we can show this relationship between a Bank and a BankAccount using a graphical notation as below:

ACM/JETT Workshop - August 4-5, Connect Bank and BankAccount Let us add –a new data member, parentBank (of type Bank) to class BankAccount. –Then we add one more constructor to initialize the new data member. Show class BankAccount

ACM/JETT Workshop - August 4-5, The topics that you should be comfortable with at this time Defining multiple constructors for a class. Composing objects with other instances. You can test your understanding of some of these topics in Lab 2.