-- it has two data members that keeps its location, and some functions that tell its attributes(location, magnitude...). class Point {public double x, y; final double PI = Math.PI; Point() {x=0; y=0}
Point(int x1; int y): x(x1),y(y1) {} double magnitude() { return Math.sqrt(x*x
+ y*y); } void print() { system.out.print(this.getclass().getstring() +
"="); system.out.println(x + " " + y); } }
-- It is a line between two points, so it has two points and function which tells if it intersects with another ray. class Ray { Point p1, p2; point intersect(Ray r); } Point Ray::intersect(Ray r) {Point p3 = r.p1; Point p4 = r.p2; double a1, b1, c1, a2, b2, c2; a1
= p2.y - p1.y; b1 = p1.x - p2.x; .... Point pi = new Point(..., ... ); return
pi; }
-- null value can be used to detect whether an object has an viable instance. if (user.object != NULL) system.out.println("Object exist");
"==" means equal / instance of Syntax:if (User.Object instance of <Class>) { ... }
Example:if (P1 instance of Point) { system.out.println(this.getclass(P1).getstring
+ "is a point"); }
-- is data type conversion. In class casting, you should only winden a class by casting it to a superclass. public class simpleDraw extends Applet { Vecter drawnShapes; void paint(Graphics g) {raw_grid(g); hape s; nt numshapes = drawnShapes.size(); or (int i=0;
i<numshapes; i++) s = (Shape)drawnShapes.elementAt(i); s.draw(g); }
}
1>. If this class is a subclass of another class, use extends to indicate the superclass of this class: Syntax: class myClassName extends mySuperClassName { ... } Example: class Target extends Shape {void draw(graphics g ) { g.setcolor(this.color); fill_circle(g, Pi, Shape.radius); }} 2>. If this class implements a specific interface, use implements to refer to that interface: Syntax: class MyRunnableClassName implements Runnable { ... }
1. What does class extension do? a. adding more classes. b. define more features to a existing class. c. new class in java can be extensions of any single existing class.