Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.

Slides:



Advertisements
Similar presentations
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.
Advertisements

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.
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.
Chapter 3: Implementing Classes Part 1. To become familiar with the process of implementing classes To be able to implement simple methods To understand.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
Function Overloading Having more than one function with the same name. list of argument of a function are called signature of a function. We can have more.
Chapter 9 – Inheritance Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
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.
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.
1 Basic Object Oriented Concepts Overview l What is Object-Orientation about? l What is an Object? l What is a Class? l Constructing Objects from Classes.
Datalogi A 2: 15/9. Java Slides based on Horstmann chapter 2&3 Objects and classes Import, methods, references Implementing a class.
Chapter 9: Classes with Instance Variables or Classes=Methods+Variables Asserting Java © Rick Mercer.
Chapter 3 Implementing Classes. Chapter Goals To become familiar with the process of implementing classes To be able to implement simple methods To understand.
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.
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.
© The McGraw-Hill Companies, 2006 Chapter 7 Implementing classes.
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
Writing Classes (Chapter 4)
Introduction to Object-Oriented Programming
Week 3 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Week 12 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Chapter Three: Implementing Classes.
Object-Oriented Programming in C++
Classes CS 21a: Introduction to Computing I First Semester,
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
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.
Chapter 3 – Implementing Classes Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Object-Oriented Design Simple Program Design Third Edition A Step-by-Step Approach 11.
Week 2 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
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:
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
CSCI 1100/1202 April 1-3, Program Development The creation of software involves four basic activities: –establishing the requirements –creating.
Week 14 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Fall 2006Slides adapted from Java Concepts companion slides1 Implementing Classes Advanced Programming ICOM 4015 Lecture 3 Reading: Java Concepts Chapter.
Encapsulation ◦ Blackbox concept Data and method(s) Hidden details InterfaceEffect(s) methods called class.
INFSY 535.  Small systems  Larger systems 1.Understand the program requirement- what 3. Write and test each part (unit testing) 4. Maintenance 2. Specify.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
72 4/11/98 CSE 143 Abstract Data Types [Sections , ]
Week 13 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 3: An Introduction to Classes 1 Chapter 3 An Introduction to Classes.
Chapter 3: Implementing Classes Part 1. To become familiar with the process of implementing classes To be able to implement simple methods To understand.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
Chapter 3 Implementing Classes
Lecture 3 John Woodward.
Data Structures and Algorithms revision
Chapter 3 – Implementing Classes
Chapter Three: Implementing Classes
Chapter 3: Using Methods, Classes, and Objects
Implementing Classes Yonglei Tao.
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 Three - Implementing Classes
Chapter 3 Implementing Classes
Introduction to Computer Science and Object-Oriented Programming
JAVA CLASSES.
AN INTRODUCTION TO OBJECTS AND CLASSES
Classes CS 21a: Introduction to Computing I
Introduction to Object-Oriented Programming
Introduction to Computer Science and Object-Oriented Programming
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham

Week 4 Topics Black Boxes Designing the Public Interface of a Class Commenting the Public Interface Instance Fields Implementing Constructors and Methods Testing a Class

4.1.1 Black Boxes A black box is any device whose inner workings are hidden A black box provides encapsulation, hiding unimportant details from people who don’t need them In order to achieve this, somebody needs to abstract out the essential concepts (abstraction) and provide a usable interface to the various users

4.1.1 Black Boxes Cont. A car can be a black box on several levels A driver interfaces with the car using pedals, knobs and buttons and does not need to know about, for example, an engine control module A mechanic can test, install and remove an engine control module, but does not need to know about the sensors and transistors that it is comprised of

4.1.1 Black Boxes Cont. A car parts manufacturer knows how to assemble an engine control module, but does not need to know about the inner workings of the capacitors and transistors provided to them by the electronic components manufacturer (they are black boxes to the car parts manufacturer)

4.1.1 Black Boxes Cont. Encapsulation: Programmer using an object knows about its behavior, but not about its internal structure For example, we cannot view the logic/code inside the String class methods However, we know the purpose of and how to use length, toUpperCase, replace and other String methods. We know what parameters to pass in (sequence and type) and what the return type is.

4.1.1 Black Boxes Cont. To abstract the essential details of a class, we must define its behavior, determine which characteristics are needed to solve our problem, then implement it For example, if we design a Car class as part of a video game, we might need to be able to accelerate, turn, shift etc. However, such behavior may be irrelevant for a vehicle registration system, so other aspects would be abstracted instead

4.1.2 Designing the Public Interface of a Class Behavior (abstraction) we might need for a Bank Account: deposit money, withdraw money, get balance Methods of a BankAccount class might be: deposit, withdraw, getBalance harrysChecking.deposit(2000); harrysChecking.withdraw(500); System.out.println(harrysChecking.getBal ance());

4.1.2 Designing the Public Interface of a Class Cont. Method definitions: 1.access specifier (such as public or private ) 2.return type (such as String or void ) 3.method name (such as deposit ) 4.list of parameters (such as double amount ) 5.method body in { }

4.1.2 Designing the Public Interface of a Class Cont. accessSpecifier returnType methodName(parameterType parameterName,...) { method body } public void deposit(double amount) {... } public void withdraw(double amount) {... } public double getBalance() {... }

