Week072. © 2004 Pearson Addison-Wesley. All rights reserved 7-2
Copyright Warning
COMMONWEALTH OF AUSTRALIA
Copyright Regulations 1969
WARNING
This material has been copied and communicated to you by or
on behalf of Bond University pursuant to Part VB of the
Copyright Act 1968 (the Act).
The material in this communication may be subject to copyright
under the Act. Any further copying or communication of this
material by you may be the subject of copyright protection
under the Act.
Do not remove this notice.
3. © 2004 Pearson Addison-Wesley. All rights reserved 7-3
Halfway Through
• We are halfway through the course!
• You now know the basics of programming:
basic data types
operators and expressions
assignment statements
selection statements: IF ... ELSE
loops: WHILE, DO ... WHILE, FOR
basic user input and output
making code modular with methods
• copying arguments into methods
• returning results out of methods
arrays to hold a set of data of the same type
4. © 2004 Pearson Addison-Wesley. All rights reserved 7-4
Programming, Not Java!
• None of these things are specific to Java.
• They are all applicable to most common
programming languages: C#, C++, PHP, Perl,
Actionscript, Javascript etc.
• This is why the course is called “Introduction to
Programming”, not “Java 1”
• Consider yourself as a budding tradesperson, e.g
carpenter, plumber
• You now have a toolbox of useful tools (loops,
methods, arrays, expressions)
• What you don't have yet is the experience of
knowing when is the right time to use each tool
5. © 2004 Pearson Addison-Wesley. All rights reserved 7-5
Some Tips
• Don't write code before having some form of a
design plan for your program
• Don't write the design before you have thought
about the problem
worry about the data you have to store and manipulate
more than the code
• Think modular:
“I'll have a method to do this, and a method to do..."
Use a bit of top-down design and a bit of bottom-up
design
• Design your interfaces to your methods before
writing any of the code for the method
• A very good programmer will design unit tests for
each method at the same time
6. © 2004 Pearson Addison-Wesley. All rights reserved 7-6
Some More Tips
• NEVER, NEVER, write all the code in one hit
• Always write one bit and test that it works, even if
the code doesn't do everything you want
• Use print statements, which you can remove
• When a bug appears, fix it before adding any new
code!
Learn to use the debugger, it will save your sanity
Computers do what you tell them, not what you wanted
them to do. Beware your own assumptions!
• Always assume that the user is an idiot. Defend
your program against them.
Similarly, test the inputs to your methods. Don't assume
that they are valid.
7. © 2004 Pearson Addison-Wesley. All rights reserved 7-7
Read Code Examples, Style
• Read code from other programmers. There is
usually a common 'style' or technique for doing
something
• Understand how it works. Don't learn a cookbook
answer: you may have to modify the technique to
suit your problem
• Style: write code as if the purpose of the code is to
explain to another programmer what the purpose
of the program is
indenting and spacing
comments: why this is being done, not how
good variable names
good structure: methods
• Will you understand your program in 6 months?
8. © 2004 Pearson Addison-Wesley. All rights reserved 7-8
Questions?
• What are the biggest gaps in your programming
knowledge so far?
• How can we help you to fill those gaps?
• What is still conceptually hard to grasp?
• Do you need more examples to read?
• Do YOU need to spend more time writing code?
“The only way to learn programming is to write code"
- Ken Thompson
9. © 2004 Pearson Addison-Wesley. All rights reserved 7-9
Group Exercise
• Sketch a design for a program to get a set of
scores from the user, and to print out the highest,
lowest, average score, and the percentage of
scores in each category: FL, PS, CR, DN, HD
• Don't write Java code
• What data is needed, how will it be stored?
• What high-level operations need to be done?
• What's the algorithm (in pseudo-code) to do the
operations?
• How will you make it modular?
10. © 2004 Pearson Addison-Wesley. All rights reserved 7-10
And Now, Classes and Objects...
• We have seen how to store individual data items:
int, char, double
• We have seen how to store and manipulate sets of
data items of the same type: arrays. What about
groups of data of different types?
• In the real world, things have several attributes,
e.g a human has
a name
an age
a height
a weight
a gender: male or female
• How can we represent a complex thing like a
human in Java?
11. © 2004 Pearson Addison-Wesley. All rights reserved 7-11
And Now, Classes and Objects...
• In Java, classes are used to group related data,
and to manipulate that data.
• A class is a template for the objects in a class
• An object is a particular example of a thing from
that class, e.g.
Jenny is an example human
• All objects that come from a class will all have the
same set of attributes, e.g
a name, an age, a height, a weight, a gender
• But, each object can have different values for
these attributes.
Jenny will have a different name, age, height, weight
gender than Tomas
12. © 2004 Pearson Addison-Wesley. All rights reserved 7-12
Some Definitions
• A Java class defines the types of attributes that all
objects in this class have.
• A Java class also defines the actions, or methods
that each object in the class can perform.
• Finally, a Java class defines how we create, or
construct, an individual object.
• From a design point of view, we use a class to
hold together:
a bunch of methods that are all somehow related, and
the data that these methods might need to do their work
13. © 2004 Pearson Addison-Wesley. All rights reserved 7-13
Designing a Class
• First up, think of what attributes every member of
the class will have.
• An example: most bank accounts are pretty
similar: you can deposit money and then withdraw
it as long as you know the PIN number.
• Each bank account has some identity, and the
amount of money in the bank.
• If we were to design a class for bank accounts,
what attributes should we include, and what Java
type will each attribute have?
• Finally, each Java class goes into its own file
14. © 2004 Pearson Addison-Wesley. All rights reserved 7-14
The BankAccount Class
• Let's go with these attributes:
account number, balance, PIN number
• The Java file for the BankAccount class would
look like:
public class BankAccount
{
int accountNumber;
double balance;
int PinNumber;
}
• That's all for now. More next week.....