Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Applications of Java to Physics Teaching (Part I) S S Tong Department of Physics CUHK."— Presentation transcript:

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

2 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

3 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

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

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

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

7 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?

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

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

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

11 Compile SayHello.java to SayHello.class C:\YourDir\javac SayHello.java SayHello.class <APPLET CODE="SayHello.class" WIDTH=300 HEIGHT=200> Include SayHello.class in a HTML file The main class View it by appletviewer C:\YourDir\appletviewer SayHello.html

12 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

13 4 Class and objects Fruit Class (Abstract) Individual objects

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

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

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

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

18 4 Adding the paint method import java.awt.*; import java.applet.*; public class HelloToFruit3 extends Applet { Fruit orange = new Fruit("spherical", "orange", true); Font f = new Font( "TimesRoman", Font.BOLD, 36); public void paint(Graphics screen) { screen.setColor(Color.red); screen.setFont(f); screen.drawString("The color of orange is" + " " + orange.color, 5, 20); if (orange.eaten) screen.drawString(“It has been eaten”, 5, 90); }

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

20 4 Delcaring methods void myMethod() {........ } return type (void return nothing) name of method argument (no argument here) 4 Using methods –suppose myMethod() is declared in MyClass myClassObject.myMethod(); argument (no argument here) name of method the object which calls the method (a MyClass object here)

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

22 4 Using methods (an example in physics) class FallingBall { double x, y, ux, uy; double g = -9.8; FallingBall(double x, double y, double ux, double uy) { this.x = x; this.y = y; this.ux = ux; this.uy = uy; } 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;} } return doubles

23 4 Using methods (continued) : import java.applet.*; import java.awt.*; public class MyFallingBall extends Applet { FallingBall myBall; myBall = newBall(0,0,5,5);...... public void paint(Graphics screen) {...... screen.drawString(“After 1s, the ball is at ”, 5, 20); screen.drawString(“x = “ + myBall.xt(1) + “, y = “ + myBall.yt(1), 5, 30);...... } becomes doubles ( real numbers)

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

25  Exercise 1 ( Vector.java ) : Contructor : x and y-compoents of a vector Method 1 : find the magnitude of a vector (two doubles) Method 2 : add two vectors (called by a vector object, returning a double) (called by a vector object, taking a vector argument, returning a vector object) e.g. vectorA.add(vectorB) 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) Contructor : How about polar coordinates?

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

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

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

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

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

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

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

33 4 Inheritance of classes (continued) MovingBall ballA = new MovingBall(0,0,3,5,1); ballA.xt(2) gives x + ux*t = 0 + 3*2 = 6 ballA.yt(2) gives y + uy*t = 0 + 5*2 = 10 FallingBall ballB = new FallingBall(0,0,3,5,-10,1); ballB.xt(2) still gives x + ux*t = 0 + 3*2 = 6 call to the xt method of in MovingBall ballB.yt(2) Now gives y + uy*t + ay*t*t/2 = 0 + 5*2 - 10*2*2/2 = -10 the method yt is overridden

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

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

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

37 4 Exercise 2 (Waves) –Construct a class of sine curve –Construct a class of traveling waves (only variable: amplitude, create a method y(x) ) (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) )

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

39 int[] x = {10,20,30,10}, y = {0,20,10,0}; Polygon poly = new Polygon(x,y,x.length); screen.drawPolygon(poly); / fillPolygon screen.drawLine(x1, y1, x2, y2); 4 Various graphics commands begin point end point Graphics object upper-left corner widthheight screen.drawRect(x, y, w, h); / fillRect x, y coordinates of corners make a Polygon object

40 4 Various graphics commands (continued) screen.drawOval(x, y, w, h); / fillOval screen.drawArc(x, y, w, h, t1, t2); / fillArc (x, y) w h h w t2 = 360  t1 = 90 

41  Implementing the paint method import java.awt.*; import java.applet.*; public class DrawSomething extends Applet { public void paint(Graphics screen) { screen.setColor(Color.red); screen.drawString( “I am Doraemon”,200,200); screen.drawRect(10,10,120,90); screen.setColor(Color.blue); screen.fillOval(150,10,120,60); screen.setColor(Color.green); screen.drawArc(10,150,120,60,0,270); }

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


Download ppt "Applications of Java to Physics Teaching (Part I) S S Tong Department of Physics CUHK."

Similar presentations


Ads by Google