Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
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? 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 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 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.acity.de/walter.fendt/physengl/physengl.htm 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/Proy ectI.html State University of New York at Stony Brook – http://www.dcc.uchile.cl/~sebrodri/JAVA/Proyecto/Proy ectI.html 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? 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 Use any text editor to edit a source file I prefer Textpad Easily to use clip library Tools for compiling and running Java programs Example: SayHello.java import java.applet.*; import java.awt.*; applet package Abstract Windowing Toolkit 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); } } The paint method does the actual painting job 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> The main class View it by appletviewer C:\YourDir\appletviewer SayHello.html 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 Class and objects Fruit Class (Abstract) Individual objects 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 MyApplet extends Applet { ...... } the main class must be a subclass of Applet class ClassA ......{ ...... } class ClassB ......{ ...... } ...... may have other classes 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”; Make a class called fruit class Fruit { String shape, color; boolean eaten; ...... } One can now create an object of fruit in an applet import java.applet.*; public class HelloToFruit 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 orange.eaten = false; } ...... } HelloFruit.class Fruit.class Compiling 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; } } 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); } } 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); } } 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); } } R1 = new Rect(1,1,2,1); give rectangles of the same size and position Point p1 = new Point(1,1); Point p2 = new Point(3,2); R2 = new Rect(p1,p2); Delcaring methods return type (void return nothing) argument (no argument here) name of method void myMethod() { ........ } Using methods – suppose myMethod() is declared in MyClass the object which calls the method (a MyClass object here) name of method myClassObject.myMethod(); argument (no argument here) Another example return type (a double here) name of method arguments (takes 1 integer and 1 string) double myNewMethod(int t, String s) { ........ return result; } return value (must be a double here) Calling the method – suppose myNewMethod() is declared in MyNewClass the object which calls the method (a MyNewClass object here) name of method an integer a string myNewClassObject.myNewMethod(10, “abc”) a double 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; } 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;} } 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) Using methods (another example) ...... public class MyRect2 extends Applet { ...... ...... R1.equals(R2) ......; ...... 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 (R1) w of the which calls the method argument (R2) 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. 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) Array objects – declare an array 0 to3 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]); Logic and loops – logical operators == – conditional statements != > < >= && <= || 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”; – if - then blocks e.g.3 if (Names[1] == “Doraemon”) { ........ } else if (Names[1] == “Tong Shiu-sing”) { ........ } else { ........ } – for loop ii+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”; ........ } – while loop e.g.5 i = 0; while (Name[i] != “Doraemon”) { i++; } currentString = “Doraemon is at position ” + i; 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 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; } } 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) { 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;} 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) { 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 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 A method calls to the superclass Class A ABC ABC(...) Going up the chain of inheritance until a definition of the method ABC is found ABC ABCB Class Class C Class D object object A method ABC is called A method is overridden by another method of the same name Class A ABC ABC(...) Going up the chain of inheritance, the first reached ABC is executed ABCB Class ABC(...) Class C Class D object object A method ABC is called Calling a overridden method in a superclass Class A ABC(...) Initial method definition super.ABC(...) Class B ABC(...) ABC(...) Method calls ABC in overriden by subclass this class calls ABC in the superclass 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 Graphics coordinate system (0,0) x (20, 20) (60, 60) y Various graphics commands screen.drawLine(x1, y1, x2, y2); Graphics object begin point end point screen.drawRect(x, y, w, h); / fillRect 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.drawPolygon(poly); make a Polygon object / fillPolygon Various graphics commands (continued) screen.drawOval(x, y, w, h); (x, y) / fillOval h w screen.drawArc(x, y, w, h, t1, t2); (x,y) t1 = 90 h / fillArc t2 = 360 w 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); } } Drawing images – Create an object of Image Image myImage; – Load the image into the applet myImage = getImage(getCodeBase(), “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.drawImage(myImage,20,10,this); ...... upper-left corner }