Using Java to Illustrate Graph Theory concepts Carol E. Wolf Computer Science Department Pace University What is Java? • Java is an object-oriented programming language developed by Sun Microsystems. • It is widely used on the Internet and was designed to be portable. • The Java Development Kit (JDK) can be downloaded for free from Sun. • It contains a compiler, an interpreter, and many classes. • Java programs are compiled to bytecode, which is independent of the operating system. • Programs come in two varieties, applications and applets. • Java applications are full programs and are executed by an interpreter on the user’s computer. • Java applets require a web browser such as Netscape Communicator or Internet Explorer. • Applets are invoked by an HTML document. Theoretically, they can then be viewed on any computer with an appropriate browser. • Many animations and graphics on web sites have been created using Java applets. • One objection to Java, is that it takes time both to download the files and then start them up. Programs in native code would execute more quickly. • Java has been adopted by a number of computer science departments for programming classes. • It is easier to learn than C++, has fewer complications, and is safer. • It is fully object-oriented, and so students start off with the current (and better) programming paradigm. • However, this is a disadvantage for instructors who have only been teaching procedural languages. Object-Oriented Programming • An object combines both data and methods (algorithms) into one package called a class. • An example is that of an integer (int). An integer object has a value (data) and operations on that data (+, -, *, /, %, ==, <=, etc.) • Integers are primitive objects and do not require definition by the programmer. • Another example is that of a date. A date has integer data: day, month, and year, and operations on that data: equal, before (less than), display, etc. • Java does not (to my knowledge) have a built-in date class. However, it provides a facility for the programmer to create one. • In fact, Java programs are entirely composed of classes such as this. Java and Graphics • Because Java was designed for viewing in a browser, it takes full advantage of screen graphics. • It provides a full range of drawing functions, fonts, and colors. • These are easy for beginners to use and so provide a convenient introduction to the language. • The coordinate system for the screen has the origin in the upper left hand corner. The xaxis extends to the left, but the y-axis extends down. • Measurements on the screen are in terms of pixels, picture elements. • Java has some graphics classes, such as a Point class. These come with JDK. However, they are simple for a programmer to construct as well. class point { private int x, y; point (int x, int y) { this.x = x; this.y = y; } // point (int x, int y) int get_x () {return x;} int get_y () {return y;} void set_xy (int x, int y) { this.x = x; this.y = y; } // void set_xy (int x, int y) }// class point class circle { private point center; private int radius; circle (point center, int radius) { this.center = center; this.radius = radius; } // circle (point center, int radius) public void drawCircle (Graphics page) { int x, y; x = center.get_x (); y = center.get_y (); page.drawOval (x-radius, y-radius, 2*radius, 2*radius); } // public void drawCircle (Graphics page) } // class circle Relationships • The circle class illustrates the relationship of has-a. A circle has a center and a radius. The radius is an integer, a primitive data type, but the center is a point, a data type defined by the programmer. • Two other relationships are is-a and uses-a. • An is-a relationship is one of inheritance. Inheritance is an important feature of any object-oriented language. class drawn_point extends point { protected int radius = 3; drawn_point (int x, int y) { super (x, y); } // drawn_point (int x, int y) void drawPoint (Graphics page) { page.fillOval (x-radius, y-radius, 2 * radius, 2 * radius); } // void drawPoint (Graphics page) } // class drawn_point extends point • Java, unlike C++, does not support multiple inheritance. Instead, a class can implement another class. • This is an example of a uses-a relationship. • The principle classes that are used are listeners. These listen for some event to take place on the applet. • Examples are ActionListener, MouseListener, MouseMotionListener, ItemListener, and AdjustmentListener. public class draw_points extends Applet implements MouseListener { private static char name_char; private named_point newPoint; Graphics page; public void init () { setBackground (Color.cyan); setForeground (Color.blue); page = getGraphics (); name_char = 'A'; addMouseListener (this); } // public void init () private String get_next_name () { String next_name = "A"; next_name = next_name.replace ('A', name_char); name_char = (char) ((int) name_char + 1); return next_name; }// private String get_next_name () public void mouseClicked (MouseEvent event) { int x, y; String next_name = get_next_name (); x = event.getX (); y = event.getY (); newPoint = new named_point (x, y, next_name); newPoint.drawPoint (page); } // public void mouseClicked (MouseEvent event)