Java 3: Object-oriented programming

5 downloads 289 Views 5MB Size Report
This book is the third in a series of books on software development. The programming language is Java, and the language


POUL KLAUSEN

JAVA 3: OBJECTORIENTED PROGRAMMING SOFTWARE DEVELOPMENT

Download free eBooks at bookboon.com 2

Java 3: Object-oriented programming: Software Development 1st edition © 2017 Poul Klausen & bookboon.com ISBN 978-87-403-1691-9 Peer review by Ove Thomsen, EA Dania

Download free eBooks at bookboon.com 3

Deloitte & Touche LLP and affiliated entities.

JAVA 3: OBJECT-ORIENTED PROGRAMMING

Contents

CONTENTS Foreword

6

1 Introduction

8

2 Classes

15



Exercise 1

20

2.1

More classes

22



Exercise 2

34



Exercise 3

35



Problem 1

38

2.2 Methods

41

2.3 Objects

46

2.4 Visibility 2.5

Statical members

2.6

The CurrencyProgram



Problem 2

360° thinking

.

360° thinking

48

.

49 53 66

360° thinking

.

Discover the truth at www.deloitte.ca/careers

© Deloitte & Touche LLP and affiliated entities.

Discover the truth at www.deloitte.ca/careers

© Deloitte & Touche LLP and affiliated entities.

Discoverfree theeBooks truth atatbookboon.com www.deloitte.ca/careers Download

Click on the ad to read more

4

Dis

JAVA 3: OBJECT-ORIENTED PROGRAMMING

Contents

3 Interfaces

69

3.1 Interfaces

70



Exercise 4

82

3.2

More students

83



Exercise 5

91

3.3 Factories

94



Exercise 6

97

4 Inheritance

98



Exercise 7

111



Problem 2

112

4.1

More about inheritance

118

5

The class Object

119

6

Typecast of objects

128

7

A last note about classes

130

7.1

Considerations about inheritance

130



Problem 3

134

7.2

The composite pattern

142

8

Final example

143

8.1 Analyse

144

8.2 Design

148

8.3 Programming

155



Appendix A

159



Comment the code

159



Debug the code

162



Unit test

164

Download free eBooks at bookboon.com 5

JAVA 3: OBJECT-ORIENTED PROGRAMMING

Foreword

FOREWORD This book is the third in a series of books on software development. The programming language is Java, and the language and its syntax and semantics fills obviously much, but the books have also largely focus on the process and how to develop good and robust applications. In the first book I have generally mentioned classes and interfaces, and although the book Java 2 also intesiv used classes and interfaces I have deferred the details to this book and also the next, that are dealing with object-oriented programming. It deals with how a running program consists of cooperating objects and how these objects are defined and created on the basis of the program’s classes. Object-oriented programming is the knowledge of how to find and write good classes to a program, classes which helps to ensure that the result is a robust program that is easy to maintain. The subject of this book is object-oriented programming and here primarily about classes and how classes are used as the basic building blocks for developing a program. The book assumes a basic knowledge of Java corresponding to the book Java 1 of this series, but since some of the examples and exercises are relating to programs with a graphical user interface it is also assumed knowledge of the book Java 2 and how to write less GUI programs. As the title says this series of books deals with software development, and the goal is to teach the reader how to develop applications in Java. It can be learned by reading about the subject and by studying complete sample programs, but most importantly by yourself to do it and write your own programs from scratch. Therefore, an important part of the books is exercises and problems, where the reader has to write programs that correspond to the substance being treated in the books. All books in the series is built around the same skeleton and will consist of text and examples and exercises and problems that are placed in the text where they naturally belongs. The difference between exercises and problems is that the exercises largely deals with repetitions of the substance that is presented in the text, and furthermore it is relatively accurately described what to do. Problems are in turn more loosely described, and are typically a little bigger and there is rarely any clear best solution. These are books to be read from start to finish, but the many code examples, including exercises and problems plays a central role, and it is important that the reader predict in detail studying the code to the many examples and also solves the exercises and problems or possibly just studying the recommended solutions. All books ends with one or two larger sample programs, which focus primarily is on process and an explanation of how the program is written. On the other hand appears the code only to a limited extent – if at all – and the reader should instead study the finished program code perhaps while testing the program. In addition to show the development of programs that are larger than the examples, which otherwise is presented, the aim of the concluding examples also is to show program examples from varying fields of application.

Download free eBooks at bookboon.com 6

JAVA 3: OBJECT-ORIENTED PROGRAMMING

Foreword

Most books also ends with an appendix dealing with a subject that would not be treated in the books. It may be issues on the installation of software or other topics in computer technology, which are not about software development, but where it is necessary to have an introductory knowledge. If the reader already is familiar with the subject, the current appendix can be skipped. The programming language is, as mentioned Java, and besides the books use the following products: -- NetBeans as IDE for application development -- MySQL to the extent there is a need for a Principal: %12.2f\n", loan.getPrincipal()); System.out.printf("Interest rate: %12.2f %%\n", loan.getInterestRate() * 100); System.out.printf("Number of periods: %12d\n\n", loan.getPeriods()); System.out.println( "Periods Payment Repayment Interest Outstanding"); System.out.println( "----------------------------------------------------------------------"); for (int n = 1; n