《JAVA OOP开发》英文版 Chapter 4 Defining Instantiable Classes

Chapter 4 Defining Instantiable Classes 2000 McGraw-Hl‖ Introduction to Object-Oriented Programming with Java-Wu Chapter 4-1
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 4 - 1 Chapter 4 Defining Instantiable Classes

Chapter 4 objectives After you have read and studied this chapter, you should be able to e Define an instantiable class with multiple methods and a constructor e Differentiate the local and instance variables e Define and use value-returning methods. Distinguish private and public methods e Distinguish private and public data members e Describe how the arguments are passed to the parameters in method definitions Use System. out for temporary output to verify the program code. C 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 4-2
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 4 - 2 Chapter 4 Objectives After you have read and studied this chapter, you should be able to Define an instantiable class with multiple methods and a constructor. Differentiate the local and instance variables. Define and use value-returning methods. Distinguish private and public methods. Distinguish private and public data members. Describe how the arguments are passed to the parameters in method definitions. Use System.out for temporary output to verify the program code

Building large programs Learning how to define instantiable classes is the first step toward mastering the skills necessary in building large programs a class is instantiable if we can create instances of the class The Main Window, Input Box, and OutputBox classes are all instantiable classes while the math class is not r In this chapter you will learn how to define instantiable classes and different types of methods included in the instantiable classes C 2000 McGraw-Hill troduction to Object-Oriented Programming with Java--Wu Chapter 4-3
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 4 - 3 Building Large Programs Learning how to define instantiable classes is the first step toward mastering the skills necessary in building large programs. A class is instantiable if we can create instances of the class. The MainWindow, InputBox, and OutputBox classes are all instantiable classes while the Math class is not. In this chapter you will learn how to define instantiable classes and different types of methods included in the instantiable classes

Currency Converter r Let's start with an example to cover the basics of defining instantiable classes r In designing an instantiable class, we start with its specification, namely how we want the class and its instances behave r a Currency Converter object will perform a conversion from a foreign currency and the U.s. dollar. r What would be the most natural way for us to interact with Currency Converter objects? C 2000 McGraw-Hill troduction to Object-Oriented Programming with Java--Wu Chapter 4-4
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 4 - 4 CurrencyConverter Let’s start with an example to cover the basics of defining instantiable classes. In designing an instantiable class, we start with its specification, namely, how we want the class and its instances behave. A CurrencyConverter object will perform a conversion from a foreign currency and the U.S. dollar. What would be the most natural way for us to interact with CurrencyConverter objects?

from Dollar and toDollar methods r We would like to have at least) two methods for conversion: from Dollar and toDollar Currencyconverter yenconverteri yenConverter new Currency Converter()i double amount InYen yenConverter. fromDollar(500)i Converting S500 Us to yen double amount InuS Converting yenConverter. toDollar(15000)i 15,000 yen to US dollar C 2000 McGraw-Hill troduction to Object-Oriented Programming with Java--Wu Chapter 4-5
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 4 - 5 fromDollar and toDollar Methods We would like to have (at least) two methods for conversion: fromDollar and toDollar. CurrencyConverter yenConverter; yenConverter = new CurrencyConverter(); double amountInYen = yenConverter.fromDollar( 500); double amountInUS = yenConverter.toDollar(15000); Converting $500 US to yen. Converting 15,000 yen to US dollar

setExchangeRate methods r Since the exchange rate fluctuates we need a method to set the exchange rate Currencyconverter yenconverter converter new Currency Converter yenConverter. setExchangeRate( 106.55)i $100Us=10655yen C 2000 McGraw-Hill troduction to Object-Oriented Programming with Java--Wu Chapter 4-6
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 4 - 6 setExchangeRate Methods Since the exchange rate fluctuates, we need a method to set the exchange rate. CurrencyConverter yenConverter; yenConverter = new CurrencyConverter; yenConverter.setExchangeRate( 106.55); $1.00 US = 106.55 yen

