Title: java
1???? ?????? ? java
- ????? ????? ????? ????? 5
- ????? ????? ?????
2????? ??? ???
- ???? ????? ??? ?????? 3 BFS
- ???? ?? ????? 4 ?'
- ????? (????), polymorphism, instanceof
- ????? '?????' ???? ?????? ?????.
- ???? ????????? ??????.
3????? - ????????
- ??? ????????? ??????? ?? ????? ????? ?????.
- ?????? ??????? ??? ???????.
- ?????? ???? ????? ??? ??????.
- ????? ??? (???? ???? ?? windows).
- ???? ???? ?? ????.
4????? 3 ???? ????? ???
- ???? ????? ??? ??? ?????? dist(p1, p2)
- ???? ?????? ??? ??????? ?????? p1,
- ???? ?????? ????? ????? ??????? ?????? ??????.
- ???? ??? ??? ??? '??? ??????' ???? ? p2.
- ???? ?????? BFS (????? ????).
5????? 4?'
- ?????
- ????? ?????, ?????
- ??????
6????? - ??????? ?? ???????
- ??? ?????? ???? ????? ??? ??????? ???? ?????.
- ??? ?????? ??? ??? ?????? ????? ???? ??? ?????
?? ??????. - ??? ???? ?????? ???? ???? ?? ?????????
7??????? ?? ?????? - ?????
- ????? ???? ???? ?? ???? (???? ???? ?????)
- ????? ??? ?????? ???? ????? (??? ?????)
- ????? ?? ?????? ???? ????? 4
8??????? ?? ?????? - ?????
- ???? ?????? ????? ????????? ??????? ?????
- ????? ????? ?????.
- Shape sp new Shape4
- sp0 new Rect()
- sp1 new Circle(new Point(1,1),4)
- sp2 new Shape()
- ???? ????? ?? ?????? ????? ????, ??????,
9??????? ?? ?????? - ?????
- ?????? Shape, ???? ????? ??? ?????
- ?????
- ???, ????, toString()
- ??? ?????, ????? ???..
- ????? Constructor
- ???? ??? (???? ???? ?? int)
10??????? ?? ?????? - ?????
public class Shape private int
_color public Shape(int c) this._color
c public void setColor(int c)this._color
c public int getColor() return
_color public double area() ??? return 1
11?????? Circle ????? ? Shape
- ????? ?????
- ???, toString
- ??? ???? ???? ?????
class Circle extends Shape public double
area() // over riding!! return
Math.PI_radiud_radius public String
toString() return Circle
cen_center rad _darius // class
Circle
12????? ????
- ?? ????? ????? ???? ???? ?????? ??? color
- ???? ????? ??????? ??????? ???? ???? ???????
??????? - area(). - ??? ????? ???? ?? ?????? ???? ?????? ?????
(????? ????? ?????? extends ?????? ??????).
13????? ????
- ???? ???? ?? ????!! ??? ???? ?????? ?? ???? ????
????? ???? ????? ????? ?? ????? ???? ?????! - ? java ????? ????? ???? ?????? ??? ????!.
- ?????? ???? ????? ???? super (????? ? this)
14?????? Circle ????? ? Shape
??? ????? ??? ???? ???? ?? ???? ???? ?? ?????
????
class Circle extends Shape public
Circle(point p, double r, int color)
super(color) // must be the first line!
// class Circle
- ???? ?? ????? ????? ???? ?????
- ??????? ????? ????? ???? ???!!
15????? ?????? ?????? ??????
- Java ????? (???? ????) ?? ??????? ????? ????? ???
????. - ???? ????? ???? ?? ???????
- ???? ??? ????? ????? ?????? (????? ????????)
- ?? ?? ???? ???? ??? ?????? ???? ???? (??? ????).
- ??????? ??? ???? poly-morphism ?????
- ????? ?? ???? ?? ????? ????? ?????? ?? ???? ?? ??
???? ????? ?????? ???? ?? ???????? ??????!!
16????? ?????? ?????? ??????
- ??? ?? ???????
- ???? ?? ??? ?? ?????? ????? (???? ???? ????? ???
??????) - ?????? ????? ?????? ????????, ?????? ???? ???????
?????? (?????? ??? \ ????? ????? ????).
17????? ??????? ???? ???
- ??? ???? ???? ??? ?????? ???? ??? ??????? ??
???? ????? ?? ?????? (????? ?? ??????? ???????). - ????? ???? ?????? instanceof, ?????
- if(arri instanceof Circle)
- instanceof ??????? ?????? ????? ??????? (?????)
?????? (?????) ????? ??? ?? ???????? ????
?????? ?? ??????. - ???? ?? ????? ??? ????? ???? ???
- If(arri instanceof Shape)
18????? ??????? ???? ???
- ????????? ???? ???? ?? ???? ???? ?? ????? ??????
???? ?? ????? ????? ??? ?????? ?????? ?? ????
???? ????, ?????? ???????. - ?????
- if(arri instanceof Circle) // run time.
- Circle c (Circle)arri // compile time.
- double rad c.radius() // return this radius
- double rad2 arri.radius() // Error (compile
time) - //.
-
19????? ??????? ???? ??? - Object
- ?? ???????? ??? ??????? ?????? ? Object
- ???? ????? ???? Object arr new Object5
- ???? ?????? ?? ????? ?? ????? (?????? ??????? ..)
- ? Object ???? ????? ???? toString()
- ????? ?????? ?? ?????? Object (java api)
- ?? ??????? ???? ?????? Object!
20???? ???? ???? - Collection
- ????? ?????? Object ??? ?????? ???? ????
(vector) - ?????
- ?????
- ?????
- ????
- ????? ????? ???????, ????????? \ ????
21???? ???? ???? - Collection
public class MyVector // data members
public static final int INIT_SIZE10,
RESCALE10 private int _sp0 private
Object _objs / Constructors creates a
empty set / public MyVector() _sp0 _obj
s new ObjectINIT_SIZE
22????? ????? - ???????
- ???? ????? ????? ?????? ???????.
- ??? ??????, ?????.
- ???? ??????
- ??? ?????? f(x)
- ????? ???????? integral(from, till, epsilon)
- ????? ???? ????? root1(from, till)
23????? ????? - ???????
- ???? ????? ???? ?????? ??????? ????????
- ????? (Monom) f(x) aXb
- ??????? (Polynum) ???? ?? ???????.
- ??????? ?????????, ???? f(x) sin(x)
- ???? ?? ???????? (Sum_f) f(x) S f(x)
- ???? ?? ???????? ???? ???? ???? ?? Sun_f.
24????? ????? - ???????
- ??????? ?? ??????
- ???? ???
- ??? ?????? (?????) f(x)
- ??????? integral(from,to,eps)
- ???? root(x1,x2)
25????? ????? Function1
public class Function1 / present a common
epsilon for numeric calcs / public static final
double EPS 0.001 public Function1() //
empty constructor public double f(double x)
return 0 // not in use!
26????? ????? Function1
double integral(double from, double till, double
eps) double ans 0 for(double x from
xlttill-eps xxeps) double y1
this.f(x), y2 this.f(xeps) double ym
(y1y2)/2 ans Math.abs(epsym) retu
rn ans
27????? ????? Function1
double root1(double x1, double x2) if(f(x1)
f(x2) gt0) System.out.println(" ERR wrong
values for root f("x1""f(x1)" ,
f("x2")"f(x2)) return -1 // may lead
to a BUG!! return root1(x1,x2,EPS)
28????? ????? Function1
/ recursive / private double root1(double
x1, double x2, double eps) double xm
(x1x2)/2 if(Math.abs(f(xm))lteps) return
xm if(f(xm)f(x1) lt 0) return root1(x1, xm,
eps) return root1(xm, x2, eps)
29????? ????? Function1
private double root2(double x1, double x2,
double eps) double xm (x1x2)/2 while(Mat
h.abs(f(xm))gteps) if(f(xm)f(x1) lt 0)
x2xm else x1xm xm (x1x2)/2 ret
urn xm
30????? ????? Monom
/ this class represent a Monom aXb, / class
Monom extends Function1 private double
_mekadem private int _power public
Monom(double mek, int pow) _mekadem mek
_power pow public double f(double x)
return Math.pow(x,_power) _mekadem
31????? ????? Monom
public void add(Monom m) if(this.equals(m))
_mekadem_mekademm.getMekadem() public void
mul(Monom m) _mekadem_mekadem
m.getMekadem() _power _power
m.getPower() String toString() return
_mekadem"X"_power
32????? ????? Sin_X
// Note no data! class Sin_X extends
Function1 public Sin_X() super() public
double f(double x) return Math.sin(x) //
debug public
String toString() return "sin(x)"
33????? ????? FunctionContainer
public class FunctionContainer extends
MyVector // no data members public
FunctionContainer() super() / overrides!,
only Functions1 can be added / public void add
(Object p) if(p instanceof Function1)
super.add(p) else System.out.println(" ERR
can not add "p" it is not a Function! ")
34????? ????? Sum_f
public class Sum_f extends Function1 private
FunctionContainer _funcs public Sum_f()
super() _funcs new FunctionContainer()
public void add(Function1 f) _funcs.add(f)
35????? ????? Sum_f
public double f(double x) double ans
0 for(int i0ilt_funcs.size()ii1) ans
_funcs.at_f(i).f(x) return ans public
int size() return _funcs.size() public
Function1 at(int i) return _funcs.at_f(i)
36????? ????? Polynom
class Polynom extends Sum_f // which extends
Func. public Polynom() super() public
void add(Function1 t) //overrides
Sum_f.add if(t!null t instanceof Monom)
super.add(t) else System.out.println("
ERR can add only Monoms ")
37????? ????? Class14
Monom m1 new Monom(.5,1) // f(x)
0.5X Sin_X sin1 new Sin_X() // f(x)
sin(x), radians! MyFrame win new
MyFrame() win.add(m1) win.add(sin1)
win.show()
38????? ????? ????
- f(x) 0.5x
- f(x) sin(x) , radians
39????? ????? ????
- f(x) 0.5x
- f(x) sin(x)0.5x
- f(x) 0.1x2
- f(x) 0.1x20.5x
40????? ????? ????
SOP("Poly "p1" p(2)"p1.f(2)) SOP("Integral(
2,4) "p1.integral(2,4,0.01)) SOP("sin(x)0,
x"sin1.root1(2,4)) // IP
41???? ????? ?????
- ???? 1 ????? ????? ???? Sum_f (??????? ???
????). - ???? 2 ?????? ???? ????? ??? ???? ???.
- ???? 3 ????? ?????? ???? ?????
Function1.f(x) -
- ????? ????? ??????? ?"? javadoc api.
- ????? ??? ?????, ????? ???.
- ??? ???? ?? ?????? class13.zip
42?????? ??? ???? ????????
- ???? ????? ?? ??????? ?????? ?????!
- ???? ??????? ???? (?? ???????), ?????? ????????
????? ????? (????? ?????) ??? ????? ????? ???
????? ???? ?? ????????, ????? ?? ???? this. - ??????? ????? ???? ?????? ?? ?? ??????? (???? ???
???? ????? ?? this).
43???? ????????? ??????
- ???? ????? ????? ?????? ?? ??????? (????? ??
this). - ???? ?????? ????? ??????? ???? ?????? ???? ???.
- ???? ??? ??? ???? ??? ???????? ????? ??????
??????? - ???? ???? ???? ????? ?????? ??? ??????? ??? ????
???? ???? ???? ?????? (?? ??????? ???? ????
?????? ????). - ??????? ????? ???? ????? ?? this ???? ?????
????? ?? ?? ???? ???? ?? ??????.
44???? ????????? ?????? - ?????
- public class Shape
- private static int _counter 0
- private int _color
- public Shape(int c)
- this._color c
- _counter
-
- public static int instances() return _counter
45?? ????? ????
- ?????
- ???? ?????? ??????, polymorphism, casting.
- ????? ????? ????????.
- ???? ?????? ?????
- ????? ????? ?????? ??????? ??????????.
- ????? ????? ??????? (??? ????? ?????).
- ???? ????????? ??????.