1 / 42

Applications of Java to Physics Teaching (Part I)

Applications of Java to Physics Teaching (Part I). S S Tong Department of Physics CUHK. 1 Interactive Teaching in Physics. IT development in HK Arose students’ interest Truly interactive? Illustrate both Phy. & Maths. concepts? Supplements to experiments. 2 Why Java?.

alvin-hess
Download Presentation

Applications of Java to Physics Teaching (Part I)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

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

  4. 3. Java Applets for Teaching Physics • 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 • Physics 2000 • http://www.colorado.edu/physics/2000/ • Java LAB • http://physicsweb.org/TIPTOP/VLAB/ • http://www.bekkoame.or.jp/~kamikawa/java_e.htm

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

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

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

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

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

  10. Abstract Windowing Toolkit applet package The paint method does the actual paintingjob Example: SayHello.java 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); } }

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

  12. 4. ABC of Java II • An Object - Oriented (O-O) language • Basic units : Class and Object • 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. • Actual manipulations are done by methods

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

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

  15. Declare a class called Fruit class Fruit { ...... } • 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. declare the object type assign attributes to orange new object Compiling HelloFruit.class Fruit.class class Fruit { String shape, color; boolean eaten; ...... } • Make a class called fruit • One can now create an object of fruit in an applet 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; } ...... }

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

  18. 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); } } • Adding the paint method

  19. w (x,y) h Rect(Point p, Point q) { this(p.x, p.y, q.x - p.x, q.y - p.y); } p(x,y) R1 = new Rect(1,1,2,1); Point p1 = new Point(1,1); Point p2 = new Point(3,2); R2 = new Rect(p1,p2); q(x,y) give rectangles of the same size and position • 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; } }

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

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

  22. return doubles • 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;} }

  23. becomes doubles ( real numbers) • 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); ...... } }

  24. true or false w of the object (R1) which calls the method w of the argument (R2) • 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); } }

  25. (two doubles) (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) • Exercise 1 (Vector.java) : Contructor : x and y-compoents of a vector Contructor : How about polar coordinates? Method 1 : find the magnitude of a vector Method 2 : add two vectors 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)

  26. 0 to3 • Array objects • declare an array 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.drawString(Names[3]);

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

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

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

  30. 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. Call the constructor of its superclass • 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;} }

  32. Call the constructor of its superclass Override its superclass’s method • 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;} }

  33. ballA.xt(2) gives x + ux*t = 0 + 3*2 = 6 ballA.yt(2) gives y + uy*t = 0 + 5*2 = 10 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 • Inheritance of classes (continued) MovingBall ballA = new MovingBall(0,0,3,5,1); FallingBall ballB = new FallingBall(0,0,3,5,-10,1);

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

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

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

  37. 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) )

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

  39. begin point end point Graphics object screen.drawRect(x, y, w, h); / fillRect upper-left corner width height 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.drawPolygon(poly); make a Polygon object / fillPolygon • Various graphics commands screen.drawLine(x1, y1, x2, y2);

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

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

More Related