Letter
ClassLetter
class
mentioned before.
The picture of a class is the same as the above one for an object. But keep in mind that a class is not an object and vice versa. A class is a template from which actual objects are created.
Letter
, can be made with
Nothing more than theclass Letter { }
class
keyword, the name of the class,
and curly brackets around an empty class body.
In the next chapter you will see how you can
relate the class that you define to other classes.
Letter
class we could start with
the following instance variables:
class Letter { String name; String fontname; int size; int x, y; }
You recognize a constructor method byLetter(String c, int s, int a, int b) { // constructor to initialize letter and position name = c; fontname = "Helvetica"; size = s; x = a; y = b; }
d
already exists, the you can create
and instantiate the object c
just by putting c = d
.
For instance,
you can give create and instantiate color1
with
color1 = Color.red
.
//
in a line is a comment.You can define in a Java class more than one method with the same name but with different parameters. At run time, the correct choice of the method is made. In OOP jargon, Java allows method overloading.
Letter() { // constructor for default initialization name = "a"; fontname = "Helvetica"; size = 12; x = 0; y = 0; } Letter(String c, int s, int a, int b) { // constructor to initialize letter and position name = c; fontname = "Helvetica";; size = s; x = a; y = b; }
(a,b)
:
Java is a strongly-typed programming language. Every variable must be declared a type and the types of the parameters of a method as well as the type of the result of a method call must be stated explicitly.void translate(int a, int b) { x = x+a; y = y+b; }
void
is the return type that indicates that nothing is
actually returned when this method is called.
The general format of defining a method looks like
public
to
denote that a method is accessible from the outside.
Letter.java
we have the following first implementation of the
Letter
class:
class Letter { String name; String fontname; int size; int x,y; Letter(String c, int s, int a, int b) { // constructor to initialize letter and position name = c; fontname = "Helvetica";; size = s; x = a; y = b; } void translate(int a, int b) { x = x+a; y = y+b; } }
Letter
Objectsnew
operator and a
constructor. To create an instance of the Letter
class
enter:
Two remarks:Letter c; c = new Letter("A", 12, 10, 10);
Letter
instance and possibly
initializes it;Letter c = new Letter("A", 12, 10, 10);
name
: the string corresponding with the letter;fontname
: the name of the font (Helvetica, Courier, ...);size
: the font size;x
and y
: integers representing the horizontal and
vertical coordinate of the letter on the screen;Letter
example, you can type:
A side remark: if a comment takes more than one line, you start the opening line with/* set properties of a letter: name=A, fontname=Helvetica, size=24, position=(20,20). */ c.name = "A"; c.fontname = "Helvetica"; c.size = 24; c.x = 20; c.y = 20;
/*
and end the comment with */
.
An example:
You can combine nested instance access and method calls:c.translate(30,40);
You read them from the inside to the outside.c.name.length(); System.out.println(c.fontname);
name
instance variable of
the Letter
object c
is taken; the result is a
String
object to which we can apply the length
method (implemented in this class).
System.out.println
reads as follows.
The object System.out
belongs to the PrintStream
class and it represents the standard output stream.
The class PrintStream
offers the println
method and
we call this with an argument, viz., the fontname of
the letter c
. This name is printed on the standard output
stream so you will see the string Helvetica
on the computer screen.
+
operator) to get more readable results. Do not worry about
the concatenation of strings and integers that apparently occurs: the
Java system takes care of automatic conversions.
The Java code of the application is as follows:
It is stored in the fileclass LetterApplication { public static void main (String[] args) { Letter c = new Letter(); printInfo(c); c.name = "A"; c.fontname = "Helvetica"; c.size = 24; c.x = 20; c.y = 20; printInfo(c); c.translate(30,40); printInfo(c); } static void printInfo(Letter c) { System.out.println( "letter = " + c.name + ", " + " fontname = " + c.fontname + ", " + " size = " + c.size + ", " + " (x,y) = (" + c.x + ", " + c.y + ")" ); } }
LetterApplication.java
that must reside
in the same directory as the file Letter.java
.
Then it suffices to compile the code in the file
LetterApplication.java
; the code in the file
Letter.java
that is used in the application will be
compiled automatically.
When you run the Java application, then you should see on your screen the
following output:
letter = a, fontname = Helvetica, size = 12, (x,y) = (0, 0) letter = A, fontname = Helvetica, size = 24, (x,y) = (20, 20) letter = A, fontname = Helvetica, size = 24, (x,y) = (50, 60)