Java Brief Review •Java’s strengths •Object-oriented terminology •Inheritance •Interfaces •An example with inheritance and interfaces: class TwoDimFig CSE 341, S. Tanimoto Java brief review - 1 Java’s Strengths •Supports the objected-oriented paradigm •Permits client-side execution of programs (Applets) on web pages. •Provides portability of applications, including a standard graphics platform •Supports networking including communication, parallelism, security. •Provides a large standard library. •Syntax close to that of C++. CSE 341, S. Tanimoto Java brief review - 2 Object-Oriented Programming Terms •object: encapsulation of data and code. •class: a formal category of objects. •instance: an object, in the context of a particular class. • message: a request to an object or class for data or service. •sender: the object or class from which the message comes. •receiver: the object or class to which the message is sent. •method: a function within a class that responds to a message. CSE 341, S. Tanimoto Java brief review - 3 Inheritance •One class can serve as a base class for defining a new, more restricted class called a subclass. •Any instance of a (derived) class inherits the data members and method members of its parent class. •A subclass typically provides additional member variables and methods. •A subclass may override a member inherited from its parent by redefining it. •An abstract class doesn’t provide full implementations of all of its methods and is typically used as a means to provide a uniform protocol to two or more separate subclasses. •The visibility of names within and across inheritance hierarchies is controlled by the keywords public, private, protected. The default access control is “package” which itself defaults to accessibility within compilation units. CSE 341, S. Tanimoto Java brief review - 4 Interfaces •Since Java does not permit multiple inheritance with subclassing, (and therefore might have made it difficult for one class to integrate the functionality of several others) it compensates by providing formal interfaces. •An interface consists of a name and a protocol (list of functions and their formal parameter types). •A class may subclass at most one superclass but may implement any number of interfaces. •A class that implements an interface must provide an implementation for each function appearing in the interface. •Interfaces do not provide any code and are used to enforce (at compile time) calling conventions within software applications. •Interfaces can be extended. CSE 341, S. Tanimoto Java brief review - 5 An Overview of the TwoDimFig example •TwoDimFig is an abstract class •Circle extends TwoDimFig •Polygon extends TwoDimFig •Triangle extends Polygon •TwoDimFig implements SizeFunctions, Paintable •TestApplet instantiates Circle, Polygon, and Triangle CSE 341, S. Tanimoto Java brief review - 6 class TwoDimFig import java.awt.*; public abstract class TwoDimFig implements SizeFunctions, Paintable { protected Color c; public public public public public abstract abstract abstract abstract abstract String describe(); double area(); double volume(); int nVertices(); int nFaces(); public void setColor(Color c) { this.c = c; } public abstract void paint(Graphics g); } CSE 341, S. Tanimoto Java brief review - 7 interface SizeFunctions interface SizeFunctions { public public public public double area(); double volume(); int nVertices(); int nFaces(); } CSE 341, S. Tanimoto Java brief review - 8 interface Paintable import java.awt.*; interface Paintable { public void setColor(Color c); public void paint(Graphics g); } CSE 341, S. Tanimoto Java brief review - 9 class Circle import java.awt.*; public class Circle extends TwoDimFig { protected double r; Circle(double r) { this.r = r; } public String describe() { return "a circle with radius " + r; } public public public public int nVertices() { return int nFaces() { return 1; double area() { return r double volume() { return 0; } } * r * 3.14159; } 0.0; } public void paint(Graphics g) { g.setColor(c); g.drawOval(0, 0, (int)r, (int)r); } } CSE 341, S. Tanimoto Java brief review - 10 class Polygon import java.awt.*; import java.util.*; public class Polygon extends TwoDimFig { protected Vector vertices; Polygon() {} Polygon(int n) { vertices = new Vector(); int R = 100; double ANGLE = 2.0 * 3.14159 / n; for(int i = 0; i < n; i++) { Point p = new Point((int)(R + R*(Math.cos(i * ANGLE))), (int)(R + R*(Math.sin(i * ANGLE)))); vertices.addElement(p); } } public String describe() { return "a polygon with " + vertices.size() + " vertices"; } CSE 341, S. Tanimoto Java brief review - 11 class Polygon (slide 2 of 3) public double area() { if (vertices.size() < 3) return 0.0; Enumeration enum = vertices.elements(); Point firstVertex = (Point) enum.nextElement(); Point lastVertex = firstVertex; double total = 0.0; while(enum.hasMoreElements()) { Point thisVertex = (Point) enum.nextElement(); total += areaUnderSegment(lastVertex, thisVertex); lastVertex = thisVertex; } total += areaUnderSegment(lastVertex, firstVertex); return total; } private double areaUnderSegment(Point p1, Point p2) { return (p1.x - p2.x) * (p1.y + p2.y) / 2; } CSE 341, S. Tanimoto Java brief review - 12 class Polygon (slide 3 of 3) public double volume() { return 0.0; } public int nVertices() { return vertices.size(); } public int nFaces() { return 1; } public void paint(Graphics g) { g.setColor(c); Enumeration enum = vertices.elements(); Point firstVertex = (Point) enum.nextElement(); Point lastVertex = firstVertex; while(enum.hasMoreElements()) { Point thisVertex = (Point) enum.nextElement(); g.drawLine(lastVertex.x, lastVertex.y , thisVertex.x, thisVertex.y); lastVertex = thisVertex; } g.drawLine(lastVertex.x, lastVertex.y, firstVertex.x, firstVertex.y); } } CSE 341, S. Tanimoto Java brief review - 13 class Triangle (1 of 2) import java.awt.*; import java.util.*; public class Triangle extends Polygon { Triangle(Point p1, Point p2, Point p3) { vertices = new Vector(); vertices.addElement(p1); vertices.addElement(p2); vertices.addElement(p3); } public String describe() { if (isIsosceles()) return "an isosceles triangle"; else return "a triangle"; } CSE 341, S. Tanimoto Java brief review - 14 class Triangle (2 of 2) public boolean isIsosceles() { Point p1 = (Point) vertices.elementAt(0); Point p2 = (Point) vertices.elementAt(1); Point p3 = (Point) vertices.elementAt(2); int d12 = dist(p1,p2); int d23 = dist(p2,p3); int d31 = dist(p3,p1); return ((d12 == d23) || (d23 == d31) || (d12 == d31)); } private int dist(Point p1, Point p2) { return (int)((double) Math.sqrt((double)(((p1.x - p2.x)*(p1.x - p2.x)) + ((p1.y - p2.y)*(p1.y - p2.y)))) ); } } CSE 341, S. Tanimoto Java brief review - 15 A Pre-Quiz Challenge •What shows up on the applet panel? •What shows up in the Java console? •How should the TestApplet code be changed in order to have the triangle described as a polygon with 3 sides? •What would a class ThreeDimFig look like that takes advantage of the same interfaces used by TwoDimFig ? •What should it be a subclass of? •Should it explicitly implement any interfaces? CSE 341, S. Tanimoto Java brief review - 16