applications of java to physics teaching (part i)

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

Upload: alvin-hess

Post on 30-Dec-2015

44 views

Category:

Documents


0 download

DESCRIPTION

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?. - PowerPoint PPT Presentation

TRANSCRIPT

Applications of Java to Applications of Java to Physics Teaching (Part I)Physics Teaching (Part I)

S S TongS S Tong

Department of PhysicsDepartment of Physics

CUHKCUHK

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.a-city.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/

ProyectI.html

State University of New York at Stony Brook– http://www.dcc.uchile.cl/~sebrodri/JAVA/Proyecto/

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

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

I prefer Textpad Easily to use clip library Tools for compiling and running Java

programs

Use any text editor to edit a source file

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

Compile SayHello.java to SayHello.class

C:\YourDir\javac SayHello.java

SayHello.class

<HTML><BODY><APPLET CODE="SayHello.class" WIDTH=300

HEIGHT=200></APPLET></BODY></HTML>

Include SayHello.class in a HTML file

The main class

View it by appletviewerC:\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)

Individualobjects

import java.applet.*;import ......may have other package

public class MyApplet extends Applet {......

}

class ClassA ......{......

}

class ClassB ......{......

}

...... may have other classes

The source of a Java applet may look like:

the applet packagemust be loaded

the main class mustbe a subclass of Applet

Declare a class called Fruitclass Fruit {

...... }

Attributes are described by instance variablesclass Fruit {

String shape, color;boolean eaten;......

}

Create an object (instance) of FruitFruit orange;orange = new Fruit();

Instance variables of orange can be accessed byorange.shape = “spherical”;

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 fruitclass Fruit {

String shape, color;boolean eaten;......

}

One can now create an object of fruit in an applet

declare the object type

new objectassignattributesto orange

Compiling HelloFruit.class Fruit.class

A constructor helps to defne a objectclass 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 refersto the objectcalling the constructor

Now an object can be created more conveniently:

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 constructorimport 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)

Delcaring methods

void myMethod() { ........

}

return type (void return nothing)

name of method

argument (no argument here)

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)

double myNewMethod(int t, String s) { ........return result;

}

name of method

arguments (takes 1 integer and 1 string)

return type (adouble here)

return value (must be a double here)

Another example

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

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

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

......}

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 theargument (R2)

true or false

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?

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

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

== !=

– 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

Inheritance of classes

Food

Meat Vegetable Fruit

Coldblooded

Warmblooded

Subclasses of Food

Subclassesof Meat

Pork

Beef

ChickenInstances of warm

blooded meat

Inheritance of classes (an example in physics)– Declare a class Ballclass 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) {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 constructorof its superclass

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 constructorof its superclass

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

Going up the chain of inheritance until a definition of the method ABC is found

ABC

A method calls to the superclass

ABC

ABC

A method ABC is called

ABC(...)Class A

Class B

Class C Class D

objectobject

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 B

Class C Class D

objectobject

ABC(...)calls ABC inthis class

Calling a overridden method in a superclass

super.ABC(...)calls ABC inthe superclass

ABC(...)Class A

ABC(...)Class B

Initialmethoddefinition

Method overridenby subclass

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

x

y

(0,0)

(20, 20)

(60, 60)

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);

Various graphics commands

beginpoint

endpoint

Graphicsobject

upper-leftcorner

width height

screen.drawRect(x, y, w, h);

/ fillRect

x, y coordinates of corners

make a Polygon object

Various graphics commands (continued)

screen.drawOval(x, y, w, h);

/ fillOval

screen.drawArc(x, y, w, h, t1, t2);

/ fillArc

(x, y)

w

h

(x,y)

h

w

t2 = 360

t1 = 90

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”);

Image file name

get the directory of the applet (here the applet and the imagefile 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