Applications of Java to Physics Teaching Part I

  • Slides: 42
Download presentation
Applications of Java to Physics Teaching (Part I) S S Tong Department of Physics

Applications of Java to Physics Teaching (Part I) S S Tong Department of Physics CUHK

1 Interactive Teaching in Physics 4 IT development in HK 4 Arose students’ interest

1 Interactive Teaching in Physics 4 IT development in HK 4 Arose students’ interest 4 Truly interactive? 4 Illustrate both Phy. & Maths. concepts? 4 Supplements to experiments

2 Why Java? 4 Programs readily distributed on WWW 4 Run on browsers’ machines

2 Why Java? 4 Programs readily distributed on WWW 4 Run on browsers’ machines – Minimize servers’ loading – Protect servers against hackers 4 Small, fast download 4 Portable, platform independent – On PCs, Mac, Workstations 4 Quite easy to learn

3. Java Applets for Teaching Physics 4 Wang F K (NTNU) – http: //www.

3. Java Applets for Teaching Physics 4 Wang F K (NTNU) – http: //www. fed. cuhk. edu. hk/sci_lab/ntnujava/ – http: //www. phy. ntnu. edu. tw/demolab/index. html – http: //www. phy. ntnu. edu. tw/java/index. html 4 Physics 2000 – http: //www. colorado. edu/physics/2000/ 4 Java LAB – http: //physicsweb. org/TIPTOP/VLAB/ 4 http: //www. bekkoame. or. jp/~kamikawa/java_e. htm

4 Interactive Physics and Math with Java (Sergey Kiselev) – http: //www. lightlink. com/sergey/java/index.

4 Interactive Physics and Math with Java (Sergey Kiselev) – http: //www. lightlink. com/sergey/java/index. html 4 Fowler's Physics Applets (Michael Fowler) – http: //www. phys. virginia. edu/classes/109 N/more_stuff/ Applets/ 4 Physlet Demonstrations (Wolfgang Christian) – http: //Web. Physics. davidson. edu/Applets. html 4 Java Applets on Physics (Walter Fendt) – http: //home. acity. de/walter. fendt/physengl. htm

Example JAVA source codes 4 Heriot-Watt University Department of Physics – http: //www. phy.

Example JAVA source codes 4 Heriot-Watt University Department of Physics – http: //www. phy. hw. ac. uk/resources/demos/index. html 4 Electric charge with JAVA – http: //www. dcc. uchile. cl/~sebrodri/JAVA/Proyecto/Pro yect. I. html 4 State University of New York at Stony Brook – http: //www. dcc. uchile. cl/~sebrodri/JAVA/Proyecto/Pro yect. I. html

4. ABC of Java I 4 Applications: Standalone Java programs 4 Applets: Programs that

4. ABC of Java I 4 Applications: Standalone Java programs 4 Applets: Programs that run on web browsers 4 We discuss applets only 4 How to install Java Development Toolkit (JDK)? 4 How to edit Java source codes? 4 How to complie a source file into an applet? 4 How to insert an applet into an HTML file?

Install JDK 4 Installation is straightforward 4 Add a path to the Autoexec. bat

Install JDK 4 Installation is straightforward 4 Add a path to the Autoexec. bat – e. g. you installed JDK at c: jdk 1. 1. 8 path = c: jdk 1. 1. 8bin

Use any text editor to edit a source file 4 I prefer Textpad 4

Use any text editor to edit a source file 4 I prefer Textpad 4 Easily to use clip library 4 Tools for compiling and running Java programs

Example: Say. Hello. java import java. applet. *; import java. awt. *; applet package

Example: Say. Hello. java import java. applet. *; import java. awt. *; applet package Abstract Windowing Toolkit public class Say. Hello extends Applet { Font f = new Font("Times. Roman", Font. BOLD, 36); public void paint(Graphics screen) { screen. set. Font(f); screen. set. Color(Color. red); screen. draw. String("Say Hello", 5, 40); } } The paint method does the actual painting job

Compile Say. Hello. java to Say. Hello. class C: Your. Dirjavac Say. Hello. java

Compile Say. Hello. java to Say. Hello. class C: Your. Dirjavac Say. Hello. java Say. Hello. class Include Say. Hello. class in a HTML file <HTML> <BODY> <APPLET CODE="Say. Hello. class" WIDTH=300 HEIGHT=200> </APPLET> </BODY> </HTML> The main class View it by appletviewer C: Your. Dirappletviewer Say. Hello. html

