develop instantiable classes. lesson plan what does that mean? why do we have to create instantiable...

16
Develop Instantiable classes

Upload: derrick-marsh

Post on 13-Jan-2016

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Develop Instantiable classes. Lesson plan What does that mean? Why do we have to create instantiable classes? How do we go about creating them? Practice

Develop Instantiable classes

Page 2: Develop Instantiable classes. Lesson plan What does that mean? Why do we have to create instantiable classes? How do we go about creating them? Practice

Lesson plan

• What does that mean?

• Why do we have to create instantiable classes?

• How do we go about creating them?

• Practice lab

Page 3: Develop Instantiable classes. Lesson plan What does that mean? Why do we have to create instantiable classes? How do we go about creating them? Practice

Why do we have to create instantiable classes?

• What does that mean by instantiable classes?– We can create instances of these classes

• Example:

(create objects from these classes)

StringDecimalFormat

Page 4: Develop Instantiable classes. Lesson plan What does that mean? Why do we have to create instantiable classes? How do we go about creating them? Practice

Why do we have to create instantiable classes?

• Steps in LoanCalculator.java or AnnuityFund.java contains:

Describe the program Get inputs from users

ComputationDisplay output to users

Page 5: Develop Instantiable classes. Lesson plan What does that mean? Why do we have to create instantiable classes? How do we go about creating them? Practice

Why do we have to create instantiable classes?

• Is there any problems with this design? What if we need to solve a more complicated problem?

Large method, impossible to manage If dealing with more complicated problem

Pre defined classes (provided by Java library, Third party, etc..) don’t provide exactly what you

need

Page 6: Develop Instantiable classes. Lesson plan What does that mean? Why do we have to create instantiable classes? How do we go about creating them? Practice

How do we create instantiable classes?

• Understand the structure of a class

• Specification for the class– How the class (that we are going to create)

interact with other classes?– Specify the behaviors and fields that this

class supports

• Implementation

Page 7: Develop Instantiable classes. Lesson plan What does that mean? Why do we have to create instantiable classes? How do we go about creating them? Practice

Structure of a class

• A class definition provides a description of a typical object within that class.

• A class has its behavior (methods) and attributes (fields)

• Example:

class String

attributes and behavior/method :length: returns the length of a stringsubstring: returns a substring from the original stringcharAt: returns a character at a specific position

Page 8: Develop Instantiable classes. Lesson plan What does that mean? Why do we have to create instantiable classes? How do we go about creating them? Practice

How a class interacts with other classes?

• When you are creating a class, we should think of the objects that can be created from this class.– Example: Class Student, each object should

be an individual student (e.g studentA, studentB)

• What would be a natural and logical way to create an object?– Example: we can create a student object if we

know his/her studentID, and name

Page 9: Develop Instantiable classes. Lesson plan What does that mean? Why do we have to create instantiable classes? How do we go about creating them? Practice

How a class interacts with other classes?

What would be a natural and logical way for us to interact with it (or to use it)?Harry: Hi! How are you? How is your Java

midterm?

Sally: Not too bad. I was working hard for the test though

Harry: So what are you doing this week-end? Any plan in particular? We have a tailgate party before the football game. Wanna join?

Sally: Sound great! See you there

Page 10: Develop Instantiable classes. Lesson plan What does that mean? Why do we have to create instantiable classes? How do we go about creating them? Practice

How a class interacts with other classes?

• Analyze the conversation:

• How are you?• Attribute: Health• Value: well, good, tired…

• Java midterm:• Attribute: Course• Value: Java, Database, GenEd, French…

• Working hard:• Attribute: Work Ethic• Value: hard working, lazy, moderate…

• Tailgate party and football game• Attribute: Hobby• Value: party, football, basketball…

Page 11: Develop Instantiable classes. Lesson plan What does that mean? Why do we have to create instantiable classes? How do we go about creating them? Practice

How a class interacts with other classes?

• Attribute: Health– Value: well, good, tired…How two students (objects) ask each other about this attribute:

How are you? (One way of saying how is your health?)

Therefore, we need a method in the Student class so that one student can provide an answer to another student if he/she is asked about his/her health

Let’s name this method: howIsYourHealth

Similarly: whatAreYourCourse? whatAreYourHobby?

Page 12: Develop Instantiable classes. Lesson plan What does that mean? Why do we have to create instantiable classes? How do we go about creating them? Practice

How a class interacts with other classes?

Step 1: Understand the purpose of the class that you are creating

Step 2: Use your imagination to identify attributes and method for this class:

• What would be a natural and logical way to create an object?

• What would be a natural and logical way for an object of this class to interact with another object of the same class?

Step 3: List them out on a paper

Page 13: Develop Instantiable classes. Lesson plan What does that mean? Why do we have to create instantiable classes? How do we go about creating them? Practice

Implementation• Class header and class body.• Member definitions in the body.

– Methods and attributes.

// class called ClassName.class ClassName {

// Attribute definitions go in the class body// Method definitions go in the class body

...}

Class Header

Page 14: Develop Instantiable classes. Lesson plan What does that mean? Why do we have to create instantiable classes? How do we go about creating them? Practice

ImplementationFor each attribute, determine the followings:

• What does it represent?Example:

attribute: health represents a student’s well-being.• Which datatype does we use to represent this attribute?

This can be determined from the possible values that this attribute will be assigned.

Example:attribute: health.possible values: tired, excellent, good,…etcpossible datatype: String

• Any default value for this attribute:Example: good

Page 15: Develop Instantiable classes. Lesson plan What does that mean? Why do we have to create instantiable classes? How do we go about creating them? Practice

Implementation• Declare an attribute in a class in the same way you declare a variable:

<data type> <attribute name>;Example: String health;

• Then add modifier (private or public) in front of the attribute declaration

Example: private String health• You can initialize an attribute with its default value as:

<data type> <attribute name> = <value>;

Example: private String health=“good”;

Page 16: Develop Instantiable classes. Lesson plan What does that mean? Why do we have to create instantiable classes? How do we go about creating them? Practice

Implementation• For each method, determine the followings:

• What does it do?

– E.g: howIsYourHealth answers the question about your health. It will return the value representing your health (whether you are tired, feeling well or not..)

• What does it return?

– E.g howIsYourHealth returns a string (“tired”, or “well”…)

• What are the parameters it requires? (parameter: additional information)

– E.g howIsYourHealth did not require any parametter– howIsYouCourse require a course number