ch09

Similar documents
Microsoft Word - EEL2 Lab4.docx

PowerPoint Presentation

rmi_박준용_final.PDF

PowerPoint 프레젠테이션

PowerPoint 프레젠테이션

PowerPoint 프레젠테이션

05-class.key

자바 프로그래밍

5장.key

JMF3_심빈구.PDF

비긴쿡-자바 00앞부속

09-interface.key

Microsoft PowerPoint - 04-UDP Programming.ppt

12-file.key

fundamentalOfCommandPattern_calmglow_pattern_jstorm_1.0_f…

교육자료

Microsoft PowerPoint - Java7.pptx

02 C h a p t e r Java

10장.key

untitled

PowerPoint Presentation

07 자바의 다양한 클래스.key

* Factory class for query and DML clause creation * tiwe * */ public class JPAQueryFactory implements JPQLQueryFactory private f

JMF2_심빈구.PDF

제8장 자바 GUI 프로그래밍 II

Java Programing Environment

Microsoft PowerPoint - 14주차 강의자료

q 이장에서다룰내용 1 객체지향프로그래밍의이해 2 객체지향언어 : 자바 2

mytalk

01-OOPConcepts(2).PDF

신림프로그래머_클린코드.key

자바GUI실전프로그래밍2_장대원.PDF

Chap12

<4D F736F F F696E74202D20C1A63139C0E520B9E8C4A120B0FCB8AEC0DA28B0ADC0C729205BC8A3C8AF20B8F0B5E55D>

Design Issues

(8) getpi() 함수는정적함수이므로 main() 에서호출할수있다. (9) class Circle private double radius; static final double PI= ; // PI 이름으로 로초기화된정적상수 public

MasoJava4_Dongbin.PDF

Microsoft Word - EEL2 Lab5 예외처리와 스레드.docx

gnu-lee-oop-kor-lec10-1-chap10

Spring Data JPA Many To Many 양방향 관계 예제

Java

Java XPath API (한글)

Microsoft PowerPoint - lec08_1311.ppt [호환 모드]

Spring Boot/JDBC JdbcTemplate/CRUD 예제

JAVA PROGRAMMING 실습 08.다형성

Interstage5 SOAP서비스 설정 가이드

자바-11장N'1-502

1

PowerPoint Presentation

ilist.add(new Integer(1))과 같이 사용하지 않고 ilist.add(1)과 같이 사용한 것은 자바 5.0에 추가된 기본 자료형과 해당 객체 자료 형과의 오토박싱/언박싱 기능을 사용한 것으로 오토박싱이란 자바 컴파일러가 객체를 요구하는 곳에 기본 자료형

Java ...

교육2 ? 그림

쉽게 풀어쓴 C 프로그래밍

Cluster management software

@OneToOne(cascade = = "addr_id") private Addr addr; public Emp(String ename, Addr addr) { this.ename = ename; this.a

Microsoft PowerPoint - 06-Chapter09-Event.ppt

11장.key

어댑터뷰

Microsoft PowerPoint - Lect04.pptx

01....b

2007백서-001-특집

(291)본문7

¾Ë·¹¸£±âÁöħ¼�1-ÃÖÁ¾

00목차

PowerPoint 프레젠테이션

PowerPoint Presentation

슬라이드 1

Java ~ Java program: main() class class» public static void main(string args[])» First.java (main class ) /* The first simple program */ public class

예제 2) Test.java class A intvar= 10; void method() class B extends A intvar= 20; 1"); void method() 2"); void method1() public class Test 3"); args) A

4장.문장

목차 INDEX JSON? - JSON 개요 - JSONObject - JSONArray 서울시공공데이터 API 살펴보기 - 요청인자살펴보기 - Result Code - 출력값 HttpClient - HttpHelper 클래스작성 - JSONParser 클래스작성 공공

Connection 8 22 UniSQLConnection / / 9 3 UniSQL OID SET

Modern Javascript

C# Programming Guide - Types

鍮뚮┰硫붾돱??李⑤낯

PowerPoint Presentation

Microsoft PowerPoint - 2강

2힉년미술

Mobile Service > IAP > Android SDK [ ] IAP SDK TOAST SDK. IAP SDK. Android Studio IDE Android SDK Version (API Level 10). Name Reference V

Microsoft PowerPoint - ÀÚ¹Ù08Àå-1.ppt

PowerPoint Presentation

Microsoft PowerPoint - java1-lecture6.ppt [호환 모드]

PowerPoint Presentation

<4D F736F F F696E74202D20C1A63138C0E520C0CCBAA5C6AE20C3B3B8AE28B0ADC0C729205BC8A3C8AF20B8F0B5E55D>

<4D F736F F F696E74202D20C1A63230C0E520BDBAC0AE20C4C4C6F7B3CDC6AE203128B0ADC0C729205BC8A3C8AF20B8F0B5E55D>

<4D F736F F F696E74202D20C1A63038C0E520C5ACB7A1BDBABFCD20B0B4C3BC4928B0ADC0C729205BC8A3C8AF20B8F0B5E55D>

03-JAVA Syntax(2).PDF

03장

gnu-lee-oop-kor-lec06-3-chap7

쉽게 풀어쓴 C 프로그래밍

[ 정보 ] 과학고 R&E 결과보고서 Monte Carlo Method 를이용한 고교배정시뮬레이션 연구기간 : ~ 연구책임자 : 강대욱 ( 전남대전자컴퓨터공학부 ) 지도교사 : 최미경 ( 전남과학고정보 컴퓨터과 ) 참여학생 : 박진명 ( 전

11 템플릿적용 - Java Program Performance Tuning (김명호기술이사)

OOP 소개

API STORE 키발급및 API 사용가이드 Document Information 문서명 : API STORE 언어별 Client 사용가이드작성자 : 작성일 : 업무영역 : 버전 : 1 st Draft. 서브시스템 : 문서번호 : 단계 : Docum

오버라이딩 (Overriding)

class Sale void makelineitem(productspecification* spec, int qty) SalesLineItem* sl = new SalesLineItem(spec, qty); ; 2. 아래의액티비티다이어그램을보고 Java 또는 C ++,

3ÆÄÆ®-14

10-Java Applet

FileMaker ODBC and JDBC Guide

스레드의우선순위 우선순위설정메소드 : void setpriority(int newpriority) newpriority 에설정할수있는등급 : 1( 가장낮은우선순위 ) 부터 10( 가장높은우선순위 ) 가장높은우선순위 : MAX_PRIORITY, 보통우선순위 : NORM_

Transcription:

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