Dale Roberts Object Oriented Programming using Java - Getters and Setters Dale Roberts, Lecturer Computer Science, IUPUI

Slides:



Advertisements
Similar presentations
Abstract Data Type Fraction Example
Advertisements

CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
Dale Roberts Object Oriented Programming using Java - Inheritance Overview Dale Roberts, Lecturer Computer Science, IUPUI
Dale Roberts Object Oriented Programming using Java - Inheritance Constructors Dale Roberts, Lecturer Computer Science, IUPUI
Road Map Introduction to object oriented programming. Classes
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Classes and Objects Systems Programming.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
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.
Classes in C++ Bryce Boe 2012/08/15 CS32, Summer 2012 B.
More C++ Bryce Boe 2013/07/18 CS24, Summer 2013 C.
Dale Roberts Object Oriented Programming using Java - Class Constructors Dale Roberts, Lecturer Computer Science, IUPUI Department.
Object Oriented Programming using Java - Polymorphism
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.
Dale Roberts Procedural Programming using Java Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and.
Dale Roberts Object Oriented Programming using Java - Packages Dale Roberts, Lecturer Computer Science, IUPUI Department.
Dale Roberts Introduction to Java - Access Specifiers Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
Dale Roberts Program Control using Java - Boolean Expressions Dale Roberts, Lecturer Computer Science, IUPUI Department of.
Dale Roberts Object Oriented Programming using Java - Enumerations Dale Roberts, Lecturer Computer Science, IUPUI Department.
 2005 Pearson Education, Inc. All rights reserved Classes and Objects: A Deeper Look.
CET203 SOFTWARE DEVELOPMENT Session 1A Revision of Classes.
Dale Roberts Program Control using Java - Selection Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
Dale Roberts GUI Programming using Java - GUI Components Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
Dale Roberts Object Oriented Programming using Java - OOD to OOP: ATM Case Study Dale Roberts, Lecturer Computer Science, IUPUI
Reformatted slides from the textbook, C++ How to Program, 6/e Pearson Education, Inc. All rights reserved Chapter 3. [Lecture 02] Introduction to.
Dale Roberts Program Control using Java - Repetition Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
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.
 Pearson Education, Inc. All rights reserved Classes and Objects: A Deeper Look.
CLASSES : A DEEPER LOOK Chapter 9 Part I 1. 2 OBJECTIVES In this chapter you will learn: How to use a preprocessor wrapper to prevent multiple definition.
Dale Roberts Object Oriented Programming using Java - Final and Static Keywords Dale Roberts, Lecturer Computer Science, IUPUI
 2005 Pearson Education, Inc. All rights reserved Classes and Objects: A Deeper Look.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
02/14/2005 Introduction to Programming with Java, for Beginners Midterm 1 Review.
AP Computer Science A – Healdsburg High School 1 Unit 9 - Why Use Classes - Anatomy of a Class.
CIS 270—Application Development II Chapter 8—Classes and Objects: A Deeper Look.
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
Dale Roberts Introduction to Java - Input, Program Control and Instantiation Dale Roberts, Lecturer Computer Science, IUPUI
 Pearson Education, Inc. All rights reserved Classes and Objects: A Deeper Look.
Dale Roberts 1 Operator Overloading Member Functions Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
COMP Information Hiding and Encapsulation Yi Hong June 03, 2015.
XuanTung Hoang 1 Something to discuss Feedbacks on Midterm Exam Final exam and term project  Final exam requires solid knowledge/skills in Java  Be more.
Dale Roberts GUI Programming using Java - Windowing Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
Object Oriented Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
Object Oriented Programming using Java - Composition
Classes and Objects: A Deeper Look
GUI Programming using Java - Key Events
Object Oriented Programming using Java - Class Instance Variables
Chapter 3: Using Methods, Classes, and Objects
Variable Declarations, Data types, Expressions
Variable Declarations, Data types, Expressions
Introduction to Classes and Objects
Object Based Programming
CSC 480 Software Engineering
Classes and Objects Encapsulation
IFS410: Advanced Analysis and Design
Introduction to Classes and Objects
Week 4 Object-based Programming (2) Classes and Objects: A Deeper Look
Chapter 8 Classes and Objects: A Deeper Look
Information Hiding and Encapsulation Section 4.2
Dale Roberts, Lecturer IUPUI
Classes and Objects CGS3416 Spring 2019.
Classes and Objects Systems Programming.
CSG2H3 Object Oriented Programming
Presentation transcript:

Dale Roberts Object Oriented Programming using Java - Getters and Setters Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and Information Science, School of Science, IUPUI

Dale Roberts 2 Software Engineering Observation 8.5 When implementing a method of a class, use the class’s set and get methods to access the class’s private data. This simplifies code maintenance and reduces the likelihood of errors.

Dale Roberts 3Outline Time2.ja va (1 of 4)

Dale Roberts 4Outline Time2.java (2 of 4)

Dale Roberts 5Outline Time2.java (3 of 4)

Dale Roberts 6Outline Time2.java (4 of 4)

Dale Roberts 7Outline Time2Test.ja va (1 of 3) Call overloaded constructors

Dale Roberts 8Outline Time2Test.ja va (2 of 3)

Dale Roberts 9Outline Time2Te st.java (3 of 3)

Dale Roberts 10 Software Engineering Observation 8.7 When necessary, provide public methods to change and retrieve the values of private instance variables. This architecture helps hide the implementation of a class from its clients, which improves program modifiability. Encapsulation

Dale Roberts Notes on Set and Get Methods Set methods Also known as mutator methods Assign values to instance variables Should validate new values for instance variables Can return a value to indicate invalid data Get methods Also known as accessor methods or query methods Obtain the values of instance variables Can control the format of the data it returns

Dale Roberts 12 Time Class Case Study: Overloaded Constructors (Cont.) Using set methods Having constructors use set methods to modify instance variables instead of modifying them directly simplifies implementation changing Class designers need not provide set or get methods for each private field. These capabilities should be provided only when it makes sense in the public interface.

Dale Roberts Notes on Set and Get Methods (Cont.) Predicate methods Test whether a certain condition on the object is true or false and returns the result Example: an isEmpty method for a container class (a class capable of holding many objects) Predicate methods always return a boolean data type. Clever naming of predicate methods can make your code easier to understand if (myStack.isEmpty()) Encapsulating specific tasks into their own methods simplifies debugging efforts

Dale Roberts Acknowledgements Deitel, Java How to Program