16
Aug 28, 2022 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 [email protected] Maj Joel Young

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

Embed Size (px)

Citation preview

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

Apr 19, 2023

Air Force Institute of TechnologyElectrical and Computer Engineering

Object-Oriented Programming in Java

Topic : Objects and Classes (cont)Object–Oriented Design

Maj Joel [email protected]

Maj Joel Young

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

Air Force Institute of TechnologyElectrical and Computer EngineeringApr 19, 2023 2

Object-Oriented Programming

DesignUsing 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)

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

Air Force Institute of TechnologyElectrical and Computer EngineeringApr 19, 2023 3

Object-Oriented Programming

DesignConstructors

• 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 valuepublic 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

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

Air Force Institute of TechnologyElectrical and Computer EngineeringApr 19, 2023 4

Object-Oriented Programming

DesignConstructors

• 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;

}

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

Air Force Institute of TechnologyElectrical and Computer EngineeringApr 19, 2023 5

Object-Oriented Programming

DesignJava 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

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

Air Force Institute of TechnologyElectrical and Computer EngineeringApr 19, 2023 6

Object-Oriented Programming

DesignClass 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 notationMyMath.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.

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

Air Force Institute of TechnologyElectrical and Computer EngineeringApr 19, 2023 7

Object-Oriented Programming

DesignPackages

• 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.VectorVector v = new Vector();

– or import entire packageimport java.util.*

• Package paths map to directory structurejava.util.Vector <CLASSPATH>\java\util\<vector package files>

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

Apr 19, 2023

Air Force Institute of TechnologyElectrical and Computer Engineering

Object-Oriented Programming in Java

Topic : Object–Oriented Design

Maj Joel [email protected]

Maj Joel Young

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

Air Force Institute of TechnologyElectrical and Computer EngineeringApr 19, 2023 9

Object-Oriented Programming

DesignDevelopment 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

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

Air Force Institute of TechnologyElectrical and Computer EngineeringApr 19, 2023 10

Object-Oriented Programming

DesignObject-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

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

Air Force Institute of TechnologyElectrical and Computer EngineeringApr 19, 2023 11

Object-Oriented Programming

DesignObject-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

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

Air Force Institute of TechnologyElectrical and Computer EngineeringApr 19, 2023 12

Object-Oriented Programming

DesignObject-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

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

Air Force Institute of TechnologyElectrical and Computer EngineeringApr 19, 2023 13

Object-Oriented Programming

DesignDesign 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

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

Air Force Institute of TechnologyElectrical and Computer EngineeringApr 19, 2023 14

Object-Oriented Programming

DesignDesign 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;

}

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

Air Force Institute of TechnologyElectrical and Computer EngineeringApr 19, 2023 15

Object-Oriented Programming

DesignHomework 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 definitionsfor the has-a relationships

– Remember each class goes in a separate file

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

Air Force Institute of TechnologyElectrical and Computer EngineeringApr 19, 2023 16

Object-Oriented Programming

DesignHomework 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 email 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