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>( <parameters> <statements> 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 <modifier> <return type> <method name> ( <parameters> ) { <statements> } public void setExchangeRagte ( double rate ) { exchangeRate = rate; } Statements Modifier Return Type Method Name Parameter