Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Develop Instantiable classes

2 Lesson plan What does that mean? Why do we have to create instantiable classes? How do we go about creating them? Practice lab

3 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) String DecimalFormat

4 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

5 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

6 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

7 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

8 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

9 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

10 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…

11 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?

12 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

13 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

14 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

15 Implementation Declare an attribute in a class in the same way you declare a variable: ; 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: = ; Example: private String health=“good”;

16 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


Download ppt "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."

Similar presentations


Ads by Google