49
Attribute - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus Email: [email protected] 1 06/19/22

Attribute - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus 12/24/2016

Embed Size (px)

DESCRIPTION

Fields field: A variable inside an object that represents part of its internal state. Each object will have its own copy of the data fields we declare. Declaring a field, general syntax: public ; or public = ; (with initialization)‏ Example: public class Dog { public String name; // each dog object has a public int age; // name and age data field } 32/24/2016

Citation preview

Page 1: Attribute - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus   12/24/2016

Attribute - CIS 1068 Program Design and Abstraction

Zhen JiangCIS Dept.

Temple UniversitySERC 347, Main Campus

Email: [email protected]/06/23

Page 2: Attribute - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus   12/24/2016

Table of Contents Introduction to field (attribute) Public and private modifiers Accessor and mutator methods Constructor Static method Instance method Overloading Static variables Packages and importing Summary

205/06/23

Page 3: Attribute - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus   12/24/2016

Fields field: A variable inside an object that represents part of

its internal state. Each object will have its own copy of the data fields we

declare.

Declaring a field, general syntax:public <type> <name>;orpublic <type> <name> = <value>;(with initialization)

Example:public class Dog { public String name; // each dog object has a public int age; // name and age data field}

305/06/23

Page 4: Attribute - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus   12/24/2016

Public and private modifiers P296-298 Type specified as public

Any other class can directly access that object by name

Classes generally specified as public Instance variables usually not public

Instead specify as private

05/06/23 4

Page 5: Attribute - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus   12/24/2016

Dog.java Change field to private http://www.cis.temple.edu/~jiang/Dog2trial.

pdf

DogDemo.java http://www.cis.temple.edu/~jiang/DogDemo

.pdf

05/06/23 5

Page 6: Attribute - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus   12/24/2016

05/06/23 6

Page 7: Attribute - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus   12/24/2016

Demonstration of need for private variables

Statement such asbalto.name = "Balto";

is illegal since name is private

05/06/23 7

Page 8: Attribute - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus   12/24/2016

Accessor and Mutator Methods

P302-305 Must also provide methods to change the

values of the private instance variable Typically named setSomeValue Referred to as a mutator method

When instance variables are private must provide methods to access values stored there Typically named getSomeValue Referred to as an accessor method

05/06/23 8

Page 9: Attribute - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus   12/24/2016

Consider the example class with accessor and mutator methods

http://www.cis.temple.edu/~jiang/Dog2.pdf

Note the mutator method setName , setBreed , setAge

Note accessor methods getName, getBreed, getAge

05/06/23 9

Page 10: Attribute - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus   12/24/2016

Using the accessor and mutator methods

http://www.cis.temple.edu/~jiang/DogDemo2.pdf

05/06/23 10

Page 11: Attribute - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus   12/24/2016

1105/06/23

1 public class DogDemo2 2 { 3 public static void main(String[] args) 4 { 5 Dog2 balto = new Dog2(); 6 balto.setName("Balto"); 7 balto.setAge(8); 8 balto.setBreed("Siberian Husky"); 9 balto.writeOutput();10 11 Dog2 scooby = new Dog2();12 scooby.setName("Scooby");13 scooby.setAge(42);14 scooby.setBreed("Great Dane");15 System.out.println(scooby.getName()+" is a "+scooby.getBreed()+".");16 System.out.print("He is " + scooby.getAge() + " years old, or ");17 int humanYears = scooby.getAgeInHumanYears();18 System.out.println(humanYears + " in human years.");19 }20 }21

Tedious

Page 12: Attribute - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus   12/24/2016

We want something more like:

// better!Dog3 balto = new Dog3(“Balto”, “Siberian Husky”, 8);

1205/06/23

Page 13: Attribute - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus   12/24/2016

Constructor constructor: A special method that initializes

the state of new objects as they are created.

Constructor syntax, p375-377:public <class name> (<parameter(s)>) { <statement(s)>;}

How does this differ from previous methods? Method name No return!

1305/06/23

Page 14: Attribute - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus   12/24/2016

public class Dog3 { private String name; private String breed; private int age;

public Dog3(String strName, String strBreed, int

intAge) { name = strName; breed = strBreed; age = intAge; }}

1405/06/23

Page 15: Attribute - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus   12/24/2016

http://www.cis.temple.edu/~jiang/Dog3.pdf http://www.cis.temple.edu/~jiang/DogDemo3.p

df

05/06/23 15

Page 16: Attribute - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus   12/24/2016

05/06/23 16

Page 17: Attribute - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus   12/24/2016

Calling method from constructor: a constructor can call methods within its method body, in order to avoid unnecessarily repeating some code, p384.

To avoid the problem with inheritance, which we will cover later, we need to make such a method private!

1705/06/23

Page 18: Attribute - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus   12/24/2016

Static Methods Some methods may have no relation

to any type of object, p390-392 One method each class

Example Compute max of two integers Convert character from upper- to lower case

Static method declared in a class Can be invoked without using an object Instead use the class name

05/06/23 18

Page 19: Attribute - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus   12/24/2016

Adding Method main to a Class, p398-400 Method main used so far in its own class Often useful to include method main within

class definition To create objects of other classes To be run as a start of program For any ordinary class, method main is

ignored

05/06/23 19

Page 20: Attribute - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus   12/24/2016

public class Point3D { public int x; public int y;

public int z;}

Every object of type Point3D contains three integers.

Point3D objects (so far) do not contain any behavior.

Class declarations are saved in a file of the same name: Point3D.java

2005/06/23

Page 21: Attribute - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus   12/24/2016

21

How would we translate several points?p1.x += 11;p1.y += 6;p1.z + 2;

p2.x += 2;p2.y += 4;p2.z += -2;

p3.x += 1;p3.y += 7;p3.z += 17;

What is unsettling about this code?05/06/23

Page 22: Attribute - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus   12/24/2016

22

Write a static method in the client code to translate points.// Shifts the location of the given point.public static void translate(

Point p, int dx, int dy, int dz) { p.x += dx; p.y += dy; p.z += dz;}

Example:// move p2 translate(p2, 2, 4, 6);

Question: Why doesn't the method need to return the modified point?05/06/23

Page 23: Attribute - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus   12/24/2016

23

Every client code (application class) that declares Point3D and wants to translate points would have to write their own static translate method!

Also, the call syntax doesn't match the way we're used to interacting with objects:

translate(p2, 2, 4);

We want something more like:

p2.translate(2, 4);

05/06/23

Page 24: Attribute - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus   12/24/2016

24

Instance methods instance method: A method inside an object

that operates on that object.

Declaring an object's method, general syntax:public <type> <name> (<parameter(s)>) { <statement(s)>;}

How does this differ from previous methods?

05/06/23

Page 25: Attribute - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus   12/24/2016

25

An object's instance methods can refer to its fields.

public void translate(int dx, int dy, int dz) { x += dx; y += dy; z += dz;

}

How does the translate method know which x, y, z to modify?05/06/23

Page 26: Attribute - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus   12/24/2016

26

Each instance method call happens on a particular object.

Example: p1.translate(11, 6, 2);

The code for an instance method has an implied knowledge of what object it is operating on.

implicit parameter: The object on which an instance method is called.05/06/23

Page 27: Attribute - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus   12/24/2016

27

Think of each Point3D object as having its own copy of the translate method, which operates on that object's state:Point3D p1 = new Point3D(7, 2, -2);Point3D p2 = new Point3D(4, 3, 2);

p1:

p2:

public void translate(int dx, int dy, int dz){ ...

}

y:x: 7 2 z: -2

public void translate(int dx, int dy, int dz){ ...

}

y:x: 4 3 z: 2

05/06/23

Page 28: Attribute - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus   12/24/2016

18

28

What happens when the following calls are made?p1.translate(11, 6, 3);p2.translate(1, 7, -10);

p1:

p2:

public void translate(int dx, int dy, int dz){ ...

}

y:x: 7 2 z: -2

public void translate(int dx, int dy, int dz){ ...

}

y:x: 4 3 z: 2

8 1

5 10 -8

05/06/23

Page 29: Attribute - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus   12/24/2016

public class Point3D { public int x; public int y;

public Point3D(int initialX, int initialY, int initialZ) {

x = initialX; y = initialY; z = initialZ; }

// Changes the location of this Point3D object. public void translate(int dx, int dy, int dz){ x += dx; y += dy; z += dz; }}

2905/06/23

Page 30: Attribute - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus   12/24/2016

30

public class PointMain3 { public static void main(String[] args) { // create two Point3D objects Point3D p1 = new Point3D(5, 2, -2);

Point3D p2 = new Point3D(4, 3, 2);

// print each point System.out.println("p1 is (" + p1.x + ", " + p1.y + ")"); System.out.println("p2 is (" + p2.x + ", " + p2.y + ")");

// move p2 and then print it again p2.translate(2, 4, -2); System.out.println("p2 is (" + p2.x + ", " + p2.y + ")"); }}

Output:p1 is (5, 2, -2)p2 is (4, 3, 2)p2 is (6, 7, 0)

05/06/23

Page 31: Attribute - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus   12/24/2016

31

Instead ofSystem.out.println("p1 is (" + p1.x + ", " + p1.y + ")");

It would be nice to have something more like:System.out.println("p1 is " + p1);

What does this line currently do? Does it even compile?It will print: p1 is Point@9e8c34

05/06/23

Page 32: Attribute - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus   12/24/2016

32

When an object is printed or concatenated with a String, Java calls the object's toString method.

System.out.println("p1 is " + p1);

is equivalent to:

System.out.println("p1 is " + p1.toString());

Note: Every class has a toString method.05/06/23

Page 33: Attribute - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus   12/24/2016

33

The default toString behavior is to return the class's name followed by gibberish (as far as you are concerned).

You can replace the default behavior by defining a toString method in your class.

05/06/23

Page 34: Attribute - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus   12/24/2016

34

The toString method, general syntax:public String toString() { <statement(s) that return a String>;}

NB: The method must have this exact name and signature (i.e., public String toString()).

Example:// Returns a String representing this Point.public String toString() { return "(" + x + ", " + y + “, “ + z + ")";}

05/06/23

Page 35: Attribute - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus   12/24/2016

The Math Class, p400-402 Provides many standard mathematical

methods See in examples

05/06/23 35

Page 36: Attribute - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus   12/24/2016

05/06/23 36

Page 37: Attribute - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus   12/24/2016

Recall that arguments of primitive type treated differently from those of a class type May need to treat primitive value as

an object Java provides wrapper classes for

each primitive type, p403-408 Methods provided to act on values

05/06/23 37

Page 38: Attribute - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus   12/24/2016

Contain useful predefined constants and methods, for instance: Integer.MAX_VALUE Double.parseDouble(“199.88”) Double.toString(199.88)

Have no default constructor Programmer must specify an initializing value

when creating new object Have no set methods

05/06/23 38

Page 39: Attribute - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus   12/24/2016

Overloading When two or more methods have same

name within the same class Java distinguishes the methods by

number and types of parameters If it cannot match a call with a definition, it

attempts to do type conversions A method's name and number and type

of parameters is called the signature

05/06/23 39

Page 40: Attribute - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus   12/24/2016

Overloading and automatic type conversion can conflict

Remember the compiler attempts to overload before it does type conversion

Use descriptive method names, avoid overloading

05/06/23 40

Page 41: Attribute - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus   12/24/2016

P420-426 http://www.cis.temple.edu/~jiang/Overl

oad.pdf

05/06/23 41

Page 42: Attribute - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus   12/24/2016

You must not overload a method where the only difference is the type of value returned

05/06/23 42

Page 43: Attribute - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus   12/24/2016

Static Variables Are shared by all objects of a class

Variables declared static final are considered constants – value cannot be changed

e.g., public static final double FEET_PER_YARD = 3; Variables declared static (without final) can be changed Only one instance of the variable exists It can be accessed by all instances of the class e.g., public static int numberOfInvocations;

05/06/23 43

Page 44: Attribute - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus   12/24/2016

Static variables also called class variables, p389-390 Contrast with instance variables

Do not confuse class variables with variables of a class type

Both static variables and instance variables are sometimes called fields or data members

05/06/23 44

Page 45: Attribute - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus   12/24/2016

Packages and Importing A package is a collection of classes

grouped together into a folder, p441-446 Name of folder is name of package Each class

Placed in a separate file Has this line at the beginning of the file

package Package_Name; Classes use packages by use of import

statement

05/06/23 45

Page 46: Attribute - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus   12/24/2016

Package name tells compiler path name for directory containing classes of package

Search for package begins in class path base directory Package name uses dots in place of / or \

Name of package uses relative path name starting from any directory in class path

05/06/23 46

Page 47: Attribute - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus   12/24/2016

Figure 6.5 A package name

05/06/23 47

Page 48: Attribute - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus   12/24/2016

Packages help in dealing with name clashes When two classes have same name

Different programmers may give same name to two classes Ambiguity resolved by using the

package name

05/06/23 48

Page 49: Attribute - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus   12/24/2016

Public and private modifier, p296-298 Accessor and mutator method, p302-305 Constructor, p375-377 Calling private method from constructor, p384 Static method, p390-392 Adding method main to a class, p398-400 Math class, p400-402 Wrapper class, p403-408 Overloading, p420-426 Static Variables, p389-390 Packages, p441-446

4905/06/23

Summary