Java_how_to_program
Java_how_to_program
ITRW 124 E
*ITRW124E*
FACULTY OF NATURAL SCIENCES
Study guide compiled by:
MM van der Westhuizen
GR Drevin
Edited nn.
Page layout by Susan van Biljon, graphikos.
Printing arrangements and distribution by Department Logistics (Distribution Centre).
Printed by The Platinum Press (018) 299 4226.
ii
MODULE CONTENTS
Introduction...............................................................................................................................iv
Allocation of time......................................................................................................................iv
Study material..........................................................................................................................iv
In this study guide.....................................................................................................................v
Laboratory assistants and study facilitator................................................................................v
Class attendance......................................................................................................................v
Work method............................................................................................................................vi
Practical work...........................................................................................................................vi
Evaluation................................................................................................................................vii
Participation mark....................................................................................................................vii
Prerequisites for exam entrance..............................................................................................vii
Module outcomes....................................................................................................................vii
Warning against plagiarism....................................................................................................viii
iii
PU-level 1
National level 5(a)
Credits 12
Contact sessions 3 theory + 3 practical a week
Prerequisites ITRW112
INTRODUCTION
Welcome back after the holiday. This module follows directly on ITRW 111 that you did the
previous semester. The purpose of this module is to offer you the opportunity to learn how to
programme in the programming language C++. I hope that you will find this module
interesting and that you will be able to use the skills that you develop here not only during the
rest of your academic career, but also thereafter.
ALLOCATION OF TIME
Activity Week Hours/Week Hours/Module
Class (contact time –> Lectures + Class tests) 12 3 36
Practical work in Laboratory 9 3 27
Practical assignments 10 2 20
Independent study 11 2 22
Semester test 1 4 4
Exams 1 11 11
Total 120
STUDY MATERIAL
PRESCRIBED TEXTBOOK
Authors: H.M. Deitel & P.J. Deitel
Title: C++ How to Program 8th Edition (International Edition)
ISBN: 0-13-136483-9
Publisher: Prentice Hall
This textbook is the only study material prescribed for this module. Throughout the study
guide the abbreviation D&D will be used in reference to the textbook. All exercises in this
study guide are from D&D.
iv
IN THIS STUDY GUIDE
The structure of a typical study unit is as follows:
Reference to the
section(s) in the
Answers to self-
textbook on which
review exercises.
this study unit is
based.
Additional practical
The outcomes for the assignment(s) that
study unit. you must do on your
own.
This work will be
done during the
contact sessions.
You must prepare
this work to be able
to participate during
the contact sessions.
The practical
assignment for the
study unit – during Revision – Make
the practical session sure that you have
help will be available mastered the study
(from laboratory unit!
assistants) in the
computer rooms.
CLASS ATTENDANCE
Class attendance will be according to schedule. Attendance of all classes (except when other
arrangements have been made) is of absolute importance, since tuition tests can be given
without any prior notice.
v
WORK METHOD
The purpose of this module is to make you familiar with the computer and therefore it is
important that you do plenty of exercises on the computer.
The practical assignments will be given weekly and is the core of this module.
The practical assignment for each week is a summary of some of the techniques you
learned in class and must be practiced.
It is not possible to give a practical assignment that covers all the work done in a
particular week. Therefore it is important that you practice the examples and work done
in class on your computer. The practical assignments only cover a small section of the
work being done.
During each practical session an assignment is given that must be completed and be
evaluated by one of the laboratory assistants before the end of the practical session.
Use a flash disk to keep back up of all your programs and bring it to the practical classes.
At the beginning of each practical session you will immediately start with a new
assignment.
Copying the work of others is considered a serious offence. Persons guilty of this, as well
as the person making his/her work available, will forfeit marks and run the risk of being
denied entrance to the exams.
The study units are compiled in such a way that most units will need two weeks, therefore
approximately 20 hours, to be completed. Each study unit will start with theoretical
knowledge and end with a practical session and (probably) a tuition test on the
practical. Every study unit has additional exercises that you must do on your own. You
are welcome to stay in the laboratory and work on the additional exercises, should you
finish your practical assignment before the end of the practical session.
PRACTICAL WORK
HELP DURING THE PRACTICAL SESSIONS
Al the practical sessions will take place in the computer laboratories. One or more assistants
(senior student) will be appointed for each laboratory to assist you with your practical
assignments.
The assistants are responsible for the evaluation of your assignments. Once you have
completed a practical assignment an assistant will verify that your program works correctly.
BACK UP
The practical work you do is saved on the P-drive of the network. You must buy a flash disk
to save your assignments on. This is to ensure you have your work in two places for
safekeeping (back-up).
vi
EVALUATION
CLASS AND TUITION TESTS
Class tests on the work of the previous week can be given with prior notice.
Regular tuition tests that contribute towards your participation will be written.
Tuition tests will normally be on the previous practical, although work done previously will
also be important.
Tuition tests on the work of the previous period can be given without prior warning.
Work marked with the icon: in the study guide must be prepared in such a way
that you’ll be able to write a tuition test on it at any time.
Learning is a cumulative process and new knowledge builds on previous knowledge. It is
very important to check whether you have reached the outcomes of each section and
that you have mastered the work.
In addition to the tuition tests, completion of the practical assignments is very important.
PARTICIPATION MARK
The participation mark is the average of the best three of the following:
Class and tuition test (5 best results);
One semester test.
MODULE OUTCOMES
Knowledge
At the end of this module the learner will have basic knowledge and insight on:
The basic structures, data types, methods, classes and objects of an object directed
programming language;
Problem solving, including: debugging, testing and executing of applications.
Skills
After completion of this module the learner will be able to show that he / she can apply the
knowledge and insight that have been obtained in problem solving by means of a computer.
Specifically he / she will:
vii
Develop a solution plan (algorithm) to solve a problem that has been defined, "translate"
(encode) the algorithm in Java, debug it, test it and execute it by means of the
computer;
Apply certain steps of problem solving on defined problems;
Use general properties of the programming language Java to develop applications;
Create and use classes in problem solving;
Handle different data types in Java;
Do arithmetic calculations;
Use classes and methods that already have been defined in Java;
Use Java's decision making structures (choice), namely if and switch, in problem solving;
Use methods for modular programming;
Use the repeating structures of Java (loops), namely while, do..while and for in problem
solving;
Use one- and two-dimensional vectors (arrays) as internal storage structures;
Use a good programming style (program readability);
Write structured classes and programs that gives neat output;
Write programs that are user friendly.
viii
Study unit 1
1 INTRODUCTION TO COMPUTERS,
THE INTERNET AND JAVA
You will need approximately 10 hours to successfully work through this study unit.
This study unit is based on chapters 1 and 2 (pages 1 to 69) as well as paragraphs 12.1 and
12.2 (pages 477 to 485) of D&D.
Theory:
1
Study unit 1
Practical:
By the end of this study unit you should be able to write programs that:
· Make use of input and output statements;
· Use Java’s primitive types;
· Use arithmetic operators;
· Use decision-making statements;
· Comply with all the Good Programming Practices given in chapters 1 and 2.
This work will be done during the contact sessions. Get an overview of the work by reading
the indicated paragraphs before the contact sessions.
Introduction to computer technology: Paragraphs 1.1 to 1.12 of D&D.
A typical Java development environment: Paragraph 1.13 of D&D.
A typical environment is given in figure 1.1.
Introduction to OO: Paragraph 1.16 of D&D.
First Java programs: Paragraphs 2.2 to 2.4 of D&D.
Arithmetic: Paragraphs 2.5 and 2.7 of D&D.
The arithmetic operators are summarised in figure 2.11 and their precedence is given
in figure 2.12.
Memory concepts: Paragraph 2.6 of D&D.
Decision making: Paragraph 2.8 of D&D.
The equality and relational operators are summarised in figure 2.14 and the
precedence of these operators together with the arithmetic operators is given in figure
2.16.
Case study: Paragraph 12.1 and 12.2 of D&D.
The UML use case diagram is discussed in this case study. A summary of all the UML
diagrams is given op pages 483 and 484 of D&D.
The practical assignment will be given and discussed before the practical session. Your
programs must comply with all the Good Programming Practices that are given in these
chapters. Deliberately make the Common Programming Errors, given in these chapters, in
a program and see what happens when you compile the program and, if it compiles
successfully, what happens when you execute the program.
2
Study unit 1
Do self-review exercises 12.1 to 12.3 of the case study (D&D pages 484 and 485).
Make sure that you can answer exercises 1.4 to 1.8 (D&D pages 34 and 35) and then do
exercises 2.8, 2.9, 2.10, 2.11, 2.12, 2.13, 2.19, 2.20, 2.21, 2.23, 2.24 and 2.25 (pages 67 to
69) in D&D.
Go back to the outcomes and check whether you have reached the outcomes set for this
study unit. Indicate what your estimated level of mastery (expressed in percentage) is.
3
Study unit 1
4
Study unit 2
You will need approximately 20 hours to successfully work through this study unit.
This study unit is based on chapter 3 (pages 72 to 105) as well as paragraph 12.3 (pages
485 to 491) of D&D.
Theory:
5
Study unit 2
Practical:
By the end of this study unit you should be able to write programs that, in
addition to the requirements set in the previous study unit, will also:
· Declare classes with methods, instance variables and constructors;
· Create objects of those classes;
· Call the methods of those objects;
· Make use of the dialog boxes of JOptionPane.
This work will be done during the contact sessions. Get an overview of the work by reading
the indicated paragraphs before the contact sessions.
Introduction: Paragraphs 3.1 and 3.2 of D&D.
Declaration of a class and creation of an object: Paragraphs 3.3, 3.4, 3.5 and 3.7 of D&D.
A basic grade book class is created in paragraph 3.3 and is then expanded in the rest of
the paragraphs. The UML class diagram is given for each version of the grade book
class.
Primitive and reference types: Paragraph 3.6 of D&D.
Type double: Paragraph 3.8 of D&D.
Graphical user interfaces (GUIs): Paragraph 3.9 of D&D.
The use of the input and message dialog boxes of JOptionPane for input and output is
illustrated in this paragraph.
Identification of classes in a problem: Paragraph 12.3 of D&D.
An example is used to describe the UML class diagram as well as the relationships
between classes.
The practical assignment will be given and discussed before the practical session. In addition
to the Good Programming Practices that your programs had to comply with in the previous
study unit, your programs must now also comply with all the Good Programming Practice
given in chapter 3. Make the General Programming Errors, given in chapter 3, in a
program see what happens when you compile it. If it compiles successfully, see what
happens when you execute the program.
Do self-review exercises 12.4 to 12.7 of the case study (D&D page 491).
6
Study unit 2
Make sure that you can answer exercises 3.5 to 3.7 and then do exercises 3.8 to 3.11
(pages 103 and 104) in D&D.
Go back to the outcomes and check whether you have reached the outcomes set for this
study unit. Indicate what your estimated level of mastery (expressed in percentage) is.
7
Study unit 2
8
Study unit 3
3 CONTROL STATEMENTS I
You will need approximately 20 hours to successfully work through this study unit.
This study unit is based on chapter 4 (pages 106 to 154) as well as paragraph 12.4 (pages
491 to 494) of D&D.
Theory:
9
Study unit 3
Practical:
By the end of this study unit you should be able to write programs that, in
addition to the requirements set in the previous study units, will also:
· Make use of both forms of the if statement;
· Make use of both counter controlled as well as sentinel controlled repetition using the
while statement;
· Make use of the conditional operator (?:), the compound assignment operators and the
increment and decrement operators;
· Contain nested control structures.
This work will be done during the contact sessions. Get an overview of the work by reading
the indicated paragraphs before the contact sessions.
Problem solving and control structures: Paragraphs 4.1 to 4.4 of D&D.
The if and if…else statements and selection control structures: Paragraphs 4.5 and 4.6
of D&D.
The selection control structures are described. The UML activity diagrams for the two
forms of the if statement are given. The conditional operator is also described. Nested
if statements as well as the problems that can be experienced are discussed.
The while statement and repletion control structures: Paragraphs 4.7 to 4.9 of D&D.
The basic use of the while statement is discussed as well as the use of the while in
counter controlled and sentinel controlled repetition structures.
Nested control structures: Paragraph 4.10 of D&D.
An example is used to illustrate the solving of a problem and the use of nested control
structures.
Compound assignment operators and the increment and decrement operators:
Paragraphs 4.11 and 4.12 of D&D.
The working of the compound assignment, increment and decrement operators as well
as their precedence is discussed. Take note of the difference in effect of the prefix and
postfix increment operators.
Identification of class attributes: Paragraph 12.4 of D&D.
An example is used to illustrate the process of identifying the attributes of classes given a
problem statement. UML class diagrams with attributes are given once more.
10
Study unit 3
The practical assignment will be given and discussed before the practical session. In addition
to the Good Programming Practices that your programs had to comply with in the previous
study unit, your programs must now also comply with all the Good Programming Practice
given in chapter 4. Make the General Programming Errors, given in chapter 4, in a
program see what happens when you compile it. If it compiles successfully, see what
happens when you execute the program.
Do self-review exercises 12.8 to 12.10 of the case study (D&D pages 495 and 496).
Use the answers on pages 148 and 149 of D&D to check your answers.
Make sure that you can answer exercises 4.10 to 4.16 and then do exercises 4.16, 4.17,
4.18, 4.19, 4.20, 4.24, 4.25, 4.31, 4.34 and 4.38 (pages 149 to 154) in D&D.
Go back to the outcomes and check whether you have reached the outcomes set for this
study unit. Indicate what your estimated level of mastery (expressed in percentage) is.
11
Study unit 3
12
Study unit 4
4 CONTROL STATEMENTS II
You will need approximately 20 hours to successfully work through this study unit.
This study unit is based on chapter 5 (pages 156 to 200) as well as paragraph 12.5 (pages
496 to 499) of D&D.
Theory:
13
Study unit 4
Practical:
By the end of this study unit you should be able to write programs that, in
addition to the requirements set in the previous study units, will also:
· Make use of the for, do...while and switch statements;
· Make use of the break and continue statements;
· Have complex logical expressions that are constructed with the and, or, not and
exclusive or logical operators.
This work will be done during the contact sessions. Get an overview of the work by reading
the indicated paragraphs before the contact sessions.
Counter controlled structures and the for statement: Paragraphs 5.1 to 5.4 of D&D.
The four essential elements of counter controlled repetition are described after which it is
shown how these elements are present in the for statement. The working of the for
statement is then described, both with examples as well as with a UML activity
diagram.
The do...while statement: Paragraph 5.5 of D&D.
The switch statement: Paragraph 5.6 of D&D.
The break and continue statement: Paragraph 5.7 of D&D
Logical operators: Paragraph 5.8 of D&D.
The and, or, not and exclusive or logical operators are used to construct complex
logical expressions. Examples are given of the use of these operators.
Structured programming: Paragraph 5.9 of D&D.
The rules for the formation of structured programs are given and illustrated with UML
activity diagrams.
Identification of the states and activities of objects: Paragraphs 12.5 of D&D.
The UML state and activity diagrams are discussed.
The practical assignment will be given and discussed before the practical session. In addition
to the Good Programming Practices that your programs had to comply with in the previous
study unit, your programs must now also comply with all the Good Programming Practice
given in chapter 5. Make the General Programming Errors, given in chapter 5, in a
program see what happens when you compile it. If it compiles successfully, see what
happens when you execute the program.
14
Study unit 4
Do self-review exercises 12.11 to 12.13 of the case study (D&D page 500).
Use the answers on pages 197 and 198 of D&D to check your answers.
Make sure that you can answer exercises 5.5 to 5.9 and then do exercises 5.11, 5.12, 5.13,
5.16 en 5.19 (pages 198 and 199) in D&D.
Go back to the outcomes and check whether you have reached the outcomes set for this
study unit. Indicate what your estimated level of mastery (expressed in percentage) is.
15
Study unit 4
16
Study unit 5
5 METHODS
You will need approximately 20 hours to successfully work through this study unit.
This study unit is based on chapter 6 (pages 202 to 246) as well as paragraph 12.6 (pages
500 to 506) of D&D.
Theory:
17
Study unit 5
Practical:
By the end of this study unit you should be able to write programs that, in
addition to the requirements set in the previous study units, will also:
· Make use of methods of the Math and Random classes;
· Have methods with multiple parameters;
· Generate random numbers in any given interval;
· Have overloaded methods.
This work will be done during the contact sessions. Get an overview of the work by reading
the indicated paragraphs before the contact sessions.
Introduction and modules in Java: Paragraphs 6.1, 6.2 and 6.8 of D&D.
Read these paragraphs for future reference.
static methods and variables and the Math class: Paragraph 6.3 of D&D.
A short introduction to static methods and variables is given after which the Math class is
discussed as an example.
Declaration of methods and parameters: Paragraphs 6.4 to 6.7 of D&D.
The declaration of methods with multiple parameters is discussed in paragraph 6.4. This
is followed by a discussion of the ways in which methods can be called as well as of
the ways in which methods can pass control back to the point from which they were
called (6.5). The use of the stack when methods are called is described in paragraph
6.6. Finally, the promotion of primitive types is discussed in paragraph 6.7.
Generating random numbers: Paragraph 6.9 of D&D.
The use of the Math class to generate random numbers is described.
Scope of declarations: Paragraph 6.11 of D&D.
All entities declared in Java have a limited existence. Java’s scope rules are discussed
in this paragraph.
Method overloading: Paragraph 6.12 of D&D.
More than one method with the same name but with different number and types of
parameters can be declared in a class. Such methods are overloaded.
Identification of class operations: Paragraph 12.6 of D&D.
An example is used to illustrate the identification of behaviours or operations of classes.
These operations are then incorporated in the class diagrams.
The practical assignment will be given and discussed before the practical session. In addition
to the Good Programming Practices that your programs had to comply with in the previous
18
Study unit 5
study unit, your programs must now also comply with all the Good Programming Practice
given in chapter 6. Make the General Programming Errors, given in chapter 6, in a
program see what happens when you compile it. If it compiles successfully, see what
happens when you execute the program.
Do self-review exercises 12.14 to 12.16 of the case study (D&D page 506).
Use the answers on pages 240 to 242 of D&D to check your answers.
Make sure that you can answer exercises 6.7, 6.10, 6.11 and 6.12 and then do exercises
6.8, 6.14, 6.19, 6.22 and 6.24 (pages 242 to 244) in D&D.
Go back to the outcomes and check whether you have reached the outcomes set for this
study unit. Indicate what your estimated level of mastery (expressed in percentage) is.
19
Study unit 6
6 ARRAYS
You will need approximately 20 hours to successfully work through this study unit.
This study unit is based on chapter 7 (pages 248 to 314) as well as paragraph 12.7 (pages
506 to 513) of D&D.
Theory:
20
Study unit 6
Practical:
By the end of this study unit you should be able to write programs that, in
addition to the requirements set in the previous study units, will also:
· Make use of one and two dimensional arrays;
· Use arrays as parameters;
· Use arrays of objects.
This work will be done during the contact sessions. Get an overview of the work by reading
the indicated paragraphs before the contact sessions.
Arrays: Paragraph 7.2 of D&D.
The array concept as well as the way in which arrays are stored in memory is discussed.
Declaration and creation of arrays: Paragraph 7.3 of D&D.
The different ways in which arrays can be declared and created is described. It is
important to understand the difference between these two actions! Note the use of
new in the creation of an array.
Use of arrays: Paragraph 7.4 of D&D.
A number of examples of the use of arrays is given and discussed.
Arrays of objects: Paragraph 7.5 of D&D.
An example is used to illustrate the declaration, creation and use of an array of objects.
Note that since the elements of the array are objects, each element (object) needs to
be created as well (with new). Take the trouble to scrutinise this example in detail!
Arrays as parameters: Paragraph 7.7 of D&D.
An example is used to illustrate the use of arrays as parameters of methods.
Case study: Paragraph 7.8 of D&D.
A complete example of the use of arrays is given and described.
Multidimensional arrays: Paragraphs 7.9 and 7.10 of D&D.
A number of examples are used to illustrate the declaration, creation and use of
multidimensional arrays.
Collaboration among objects: Paragraph 12.7 of D&D.
An example is used to illustrate the identification and modelling of collaboration among
objects in a given problem. The UML communication and sequence diagrams are used
for the modelling.
21
Study unit 6
The practical assignment will be given and discussed before the practical session. In addition
to the Good Programming Practices that your programs had to comply with in the previous
study unit, your programs must now also comply with all the Good Programming Practice
given in chapter 7. Make the General Programming Errors, given in chapter 7, in a
program see what happens when you compile it. If it compiles successfully, see what
happens when you execute the program.
Do self-review exercises 12.17 to 12.19 of the case study (D&D page 513).
Use the answers on page 299 and 300 of D&D to check your answers.
Make sure that you can answer exercises 7.6, 7.7 and 7.9 and then do exercises 7.14 and
7.15 (pages 300 to 302) in D&D.
Go back to the outcomes and check whether you have reached the outcomes set for this
study unit. Indicate what your estimated level of mastery (expressed in percentage) is.
22
Study unit 6
23
Study unit 7
7 REVISION
You will need approximately 10 hours to successfully work through this study unit.
Final practical assignments covering all the work will be given and discussed before the
practical session.
Practice:
Do as many of the exercises marked with the icon: that you haven’t done yet.
24
Study unit 7
Now write a program to test the ScoreSheet class.
Go back to each study unit and check whether you have achieved its outcomes. Indicate
your estimated level of mastery (as a percentage).
25