Title: Develop Instantiable classes
1Develop Instantiable classes
2Lesson plan
- What does that mean?
- Why do we have to create instantiable classes?
- How do we go about creating them?
- Practice lab
3Why 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)
String DecimalFormat
4Why 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
5Why 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..) dont provide exactly what
you need
6How 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
7Structure 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
8How 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
9How 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
-
10How 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
11How 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 - Lets name this method howIsYourHealth
- Similarly whatAreYourCourse?
- whatAreYourHobby?
12How 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
-
13Implementation
- Class header and class body.
- Member definitions in the body.
- Methods and attributes.
-
Class Header
// class called ClassName. class
ClassName // Attribute definitions go in the
class body // Method definitions go in the class
body ...
14Implementation
- For each attribute, determine the followings
- What does it represent?
- Example
- attribute health represents a students
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
-
-
-
15Implementation
- Declare an attribute in a class in the same way
you declare a variable - ltdata typegt ltattribute namegt
- 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 - ltdata typegt ltattribute namegt ltvaluegt
- Example private String healthgood
-
-
16Implementation
- 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
-
-