4. ABC of Java II 4 An Object - Oriented (O-O) language 4 Basic

4. ABC of Java II 4 An Object - Oriented (O-O) language 4 Basic units : Class and Object 4 The concept of a class – How do we usually classify things? – e. g. , Fruit is a class – Attributes : shape, color, etc. – Objects of Fruit : orange, apple, bannna, etc. – e. g. , Attributes of an apple : shape is spherical, color is red. 4 Actual manipulations are done by methods

4 Class and objects Fruit Class (Abstract) Individual objects

4 Class and objects Fruit Class (Abstract) Individual objects

4 The source of a Java applet may look like: the applet package must

4 The source of a Java applet may look like: the applet package must be loaded import java. applet. *; import. . . may have other package public class My. Applet extends Applet {. . . } the main class must be a subclass of Applet class Class. A. . . {. . . } class Class. B. . . {. . . }. . . may have other classes

4 Declare a class called Fruit class Fruit {. . . } 4 Attributes

4 Declare a class called Fruit class Fruit {. . . } 4 Attributes are described by instance variables class Fruit { String shape, color; boolean eaten; . . . } 4 Create an object (instance) of Fruit orange; orange = new Fruit(); 4 Instance variables of orange can be accessed by orange. shape = “spherical”;

4 Make a class called fruit class Fruit { String shape, color; boolean eaten;

4 Make a class called fruit class Fruit { String shape, color; boolean eaten; . . . } 4 One can now create an object of fruit in an applet import java. applet. *; public class Hello. To. Fruit extends Applet { Fruit orange; declare the object type pubic void init() { new object orange = new Fruit(); assign orange. shape = “spherical”; attributes orange. color = “orange”; to orange. eaten = false; }. . . } Hello. Fruit. class Compiling

4 A constructor helps to defne a object class Fruit { String shape, color;

4 A constructor helps to defne a object class Fruit { String shape, color; boolean eaten; Fruit(String shape, String color, boolean eaten) { this refers this. shape = shape; to the object this. color = color; calling the constructor this. eaten = eaten; } } 4 Now an object can be created more conveniently: import java. applet. *; public class Hello. To. Fruit 2 extends Applet { Fruit orange; public void init() { orange = new Fruit(“spherical”, “orange”, false); } }

4 Adding the paint method import java. awt. *; import java. applet. *; public

4 Adding the paint method import java. awt. *; import java. applet. *; public class Hello. To. Fruit 3 extends Applet { Fruit orange = new Fruit("spherical", "orange", true); Font f = new Font( "Times. Roman", Font. BOLD, 36); public void paint(Graphics screen) { screen. set. Color(Color. red); screen. set. Font(f); screen. draw. String("The color of orange is" + " " + orange. color, 5, 20); if (orange. eaten) screen. draw. String(“It has been eaten”, 5, 90); } }

4 Overloading a constructor (x, y) w import java. awt. *; class Rect {

4 Overloading a constructor (x, y) w import java. awt. *; class Rect { int x, y, w, h; h Rect(int x, int y, int w, int h) { this. x = x; p(x, y) this. y = y; this. w = w; this. h = h; } } q(x, y) Rect(Point p, Point q) { this(p. x, p. y, q. x - p. x, q. y - p. y); } R 1 = new Rect(1, 1, 2, 1); give rectangles of the same size and position Point p 1 = new Point(1, 1); Point p 2 = new Point(3, 2); R 2 = new Rect(p 1, p 2);

4 Delcaring methods return type (void return nothing) argument (no argument here) name of

4 Delcaring methods return type (void return nothing) argument (no argument here) name of method void my. Method() {. . . . } 4 Using methods – suppose my. Method() is declared in My. Class the object which calls the method (a My. Class object here) name of method my. Class. Object. my. Method(); argument (no argument here)

4 Another example return type (a double here) name of method arguments (takes 1

4 Another example return type (a double here) name of method arguments (takes 1 integer and 1 string) double my. New. Method(int t, String s) {. . . . return result; } return value (must be a double here) 4 Calling the method – suppose my. New. Method() is declared in My. New. Class the object which calls the method (a My. New. Class object here) name of method an integer a string my. New. Class. Object. my. New. Method(10, “abc”) a double

4 Using methods (an example in physics) class Falling. Ball { double x, y,

4 Using methods (an example in physics) class Falling. Ball { double x, y, ux, uy; double g = -9. 8; Falling. Ball(double x, double y, double ux, double uy) { this. x = x; this. y = y; this. ux = ux; this. uy = uy; } return doubles double xt(double t) { return x + ux*t; } double yt(double t) { return y + uy*t + 0. 5*g*t*t; } double uxt(double t) { return ux; } double uyt(double t) { return uy + g*t; } }

4 Using methods (continued) : import java. applet. *; import java. awt. *; public

4 Using methods (continued) : import java. applet. *; import java. awt. *; public class My. Falling. Ball extends Applet { Falling. Ball my. Ball; my. Ball = new. Ball(0, 0, 5, 5); . . . } public void paint(Graphics screen) {. . . screen. draw. String(“After 1 s, the ball is at ”, 5, 20); screen. draw. String(“x = “ + my. Ball. xt(1) + “, y = “ + my. Ball. yt(1), 5, 30); . . . } becomes doubles ( real numbers)

4 Using methods (another example). . . public class My. Rect 2 extends Applet

4 Using methods (another example). . . public class My. Rect 2 extends Applet {. . . R 1. equals(R 2). . . ; . . . true or false } class Rect {. . . boolean equals(Rect R) { return (R. x == x) && (R. y == y) && (R. w == w) && (R. h == h); } } w of the object (R 1) w of the which calls the method argument (R 2)

4 Exercise 1 (Vector. java) : Contructor : x and y-compoents of a vector

4 Exercise 1 (Vector. java) : Contructor : x and y-compoents of a vector (two doubles) Contructor : How about polar coordinates? Method 1 : find the magnitude of a vector (called by a vector object, returning a double) Method 2 : add two vectors (called by a vector object, taking a vector argument, returning a vector object) e. g. vector. A. add(vector. B) Method 3 : subtract two vectors Method 4 : find the dot product of two vectors (called by a vector object, taking a vector argument, returning a double)

4 Array objects – declare an array 0 to 3 String[] Names = new

4 Array objects – declare an array 0 to 3 String[] Names = new String[4]; – access individual elements Names[0] = “Put your name here”; Names[1] = “Tong Shiu-sing”; . . . . Names[3] = “Doraemon”; – may also declare and initialize an array by String[] Names = {“Your name here”, “Tong Shiu-sing”, . . . , “Doraemon”}; – assigning values to elements screen. draw. String(Names[3]);

4 Logic and loops – logical operators == – conditional statements != > <

4 Logic and loops – logical operators == – conditional statements != > < >= && <= || e. g. 1 if (Names[3] == “Doraemon”) current. String = “I am Doraemon”; e. g. 2 if (Names[3]. length > 15) current. String = “a long name”; else current. String = “a short name”; – if - then blocks e. g. 3 if (Names[1] == “Doraemon”) {. . . . } else if (Names[1] == “Tong Shiu-sing”) {. . . . } else {. . . . }

– for loop i i+1 e. g 4 for (int i = 0; i

– for loop i i+1 e. g 4 for (int i = 0; i < 4; i++) { if (Names[i]. length() > 15) current. String = “a long name”; else current. String = “a short name”; . . . . } – while loop e. g. 5 i = 0; while (Name[i] != “Doraemon”) { i++; } current. String = “Doraemon is at position ” + i;

4 Inheritance of classes Food Subclasses of Food Meat Subclasses Cold of Meat blooded

4 Inheritance of classes Food Subclasses of Food Meat Subclasses Cold of Meat blooded Warm blooded Vegetable Chicken Beef Pork Fruit Instances of warm blooded meat

4 Inheritance of classes (an example in physics) – Declare a class Ball {

4 Inheritance of classes (an example in physics) – Declare a class Ball { double x, y, radius; Ball(double x, double y, double radius) { this. x = x; this. y = y; this. radius = radius; } }

4 Inheritance of classes (continued) – A subclass Moving. Ball carries more information –

4 Inheritance of classes (continued) – A subclass Moving. Ball carries more information – It also contains methods class Moving. Ball extends Ball { double ux, uy; Moving. Ball(double x, double y, double ux, double uy, double radius) { Call the super(x, y, radius); constructor this. ux = ux; of its superclass this. uy = uy; } double xt(double return x + double yt(double return y + } t) { ux*t; } t) { uy*t; }

4 Inheritance of classes (continued) – A subclass Falling. Ball of Moving. Ball class

4 Inheritance of classes (continued) – A subclass Falling. Ball of Moving. Ball class Falling. Ball extends Moving. Ball { double gy; Falling. Ball (double x, double y, double ux, double uy, double gy, double radius) { Call the super(x, y, ux, uy, radius); constructor this. gy = gy; of its superclass } double yt(double t) { return y + uy*t + gy*t*t/2; } } Override its superclass’s method

4 Inheritance of classes (continued) Moving. Ball ball. A = new Moving. Ball(0, 0,

4 Inheritance of classes (continued) Moving. Ball ball. A = new Moving. Ball(0, 0, 3, 5, 1); ball. A. xt(2) gives x + ux*t = 0 + 3*2 = 6 ball. A. yt(2) gives y + uy*t = 0 + 5*2 = 10 Falling. Ball ball. B = new Falling. Ball(0, 0, 3, 5, -10, 1); ball. B. xt(2) still gives x + ux*t = 0 + 3*2 = 6 call to the xt method of in Moving. Ball ball. B. yt(2) Now gives y + uy*t + ay*t*t/2 = 0 + 5*2 - 10*2*2/2 = -10 the method yt is overridden

A method calls to the superclass Class A ABC(. . . ) Going up

A method calls to the superclass Class A ABC(. . . ) Going up the chain of inheritance until a definition of the method ABC is found ABCB Class C Class D object A method ABC is called

A method is overridden by another method of the same name Class A ABC(.

A method is overridden by another method of the same name Class A ABC(. . . ) Going up the chain of inheritance, the first reached ABC is executed ABCB Class ABC(. . . ) Class C Class D object A method ABC is called

Calling a overridden method in a superclass Class A ABC(. . . ) Initial

Calling a overridden method in a superclass Class A ABC(. . . ) Initial method definition Method overriden by subclass super. ABC(. . . ) Class B ABC(. . . ) calls ABC in this class calls ABC in the superclass

4 Exercise 2 (Waves) – Construct a class of sine curve (only variable: amplitude,

4 Exercise 2 (Waves) – Construct a class of sine curve (only variable: amplitude, create a method y(x) ) – Construct a class of traveling waves (2 more variables: wavelength, period, create y(x, t) making use of the superclass’s y(x) ) – Construct a class of standing waves (any more variables needed? , think of a standing wave as a superposition of 2 waves traveling in opposite direction, create y(x, t) making use of the superclass’s y(x, t) )

5. ABC of Java III 4 Graphics coordinate system (0, 0) x (20, 20)

5. ABC of Java III 4 Graphics coordinate system (0, 0) x (20, 20) (60, 60) y

4 Various graphics commands screen. draw. Line(x 1, y 1, x 2, y 2);

4 Various graphics commands screen. draw. Line(x 1, y 1, x 2, y 2); Graphics object begin point end point screen. draw. Rect(x, y, w, h); / fill. Rect upper-left width height corner x, y coordinates of corners int[] x = {10, 20, 30, 10}, y = {0, 20, 10, 0}; Polygon poly = new Polygon(x, y, x. length); screen. draw. Polygon(poly); make a Polygon object / fill. Polygon

4 Various graphics commands (continued) screen. draw. Oval(x, y, w, h); (x, y) /

4 Various graphics commands (continued) screen. draw. Oval(x, y, w, h); (x, y) / fill. Oval h w screen. draw. Arc(x, y, w, h, t 1, t 2); (x, y) t 1 = 90 h / fill. Arc t 2 = 360 w

4 Implementing the paint method import java. awt. *; import java. applet. *; public

4 Implementing the paint method import java. awt. *; import java. applet. *; public class Draw. Something extends Applet { public void paint(Graphics screen) { screen. set. Color(Color. red); screen. draw. String( “I am Doraemon”, 200); screen. draw. Rect(10, 120, 90); screen. set. Color(Color. blue); screen. fill. Oval(150, 120, 60); screen. set. Color(Color. green); screen. draw. Arc(10, 150, 120, 60, 0, 270); } }

4 Drawing images – Create an object of Image my. Image; – Load the

4 Drawing images – Create an object of Image my. Image; – Load the image into the applet my. Image = get. Image(get. Code. Base(), “hi. gif”); get the directory of the applet (here the applet and the image file are in the same directory) Image file name – Draw the image on screen in the paint method public void paint(Graphics screen) {. . . screen. draw. Image(my. Image, 20, 10, this); . . . upper-left corner }