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