Engineering Programming using Java - vetres

4 downloads 165 Views 916KB Size Report
Learner Workbook. Version 1. Training and Education Support. Industry Skills Unit. Meadowbank. Product Code: 5486. Engineering Programming using Java.
UEE07 Electrotechnology Training Package

M PL

E

Engineering Programming using Java Incorporates:

UEENEED027B Develop structured programs to control external devices

SA

UEENEED003B Evaluate and modify programs written in object oriented code

Learner Workbook Version 1

Training and Education Support Industry Skills Unit Meadowbank

Product Code: 5486

Engineering Programming using Java

Acknowledgments The TAFE NSW Training and Education Support Industry Skills Unit, Meadowbank would like to acknowledge the support and assistance of the following people in the production of this learner workbook:

Writers: John Zervos TAFE NSW

Reviewers: ISU Meadowbank TAFE NSW

Project Manager:

E

Steve Parkinson TAFE NSW

M PL

Enquiries

Enquiries about this and other publications can be made to: Training and Education Support Industry Skills Unit, Meadowbank Meadowbank TAFE Level 3, Building J, See Street, MEADOWBANK NSW 2114 Fax: 02-9942 3257

SA

Tel: 02-9942 3200

© The State of New South Wales, Department of Education and Training, TAFE NSW, Training and Education Support Industry Skills Unit, Meadowbank, 20011. Copyright of this material is reserved to TAFE NSW Training and Education Support Industry Skills Unit, Meadowbank. Reproduction or transmittal in whole or in part, other than for the purposes of private study or research, and subject to the provisions of the Copyright Act, is prohibited without the written authority of, TAFE NSW. Training and Education Support Industry Skills Unit, Meadowbank.

ISBN 978-1-74236-250-2

Developed by Training & Education Support Industry Skills Unit, Meadowbank @ TAFE NSW 2011

Engineering Programming using Java

Table of contents Introduction .................................................................................. 9 1.

General introduction ........................................................................... 9

2.

Using this learner workbook ................................................................ 9

3.

Prior knowledge and experience ......................................................... 11

4.

Units of competency overview ........................................................... 12

5.

Assessment ..................................................................................... 19

Section 1 ..................................................................................... 21

E

Introduction to java programming and data types ...................................... 21 Summary of technical information ............................................................ 22

M PL

Review questions.................................................................................... 36

Section 2 ..................................................................................... 39 Console input and “number crunching” ...................................................... 39 Summary of technical information ............................................................ 40 Review questions.................................................................................... 53

SA

Section 3 ..................................................................................... 55 Selection (decision-making) ..................................................................... 55 Summary of technical information ............................................................ 56 Review questions.................................................................................... 70

Section 4 ..................................................................................... 73 Iteration (looping) .................................................................................. 73 Summary of technical information ............................................................ 74 Review questions.................................................................................... 86

Section 5 ..................................................................................... 89 Arrays ................................................................................................... 89 Summary of technical information ............................................................ 90

Developed by Training & Education Support Industry Skills Unit, Meadowbank @ TAFE NSW 2011

Engineering Programming using Java

Review questions.................................................................................... 99

Section 6 ................................................................................... 101 Object oriented design and parameter passing ......................................... 101 Summary of technical information .......................................................... 102 Review questions.................................................................................. 116

Section 7 ................................................................................... 119 Inheritance .......................................................................................... 119 Summary of technical information .......................................................... 120 Review questions.................................................................................. 126

E

Section 8 ................................................................................... 127

M PL

Introduction to GUI programming ........................................................... 127 Summary of technical information .......................................................... 128 Review questions.................................................................................. 138

Section 9 ................................................................................... 139 Layout managers and dialog boxes ......................................................... 139

SA

Summary of technical information .......................................................... 140 Review questions.................................................................................. 148

Section 10 ................................................................................. 149 Advanced GUI components and menus.................................................... 149 Summary of technical information .......................................................... 150 Review questions.................................................................................. 159

Section 11 ................................................................................. 161 Applets ............................................................................................... 161 Summary of technical information .......................................................... 162 Review questions.................................................................................. 171

Section 12 ................................................................................. 173

Developed by Training & Education Support Industry Skills Unit, Meadowbank @ TAFE NSW 2011

Engineering Programming using Java

Graphics .............................................................................................. 173 Summary of technical information .......................................................... 174 Review questions.................................................................................. 184

Section 13 ................................................................................. 185 File I/O ............................................................................................... 185 Summary of technical information .......................................................... 186 Review questions.................................................................................. 195

Section 15 ................................................................................. 197 I/O and exception handling .................................................................... 197

E

