CSC 205 Java Programming II Defining & Implementing Classes.

Slides:



Advertisements
Similar presentations
Based on Java Software Development, 5th Ed. By Lewis &Loftus
Advertisements

Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, toString reading: self-checks: #13-18,
CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
1 Classes, Encapsulation, Methods and Constructors (Continued) Class definitions Instance data Encapsulation and Java modifiers Method declaration and.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Classes and Objects Systems Programming.
CS-212 Intro to C++. Abstract Data Type Abstraction of a real world object as a mathematical entity Implementation independent model of the entity Emphasis.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
ECE122 L6: Problem Definition and Implementation February 15, 2007 ECE 122 Engineering Problem Solving with Java Lecture 6 Problem Definition and Implementation.
C++ Classes in Depth. Topics Designing Your Own Classes Attributes and Behaviors Writing Classes in C++ Creating and Using Objects.
1 Fall 2007ACS-1903 Chapter 6: Classes Classes and Objects Instance Fields and Methods Constructors Overloading of Methods and Constructors Scope of Instance.
More C++ Bryce Boe 2013/07/18 CS24, Summer 2013 C.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
UML Basics & Access Modifier
Encapsulation CMSC 202. Types of Programmers Class programmers – Developers of new classes – Goal: Expose the minimum interface necessary to use a new.
Classes and Class Members Chapter 3. 3 Public Interface Contract between class and its clients to fulfill certain responsibilities The client is an object.
Writing Classes (Chapter 4)
Chapter 5 - Writing a Problem Domain Class Definition1 Chapter 5 Writing a Problem Domain Class Definition.
Classes and Objects. Topics The Class Definition Declaring Instance Member Variables Writing Instance Member Methods Creating Objects Sending Messages.
Mason Vail.  A data type definition – “blueprint for objects”  Includes properties and/or methods ◦ “instance” data / methods – specific to one object.
P Chapter 2 introduces Object Oriented Programming. p OOP is a relatively new approach to programming which supports the creation of new data types and.
10-Nov-15 Java Object Oriented Programming What is it?
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
© 2004 Pearson Addison-Wesley. All rights reserved September 12, 2007 Encapsulation ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
CSC 142 Computer Science II Zhen Jiang West Chester University
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
© 2004 Pearson Addison-Wesley. All rights reserved September 14, 2007 Anatomy of a Method ComS 207: Programming I (in Java) Iowa State University, FALL.
Chapter 3 Part I. 3.1 Introduction Programs written in C ◦ All statements were located in function main Programs written in C++ ◦ Programs will consist.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
08 Encapsulation and Abstraction. 2 Contents Defining Abstraction Levels of Abstraction Class as Abstraction Defining a Java Class Instantiating a Class.
Classes Modeling the Object. Objects model the world Classes are programmer defined types that model the parts of a system Class serve as blueprints for.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Objects and Classes.
CS305j Introduction to Computing Classes II 1 Topic 24 Classes Part II "Object-oriented programming as it emerged in Simula 67 allows software structure.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7: Introduction to Classes and Objects Starting Out with C++ Early.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
Chapter 5Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 5 l Programming with Methods l Polymorphism l Constructors.
Chapter 7 Programming by contract: preconditions and postconditions.
Defining Classes I Part B. Information hiding & encapsulation separate how to use the class from the implementation details separate how to use the class.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-1 Learning Objectives  Classes  Constructors  Principles of OOP  Class type member.
CPSC 252 ADTs and C++ Classes Page 1 Abstract data types (ADTs) An abstract data type is a user-defined data type that has: private data hidden inside.
Outline Anatomy of a Class Encapsulation Anatomy of a Method Copyright © 2014 Pearson Education, Inc.
CMSC 202 Lesson 8 Classes II. Warmup Declare a class (.h part) named “Cup” It has a data member that says how full it is One method called “Drink” that.
Comp1004: Building Better Objects II Encapsulation and Constructors.
COMP Information Hiding and Encapsulation Yi Hong June 03, 2015.
Class Definitions and Writing Methods Chapter 3 3/31/16 & 4/4/16.
Class Definitions and Writing Methods Chapter 3 10/12/15 & 10/13/15 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes : Review Java Software Solutions Foundations of Program Design Seventh Edition John.
Classes and Objects 2nd Lecture
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
CSC 480 Software Engineering
Ch 4: Writing Classes Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: Classes and Objects.
Chapter 4: Writing classes
An Introduction to Java – Part II
Encapsulation and Constructors
Classes, Encapsulation, Methods and Constructors (Continued)
Learning Objectives Classes Constructors Principles of OOP
Introduction to Classes and Objects
The Object-Oriented Thought Process Chapter 04
Defining Classes and Methods
Object-Oriented Programming
CMSC 202 Lesson 8 Classes II.
Information Hiding and Encapsulation Section 4.2
ITE “A” GROUP 2 ENCAPSULATION.
Classes and Objects Systems Programming.
CSG2H3 Object Oriented Programming
Presentation transcript:

CSC 205 Java Programming II Defining & Implementing Classes

Topics Objects and their responsibilities Define a class Implement and test a class Use a class

A Dummy Class Write a class can be simple A dummy class with only one line of code class Dummy {} A default constructor will be provided: it’s equivalent to Dummy(){} You can create a Dummy object Dummy d = new Dummy(); But such a class is not useful You can do very little with object d

Responsibilities An object should Maintain its own state: know something A String object should know its length A Date class should know the year, month, day associated with it Be able to provide certain services: do something A String object should be able to return a portion of itself (a substring) as required A JOptionPane object should be able to show itself as a input or message dialog as required

Classes & Their Members A class defines what its instances (or objects) should know and should be able to do Data members: used to maintain state Method members: define behaviors Example: the Throttle class Throttle top:int position:int getFlow():double isOn():boolean shift(amt:int) shutOff() Class name Data members Methods

Define A Class Classes Entity classes (or problem domain class) Usually don’t have a main method Main classes: classes with a main method Only one such class in an application Possible members in a class entity class main class constructorV V* main methodX V methodsV V* variablesV V*

Encapsulation Information hiding Make instance variables private Provide public accessors (or getters) Provide public mutators (or setters) only when needed Example: when designing a Person class Define the age variable as private Provide a public getAge() method What about a public setAge(int n) method? NO! Provide a updateAgeAnnualy() method

Information Hiding top position getFlow isOn shift shuttOff A Throttle object Each object is identified by a unique (hash-)code A handle Private data members Public methods Serve as interfaces

The Client (or External) View getFlowisOnshutOff

Writing Methods An entity class needs to interact with other classes Message passing Message: receiver + operation name + parameter list Return value is optional Contracts between client and server objects Accessibility modifier Return type, maybe void Method name Parameter list, maybe none

Specification Format Signature and description Parameters Returns Precondition Postcondition Throws

The Throttle Class See source code files for the class and a test driver class under our class folder How to test an entity class? Write a test driver class, that has a main method Testing is trying to break the application Use extreme values Use values beyond the normal ranges  Creating a Throttle object with a negative size  Trying to shift it to illegal positions