Week 7 slides
Download
Report
Transcript Week 7 slides
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 lab
Why do we have to create
instantiable classes?
(create objects from these
• What does that mean by
instantiable
classes)
classes?
– We can create instances of these classes
• Example:
String
DecimalFormat
Why do we have to create
instantiable classes?
• Steps in LoanCalculator.java or
AnnuityFund.java contains:
Describe the program
Get inputs from users
Computation
Display output to users
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
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
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 string
substring: returns a substring from the original string
charAt: returns a character at a specific position
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
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
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…
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?
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
Implementation
• Class header and class body.
• Member definitions in the body.
Class Header
– Methods and attributes.
// class called ClassName.
class ClassName {
// Attribute definitions go in the class body
// Method definitions go in the class body
...
}
Implementation
For 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,…etc
possible datatype: String
• Any default value for this attribute:
Example: good
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”;
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