Summary of technical information .......................................................... 198

M PL

Review questions.................................................................................. 211

Sample skills assessment project 1 ........................................... 213 A GUI stopwatch application .................................................................. 213

Sample skills assessment project 2 ........................................... 219 A data file plotter ................................................................................. 219

SA

Sample knowledge assessment ................................................. 225 Answers to review questions ..................................................... 239 Answers to sample knowledge assessment ............................... 255 Reference list ............................................................................ 259 Resource Evaluation Form ......................................................... 261

Developed by Training & Education Support Industry Skills Unit, Meadowbank @ TAFE NSW 2011

Engineering Programming using Java

Introduction 1.

General introduction

Welcome to Engineering Programming using Java. This workbook fulfils the requirements of the national units of competence UEENEED027B Develop structured programs to control external devices and UEENEED003B Evaluate and modify programs written in object oriented code, which are part of the UEE07 Electrotechnology Industry Training Package. If you have previously achieved competency in unit UEENEED027B Develop structured programs to control external devices using an alternate programming language such as C or C++, it may be possible to work through the earlier sections of this workbook at an accelerated rate; concentrating mainly on any differences specific to the Java language.

Using this learner workbook

E

2.

Units and sections

M PL

This learner workbook may be used on its own, or it may be used as additional material to support the development of knowledge and skills required to achieve this unit of competence.

The following table shows the sections in this workbook and how they relate to the units of competency. UEENEED027B

UEENEED003B

1. Introduction to Java programming and data types





2. Console input and “number crunching”





3. Selection (decision-making)





4. Iteration (looping)





5. Arrays





6. Object oriented design and parameter passing





SA

Topic and Title

7. Inheritance



8. Introduction to GUI programming



9. Layout managers and dialog boxes



10. Advanced GUI components and menus



11. Applets



12. Graphics



13. File I/O





14. I/O and Exception handling





Page 9 of 264

Engineering Programming using Java

Developing Java programs 

There are many different systems available for developing Java programs, and each delivery point is likely to use a different system. This is perfectly valid, and so no specific system will be assumed in this workbook.



However, one of the simplest methods for creating and running Java programs is to first download and install the Java Development Kit (JDK) from the following address: http://www.oracle.com/technetwork/java/javase/downloads/jdk6-jsp-136632.html



Then, download and install a programmer-friendly text editor like TextPad (Windows) from the following address: http://www.textpad.com/ TextPad will pick up the fact that the JDK has been installed and add menu items to compile and run your programs. It‟s that easy!



Both tools are free and can easily be installed at home.

E



M PL

Activity 1 – Java development software

SA

Complete the following internet and computer activities by using the above URLs

1. If you have access to a computer with Java development software installed at college, work, or home, record the details of this software. 2. If you plan to install the JDK and TextPad as described above, do so using the URLs provided and comment on its success. 3.

If you plan to install any other system, record the details here.

Page 23 of 264

Engineering Programming using Java

Simple console output (printing to the screen) 

The System.out.println() method (part of a supplied library package) is used to print to the console (screen).



Here‟s a simple program that displays the greeting “Hello World!” to the screen.

public class Greeting { public static void main(String[] args) { System.out.println("Hello World"); } } The code for our program, the call to the System.out.println() method goes inside the main method.



The string (text) to be displayed is placed between double quotation marks.

E



M PL

Note: Word processors tend to use special open and closed quotation marks like “ and ”. The Java compile does not recognise these and will give an error! The entire statement is terminated with a semicolon (;).



This program will need to be saved as Greeting.java to match the class name.



Figure 1 shows how this program looks when entered and saved in TextPad.

SA



Figure 1: The Greeting.java program in TextPad.

Page 26 of 264

Engineering Programming using Java

Programming Task 3 – Maths and formatting

Complete the following programming tasks.

E

1. Enter, compile and run the SineWave.Java program given above. If there are any errors, correct them and recompile until there are no more errors. Run the program and verify its correct operation.

M PL

2. Modify the SineWave.Java program so that it uses the DecimalFormat class to display the result with an accuracy of three decimal places.

3. Write a program that prompts the user for an amplifier‟s input voltage and output voltage and displays the gain as a ratio (to 2 decimal places) as well as in dB (to 1 decimal place). 4. Write a program that prompts the user for the parameters in an RC charge circuit, and displays the instantaneous capacitor voltage using:

   

SA

t   RC   1  e 

V  VMAX

5. Write a program that determines the number of bits needed to represent an integer number entered by the user based on the following relationship (where n = no. of bits, and number = the integer to be represented):

n

log( number ) log( 2)

Page 49 of 264