9 Chapter CHAPTER GOALS B I G J A V A
436 CHAPTER CONTENTS 9.1 436 Syntax 9.1 441 Syntax 9.2 442 Common Error 9.1 442 9.2 443 Syntax 9.3 445 Advanced Topic 9.1 445 9.3 446 9.4 448 Syntax 9.4 454 Advanced Topic 9.2 455 9.5 456 Common Error 9.1 463 Random Fact 9.1 463 9.1 public class DataSet // BankAccount public void add(bankaccount x) sum = sum + x.getbalance( ); if (count == 0 maximum.getbalance( ) < x.getbalance( )) maximum = x; count++; public BankAccount getmaximum( ) return maximum; private double sum; private BankAccount maximum; private int count;
Chapter 9 437 public class DataSet // Coin public void add(coin x) sum = sum + x.getvalue( ); if (count == 0 maximum.getvalue( ) < x.getvalue( )) maximum = x; count++; public Coin getmaximum( ) return maximum; Chapter 09 private double sum; private Coin maximum; private int count; sum = sum + x.getmeasure( ); if (count == 0 maximum.getmeasure( ) < x.getmeasure( )) maximum = x; count++;
438 public interface Measurable double getmeasure( ); public class DataSet public void add(measurable x) sum = sum + x.getmeasure( ); if (count == 0 maximum.getmeasure( ) < x.getmeasure( )) maximum = x; count++; public Measurable getmaximum( ) return maximum; private double sum; private Measurable maximum; private int count;
Chapter 9 439 class ClassName implements Measurable public double getmeasure( ) Chapter 09 public class BankAccount implements Measurable public double getmeasure( ) return balance; public class Coin implements Measurable public double getmeasure( ) return value;
440 01 /** 02 DataSet 03 */ 04 public class DataSetTest 05 06 public static void main(string [ ] args)) 07 08 09 DataSet bankdata =new DataSet( ); 10 11 bankdata.add(new BankAccount(0)); 12 bankdata.add(new BankAccount(10000)); 13 bankdata.add(new BankAccount(2000)); 14 15 System.out.println("Average balance =" 16 + bankdata.getaverage( )); 17 Measurable max =bankdata.getmaximum( ); 18 System.out.println("Highest balance =" 19 + max.getmeasure( )); 20 21 DataSet coindata =new DataSet( ); 22 23 coindata.add(new Coin(0.25,"quarter")); 24 coindata.add(new Coin(0.1,"dime")); 25 coindata.add(new Coin(0.05,"nickel")); 26 27 System.out.println("Average coin value =" 28 + coindata.getaverage( )); 29 max =coindata.getmaximum( ); 30 System.out.println("Highest coin value =" 31 + max.getmeasure( )); 32 33
Chapter 9 441 Chapter 09 public interface public interface Measurable double getmeasure( );
442 public class implements,, public class bankaccount implements Measurable // BankAccount public double getmeasure( ) // Common Error 9.1 public class BankAccount implements Measurable double getmeasure( ) // public return balance;
Chapter 9 443 9.2 bankdata.add(new BankAccount(10000)); Chapter 09 BankAccount account = new BankAccount(10000); Measurable x = account; // OK Coin dime = new Coin(0.1, "dime"); x = dime; // OK x = new Rectangle(5, 10, 20, 30); //
444 DataSet coindata = new DataSet( ); coindata.add(new Coin(0.25, "quarter")); coindata.add(new Coin(0.1, "dime")); coindata.add(new Coin(0.05, "nickel")); Measurable max = coindata.getmaximum( ); String name = max.getname( ); // Coin maxcoin = (Coin)max; String name = maxcoin.getname( );
Chapter 9 445 max instnaceof Coin if (max instance of Coin) Coin maxcoin = (Coin)max; instanceof Chapter 09 if (x instanceof Coin) Coin c = (Coin)x; ] Advanced Topic 9.1
446 public interface SwingConstants int NORTH = 1; int NORTHEAST = 2; int EAST = 3; 9.3 Measurable x; x = new BankAccount(10000); // OK x = new Coin(0.1, "dime"); // OK x = new Measurable( ); //
Chapter 9 447 double m = x.getmeasure( ); Chapter 09 double m = x.getmeasure( ); account = new BankAccount( ); // BankAccount( ) account = new BankAccount(10000); // BankAccount(double)
448 9.4 public interface Measurer double measure(object anobject);
Chapter 9 449 public void add(object x) sum = sum + measurer.measure(x); if (count == 0 measurer.measure(maximum) < measurer.measure(x)) maximum = x; count++; Chapter 09 class RectangleMeasurer implements Measurer public double measure(object anobject) Rectangle arectangle = (Rectangle)anObject; double area = arectangle.getwidth( ) * arectangle.getheight( ); return area; Rectangle arectangle = (Rectangle)anObject; Measurer m = new RectangleMeasurer( ); DataSet data = new DataSet(m);
450 data.add(new Rectangle(5, 10, 20, 30)); data.add(new Rectangle(10, 20, 30, 40));
Chapter 9 451 public static void main(string[ ] args) class RectangleMeasurer implements Measurer Measurer m = new Rectanglemeasurer( ); DataSet data = new DataSet(m); Chapter 09 01 /** 02 03 */ 04 public class DataSet 05 06 /** 07 mesurer 08 @param ameasurer 09 */ 10 public DataSet(Measurer ameasurer) 11 12 sum = 0; 13 count = 0; 14 maximum = null; 15 measurer = ameasurer; 16
452 17 18 /** 19 20 @param x 21 */ 22 public void add(object x) 23 24 sum = sum + measurer.measure(x); 25 if (count == 0 26 measurer.measure(maximum)<measurer.measure(x)) 27 maximum = x; 28 count++; 29 30 31 /** 32 33 @return 0 34 */ 35 public double getaverage( ) 36 37 if (count ==0)return 0; 38 else return sum /count; 39 40 41 /** 42 33 @return 0 44 */ 45 public Object getmaximum( ) 46 47 return maximum; 48 49 50 private double sum; 51 private Object maximum; 52 private int count; 53 private Measurer measurer; 54 01 import java.awt.rectangle; 02
Chapter 9 453 03 /** 04 Measurer 05 */ 06 public class DataSetTest 07 08 public static void main(string [ ] args) 09 10 class RectangleMeasurer implements Measurer 11 12 public double measure(object anobject) 13 14 Rectangle arectangle = (Rectangle)anObject; 15 double area = arectangle.getwidth( ) 16 * arectangle.getheight( ); 17 return area; 18 19 20 21 Measurer m = new RectangleMeasurer( ); 22 23 DataSet data = new DataSet(m); 24 25 data.add(new Rectangle(5,10,20,30)); 26 data.add(new Rectangle(10,20,30,40)); 27 data.add(new Rectangle(20,30,5,10)); 28 29 System.out.println("Average area =" 30 + data.getaverage( )); 31 Rectangle max = (Rectangle)data.getMaximum( ); 32 System.out.println("Maximum area =" + max); 33 34 Chapter 09 01 /** 02 03 */ 04 public interface Measurer 05 06 /** 07 08 @param anobject
454 09 @return the measure 10 */ 11 double measure(object anobject); 12 class OuterClassName method signature class InnerClassName methods fields class OuterClassName methods fields accessspecifier class InnerClassName methods fields public class Test public static void main(string[ ] args)
Chapter 9 455 class RectangleMeasurer implements Measurer Advanced Topic 9.2 Chapter 09 Coin a Coin = new Coin(0.1, "dime"); data.add(acoin); data.add(new Coin(0.1, dime)); public static void main(string[ ] args) Measurer m = new Measurer( ) //
456 // public double measure(object anobject) Rectangle arectangle = (Rectangle)anObject; double area = arectangle.getwidth( ) * arectangle.getheight( ); return area; ; DataSet data = new DataSet(m); 9.5
Chapter 9 457 public interface ActionListener void actionperformed(actionevent event); class MyListener implements ActionListener public void actionperformed(actionevent event) // Chapter 09 MyListener listener = new MyListener( ); Timer t = new Timer(interval, listener); t.start( ); 10 9 2 1 0 Liftoff!
458 01 import java.awt.event.actionevent; 02 import java.awt.event.actionlistener; 03 import javax.swing.joptionpane; 04 import javax.swing.timer; 05 06 /** 07 08 */ 09 public class TimerTest 10 11 public static void main(string[ ] args) 12 13 class CountDown implements ActionListener 14 15 public CountDown(int initialcount) 16 17 count = initialcount; 18 19 20 public void actionperformed(actionevent event) 21 22 if (count >= 0) 23 System.out.println(count); 24 if (count == 0) 25 System.out.println("Liftoff!"); 26 count--; 27 28 29 private int count; 30 31 32 CountDown listener = new CountDown(10); 33 34 final int DELAY = 1000;// 1 1000 35 Timer t = new Timer(DELAY,listener);
Chapter 9 459 36 t.start( ); 37 38 JOptionPane.showMessageDialog(null,"Quit?"); 39 System.exit(0); 40 41 Chapter 09 Balance = 1050.0 Balance = 1102.5 Balance = 1157.625 Balance = 1215.50625 public class TimerTest public static void main(string[ ] args) final BankAccount account = new BankAccount(1000); private static final double RATE = 5; class InterestAdder implements ActionListener
460 public void actionperformed(actionevent event) double interest = account.getbalance( ) * RATE / 100; account.deposit(interest); System.out.println(Balance = + account.getbalance( )); public static void main(string[ ] args) final BankAccount account = new BankAccount(1000); class InterestAdder implements ActionListener public void actionperformed(actionevent event) double interest = account.getbalance( ) * RATE / 100; // account
Chapter 9 461 Chapter 09 01 import java.awt.event.actionevent; 02 import java.awt.event.actionlistener; 03 import javax.swing.joptionpane; 04 import javax.swing.timer; 05 06 /** 07 1 08 09 */ 10 public class TimerTest 11 12 public static void main(string [ ] args) 13 14 final BankAccount account = new BankAccount(1000); 15 16 class InterestAdder implements ActionListener 17 18 public void actionperformed(actionevent event) 19 20 double interest = account.getbalance( )
462 21 * RATE /100; 22 account.deposit(interest); 23 System.out.println("Balance = " 24 + account.getbalance( )); 25 26 27 28 InterestAdder listener = new InterestAdder( ); 29 30 final int DELAY =1000;// 1 1000 31 Timer t = new Timer(DELAY,listener); 32 t.start( ); 33 34 JOptionPane.showMessageDialog(null,"Quit?"); 35 System.exit(0); 36 37 38 private static final double RATE = 5; 39
Chapter 9 463 Common Error 9.1 class MyAction implements ActionListener public void actionperformed( ) // ActionEvent Chapter 09 public void actionperformed(actionevent event) public void actionperformed( ) Random Fact 9.1
464
Chapter 9 465 Chapter 09
466 java.awt.event.actionevent java.awt.event.actionlistener actionperformed javax.swing.timer start C c = ;
Chapter 9 467 I i = ; J j = ; c = i; // 1 j = c; // 2 i = j; // 3 C c = new C( ); I i = c; // 1 J j = (J)i; // 2 C d = (C)i; // 3 Chapter 09 Sandwich sub = new Sandwich( ); Edible e = sub; // 1 Rectangle cerealbox = new Rectangle(5, 10, 20, 30); Edible f = cerealbox; // 2 f = (Edible)cerealBox; // 3 sub = e; // 4 sub = (Sandwich)e; // 5 sub = (Sandwich)cerealBox; // 6
468 Rectangle getbounds( ) Shape s = ; Rectangle r = s.getbounds( );
Chapter 9 469 public class T public void m(final int x, int y) int a; final int b; class C implements I public void f( ) Chapter 09 final int c; private int t;
470 public interface Filter boolean accept(object x); public interface Drawable void draw(graphics2d g2);
Chapter 9 471 Date now = new Date( ); System.out.println(now); Chapter 09 public interface InputReader String readline(string prompt) throws IOException; public Coin readcoin(inputreader reader)
472