SlideShare une entreprise Scribd logo
1  sur  46
Chapter 7 Defining Your Own Classes Part 2
Objectives ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Returning an Object from a Method ,[object Object],[object Object],[object Object]
Sample Object-Returning Method ,[object Object],public  Fraction simplify ( ) { Fraction simp; int  num  = getNumberator () ; int  denom = getDenominator () ; int  gcd  = gcd ( num, denom ) ; simp =  new  Fraction ( num/gcd, denom/gcd ) ; return  simp; } Return type indicates the class of an object we're returning from the method. Return an instance of the Fraction class
A Sample Call to  simplify
A Sample Call to  simplify  (cont'd)
Reserved Word  this ,[object Object],[object Object],:  Object this
The Use of  this  in the add Method public   Fraction add ( Fraction frac ) { int   a, b, c, d; Fraction sum; a =  this .getNumerator () ;  //get the receiving b =  this .getDenominator () ;  //object's num and denom c = frac.getNumerator () ;  //get frac's num d = frac.getDenominator () ;  //and denom sum =  new   Fraction ( a*d + b*c, b*d ) ; return   sum; }
f1.add(f2) Because  f1  is the receiving object (we're calling  f1 's method), so the reserved word  this  is referring to  f1 .
f2.add(f1) This time, we're calling  f2 's method, so the reserved word  this  is referring to  f2 .
Using this to Refer to Data Members ,[object Object],[object Object],class   Person  { int   age ;   public   void   setAge( int   val)  {   this .age = val; } . . . }
Overloaded Methods ,[object Object],[object Object],[object Object],public   void   myMethod ( int   x,  int   y ) {  ...  } public   void   myMethod ( int   x ) {  ...  } public   void   myMethod ( double   x ) {  ...  } public   void   myMethod ( int   x ) {  ...  } Rule 1 Rule 2
Overloaded Constructor ,[object Object],[object Object],public   Person ( ) {  ...  } public   Person ( int   age ) {  ...  } public   Pet ( int   age ) {  ...  } public   Pet ( String name ) {  ...  } Rule 1 Rule 2
Constructors and  this ,[object Object],public  Fraction ( ) {  //creates 0/1 this ( 0. 1 ) ; } public  Fraction ( int  number ) {  //creates number/1 this ( number, 1 ) ; } public  Fraction ( Fraction frac ) {  //copy constructor this ( frac.getNumerator () , frac.getDenominator ()) ; } public  Fraction ( int  num,  int  denom ) { setNumerator ( num ) ; setDenominator ( denom ) ; }
Class Methods ,[object Object],public static   int   gcd ( int   m,  int   n ) { //the code implementing the Euclidean algorithm } public static   Fraction min ( Fraction f1, Fraction f2 ) { //convert to decimals and then compare }
Call-by-Value Parameter Passing ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Call-by-Value Example produces 10 20 ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Tester tester; int   x, y; tester =  new   Tester () ; x = 10; y = 20; tester.myMethod ( x, y ) ; System.out.println ( x +  " "   + y ) ;
Memory Allocation for Parameters
Memory Allocation for Parameters (cont'd)
Parameter Passing: Key Points ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Organizing Classes into a Package ,[object Object],[object Object],[object Object],[object Object]
Creating a Package ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Using Javadoc Comments ,[object Object],[object Object],[object Object],[object Object]
javadoc for Fraction ,[object Object],[object Object]
javadoc Tags ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Example: javadoc Source . . . /** * Returns the sum of this Fraction * and the parameter frac. The sum * returned is NOT simplified. * * @param frac the Fraction to add to this *  Fraction * * @return the sum of this and frac */ public   Fraction add ( Fraction frac ) { ... } . . . this javadoc will produce
Example: javadoc Output
javadoc Resources ,[object Object],[object Object],[object Object],[object Object]
Problem Statement ,[object Object],[object Object]
Overall Plan ,[object Object],[object Object],[object Object],[object Object]
Required Classes helper class OverdueChecker Scanner LibraryBook BookTracker
Development Steps ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Step 1 Design ,[object Object],[object Object],[object Object],[object Object]
Step 1 Code ,[object Object],[object Object],[object Object],Program source file is too big to list here. From now on, we ask you to view the source files using your Java IDE.
Step 1 Test ,[object Object]
Step 2 Design ,[object Object],[object Object]
Step 2 Code ,[object Object],[object Object],[object Object]
Step 2 Test ,[object Object],[object Object]
Step 3 Design ,[object Object],[object Object],[object Object],[object Object]
Step 3 Pseudocode GregorianCalendar returnDate; String reply, table; double  totalCharge; inputBooks () ;  //read in all book information table = bookTracker.getList () ; System.out.println ( table ) ; //try different return dates do   { returnDate =  read return date  ; totalCharge = bookTracker.getCharge ( returnDate ) ; displayTotalCharge(totalCharge); reply =  prompt the user to continue or not ; }   while   (   reply is yes   ) ;
Step 3 Code ,[object Object],[object Object],[object Object]
Step 3 Test ,[object Object],[object Object],[object Object],[object Object]
Step 4: Compute the Charge ,[object Object],[object Object],[object Object],[object Object],[object Object]
Step 4 Code ,[object Object],[object Object],[object Object]
Step 4 Test ,[object Object],[object Object],[object Object],[object Object]
Step 5: Finalize / Extend ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]

Contenu connexe

Tendances

Chapter 9 - Characters and Strings
Chapter 9 - Characters and StringsChapter 9 - Characters and Strings
Chapter 9 - Characters and StringsEduardo Bergavera
 
Built in classes in java
Built in classes in javaBuilt in classes in java
Built in classes in javaMahmoud Ali
 
Object oriented design
Object oriented designObject oriented design
Object oriented designlykado0dles
 
Lecture 4_Java Method-constructor_imp_keywords
Lecture   4_Java Method-constructor_imp_keywordsLecture   4_Java Method-constructor_imp_keywords
Lecture 4_Java Method-constructor_imp_keywordsmanish kumar
 
Unit 2-data types,Variables,Operators,Conitionals,loops and arrays
Unit 2-data types,Variables,Operators,Conitionals,loops and arraysUnit 2-data types,Variables,Operators,Conitionals,loops and arrays
Unit 2-data types,Variables,Operators,Conitionals,loops and arraysDevaKumari Vijay
 
Lecture 8 abstract class and interface
Lecture   8 abstract class and interfaceLecture   8 abstract class and interface
Lecture 8 abstract class and interfacemanish kumar
 
JAVA Notes - All major concepts covered with examples
JAVA Notes - All major concepts covered with examplesJAVA Notes - All major concepts covered with examples
JAVA Notes - All major concepts covered with examplesSunil Kumar Gunasekaran
 
Lecture 6 inheritance
Lecture   6 inheritanceLecture   6 inheritance
Lecture 6 inheritancemanish kumar
 
Introduction to class in java
Introduction to class in javaIntroduction to class in java
Introduction to class in javakamal kotecha
 
Java Foundations: Methods
Java Foundations: MethodsJava Foundations: Methods
Java Foundations: MethodsSvetlin Nakov
 
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteChapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteTushar B Kute
 
Java Beans Unit 4(Part 1)
Java Beans Unit 4(Part 1)Java Beans Unit 4(Part 1)
Java Beans Unit 4(Part 1)SURBHI SAROHA
 
Chapter 13 introduction to classes
Chapter 13 introduction to classesChapter 13 introduction to classes
Chapter 13 introduction to classesrsnyder3601
 

Tendances (20)

Chapter 9 - Characters and Strings
Chapter 9 - Characters and StringsChapter 9 - Characters and Strings
Chapter 9 - Characters and Strings
 
Built in classes in java
Built in classes in javaBuilt in classes in java
Built in classes in java
 
Object oriented design
Object oriented designObject oriented design
Object oriented design
 
Lecture 4_Java Method-constructor_imp_keywords
Lecture   4_Java Method-constructor_imp_keywordsLecture   4_Java Method-constructor_imp_keywords
Lecture 4_Java Method-constructor_imp_keywords
 
Java class
Java classJava class
Java class
 
Unit 2-data types,Variables,Operators,Conitionals,loops and arrays
Unit 2-data types,Variables,Operators,Conitionals,loops and arraysUnit 2-data types,Variables,Operators,Conitionals,loops and arrays
Unit 2-data types,Variables,Operators,Conitionals,loops and arrays
 
Advanced core java
Advanced core javaAdvanced core java
Advanced core java
 
Lecture 8 abstract class and interface
Lecture   8 abstract class and interfaceLecture   8 abstract class and interface
Lecture 8 abstract class and interface
 
JAVA Notes - All major concepts covered with examples
JAVA Notes - All major concepts covered with examplesJAVA Notes - All major concepts covered with examples
JAVA Notes - All major concepts covered with examples
 
Lecture 6 inheritance
Lecture   6 inheritanceLecture   6 inheritance
Lecture 6 inheritance
 
Introduction to class in java
Introduction to class in javaIntroduction to class in java
Introduction to class in java
 
JAVA CONCEPTS
JAVA CONCEPTS JAVA CONCEPTS
JAVA CONCEPTS
 
Generics in java
Generics in javaGenerics in java
Generics in java
 
LectureNotes-02-DSA
LectureNotes-02-DSALectureNotes-02-DSA
LectureNotes-02-DSA
 
Java Foundations: Methods
Java Foundations: MethodsJava Foundations: Methods
Java Foundations: Methods
 
Class introduction in java
Class introduction in javaClass introduction in java
Class introduction in java
 
LectureNotes-05-DSA
LectureNotes-05-DSALectureNotes-05-DSA
LectureNotes-05-DSA
 
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteChapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
 
Java Beans Unit 4(Part 1)
Java Beans Unit 4(Part 1)Java Beans Unit 4(Part 1)
Java Beans Unit 4(Part 1)
 
Chapter 13 introduction to classes
Chapter 13 introduction to classesChapter 13 introduction to classes
Chapter 13 introduction to classes
 

En vedette

Java căn bản - Chapter6
Java căn bản - Chapter6Java căn bản - Chapter6
Java căn bản - Chapter6Vince Vo
 
Java căn bản - Chapter2
Java căn bản - Chapter2Java căn bản - Chapter2
Java căn bản - Chapter2Vince Vo
 
Java căn bản - Chapter12
Java căn bản - Chapter12Java căn bản - Chapter12
Java căn bản - Chapter12Vince Vo
 
Java căn bản - Chapter4
Java căn bản - Chapter4Java căn bản - Chapter4
Java căn bản - Chapter4Vince Vo
 

En vedette (8)

Java căn bản - Chapter6
Java căn bản - Chapter6Java căn bản - Chapter6
Java căn bản - Chapter6
 
Rama Ch7
Rama Ch7Rama Ch7
Rama Ch7
 
Rama Ch12
Rama Ch12Rama Ch12
Rama Ch12
 
Java căn bản - Chapter2
Java căn bản - Chapter2Java căn bản - Chapter2
Java căn bản - Chapter2
 
Rama Ch14
Rama Ch14Rama Ch14
Rama Ch14
 
Java căn bản - Chapter12
Java căn bản - Chapter12Java căn bản - Chapter12
Java căn bản - Chapter12
 
Java căn bản - Chapter4
Java căn bản - Chapter4Java căn bản - Chapter4
Java căn bản - Chapter4
 
Rama Ch11
Rama Ch11Rama Ch11
Rama Ch11
 

Similaire à Java căn bản - Chapter7

Application package
Application packageApplication package
Application packageJAYAARC
 
CJP Unit-1 contd.pptx
CJP Unit-1 contd.pptxCJP Unit-1 contd.pptx
CJP Unit-1 contd.pptxRAJASEKHARV10
 
Introduction to objects and inputoutput
Introduction to objects and inputoutput Introduction to objects and inputoutput
Introduction to objects and inputoutput Ahmad Idrees
 
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...vekariyakashyap
 
Ap Power Point Chpt4
Ap Power Point Chpt4Ap Power Point Chpt4
Ap Power Point Chpt4dplunkett
 
Object oriented concepts & programming (2620003)
Object oriented concepts & programming (2620003)Object oriented concepts & programming (2620003)
Object oriented concepts & programming (2620003)nirajmandaliya
 
9781439035665 ppt ch08
9781439035665 ppt ch089781439035665 ppt ch08
9781439035665 ppt ch08Terry Yoast
 
C++ Object oriented concepts & programming
C++ Object oriented concepts & programmingC++ Object oriented concepts & programming
C++ Object oriented concepts & programmingnirajmandaliya
 
UNIT-2.pptx CS3391 Inheritance , types, packages and Interfaces
UNIT-2.pptx CS3391 Inheritance , types, packages and InterfacesUNIT-2.pptx CS3391 Inheritance , types, packages and Interfaces
UNIT-2.pptx CS3391 Inheritance , types, packages and InterfacesSakkaravarthiS1
 
packages and interfaces
packages and interfacespackages and interfaces
packages and interfacesmadhavi patil
 
Introduction to OOP with PHP
Introduction to OOP with PHPIntroduction to OOP with PHP
Introduction to OOP with PHPMichael Peacock
 
Structural pattern 3
Structural pattern 3Structural pattern 3
Structural pattern 3Naga Muruga
 
Presentation 3rd
Presentation 3rdPresentation 3rd
Presentation 3rdConnex
 
CIS 1403 lab 3 functions and methods in Java
CIS 1403 lab 3 functions and methods in JavaCIS 1403 lab 3 functions and methods in Java
CIS 1403 lab 3 functions and methods in JavaHamad Odhabi
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programmingRenas Rekany
 

Similaire à Java căn bản - Chapter7 (20)

Application package
Application packageApplication package
Application package
 
CJP Unit-1 contd.pptx
CJP Unit-1 contd.pptxCJP Unit-1 contd.pptx
CJP Unit-1 contd.pptx
 
Introduction to objects and inputoutput
Introduction to objects and inputoutput Introduction to objects and inputoutput
Introduction to objects and inputoutput
 
C sharp chap5
C sharp chap5C sharp chap5
C sharp chap5
 
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...
 
Inheritance
InheritanceInheritance
Inheritance
 
Ap Power Point Chpt4
Ap Power Point Chpt4Ap Power Point Chpt4
Ap Power Point Chpt4
 
Object oriented concepts & programming (2620003)
Object oriented concepts & programming (2620003)Object oriented concepts & programming (2620003)
Object oriented concepts & programming (2620003)
 
Computer programming 2 Lesson 15
Computer programming 2  Lesson 15Computer programming 2  Lesson 15
Computer programming 2 Lesson 15
 
9781439035665 ppt ch08
9781439035665 ppt ch089781439035665 ppt ch08
9781439035665 ppt ch08
 
C++ Object oriented concepts & programming
C++ Object oriented concepts & programmingC++ Object oriented concepts & programming
C++ Object oriented concepts & programming
 
UNIT-2.pptx CS3391 Inheritance , types, packages and Interfaces
UNIT-2.pptx CS3391 Inheritance , types, packages and InterfacesUNIT-2.pptx CS3391 Inheritance , types, packages and Interfaces
UNIT-2.pptx CS3391 Inheritance , types, packages and Interfaces
 
Java Programming - 04 object oriented in java
Java Programming - 04 object oriented in javaJava Programming - 04 object oriented in java
Java Programming - 04 object oriented in java
 
packages and interfaces
packages and interfacespackages and interfaces
packages and interfaces
 
Introduction to OOP with PHP
Introduction to OOP with PHPIntroduction to OOP with PHP
Introduction to OOP with PHP
 
Structural pattern 3
Structural pattern 3Structural pattern 3
Structural pattern 3
 
Unit ii
Unit   iiUnit   ii
Unit ii
 
Presentation 3rd
Presentation 3rdPresentation 3rd
Presentation 3rd
 
CIS 1403 lab 3 functions and methods in Java
CIS 1403 lab 3 functions and methods in JavaCIS 1403 lab 3 functions and methods in Java
CIS 1403 lab 3 functions and methods in Java
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
 

Plus de Vince Vo

Java căn bản - Chapter13
Java căn bản - Chapter13Java căn bản - Chapter13
Java căn bản - Chapter13Vince Vo
 
Java căn bản - Chapter10
Java căn bản - Chapter10Java căn bản - Chapter10
Java căn bản - Chapter10Vince Vo
 
Java căn bản - Chapter9
Java căn bản - Chapter9Java căn bản - Chapter9
Java căn bản - Chapter9Vince Vo
 
Java căn bản - Chapter8
Java căn bản - Chapter8Java căn bản - Chapter8
Java căn bản - Chapter8Vince Vo
 
Java căn bản - Chapter5
Java căn bản - Chapter5Java căn bản - Chapter5
Java căn bản - Chapter5Vince Vo
 
Java căn bản - Chapter3
Java căn bản - Chapter3Java căn bản - Chapter3
Java căn bản - Chapter3Vince Vo
 
Java căn bản- Chapter1
Java  căn bản- Chapter1Java  căn bản- Chapter1
Java căn bản- Chapter1Vince Vo
 
Hướng dẫn cài đặt Java
Hướng dẫn cài đặt JavaHướng dẫn cài đặt Java
Hướng dẫn cài đặt JavaVince Vo
 

Plus de Vince Vo (19)

Java căn bản - Chapter13
Java căn bản - Chapter13Java căn bản - Chapter13
Java căn bản - Chapter13
 
Java căn bản - Chapter10
Java căn bản - Chapter10Java căn bản - Chapter10
Java căn bản - Chapter10
 
Java căn bản - Chapter9
Java căn bản - Chapter9Java căn bản - Chapter9
Java căn bản - Chapter9
 
Java căn bản - Chapter8
Java căn bản - Chapter8Java căn bản - Chapter8
Java căn bản - Chapter8
 
Java căn bản - Chapter5
Java căn bản - Chapter5Java căn bản - Chapter5
Java căn bản - Chapter5
 
Java căn bản - Chapter3
Java căn bản - Chapter3Java căn bản - Chapter3
Java căn bản - Chapter3
 
Java căn bản- Chapter1
Java  căn bản- Chapter1Java  căn bản- Chapter1
Java căn bản- Chapter1
 
Hướng dẫn cài đặt Java
Hướng dẫn cài đặt JavaHướng dẫn cài đặt Java
Hướng dẫn cài đặt Java
 
Rama Ch13
Rama Ch13Rama Ch13
Rama Ch13
 
Rama Ch12
Rama Ch12Rama Ch12
Rama Ch12
 
Rama Ch10
Rama Ch10Rama Ch10
Rama Ch10
 
Rama Ch8
Rama Ch8Rama Ch8
Rama Ch8
 
Rama Ch9
Rama Ch9Rama Ch9
Rama Ch9
 
Rama Ch6
Rama Ch6Rama Ch6
Rama Ch6
 
Rama Ch5
Rama Ch5Rama Ch5
Rama Ch5
 
Rama Ch4
Rama Ch4Rama Ch4
Rama Ch4
 
Rama Ch3
Rama Ch3Rama Ch3
Rama Ch3
 
Rama Ch2
Rama Ch2Rama Ch2
Rama Ch2
 
Rama Ch1
Rama Ch1Rama Ch1
Rama Ch1
 

Dernier

9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...Pooja Nehwal
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
The byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxThe byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxShobhayan Kirtania
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 

Dernier (20)

9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
The byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxThe byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptx
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 

Java căn bản - Chapter7

  • 1. Chapter 7 Defining Your Own Classes Part 2
  • 2.
  • 3.
  • 4.
  • 5. A Sample Call to simplify
  • 6. A Sample Call to simplify (cont'd)
  • 7.
  • 8. The Use of this in the add Method public Fraction add ( Fraction frac ) { int a, b, c, d; Fraction sum; a = this .getNumerator () ; //get the receiving b = this .getDenominator () ; //object's num and denom c = frac.getNumerator () ; //get frac's num d = frac.getDenominator () ; //and denom sum = new Fraction ( a*d + b*c, b*d ) ; return sum; }
  • 9. f1.add(f2) Because f1 is the receiving object (we're calling f1 's method), so the reserved word this is referring to f1 .
  • 10. f2.add(f1) This time, we're calling f2 's method, so the reserved word this is referring to f2 .
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18. Memory Allocation for Parameters
  • 19. Memory Allocation for Parameters (cont'd)
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26. Example: javadoc Source . . . /** * Returns the sum of this Fraction * and the parameter frac. The sum * returned is NOT simplified. * * @param frac the Fraction to add to this * Fraction * * @return the sum of this and frac */ public Fraction add ( Fraction frac ) { ... } . . . this javadoc will produce
  • 28.
  • 29.
  • 30.
  • 31. Required Classes helper class OverdueChecker Scanner LibraryBook BookTracker
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40. Step 3 Pseudocode GregorianCalendar returnDate; String reply, table; double totalCharge; inputBooks () ; //read in all book information table = bookTracker.getList () ; System.out.println ( table ) ; //try different return dates do { returnDate = read return date ; totalCharge = bookTracker.getCharge ( returnDate ) ; displayTotalCharge(totalCharge); reply = prompt the user to continue or not ; } while ( reply is yes ) ;
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.

Notes de l'éditeur

  1. A class is instantiable if we can create instances of it. For example, the JFrame class is instantiable. A class is noninstantiable if we cannot create its instances. The Math class is one example of noninstantiable classes. The main classes we have been writing are all noninstantiable. We will learn how to define instantiable classes in this lesson. A constructor is a special method that is used to initialize an instance of a class when it is first created.
  2. We already know how to return an object from a method, because we have been returning a string from a method and a string is an object (an instance of the String class). However, String objects are treated much like primitive data. We will explore a little deeper into this topic.
  3. The use of the reserved word this is option in this context. Calling a method of a class or accessing a data member of the class can be achieved without the reserved word this. Consider, for example, class One { public int m1( ) { return 10; } public void m2( ) { int num; num = m1();  this is equivalent to num = this .m1(); } }
  4. Notice that the difference in the return type alone is not enough to overload the methods. For example, the following declaration is invalid public double convert(int num) { . . . } public float convert(int num) { . . . }
  5. Notice that the difference in the return type alone is not enough to overload the methods. For example, the following declaration is invalid public double convert(int num) { . . . } public float convert(int num) { . . . }
  6. Without using the reserved word this , we need to define the four constructors as follows (it forces us to repeat the same code): public Fraction ( ) { //creates 0/1 setNumerator ( 0 ) ; setDenominator ( 1 ) ; } public Fraction ( int number ) { //creates number/1 setNumerator ( number ) ; setDenominator ( 1 ) ; } public Fraction ( Fraction frac ) { //copy constructor setNumerator ( frac.getNumerator ()) ; setDenominator ( frac.getDenominator ()) ; } public Fraction ( int num, int denom ) { setNumerator ( num ) ; setDenominator ( denom ) ; }
  7. In the command prompt window, we used the commands javac and java to compile and run Java programs, respectively. Similarly, to generate javadoc files, we use the javadoc command. For example, to generate a javadoc file for the Fraction class, we enter javadoc -private Fraction.java We specify the -private option because we want to generate the documentation for all types of methods (so far, we have covered two of these— private and public ). The private option generates the most complete documentation.
  8. As a part of the overall plan, we begin by identifying the main tasks for the program. Unlike the overall plan for the previous sample developments, we will use a pseudo code to express the top level logic of the program.
  9. The structure of this program is very simple. We will use two standard classes, one for input and output and another for generating random numbers.
  10. The second and the third steps correspond to the two major tasks identified in the overall plan.
  11. In the first step, we determine a little more detailed control logic than the one stated in the overall plan. For each of the five identified functions, we will define a method: describeRules, generateSecretNumber, playGame, and prompt.
  12. Please use your Java IDE to view the source files and run the program.
  13. Run the program and verify that the topmost control loop is functioning correctly.
  14. In order to verify whether our code is working correctly or not, we need to know what is the secret number. The easiest way to do this is to use a fixed number, such as 45, make the temporary generateRandomNumber to return this fixed number.
  15. We implement the playGame and getNextGuess methods in this step.
  16. We need to verify the correctness of two methods: playGame and getNextGuess. Try all cases presented here and confirm that you get the expected responses.
  17. Notice that we have one temporary statement to output the value of secretNumber. We include it for the testing purpose, i.e., we need to check the numbers generated are valid.
  18. The body of the inputBooks method is as follows: while ( isContinue ()) { title = readString ( "Title : " ) ; chargePerDay = readDouble ( "Charge per day: " ) ; maxCharge = readDouble ( "Maximum charge: " ) ; dueDate = readDate ( "Due Date : " ) ; book = createBook ( title, chargePerDay, maxCharge, dueDate ) ; bookTracker.add ( book ) ; }
  19. As always, we run the final test by running the program numerous times trying out as many variations as possible. Before testing the generateSecretNumber method as a part of the final program, we will use a separate test driver to generate 1000 (or more) secret numbers and verify that they are valid. class TestRandom { public static void main (String[] args) { int N = 1000, count = 0, number; double X; do { count++; X = Math.random(); number = (int) Math.floor( X * 100 ) + 1; } while ( count < N && 1 <= number && number <= 100 ); if ( number < 1 || number > 100 ) { System.out.println("Error: " + number); } else { System.out.println("Okay"); } } }
  20. As always, we run the final test by running the program numerous times trying out as many variations as possible. Before testing the generateSecretNumber method as a part of the final program, we will use a separate test driver to generate 1000 (or more) secret numbers and verify that they are valid. class TestRandom { public static void main (String[] args) { int N = 1000, count = 0, number; double X; do { count++; X = Math.random(); number = (int) Math.floor( X * 100 ) + 1; } while ( count < N && 1 <= number && number <= 100 ); if ( number < 1 || number > 100 ) { System.out.println("Error: " + number); } else { System.out.println("Okay"); } } }