Lecture 8 Slides
Download
Report
Transcript Lecture 8 Slides
Programming for Beginners
Lecture 8: Using External Libraries
Martin Nelson
Elizabeth FitzGerald
Revision of session 7
Classes/Objects can contain both data and methods.
Methods have a return type stating what sort of data is
passed back to the calling code.
Could be void (if the method doesn’t return anything), any
primitive data type or any Class.
Methods will sometimes require a list of input arguments,
given in ()’s after the method name.
Static methods can be used without instantiating the
class.
The UserInput class contains 3 static methods for
obtaining information from the user:
readint, readdouble, readString.
Session 8 - aims & objectives
Learn how to use external class libraries, distributed with
the JDK.
Investigate the Core Class Library documentation.
See useful methods in the String class and the Math class.
Using external classes
External classes usually contain methods and variables.
Once you know where to find these methods and
variables, you can use them in your program.
For classes in the Core Class Libraries, you need to have
the command
import classname;
at the start of your program if you want to refer to a
method in that external class.
Note: If you use the import command for your own
classes you will get an error!
Core Class Libraries
Java is distributed with a collection of ready-to-use
classes, called the Core Class Libraries.
A full list of the classes available can be found on the Java
Core Class Libraries documentation page:
http://download.oracle.com/javase/1.4.2/docs/api/overview-summary.html
Today, we will focus on two classes within the java.lang
library:
String
Math
The java.lang library contains everything that is needed to
define the Java language itself. Since it is needed for
every code, the “import java.lang” statement is not
needed. This is the only library for which this is true.
Java.lang
The java.lang library contains:
The System class, which we have already used.
The String class – containing loads of methods for
manipulating Strings.
The Math class – which contains numerous mathematical
constants and operations.
Classes for handling errors (which we’ll use in Session 11).
Loads more...
The String class – 1
String is a class (in the java.lang package)
String is a special class: it is also a literal data type.
Once the String is declared, values can be assigned using the ‘=‘.
String is the only class for which this is true.
Strings can also be instantiated in other ways using the
'new' keyword.
We’ll talk more about these in Session 10...
However, because String is a class, it has a number of
useful methods which we can use...
The String class – 2
Class String contains several methods
For looking at individual characters of the sequence (charAt)
For comparing Strings (equals, equalsIgnoreCase)
For searching Strings (indexOf)
For extracting substrings (substring)
For converting characters to upper/lower case (toUpperCase,
toLowerCase)
Plus many more!
Check Java core class documentation for more details
String functions
See example code:
ex8_01.java: The String class (revision)
ex8_02.java: The String class: concatenation & case conversions
ex8_03.java: The String class: comparing Strings
ex8_04.java: The String class: extracting data by position
ex8_05.java: The String class: extracting data by searching
The Math class – 1
Class within the java.lang package.
Contains static variables representing mathematical
constants:
Pi and E (Pi and E)
Plus static methods for common mathematical operations:
Square root (sqrt)
Natural logs (log)
Sine/cosine/arc tangent etc. (sin, cos, atan etc.)
Raising to powers (pow)
Exponential function (exp)
Rounding up/down (floor, ceil, round)
The Math class – 2
See example code:
ex8_06.java: Using mathematical constants
ex8_07.java: Calculating powers and square roots
ex8_08.java: Rounding methods
ex8_09.java: Calculating natural logs
The Math class – 3
We have a method to calculate natural logs (log), but
what if we want to calculate logs to a different base?
e.g. How do we calculate log[10]?
We could use the in-built log function, to make our own
method which calculates log[n], for any integer n.
Note that:
log[n](x)=ln(x)/ln(n).
See exercise 8.1.
In newer versions of java, there is an extra method log10,
which calculates the above for n=10.
Coming up in Session 9...
An introduction to Arrays...
Really useful for storing large amounts of data.
Recap of the course so far...
Plenty of time for revision of anything tricky!
Let us know if there is anything you’d like to focus upon...