Introduction to Computer Science and Object-Oriented Programming

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.
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.
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 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.
CHAPTER 2 OBJECTS AND CLASSES Goals: To understand the concepts of classes and objects To realize the difference between objects and object references.
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.
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
1 OOP in C#: Object Interaction. Inheritance and Polymorphism OOP in C# - Del 1.
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Introduction to Java and Object-Oriented Programming AJSS Computer Camp Department of Information Systems and Computer Science Ateneo de Manila University.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Object-Oriented Programming in C++
Classes CS 21a: Introduction to Computing I First Semester,
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.
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.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
Session 02 and 03: C# OOP 1 OOP in C#: Classes and Objects in C#. Object-Oriented Design. UML Class Diagram. Object Interaction. FEN AK IT:
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
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.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
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.
1 Classes, Encapsulation, Methods and Constructors Class definitions Scope of Data –Instance data –Local data The this Reference Encapsulation and Java.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
OOP in C# - part 1 OOP in C#: –Object Interaction. –Inheritance and Polymorphism (next module). FEN 20121UCN Technology: Computer Science.
Chapter 3 Implementing Classes
Lecture 3 John Woodward.
Data Structures and Algorithms revision
Programming Logic and Design Seventh Edition
Chapter 3 – Implementing Classes
Chapter Three: Implementing Classes
Object-Oriented Programming: Classes and Objects
Classes Object-oriented programming: Example: Bank transactions
Chapter 3: Using Methods, Classes, and Objects
Implementing Classes Yonglei Tao.
Object-Oriented Programming: Classes and Objects
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
Inheritance Basics Programming with Inheritance
Chapter 3 Implementing Classes
Dr. Bhargavi Dept of CS CHRIST
Computer Programming with JAVA
Defining Classes and Methods
JAVA CLASSES.
Defining Classes and Methods
AN INTRODUCTION TO OBJECTS AND CLASSES
Classes CS 21a: Introduction to Computing I
Introduction to Object-Oriented Programming
Defining Classes and Methods
Handout-2(a) Basic Object Oriented Concepts
CS 1054: Lecture 2, Chapter 1 Objects and Classes.
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

Introduction to Computer Science and Object-Oriented Programming Week 4

Examples What is a car? What is a truck? Abstraction Taking away inessential features until only the essence of some concept remains Examples What is a car? What is a truck? What is a mammal? Week 4

We say a black box provides Related to abstraction A black box Does something (often useful) Provides an interface for controlling it But its inner workings are hidden Examples Cell phone Video game Car We say a black box provides encapsulation Week 4

Depends How You Look At It Week 4

Who Cares? Abstraction and black boxes help humans deal with complexity Make things simpler and more efficient You don’t care how things work inside You care only the interface is well-defined Leads to a separation of concerns User of the black box Designer and implementer of inner workings Week 4

Let’s Try Abstraction Week 4

What’s It Mean to Software? Our black boxes are objects OOP focuses on Use Interfaces Design of objects Week 4

Encapsulation in OOP The use of abstraction to design programs in terms of classes/objects The definition of the class Focused on the interface On one outside - what users know and use On the inside - what programmers design & code Week 4

Encapsulation in OOP In COMP 111 we learn: To use To define (the interface) To design and implement (the inner workings) Week 4

Example of Use We don’t know how the String class is implemented But we do know the purpose and how to use public methods of String e.g. length, toUpperCase, replace But we know What parameters (if any) we need to pass in Their sequence and type of each The type of the return value (if any) Week 4

Example of Defining We need to define an abstraction Then translate With attributes With behavior Then translate Attributes to data Behavior to methods Week 4

Public Interface of a Class Behavior (abstraction) 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.getBalance()); Week 4

Method Definitions access specifier (such as public or private) return type (such as String or void) method name (such as deposit) list of parameters (such as double amount) method body in { } Week 4

Specifying Methods accessSpecifier returnType methodName(parameters) { method body } Where parameters is a comma-separated list of parameterType parameterName Week 4

Examples public void deposit(double amount) { . . . } public void withdraw(double amount) public double getBalance() Week 4

Constructors are similar to methods an access specifier, parameter list and body but their job is not to define a behavior but to properly initialize an object for later use Week 4

Constructors . Named the same as the class, Never have a return value All constructors of a class have the same name Compiler can tell constructors apart because they take different parameters Week 4

Constructors Constructor body is executed when a new object is created Statements in constructor body will set the internal data of the object Week 4

Commenting the Public Interface Use documentation comments to describe the classes and public methods Use the standard documentation notation so program called javadoc can automatically generate a set of HTML pages Week 4

Commenting the Public Interface Provide documentation comments for every class every method every parameter every return value Javadoc comment are within /** and */ Best to write the method comments first, before writing code in the body Week 4

Example /** Withdraws money from the bank account. @param amount the amount to withdraw */ public void withdraw(double amount) { // implementation filled in later } Week 4

Example /** Gets the current balance of the bank account. @return the current balance */ public double getBalance() { // implementation filled in later } Week 4

Generated by Javadoc Week 4

Instance Fields A class is the pattern for its instances (objects) Each object stores its data in instance fields (or instance variables) 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 present in each object of the class Week 4

Defining Instance Fields To define an instance field: An access specifier (usually private) The type of the instance field (such as double) The name of the instance field (such as balance) Week 4

Example public class BankAccount { . . . private double balance; } Week 4

Instance Fields Each object instance of a class has its own set of instance fields Instance fields are generally declared with the access specifier private The private specifier means cannot be accessed directly other than by methods defined in the class must use methods Week 4

More on Access The balance instance variable can be accessed by the BankAccount deposit method but not by the main method of another class Since instance variables are private all access to their values must be through public methods This is how we implement encapsulation Week 4

Next Week Read Module 5 introduction and key points In Big Java 3-7 to 3.8 4.1 to 4.5 Week 4

Next Week Submit homework Assignment 4-2 Work on Assignment 5-1 Submit on or before May 29 Into Course Drop Box Worth 15 points Work on Assignment 5-1 Week 5 Guided Learning Activities Nothing to submit Week 4