18-Aug-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming in Java Topic : Objects and Classes (cont)

Slides:



Advertisements
Similar presentations
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Four Defining Your Own Classes.
Advertisements

CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
OBJECT-ORIENTED PROGRAMMING. What is an “object”? Abstract entity that contains data and actions Attributes (characteristics) and methods (functions)
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Immutable Objects and Classes.
Road Map Introduction to object oriented programming. Classes
COMP171 Data Structure & Algorithm Tutorial 1 TA: M.Y.Chan.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
1 Classes Overview l Classes as Types l Declaring Instance Variables l Implementing Methods l Constructors l Accessor and Mutator Methods.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Applying OO Concepts Using Java. In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The.
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
16-Aug-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming in Java Topic : Interfaces, Copying/Cloning,
Object-Oriented Programming Design Topic : Objects and Classes
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Chapter 4 Objects and Classes.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
CSC 212 Object-Oriented Programming and Java Part 1.
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.
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Question of the Day  On a game show you’re given the choice of three doors: Behind one door is a car; behind the others, goats. After you pick a door,
Question of the Day  On a game show you’re given the choice of three doors: Behind one door is a car; behind the others, goats. After you pick a door,
Java Objects and Classes. Overview n Creating objects that belong to the classes in the standard Java library n Creating your own classes.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
Programming in Java CSCI-2220 Object Oriented Programming.
CSC 142 Computer Science II Zhen Jiang West Chester University
JAVA COURSE 1 Computer Engineering Association. Compile your first program Public class Hello{ public class Hello(){ System.out.println(“Hello”); } puclic.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Designing Classes CS239 – Jan 26, Key points from yesterday’s lab  Enumerated types are abstract data types that define a set of values.  They.
Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
IT108 Objects and Classes Part I George Mason University Revised 4/3/2012.
Object Oriented Programming
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
Classes, Interfaces and Packages
CPS120: Introduction to Computer Science Lecture 16 Data Structures, OOP & Advanced Strings.
Chapter 5Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 5 l Programming with Methods l Polymorphism l Constructors.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten.
Attribute - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus 12/24/2016.
Chapter 4: More Object Concepts. Objectives Understand blocks and scope Overload a method Avoid ambiguity Create and call constructors with parameters.
Lecture 4 – Function (Part 1) FTMK, UTeM – Sem /2014.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Methods, classes, and Objects Dr. Jim Burns. Question  Which of the following access modifiers is the default modifier?  public  private  protected.
More About Objects and Methods
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Yanal Alahmad Java Workshop Yanal Alahmad
Chapter 3: Using Methods, Classes, and Objects
CSC240 Computer Science III
Corresponds with Chapter 7
Java Inheritance.
Applying OO Concepts Using Java
Java Programming Language
Presentation transcript:

18-Aug-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming in Java Topic : Objects and Classes (cont) Object–Oriented Design Maj Joel Young Maj Joel Young

Air Force Institute of Technology Electrical and Computer Engineering 18-Aug-152 Object-Oriented Programming Design Using Class Instances Accessing an instance method: newGuy.raiseSalary( 10.0 ); Accessing an instance attribute: double hisSalary = newGuy._salary; newSalary = this._salary * ( 1 + percent/100); //”this” refers to current instance Note: direct access to other object’s attributes is discouraged –(and in fact it is not allowed in above case due to private attribute)

Air Force Institute of Technology Electrical and Computer Engineering 18-Aug-153 Object-Oriented Programming Design Constructors Automatically called when object is created Same name as class -- e.g. Date(); No return value May be more than one constructor per class Default constructor with no arguments should be specified –initialize to reasonable default value public Date() { _day = 1; _month = 1; _year = 1; } Constructor with arguments -- initialize to user specified values public Date( int d, int m, int y) { _day = d; _month = m; _year = y; } Cannot be invoked on existing objects

Air Force Institute of Technology Electrical and Computer Engineering 18-Aug-154 Object-Oriented Programming Design Constructors A quick quiz on constructors– can you spot the error? class Employee { public Employee( String n, double s ) { String _name = n; double _salary = s; } … private String _name; private double _salary; }

Air Force Institute of Technology Electrical and Computer Engineering 18-Aug-155 Object-Oriented Programming Design Java Example class Person { // Attributes int m_age; double m_gpa; String m_name; // Operations public Person(String name, int age, double gpa) { m_age = age; m_name = name; m_gpa = gpa; } private void printName() { System.out.print(m_name); } static public void main(String args[]) { // Create a Person instance Person p1 = new Person("Pyle",27,1.9); Person p2; // Print the name of // the person System.out.print("Name: "); p1.printName(); p2 = p1; // Print the same name again System.out.print(" Name: "); p2.printName(); } } // End class Person

Air Force Institute of Technology Electrical and Computer Engineering 18-Aug-156 Object-Oriented Programming Design Class Attributes/Methods Class methods/attributes – Use static keyword in front of declaration Class MyMath { … static void add( int val1, int val2 ); } – May be accessed by using ClassName.method() or ClassName.attribute notation MyMath.add( 1, 3 ); – An instance of the class is not required for access Class-wide Attributes – An attribute shared by all instances of a class – If one instance changes it … all others see the change – Analog: Global Variable … use SPARINGLY Class-wide Operation – A “meta operation” … operates on the class, not instances of the class – Typical application: –Creating new class members, assigning class ID numbers, etc.

Air Force Institute of Technology Electrical and Computer Engineering 18-Aug-157 Object-Oriented Programming Design Packages Java code is distributed in separate packages (you can also create your own packages) To access code in a package –refer to class by full name java.util.Vector v = new java.util.Vector(); –or import class into your program import java.util.Vector Vector v = new Vector(); –or import entire package import java.util.* Package paths map to directory structure java.util.Vector  \java\util\

18-Aug-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming in Java Topic : Object–Oriented Design Maj Joel Young Maj Joel Young

Air Force Institute of Technology Electrical and Computer Engineering 18-Aug-159 Object-Oriented Programming Design Development Process Analysis –Transform vague understanding of a problem into a precise description of the tasks to be solved –Concerned with description of what must be done, not how it should be done Design –Structure programming tasks into classes and packages (logically grouped clusters of objects) –Specify operations and attributes of each class –Specify relationships with other classes in the system Implementation –Classes and operations are coded, tested, and integrated –Object-orientation encourages evolutionary development since behavior and state is encapsulated into objects with predefined interfaces – thus objects or packages can be incrementally added and tested more easily

Air Force Institute of Technology Electrical and Computer Engineering 18-Aug-1510 Object-Oriented Programming Design Object-Oriented Design Goal: decompose a programming task into data types or class and define the functionality of these classes Sub-goals –Identify classes –Identify functionality of classes –Identify relationships among classes A good design greatly reduces time required for implementation and testing

Air Force Institute of Technology Electrical and Computer Engineering 18-Aug-1511 Object-Oriented Programming Design Object-Oriented Design Finding classes –Look for nouns in problem analysis –Many are good choices for classes –Other classes may be necessary Finding operations –Look for verbs in problem analysis –Each operation must have exactly one class responsible for carrying it out Finding class relationships –Association  uses; a class uses another class if manipulates objects of that class in any way; should be minimized –Aggregation  “has-a”, contains; –Inheritance  “is-a”, specialization; useful in select places

Air Force Institute of Technology Electrical and Computer Engineering 18-Aug-1512 Object-Oriented Programming Design Object-Oriented vs. Traditional Design Traditional Decomposition into sub-tasks –Often results in numerous procedures or functions –Difficult to manage and understand Task modules can simulate objects by organizing related tasks and attributes –Only one instance (e.g. queue) Encapsulation achieved with opaque types (e.g. Unix file interface) –No inheritance Object-Oriented Decomposition into objects with associated tasks –Convenient clustering –Data encapsulation helps debugging –Multiple instances with similar behavior –Inheritance captures commonalities among related classes of objects

Air Force Institute of Technology Electrical and Computer Engineering 18-Aug-1513 Object-Oriented Programming Design Design Hints Always keep data private –If user of the class has a need to both read and change a field (attribute), the following 3 items must be implemented in the class –A private data field –A public field accessor method –A public field mutator method –Benefits –Internal implementation can be changed without affecting any code outside of the class –Mutator methods can perform error-checking to ensure field is set properly –Accessors to fields with mutable objects –Should return clone of object so that calling object can not modify the field object contained in the class instance Always initialize data –Avoids unexpected results –Simplifies debugging

Air Force Institute of Technology Electrical and Computer Engineering 18-Aug-1514 Object-Oriented Programming Design Design Hints Don't use too many basic types in a class –Group related types into a separate class and use that class instead –Bad class Employee { –private String _lname; –private String _fname; –private char _initial; –private String _street; –private String _city; –private String _state; –private long _zip; } Not all fields need individual field accessors and mutators Use a standard format for class definitions Break up classes with too many responsibilities Make the names of your classes and methods reflect their responsibilities –Good class Employee { private Name _name; private Address _address; private Date _hiredate; }

Air Force Institute of Technology Electrical and Computer Engineering 18-Aug-1515 Object-Oriented Programming Design Homework Discussion Compile and run the Person example code Write and test a swap method –Create a simple class with one int data member –Write a swap method for that class that swaps values: –: static public void swap(mytype a, mytype b); –Such that after swap(a,b) is called a looks like b looked and b looks like a looked Analyze the requirements for a Calculator program –Identify the classes, the operations, and the relationships between classes –Make stub code for all your classes and operations with definitions for the has-a relationships –Remember each class goes in a separate file

Air Force Institute of Technology Electrical and Computer Engineering 18-Aug-1516 Object-Oriented Programming Design Homework Assignment Due Monday COMACC Calculator Question and Answer Session Analyze the requirements for a Calculator program –Provide a narrative discussion of the requirements and behavior COMACC expects from the Calculator –Prepare clarification questions. Proposing alternatives is a strong plus –Discuss what is feasible in the next 3 weeks. Remember that you will know more in the next few weeks –Provide me this in hard copy with your address on it Be prepared to: –Identify the classes, the operations, and the relationships between classes –Make stub code for all your classes and operations with definitions for the has-a relationships