4.1.2 Designing the Public Interface of a Class Cont. Constructors are similar to methods in that they have an access specifier, parameter list and body However, constructors are different in that their job is not to define a behavior but rather to properly initialize an object for later use

4.1.2 Designing the Public Interface of a Class Cont. Constructors are always named the same as the class, and constructors never have a return value All constructors of a class have the same name Compiler can tell constructors apart because they take different parameters

4.1.2 Designing the Public Interface of a Class Cont. The constructor body is executed when a new object is created Statements in constructor body will set the internal data of the object A class is a construct that holds all the methods, constructors, and instance fields needed to model an entity

4.1.2 Designing the Public Interface of a Class Cont. accessSpecifier ClassName(parameterType parameterName,...) { constructor body } public BankAccount(double initialBalance) {... } Example: BankAccount harrysChecking = new BankAccount(250.00);

4.1.3 Commenting the Public Interface Use documentation comments to describe the classes and public methods of your programs If you use the standard documentation form in your class, a program called javadoc can automatically generate a set of HTML pages Where have we previously seen javadoc pages?

4.1.3 Commenting the Public Interface Cont. Provide documentation comments for every class, every method, every parameter, and every return value Javadoc comment are contained within the symbols /** and */ It is a good idea to write the method comments first, before writing the code in the body of the method

4.1.3 Commenting the Public Interface Cont. /** Withdraws money from the bank amount the amount to withdraw */ public void withdraw(double amount) { // implementation filled in later }

4.1.3 Commenting the Public Interface Cont. /** Gets the current balance of the bank the current balance */ public double getBalance() { // implementation filled in later }

4.1.4 Instance Fields An object stores its data in instance fields A field is a term for a storage location in memory An instance of a class is an object of that class Thus, an instance field is a storage location that is present in each object of the class

4.1.4 Instance Fields Cont. An instance field declaration usually consists of the following parts: An access specifier (usually private ) The type of the instance field (such as double ) The name of the instance field (such as balance )

4.1.4 Instance Fields Cont. Example: public class BankAccount {... private double balance;... }

4.1.4 Instance Fields Cont. Each object of a class has its own set of instance fields Instance fields are generally declared with the access specifier private The private specifier means that they can be accessed only by methods of the same class, not by any other method

4.1.4 Instance Fields Cont. For example, the BankAccount balance instance field can be accessed by the BankAccount deposit method, but not by the main method of another class Since the instance fields are private, all data access must occur through the public methods This approach is how object-oriented languages implement encapsulation

4.1.5 Implementing Constructors and Methods We now turn our attention to supplying the bodies of the constructors and methods of a class Each body contains a sequence of statements Again, a constructor is designed to simply initialize the instance fields of an object The BankAccount class has two constructors

4.1.5 Implementing Constructors and Methods Cont. public BankAccount() { balance = 0; } public BankAccount(double initialBalance) { balance = initialBalance; } What does this do? BankAccount harrysChecking = new BankAccount(1000);

4.1.5 Implementing Constructors and Methods Cont. Create a new object of type BankAccount Call the second constructor (since a construction parameter is supplied) Set the parameter variable initialBalance to 1000 Set the balance instance field of the newly created object to initialBalance Return an object reference, that is, the memory location of the object, as the value of the new expression Store that object reference in the harrysChecking variable

4.1.5 Implementing Constructors and Methods Cont. public void deposit(double amount) { double mewBalance = balance + amount balance = newBalance; } What does this do? harrysChecking.deposit(500);

4.1.5 Implementing Constructors and Methods Cont. Set the parameter variable amount to 500 Fetch the balance field of the object whose location is stored in harrysChecking Add the value of amount to balance and store the result in the variable newBalance Store the value of newBalance in the balance instance field, overwriting the old value

4.1.5 Implementing Constructors and Methods Cont. public double getBalance() { return balance } double d = harrysChecking.getBalance(); return statement: specifies the value that a method returns, and exits the method immediately

4.1.6 Testing a Class 1.Construct one or more objects of the class that is being tested 2.Invoke one or more methods 3.Print out one or more results For this course, the above approach is a secondary technique to provide a visual confirmation only; our primary testing approach is to use JUnit to test all class methods

4.1.6 Testing a Class Cont. /** A class to test the BankAccount class. */ public class BankAccountTester { /** Tests the methods of the BankAccount args not used */ public static void main(String[] args) { BankAccount harrysChecking = new BankAccount(); harrysChecking.deposit(2000); harrysChecking.withdraw(500); System.out.println(harrysChecking.getBalance()); }

4.1.6 Testing a Class Cont. JUnit approach: /** Test the BankAccount withdraw method. */ public void testWithdraw() { BankAccount harrysChecking = new BankAccount(); harrysChecking.deposit(2000); harrysChecking.withdraw(500); assertEquals(1500.0, harrysChecking.getBalance(),.0001); }

Reference: Big Java 2 nd Edition by Cay Horstmann Black Boxes (section 3.1 in Big Java) Designing the Public Interface of a Class (section 3.2 in Big Java) Commenting the Public Interface (section 3.3 in Big Java) Instance Fields (section 3.4 in Big Java) Implementing Constructors and Methods (section 3.5 in Big Java) Testing a Class (section 3.6 in Big Java)