Using multiple instances r Once the currency Converter class is defined we can use its multiple instances Currencyconverter yenConverterr markConverteri double amountInYen, amountInMark, amount InDollari yenConverter new CurrencyConverter()i yenConverter. setExchangeRate(13077) markConverter new CurrencyConverter()i markConverter. setExchangeRate(1. 792) amountinyen yenConverter. fromDollar(200)i amountInMark markConverter. fromDollar( 200)i amountInDollar yenConverter. toDollar( 10000)i amountInMark markConverter. fromDollar (amount InDollar C 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 4-7
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 4 - 7 Using Multiple Instances Once the CurrencyConverter class is defined, we can use its multiple instances. CurrencyConverter yenConverter, markConverter; double amountInYen, amountInMark, amountInDollar; yenConverter = new CurrencyConverter(); yenConverter.setExchangeRate(130.77); markConverter = new CurrencyConverter( ); markConverter.setExchangeRate(1.792); amountInYen = yenConverter.fromDollar( 200 ); amountInMark = markConverter.fromDollar( 200 ); amountInDollar = yenConverter.toDollar( 10000 ); amountInMark = markConverter.fromDollar(amountInDollar);

Template for Class definition Import statement Class Comment Describe the class in the javadoc fomat class Class Name Declarations Declare data members shared by multiple methods here Methods C 2000 McGraw-Hill troduction to Object-Oriented Programming with Java--Wu Chapter 4-8
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 4 - 8 Template for Class Definition class { } . . . Import Statement Class Comment Describe the class in the javadoc format. Class Name Declarations Declare data members shared by multiple methods here. Methods

The Currency Converter Class This class is used to do the currency conversion between a foreign currency and the 大 author Dr. caffeine lass CurrencyConverter how much $1.00 U.s. is worth in the foreign currency private double exchangeRate //method declarations come here C 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 4-9
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 4 - 9 The CurrencyConverter Class /** * This class is used to do the currency conversion * between a foreign currency and the U.S. dollar. * * @author Dr. Caffeine */ class CurrencyConverter { /** * how much $1.00 U.S. is worth in the foreign currency */ private double exchangeRate; //method declarations come here }

Method declaration ( Modifier Return Type Method name Parameter ●●●●●●●●●●●● publ oid setExchangeRagte double rate exchangeRate ratei Statements C 2000 McGraw-Hill troduction to Object-Oriented Programming with Java--Wu Chapter 4-10
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 4 - 10 Method Declaration ( ) { } public void setExchangeRagte ( double rate ) { exchangeRate = rate; } Statements Modifier Return Type Method Name Parameter
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
- 《JAVA OOP开发》英文版 Chapter 3 Numerical Data.ppt
- 《JAVA OOP开发》英文版 Chapter 2 Java Programming Basics.ppt
- 《JAVA OOP开发》英文版 Chapter 1 Introduction to Object-oriented Programming and Software Development.ppt
- 《JAVA OOP开发》英文版 Introduction to Computers and Programming Languages.ppt
- 《Windows DNA应用程式》 面向对象分析与设计讲义.ppt
- 北大青鸟:《程序设计基础:C语言实现》课程教学资源(PPT课件讲稿)第四章 第四讲 分支结构.ppt
- 北大青鸟:《程序设计基础:C语言实现》课程教学资源(PPT课件讲稿)第十二章 文件.ppt
- 北大青鸟:《程序设计基础:C语言实现》课程教学资源(PPT课件讲稿)第十一章 复杂数据类型及排序.ppt
- 北大青鸟:《程序设计基础:C语言实现》课程教学资源(教案讲义)第六讲 数组.doc
- 北大青鸟:《程序设计基础:C语言实现》课程教学资源(PPT课件讲稿)第六讲 数组.ppt
- 北大青鸟:《程序设计基础:C语言实现》课程教学资源(PPT课件讲稿)第七章(7-2)指针与指针变量.ppt
- 北大青鸟:《程序设计基础:C语言实现》课程教学资源(PPT课件讲稿)第五章 循环结构.ppt
- 北大青鸟:《程序设计基础:C语言实现》课程教学资源(PPT课件讲稿)第二章 数据类型与运算符.ppt
- 北大青鸟:《程序设计基础:C语言实现》课程教学资源(教案讲义)第二讲 数据类型与算术运算.doc
- 北大青鸟:《程序设计基础:C语言实现》课程教学资源(教案讲义)第九讲 函数.doc
- 北大青鸟:《程序设计基础:C语言实现》课程教学资源(PPT课件讲稿)第九章 函数.ppt
- 北大青鸟:《程序设计基础:C语言实现》课程教学资源(PPT课件讲稿)第三章 运算符与表达式.ppt
- 北大青鸟:《程序设计基础:C语言实现》课程教学资源(教案讲义)第三讲 运算与顺序结构.doc
- 北大青鸟:《程序设计基础:C语言实现》课程教学资源(PPT课件讲稿)第七章 指针.ppt
- 北大青鸟:《程序设计基础:C语言实现》课程教学资源(教案讲义)第七讲 数据存储机制.doc
- 《JAVA OOP开发》英文版 Chapter 5 Processing Input with Applets.ppt
- 《JAVA OOP开发》英文版 Chapter 6 Selection statements.ppt
- 《JAVA OOP开发》英文版 Chapter 7 Repetition Statements.ppt
- 《JAVA OOP开发》英文版 Chapter 8 Characters and strings.ppt
- 《JAVA OOP开发》英文版 Chapter 9 objectives.ppt
- 《JAVA OOP开发》英文版 Chapter 10 Sorting and Searching.ppt
- 《JAVA OOP开发》英文版 Chapter 11 File Input and Output.ppt
- 《JAVA OOP开发》英文版 Chapter 12 Reusable classes and packages.ppt
- 《JAVA OOP开发》英文版 Chapter 13 GUI Objects and Event-Driven Programming.ppt
- 《JAVA OOP开发》英文版 Chapter 14 Inheritance and Polymorphism.ppt
- 《JAVA OOP开发》英文版 Chapter 15 Case Study Class Roster Maintenance program.ppt
- 《JAVA OOP开发》英文版 Chapter 16 Chapter 16 Recursive algorithms.ppt
- 人民邮电出版社:高职高专现代信息技术系列教材《单片机原理与接口技术》课程电子教案(PPT课件讲稿)第一章 绪论.ppt
- 人民邮电出版社:高职高专现代信息技术系列教材《单片机原理与接口技术》课程电子教案(PPT课件讲稿)第七章 牛行接与应用.ppt
- 人民邮电出版社:高职高专现代信息技术系列教材《单片机原理与接口技术》课程电子教案(PPT课件讲稿)第三章 MCS-51单片机指令系统.ppt
- 人民邮电出版社:高职高专现代信息技术系列教材《单片机原理与接口技术》课程电子教案(PPT课件讲稿)第九章 A/D、D/A转换接口.ppt
- 人民邮电出版社:高职高专现代信息技术系列教材《单片机原理与接口技术》课程电子教案(PPT课件讲稿)第二章 MCS-51学机组成理.ppt
- 人民邮电出版社:高职高专现代信息技术系列教材《单片机原理与接口技术》课程电子教案(PPT课件讲稿)第五章 输入/输出与中断.ppt
- 人民邮电出版社:高职高专现代信息技术系列教材《单片机原理与接口技术》课程电子教案(PPT课件讲稿)第八章 并行接口与应用.ppt
- 人民邮电出版社:高职高专现代信息技术系列教材《单片机原理与接口技术》课程电子教案(PPT课件讲稿)第六章 定时器/计数器及应用.ppt