CSC 204 - Programming I Lecture 8 September 9, 2002.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming Lecture 3 Writing Java Applications, Java Development Tools.
Advertisements

Topic 10 Java Memory Management. 1-2 Memory Allocation in Java When a program is being executed, separate areas of memory are allocated for each class.
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.
1 Chapter Three Using Methods. 2 Objectives Learn how to write methods with no arguments and no return value Learn about implementation hiding and how.
Introduction to Object-Oriented Programming CS 21a: Introduction to Computing I First Semester,
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.
 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Road Map Introduction to object oriented programming. Classes
11 Values and References Chapter Objectives You will be able to: Describe and compare value types and reference types. Write programs that use variables.
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Writing Classes (Chapter 4)
Data Objects (revisited) Recall that values are stored in data objects, and that each data object holds one value of a particular type. Data objects may.
Introduction to Object-Oriented Programming
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.
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.
Constructors CMSC 202. Object Creation Objects are created by using the operator new in statements such as… The following expression invokes a special.
CPSC 252 Dynamic Memory Allocation Page 1 Dynamic memory allocation Our first IntVector class has some serious limitations the capacity is fixed at MAX_SIZE.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
CSC 205 Java Programming II Inheritance Inheritance In the real world, objects aren’t usually one-of-a-kind. Both cars and trucks are examples of.
Chapter 3: Classes and Objects Java Programming FROM THE BEGINNING 1 Chapter 3 Classes and Objects.
Chapter 5 Introduction to Defining Classes
 Objects versus Class  Three main concepts of OOP ◦ Encapsulation ◦ Inheritance ◦ Polymorphism  Method ◦ Parameterized ◦ Value-Returning.
Using Objects. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 2 Java.
Finalizers, this reference and static Sangeetha Parthasarathy 06/13/2001.
CSI 3125, Preliminaries, page 1 Compiling the Program.
Chapter 3: Classes and Objects Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Classes and.
Chapter 3: Classes and Objects Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Classes and.
CSI 3125, Preliminaries, page 1 Class. CSI 3125, Preliminaries, page 2 Class The most important thing to understand about a class is that it defines a.
Programming Languages and Paradigms Activation Records in Java.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
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 Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
Peyman Dodangeh Sharif University of Technology Spring 2014.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
Chapter 4: More Object Concepts. Objectives Understand blocks and scope Overload a method Avoid ambiguity Create and call constructors with parameters.
Chapter 3: Classes and Objects Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Classes and.
Chapter 3: Classes and Objects Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Classes and.
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.
Internet Computing Module II. Syllabus Creating & Using classes in Java – Methods and Classes – Inheritance – Super Class – Method Overriding – Packages.
Chapter 3 Implementing Classes
Chapter 3: Classes and Objects Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Classes and.
© 2004 Pearson Addison-Wesley. All rights reserved September 5, 2007 Packages & Random and Math Classes ComS 207: Programming I (in Java) Iowa State University,
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
The need for Programming Languages
User-Written Functions
Lecture 3 John Woodward.
Introduction to Classes and Objects
Classes and Objects Chapter 3 Copyright © 2000 W. W. Norton & Company.
Constructors CMSC 202.
Classes and Objects Chapter 3 Copyright © 2000 W. W. Norton & Company.
Chapter Three - Implementing Classes
Classes and Objects Chapter 3 Copyright © 2000 W. W. Norton & Company.
Chapter 3 Introduction to Classes, Objects Methods and Strings
Anatomy of a Method.
Classes and Objects Chapter 3 Copyright © 2000 W. W. Norton & Company.
Assessment – Java Basics: Part 1
Classes and Objects Chapter 3 Copyright © 2000 W. W. Norton & Company.
Java Programming Language
Submitted By : Veenu Saini Lecturer (IT)
Introduction to Object-Oriented Programming
Classes and Objects Object Creation
Corresponds with Chapter 5
CMSC 202 Constructors Version 9/10.
Presentation transcript:

CSC Programming I Lecture 8 September 9, 2002

9/9/2002CSC Programming I2 Quiz 1 Score 75%

9/9/2002CSC Programming I3 Example: An Account Class Account.java public class Account { // Instance variables private double balance; // Constructors public Account(double initialBalance) { balance = initialBalance; } public Account() { balance = 0.0; }

9/9/2002CSC Programming I4 // Instance methods public void deposit(double amount) { balance += amount; } public void withdraw(double amount) { balance -= amount; } public double getBalance() { return balance; } public void close() { balance = 0.0; }

9/9/2002CSC Programming I5 Creating Objects  Once a class has been declared, it can be used to create objects (instances of the class).  Each instance will contain its own copy of the instance variables declared in the class.

9/9/2002CSC Programming I6 The new Keyword  The keyword new, when placed before a class name, causes an instance of the class to be created.  A newly created object can be stored in a variable: acct = new Account( ); Account acct = new Account( );  An object can also be created using the second constructor in the Account class: acct = new Account();

9/9/2002CSC Programming I7 Calling Instance Methods  Once an object has been created, operations can be performed on it by calling the instance methods in the object’s class.  Form of an instance method call: object. method-name ( arguments ) The parentheses are mandatory, even if there are no arguments.

9/9/2002CSC Programming I8 Message Passing YourBicycle.changeGears(lowerGear)

9/9/2002CSC Programming I9 Calling Account Instance Methods  Suppose that acct contains an instance of the Account class.  Example calls of Account instance methods: acct.deposit( ); acct.withdraw(500.00); acct.close();  An object must be specified when an instance method is called, because more than one instance of the class could exist: acct1.deposit( ); acct2.deposit( );

9/9/2002CSC Programming I10 Value Returned by Instance Method  When an instance method returns no result, a call of the method is an entire statement: acct.deposit( );  Result returned from a method call can be used in a variety of ways.  One possibility is to store it in a variable: double newBalance = acct.getBalance();  Another possibility is to print it: System.out.println(acct.getBalance());

9/9/2002CSC Programming I11 How Instance Methods Work  Sequence of events when an instance method is called: The program “jumps” to that method. The arguments in the call are copied into the method’s corresponding parameters. The method begins executing. When the method is finished, the program “returns” to the point at which the method was called.

9/9/2002CSC Programming I12 Programs with Multiple Classes  A program that tests the Account class: public class TestAccount { public static void main(String[] args) { Account acct1 = new Account( ); System.out.println("Balance in account 1: " + acct1.getBalance()); acct1.deposit(100.00); System.out.println("Balance in account 1: " + acct1.getBalance()); acct1.withdraw(150.00); System.out.println("Balance in account 1: " + acct1.getBalance()); acct1.close(); System.out.println("Balance in account 1: " + acct1.getBalance());

9/9/2002CSC Programming I13 Account acct2 = new Account(); System.out.println("Balance in account 2: " + acct2.getBalance()); acct2.deposit(500.00); System.out.println("Balance in account 2: " + acct2.getBalance()); acct2.withdraw(350.00); System.out.println("Balance in account 2: " + acct2.getBalance()); acct2.close(); System.out.println("Balance in account 2: " + acct2.getBalance()); }

9/9/2002CSC Programming I14 Output of the Program Balance in account 1: Balance in account 1: Balance in account 1: Balance in account 1: 0.0 Balance in account 2: 0.0 Balance in account 2: Balance in account 2: Balance in account 2: 0.0

9/9/2002CSC Programming I15 Compiling a Program with Multiple Classes  The TestAccount class, together with the Account class, form a complete program.  If the classes are stored in separate files, they could be compiled using the following commands: javac Account.java javac TestAccount.java  As an alternative, both files can be compiled with a single command: javac TestAccount.java

9/9/2002CSC Programming I16 Compiling a Program with Multiple Classes  When a file is compiled, the compiler checks whether its dependent classes are up-to-date.  If the.java file containing a dependent class has been modified since the.class file was created, javac will recompile the.java file automatically.  When TestAccount.java is compiled, the javac compiler will look for Account.java and compile it if necessary.

9/9/2002CSC Programming I17 Executing a Program with Multiple Classes  Command to execute the TestAccount program: java TestAccount The Account class is not mentioned.

9/9/2002CSC Programming I18 Using a Single File  The Account and TestAccount classes can be put in the same file. The file will need to be named TestAccount.java, because TestAccount contains the main method. The public access modifier will have to be removed from the beginning of the Account class declaration.  Compiling TestAccount.java causes TestAccount.class and Account.class to be generated.

9/9/2002CSC Programming I19 Using a Single File  It’s often better to put only one class in each file.  Advantages: Classes are easier to locate. Files are smaller and easier to edit. If a class declaration is changed, only the class itself will have to be recompiled.

9/9/2002CSC Programming I20 3.7How Objects Are Stored  A variable of an ordinary (non-object) type can be visualized as a box: int i;  Assigning a value to the variable changes the value stored in the box: i = 0;

9/9/2002CSC Programming I21 Object Variables  An object variable, on the other hand, doesn’t actually store an object. Instead, it will store a reference to an object.  An object variable can still be visualized as a box: Account acct;  Suppose that a new object is stored into acct : acct = new Account(500.00); balance 1000

9/9/2002CSC Programming I22 Object Variables  The Account object isn’t stored in the acct box. Instead, the box contains a reference that “points to” the object: acct  In many programming languages, including C++, a variable such as acct would be called a pointer variable. balance 1000

9/9/2002CSC Programming I23 The null Keyword  To indicate that an object variable doesn’t currently point to an object, the variable can be assigned the value null : acct = null;  When an object variable stores null, it’s illegal to use the variable to call an instance method.  If acct has the value null, executing the following statement will cause a run-time error ( NullPointerException ): acct.deposit(500.00);

9/9/2002CSC Programming I24 Object Assignment  If i has the value 10, assigning i to j gives j the value 10 as well: j = i;  Changing the value of i has no effect on j : i = 20;  Assignment of objects doesn’t work the same way.

9/9/2002CSC Programming I25 Object Assignment  Assume that acct1 contains a reference to an Account object with a balance of $500.  Assigning acct1 to acct2 causes acct2 to refer to the same object as acct1 : acct2 = acct1;  acct1 and acct2 are said to be aliases, because both represent the same object. balance 1000 acct2 acct1

9/9/2002CSC Programming I26 Object Assignment  An operation that changes the acct1 object will also change the acct2 object, and vice-versa.  The statement acct1.deposit(500.00); will change the balance of acct2 to $ : balance 1000 acct2 acct1

9/9/2002CSC Programming I27 Cloning  Some classes allow the creation of a new object that’s identical to an existing object.  The new object is said to be a clone of the old one.  Clones are created by calling the clone method.

9/9/2002CSC Programming I28 Garbage  Objects can become “orphaned” during program execution.  Consider the following example: acct1 = new Account(100.00); acct2 = new Account(200.00); acct1 = acct2;  After these assignments, the object that acct1 previously referred to is lost. We say that it is garbage.

9/9/2002CSC Programming I29 Garbage Collection  Java provides automatic garbage collection: as a Java program runs, a software component known as the garbage collector watches for garbage and periodically “collects” it.  The recycled memory can be used for the creation of new objects.  Garbage collection normally takes place when the program isn’t doing any other useful activity.

9/9/2002CSC Programming I30 Memory Leaks  Other popular languages rely on the program to explicitly release memory that’s no longer needed.  This practice is potentially more efficient, but it’s also error-prone. Failing to recover garbage causes available memory to decrease (a memory leak). After a period of time, a program with a memory leak may run out of memory entirely. Releasing memory prematurely is even worse, often causing programs to crash.