Presentation on Classes and Methods

Download Report

Transcript Presentation on Classes and Methods

2. Program Construction
in Java
1
2.6 Classes and methods
Classes
• So far, only one class has been used in each
project (each package) i.e. you only see one
icon in the main window.
• However, larger programs are easier to
develop and debug if they are divided into
separate classes each doing specific things.
3
Classes
• For example, handling input and output is a
specific task so you could write all the IBIO
methods into a separate class called IBIO.
• This has other benefits than just being tidier
and preventing you from making unintentional
syntax errors.
4
Classes
• To make use of the new class IBIO's
methods from outside it, use the class name
when calling one of its methods e.g.
‣
String name = IBIO.input("What
is your name?");
• Note the full stop used as a separator.
5
Classes
• When the method IBIO.input is called,
control jumps to the IBIO class and then
returns when the job is done.
• Sometimes one class will not compile
because of an error in another that it depends
on - the compiler will jump to the other class
and tell you.
6
Packages (projects)
• You end up with a whole package of
classes,
some acting as helpers to the principal class
(the one with the main method).
• Once working, you don't have to remember
how they work or keep looking at their code,
just how to call their methods and handle any
returns.
• Effectively, they become black boxes.
7
Methods
• Even within each class, methods (outside
Java, also called procedures or
subroutines) should be used to break down
the program into a series of smaller, more
manageable operations.
8
Methods
• Even within each class, methods (outside
Java, also called procedures or
subroutines) should be used to break down
the program into a series of smaller, more
manageable operations.
9
Signature
• The first line of any method is called its
signature,
e.g.
• public
static int average (int
n, int p, int t){}
•
The signature contains:
‣
‣
‣
‣
an access modifier e.g. public
a return type e.g. int
a name e.g. average
any parameter types and names e.g. int
n...
10
Signature
• A method is identified by its whole signature,
not just by its name, so these are three
different methods:
• public static int average (int n, int p, int t)
• public static double average (int n, int p, int
t)
•
public static int average (int n, int p)
• For now, avoid confusion by using different
names (but see Polymorphism later if you are
doing HL).
11
Scope
• This means how far across the project a
variable or method applies.
• A variable only applies in the block
({.......}) in which it is declared.
• Most variables are declared at the start of the
class to allow them to be used throughout.
12
Static methods
• For now, use the static keyword when
declaring a method or variable as it limits the
package to just one version of that method or
variable.
• In other circumstances (see Objects later) it
can be useful to mass-produce many versions
(instances) of the same (non-static) items.
13
Access
• Using public makes the variable or method
available throughout the package.
• Always use public for now.
• Later on, HL candidates will declare variables
private to their class (to protect them from
unintended change) and use public
getter and setter methods to access
them.
14
Parameter passing
• Pass any necessary information over to a
method in the round brackets after its name.
• So, to run the public
static int
inputInt (String prompt) method, we
called something like inputInt ("What is
your age?"); and the method made use of
that String, which it called prompt.
15
Parameter passing
• Every method must have round brackets even
if it has no parameters to be passed.
• You often see methods with empty brackets,
e.g.
‣
‣
Math.random()
sort()
16
Return type
• public
static int inputInt
(String prompt); returns an answer, a
value which will be an int in this case.
• Methods which do not return a value have the
return type void.
• Methods that return answers (return type
other than void) are called functions .
17
Classes and objects
• A class can be used as a kind of template to
describe the common features of a set of
similar objects, e.g.
‣
a class called Tree might have features like
trunkWidth, leafStyle, height etc. ,
features (attributes) that all its members
will have.
18
Classes and objects
• What are possible attributes for the class
Student?
• What are possible attributes for the class
DVD?
19
Classes and objects
• The class called String already exists in
Java and so has some specific attributes such
as length, charAt, etc. that you have
already used.
• In using the general class String, programs
construct specific String objects
(instances) using that class (with unique
names like firstName) and then set their
properties as appropriate.
20
Classes and objects
• A Paragraph object would have some
attributes in common with a String (e.g. it
has text and punctuation) and some which
distinguish it (e.g. margins and alignment).
• Attributes of a class are built up from the
primitive data types (int, double, boolean,
char, etc.)
21
Class libraries
• Java has extensive libraries of pre- existing
classes for you to use and modify (extend).
• A full listing is in the docs directory of the Java
SDK.
• However, ask before using them in your
program dossier - some things you have to
prove you can do from scratch!
22
•
Class library examples
For reference only:
‣
‣
‣
‣
‣
java.io - classes that enable file creation
and reading and writing to the screen,
java.math - classes allowing arithmetic,
java.net - classes allowing reference to
URLs and network communication,
java.text - classes to allow you to
"internationalise" your code,
java.util - utility classes.
23
Class library examples
•
Programmers often explicitly import the class libraries
(or parts of) they know they need at the very start of
their packages, e.g.
• import
java.io.*;
24