computional physics

58
8/13/2019 Computional Physics http://slidepdf.com/reader/full/computional-physics 1/58  

Upload: karuna-avatara-dasa

Post on 04-Jun-2018

228 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Computional Physics

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 158

983107983119983117983120983125983124983105983124983113983119983118983105983116 983120983112983129983123983113983107983123

983080983123983123983120983090983089983090983090983081

983107983137983148983139983157983148983137983156983145983151983150 983151983142 983143983154983137983158983145983156983137983156983145983151983150983137983148983142983151983154983139983141 983157983155983145983150983143 983137983154983154983137983161 983158983137983154983145983137983138983148983141

983138983161983105983138983140983086 983115983144983137983149983145983149 983113983155983149983137983145983148

983117983151983144983140 983115983144983137983148983145983140 983115983137983155983149983145983150

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 258

Example of usage of array variable Calculation of gravitational force on an obejct due to many other

neighboring objects Array of m[ ] and r[ ] Summations of F on m[ i ]

y y x xr ˆˆ111 +=

v

y y x xr ˆˆ222 +=

v

( ) ( ) y y y x x xr r r ˆˆ121212 minus+minus=minus=

vvv

( )

( )12

12

y yr

x xr

y

x

minus=

minus=

yr xr r y xˆˆ +=

v

22

y x r r r +=

X

r x

Y

m1 (x1y1)

m2 (x2y2)

m4 (x4y4)

m5 (x5y5)

1r v

2r v

1212 r r r r

vvvv

minus==

mi (xiyi)m3 (x3y3)

r y

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 358

r r

mGmF ˆ

2

2112 =

v

r r r

v

=ˆ is a unit vector

r r

mGmF

v

v

3

2112 =

( ) yr xr r mGmF y x

ˆˆ3

2112 +=

v

yr r

mGm xr

r

mGmF y x

ˆˆ3

21

3

2112

+

=

v

( ) ( ) yr

r r

mGm xr

r r

mGmF y

y x

x

y x

ˆˆ2

32

322

21

22

2112

+

+

+

=

v

( ) ( ) 23

223

223

y x y x p r r r r r r +=+==

21co mGm=

yr r

xr r

F y

p

x

p

ˆco

ˆco

12

+

=

v

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 458

Components of vector

co

12

= x

p

x r r

F

= y

p

y r r

F co

12

Magnitude of 12F v

( ) ( )2

12

2

12 12 y x F F F +=

Direction

12

121tan x

y

F

F minus

For n total number of particles

nF F F F F F net 1151413121

vvvvvv

++++=

i

n

i

F F net 1

2

1

vv

sum=

=

inet x

n

i

x F F 11

2

sum=

= inet y

n

i

y F F 11

2

sum=

=

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 558

Given m1 to m5 and their coordinates are as above Calculate the gravitational force at m1 dueto m2 m3 m4 and m5

m[1]=25 x[1]=12 y[1]=25m[2]=15 x[2]=15 y[2]=15m[3]=75 x[3]=02 y[3]=55m[4]=92 x[4]=52 y[4]=25m[5]=66 x[5]=42 y[5]=45

The algorithm

1 Declare the classmethod to be used2 Declare and assign all variables to be used3 Initialize value to all respective variables4 Decide on which mass the gravitational force to be calculated

(eg on m j where j =1)5 Set f x =0 and f y =06 Start the loop from j =2 to j =5 to calculate the summation of f x and f y 7 Resolve the gravitational force into unit vector (2D Cartesian coordinate)

8 Calculate the mass vector position ( ) ( ) y y y x x xr r r ˆˆ121212 minus+minus=minus=

vvv

Therefore r x = x [j]-x [1] and r y = y [j]-y [1]9 Defineassign all respective equations to be used

X

Y

m1 (1225)

m2 (1515)

m4 (5225)

m5 (4245)

m3 (0255)75 kg

25 kg

15 kg

92 kg

66 kg

21 3 4 5

1

2

3

4

5

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 658

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 758

Source code GravityAttract05ajava

Java class for calculating the resultant force on an object m1due to for other objects m2 m3 m4 and m5

import static javalangMath

public class GravityAttract05a

public static void main(String args[])

double[] m = new double[6] mass of object double[] x = new double[6] x-coor of object double[] y = new double[6] y-coor of object double f resultant forcedouble fxfy x amp y component of force double rx ry rp co Gint j

G = 667e-11m[1]=25 x[1]=12 y[1]=25m[2]=15 x[2]=15 y[2]=15m[3]=75 x[3]=02 y[3]=55m[4]=92 x[4]=52 y[4]=25m[5]=66 x[5]=42 y[5]=45

fx = 00fy = 00

for(j=2 jlt=5 j++)rx = x[j]-x[1]ry = y[j]-y[1]rp = pow((rxrx+ryry)15) (rx^2 + ry^2) raised to the power of 32 co = Gm[1]m[j]fx = fx + corxrpfy = fy + coryrp

f = sqrt(fxfx + fyfy)Systemoutprintf(ntThe force on m[1] due to the other masses is e Nnnf)

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 858

Source code GravityAttract06ajava

Java class for calculating the resultant force on each object(on m1 through m5)

import static javalangMath

public class GravityAttract06a

public static void main(String args[])

double[] m = new double[6] mass of objectdouble[] x = new double[6] x-coor of objectdouble[] y = new double[6] y-coor of objectdouble f resultant forcedouble fxfy x amp y component of forcedouble rx ry rp co G dirfangdirfint j mi

G = 667e-11m[1]=25 x[1]=12 y[1]=25m[2]=15 x[2]=15 y[2]=15m[3]=75 x[3]=02 y[3]=55m[4]=92 x[4]=52 y[4]=25m[5]=66 x[5]=42 y[5]=45

for (mi=1 milt=5 mi++)

fx = 00fy = 00for(j=1 jlt=5 j++)

if (j=mi)rx = x[j]-x[mi]ry = y[j]-y[mi]rp = pow((rxrx+ryry)15) (rx^2 + ry^2) raised to the power of 32co = Gm[mi]m[j]fx = fx + corxrpfy = fy + coryrp

f = sqrt(fxfx + fyfy)dirf = atan(fyfx)angdirf = (dirf(22070))180Systemoutprintf(The force on m[+ mi +] due to the other masses is eN fdegnfangdirf)

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 958

Source code GravityAttract07ajava

Java class for calculating the resultant force on any object(ie either on m1m2 m3 m4 or m5)

import static javalangMathimport javaiopublic class GravityAttract07a

public static void main(String args[])

double[] m = new double[6] mass of objectdouble[] x = new double[6] x-coor of objectdouble[] y = new double[6] y-coor of objectdouble f resultant forcedouble fxfy x amp y component of forcedouble rx ry rp co G dirfangdirfint j

G = 667e-11m[1]=25 x[1]=12 y[1]=25m[2]=15 x[2]=15 y[2]=15m[3]=75 x[3]=02 y[3]=55m[4]=92 x[4]=52 y[4]=25m[5]=66 x[5]=42 y[5]=45BufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin))String mass=Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))

try mass=dataInreadLine()catch(IOException e)Systemoutprintln(Not a valid input value)int mi = IntegerparseInt(mass)fx = 00fy = 00

for(j=1 jlt=5 j++)

if (j=mi)rx = x[j]-x[mi]ry = y[j]-y[mi]rp = pow((rxrx+ryry)15) (rx^2 + ry^2) raised to the power of 32

co = Gm[mi]m[j]fx = fx + corxrpfy = fy + coryrp

f = sqrt(fxfx + fyfy)dirf = toDegrees(atan(fyfx))Systemoutprintf(The force on m[+ mi +] due to the other masses is eN fdegnfdirf)

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1058

983107983119983117983120983125983124983105983124983113983119983118983105983116

983120983112983129983123983113983107983123 983080983123983123983120983090983089983090983090983081

983109983148983141983139983156983154983151983155983156983137983156983145983139 983152983151983156983141983150983156983145983137983148983138983161

983105983138983140983086 983115983144983137983149983145983149 983113983155983149983137983145983148

983117983151983144983140 983115983144983137983148983145983140 983115983137983155983149983145983150

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1158

1 Calculation of electrostatic potential for a group of point charges

You will learn about

- mesh method

a) using a for() loop inside another for() loop

to generate ordered pair (xy) points in a mesh on x-y plane

b) calculate physical value in this case V(xy) forevery point (xy) generated

- saving calculated values into a file

a) making a sequential text file

b) saving in a format that gnuplot can understand

for making a surface plot

- use gnuplot to visualize potential surface

a) making a surface plot from data points in a text file

b) making contour plot to visualize equipotential lines

c) changing the z-axis range to zoom in or zoom out on the surface

d) labelling the plot

e) save as an image the potential surface amp the contour plotf) save as an eps file the potential surface amp the contour plot

- including an eps file into a document

2 Calculation of electrostatic field for a group of point charges

You will learn again

- using mesh method to generate ordered pair (xy) points

in a mesh on x-y plane

You will also learn how to

- calculate vector field values in this case E(xy) for every

generated (xy) mesh point

- save the calculated vector points into a text file

in a format understood by gnuplot

- make a vector field plot using gnuplot

Source code

import static javalangMath

public class estatpot01b

public static void main(String[] args)

double[] q = new double[100]

double[] xq = new double[100]

double[] yq = new double[100]

double V r rx ry k x y

int i n

q[1] = 20e-6 xq[1] = -10 yq[1] = 10

q[2] =-30e-6 xq[2] = 30 yq[2] =-20

q[3] = 30e-6 xq[3] = -20 yq[3] = 40

q[4] =-50e-6 xq[4] = 20 yq[4] = 50

k = 9e9n = 4

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1258

for (x=-5 xlt=5 x = x+02)

for (y=5 ygt=-5 y = y-02)

V = 00

for(i=1 ilt=n i++)

rx = x - xq[i]

ry = y - yq[i]r = sqrt(rxrx+ryry)

if (rlt1e-6)

if (q[i]lt0) V = -10 else V = 10

break

V = V + q[i]r

V = kV

Systemoutprintf(f f fn x y V)

Systemoutprintf(n)

How to do surface plot using Gnuplot

To do a surface plotgnuplotgt splot epotdat with lineTo zoom in and zoom out z axisgnuplotgt set zrange [-2000020000]To convert the graph into imagegnuplotgt set term pngTerminal type set to pngOptions are small color picsize 640 480 Then typegnuplotgt set output epotpngAgain do splot as followgnuplotgt splot epotdat with lineNow the graph is saved into a file in png format named as epotpngTo go back into terminal ouputgnuplotgt set term x11To set to countor plotgnuplotgt set pm3dTo remove countor meshgnuplotgt unset surf

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1358

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1458

983107983119983117983120983125983124983105983124983113983119983118983105983116 983120983112983129983123983113983107983123

983080983123983123983120983090983089983090983090983081

983109983157983148983137983154 983149983141983156983144983151983140

983138983161

983105983138983140983086 983115983144983137983149983145983149 983113983155983149983137983145983148

983117983151983144983140 983115983144983137983148983145983140 983115983137983155983149983145983150

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1558

Free fall body problem

Consider an object thrown upward under gravitational force ( f=mg) with an initial velocity v0The object goes up and then stop at maximum y position ymax before it start to fall downback to the original position at y0 and then to the ground level (See figure)

ymdt

yd m

dt

dvmmaF ampamp====

2

2

t

Y

v1 (t1y1)

v2 (t2y2)

v4 (t4y4)

v5 (t5y5)

vn (tnyn)

v3 (t3y3)

v0 (t0y0)

∆t∆t ∆t ∆t

y1

y0

∆t ∆t

∆yv0

a = g

y0

t0

t0 t1 t2 t3 t4 t5 tn

ymax

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1658

Exact theoretical solution

( ) 2

21

00 at t v yt y ++=

The y projection of the exact solution can be plotted using the following java source code

Java class for solving free fall object under gravity (g = 98 mss)Exact solution y = y0 + v0t + 12at^2

public class freefallsol

public static void main(String args[])double av0yy0deltt

a = -98y0 = 10v0 = 10delt = 00001for(t=0 tlt=05 t=t+delt)

y = y0 + v0t + 05attSystemoutprintf(84f 84fnty)

Complile freefallsoljava to get the plot of the exact solution and then run and save theoutput into a file name as follows

[khamimsetpar65 ~]$ java freefallsol gtffsoldat

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1758

Euler Method

There is another way to see the projection of y position of the object using a numericalmethod called ldquoEuler methodrdquo See the following derivation

t avt v y

t

va

t

yv

∆=∆∆=∆

∆=

∆=

t v y y y y

t avvvv

t v y y y y

t avvvv

t v y y y y

t avvvv

nnnn

nnn

∆+=∆+=

∆+=∆+=

∆+=∆+=

∆+=∆+=

∆+=∆+=

∆+=∆+=

minus

minusminus

1

11

2112

112

1001

001

M

if ∆∆∆∆t rarrrarrrarrrarr 0 then y will close to the exact solution ( ( ) 2

21

00 at t v yt y ++= )

The algorithm 1 Declare the classmethod to be used

2 Declare and assign all variables to be used av0v1yy0y1deltt and j 3 Initialize value to all respective variables a = -98 y0 = 10 v0 = 10

4 Select appropriate value for ∆t

∆t = 00055 Start the loop to calculate the velocity and position for t=0

6 Calculate velocity v1 = v0 + ∆v = v0 + a ∆t

7 Calculate position y1 = y0 + ∆y = y0 + v1 ∆t8 Print out values of t and y1 9 Swap v0 = v1 and y0 = y1

10 Repeat from step 5 and increase the value of t by ∆t (t =t + ∆t) until stoppingcondition is satisfied at t=05

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1858

The java code

Java class for solving free fall object under gravity (g = 98 mss)by numerical method called ldquoEulers Methodrdquo

public class freefalleuler

public static void main(String args[])double av0v1yy0y1delttint j

a = -98y0 = 10v0 = 10delt = 0005

for(t=0 tlt=05 t =t + delt)v1 = v0 + adelty1 = y0 + v1deltSystemoutprintf(84f 84fnty1)v0 = v1y0 = y1

Complile freefalleulerjava for different delt ie 00005 0005 005 and then run and savethe output into a file name as follows

[khamimsetpar65 ~]$ java freefalleuler gtffeuler00005dat[khamimsetpar65 ~]$ java freefalleuler gtffeuler0005dat[khamimsetpar65 ~]$ java freefalleuler gtffeuler005dat

Plotting the results using gnuplot

Using gnuplot plot all the graphs using the following command and compare the results

gnuplotgt plot ffeuler00005dat using 12 title euler delt=00005 with linegt ffeuler0005dat using 12 title euler delt=0005 with linegt ffeuler005dat using 12 title euler deltt=005 with linegt ffsoldat using 12 title exact solution with line

In MS windows

gnuplotgt plot ffeuler00005dat using 12 title euler delt=00005 with lineffeuler0005dat using 12 title euler delt=0005 with lineffeuler005dat using 12 titleeuler delt=005 with line ffsoldat using 12 title exact solution with line

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1958

Gnuplot

The smaller the delt the closer the curve to the exact solution

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2058

983107983119983117983120983125983124983105983124983113983119983118983105983116 983120983112983129983123983113983107983123

983080983123983123983120983090983089983090983090983081

983113983149983152983154983151983158983141983140 983109983157983148983137983154 983149983141983156983144983151983140

983138983161

983105983138983140983086 983115983144983137983149983145983149 983113983155983149983137983145983148

983117983151983144983140 983115983144983137983148983145983140 983115983137983155983149983145983150

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2158

Improved Euler method

Comparison between analytical solution of F=ma with the Euler method shows that although the

method is able to find approximate solution the error is quite significant if computational step is

reduced The error is also cumulative ie as the iteration of the calculation increases the error also gets

bigger hence value furthest away from initial condition has the largest error

The Euler method gives us a way to approximate positions of object with respect to time but requires

an order of magnitude of computational iterations for each order of magnitude of accuracy For large

number of objects this method will certainly consume a great deal of computation time Hence

students are introduced to Initial Half-Step Method an Euler method with a much improved

computational effeciency

Source code

Study of motion of an object with mass m

experiencing a constant force F using

Improved Euler Method (Initial half step method)

In X and Y dimension

import static javalangMath

public class fma04

public static void main(String args[])

double m fx fy x y vo vx vy ax ay t delt

double pi th theta ymax tmax xmax

pi = 40atan(10)

m = 01 in kg

fy = -02 in N (pulling down force)

fx = 00 in N (retarding force)

ay = fym

ax = fxm

vo = 500 in ms

theta = 300 in degrees

th = theta(pi1800) degrees change to radian

vx = vo cos(th) in ms vy = vo sin(th) in ms

y = 12 in m

x = 00 in m

ymax=y initialize var ymax with initial value of y

xmax=00

tmax=00

delt = 001 in s

for(t=00 tlt=300 t=t+delt)

System outprintf(f f fntxy)

if (t==00)vx = vx + 05axdelt

vy = vy + 05aydelt

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2258

else

vx = vx + axdelt

vy = vy + aydelt

x = x + vxdelt

y = y + vydelt

if (ygtymax)

ymax=y

xmax=xtmax=t

if (ylt=00) break

System outprintf( Value of Y maximum is f happened when t = f

nymaxtmax)

System outprintf( at x = fnxmax)

System outprintf( The object reach the ground at x=f and t=fn xt)

gnuplot result

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2358

983107983119983117983120983125983124983105983124983113983119983118983105983116

983120983112983129983123983113983107983123 983080983123983123983120983090983089983090983090983081

983122983141983155983156983151983154983145983150983143 983110983151983154983139983141

983138983161

983105983138983140983086 983115983144983137983149983145983149 983113983155983149983137983145983148

983117983151983144983140 983115983144983137983148983145983140 983115983137983155983149983145983150

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2458

Damped Harmonic Oscillation

Object m is on a flat surface attached to a spring as in figure above The object is pulled

to position A and released so that it oscillates about x =0 Assuming that the force F=ma= -bv-kx where x is position of the object v is the velocity k the spring constant andb is a constant associated with friction with the flat surface

The algorithm

1 Declare the classmethod to be used2 Declare and assign all variables to be used

x t delt k b A m F a v v0 and x03 Declare and assign constants to the respective variables

k = 02 b = 01 A = 01 m = 054 Select appropriate value for ∆t 5 Initialized initial values xo=A vo=00

6 set x=xo v=vo and t = 07 calculate the value of force F = -kx-bv 8 calculate the value of acceleration a = Fm

9 calculate velocity v = vo + ∆v = vo + a ∆t

10 calculate position x = xo + ∆x = xo + v ∆t

11 increment value of t by ∆t 12 print out values of t and x

13 swap the value of xo=x vo=v 14 repeat from step 4 until stopping condition is satisfied (tlt=600)

The java code

public class simplehar01

public static void main(String args[])double x t deltdouble k b A m F a v v0 x0

k = 02b = 01A = 0 1

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2558

m = 05delt = 0001v0 = 00x0 = Ax = x0 v = v0

t = 00while(tlt=600)

F = -kx-bva = Fmv = v0 + adeltx = x0 + vdeltt = t + deltSystemoutprintf(f fntx)x0 = xv0 = v

gnuplot result

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2658

983107983119983117983120983125983124983105983124983113983119983118983105983116 983120983112983129983123983113983107983123

(983123983123983120983090983089983090983090983081

983106983157983145983148983140983145983150983143 983137 983152983144983161983155983145983139983155 983155983151983148983158983141983154

983157983155983145983150983143 983118983141983156983138983141983137983150983155

983138983161

983105983138983140983086 983115983144983137983149983145983149 983113983155983149983137983145983148

983117983151983144983140 983115983144983137983148983145983140 983115983137983155983149983145983150

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2758

How to create a user interface (UI) using Netbeans

Based on the ldquoGravityAttract07ajavardquo source code (shown below) build a user interface usingNetbeans The design of the UI could be look like this (Figure1)

Figure 1

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2858

ldquoGravityAttract07ajavardquo source code

Java class for calculating the resultant force on any object(ie either on m1m2 m3 m4 or m5)

import static javalangMathimport javaiopublic class GravityAttract07a

public static void main(String args[])

double[] m = new double[6] mass of objectdouble[] x = new double[6] x-coor of objectdouble[] y = new double[6] y-coor of objectdouble f resultant forcedouble fxfy x amp y component of forcedouble rx ry rp co G dirfangdirf

int j

G = 667e-11m[1]=25 x[1]=12 y[1]=25m[2]=15 x[2]=15 y[2]=15m[3]=75 x[3]=02 y[3]=55m[4]=92 x[4]=52 y[4]=25m[5]=66 x[5]=42 y[5]=45BufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin))String mass=Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))try mass=dataInreadLine()

catch(IOException e)Systemoutprintln(Not a valid input value)int mi = IntegerparseInt(mass)fx = 00fy = 00

for(j=1 jlt=5 j++)

if (j=mi)rx = x[j]-x[mi]ry = y[j]-y[mi]rp = pow((rxrx+ryry)15) (rx^2 + ry^2) raised to the power of 32co = Gm[mi]m[j]

fx = fx + corxrpfy = fy + coryrp

f = sqrt(fxfx + fyfy)dirf = toDegrees(atan(fyfx))Systemoutprintf(The force on m[+ mi +] due to the other masses is eN fdegnfdirf)

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2958

This example will not show you step by step details procedure since it already explained inthe class and in the e-learning Please refer to the e-learning about how to create a project how tocreate JFrame Form and as well how to create JButton JTextField JLabel JTextPane and so on

First create a new project and give a name eg ldquogravityrdquo and then create JFrame Form andthe following JButton JTextField JLabel JTextPane etc by using ldquoswing paletterdquo as shown in Figure2

Figure 2

To insert a picture into JLabel2 click on the JLabel2 go to properties and select ldquoiconrdquo Onthe icon editor choose file and then select file to locate the directory of your graphicpicture file(Figure 3) Select the file and click OK when finished Make sure you have already created your ownpicture file and save it as jpg png bmp etc format eg ldquogravityjpgrdquo Use any graphic package todraw your diagrampicture or use drawing tools available in Open Office or Microsoft Word

JTextPane1JLabel1

JLabel2

JLabel3JLabel4

JLabel5

JLabel6 JLabel7

JLabel8

JTextField2 JTextField3 JTextField4

JTextF

JButton1JButton2

JButton3

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3058

Figure 3

To write documentation or question eg in JTextPane1 click on the JTextPane1 go toproperties and select ldquotextrdquo Type the documentation in the text editor as shown in Figure 4 Click OKwhen finished

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3158

To do formatting on the text such as superscript and subscript eg in jLabel3 click on the

JLabel3 go to properties and select ldquotextrdquo Type the documentationmessage on the text editor asshown in Figure 5 as follows

eg

lthtmlgtOn which particle (mltsubgt1ltsubgtmltsubgt5ltsubgt) you want to calculate theresultant force

Use sup instead if you want a superscriptThe result will look like this

Click OK when finished

Figure 5

To do the calculation you need to put the coding similar like ldquoGravityAttract07ajavardquo and put itinto the ldquocalculaterdquo button (this is ldquoJButton1rdquo) You can copy ldquoGravityAttract07a javardquo source code

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3258

paste it and then do a few alterations First click on the ldquocalculaterdquo button right click choose Eventsand then choose Action then actionPerformed (Figure 6) It will bring you to where you should insertyour coding (Figure 7) You can write your own coding or copypaste any coding from somewhereelse (in this case from ldquoGravityAttract07ajavardquo)

Figure 6

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3358

Figure 7

Copy ldquoGravityAttract07ajavardquo source code and paste to the place after ldquo TODO add youhandling code hererdquo see Figure 7 To see which part has been modified the coding in Figure 7 hasbeen color coded for better understanding as follows

private void jButton1ActionPerformed(javaawteventActionEvent evt) TODO add your handling code here

double[] m = new double[6] mass of objectdouble[] x = new double[6] x-coor of objectdouble[] y = new double[6] y-coor of objectdouble f resultant forcedouble fxfy x amp y component of forcedouble rx ry rp co G dirfangdirfDecimalFormat dff dfangdirfint j mi

G = 667e-11m[1]=25 x[1]=12 y[1]=25m[2]=15 x[2]=15 y[2]=15m[3]=75 x[3]=02 y[3]=55m[4]=92 x[4]=52 y[4]=25m[5]=66 x[5]=42 y[5]=45

BufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin)) String mass= Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3458

catch(IOException e)Systemoutprintln(Not a valid input value) int mi = IntegerparseInt(mass)mi = IntegerparseInt(jTextField1getText())fx = 00fy = 00for(j=1 jlt=5 j++)

if (j=mi)rx = x[j]-x[mi]ry = y[j]-y[mi]rp = Mathpow((rxrx+ryry)15) (rx^2 + ry^2) raised to the power of 32co = Gm[mi]m[j]fx = fx + corxrpfy = fy + coryrp

f = Mathsqrt(fxfx + fyfy)

dirf = Mathatan(fyfx)angdirf = (dirf(22070))1800 Systemoutprintf(ldquoThe resultant force on particle [ldquo+ mi +rdquo] due to the other masses is

14e N 22f degnrdquofangdirf)dff = new javatextDecimalFormat(ldquoE00rdquo)dfangdirf = new DecimalFormat(ldquo000rdquo)

jTextField2setText(ldquordquo + mi) jTextField3setText(ldquordquo + dffformat(f)) jTextField4setText(ldquordquo + dfangdirfformat(angdirf))

Note

Brown = automatically generatedBlue = the original code from ldquoGravityAttract07ajavardquoGreen = coding from ldquoGravityAttract07ajavardquo which were being disabled due to

incompatibilities with NetbeansRed = a new inserted coding a few of them is to rephrasesubstitute the green one

Javascripts command line useBufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin))String mass=Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))try mass=dataInreadLine()catch(IOException e)Systemoutprintln(Not a valid input value)

int mi = IntegerparseInt(mass)

to read any input from the keyboard typed by the user while Netbeans uses egldquoIntegerparseInt(jTextField1getText())rdquo etc Javascripts command line uses ldquoSystemoutprintfrdquo to format the printed output whileNetbeans uses more complicated way to do the same job (see below)

Javascript command line

Systemoutprintf(The resultant force on particle [+ mi +] due to the other masses is 14e N22f degnfangdirf)

Netbeansdff = new javatextDecimalFormat(ldquo0E00rdquo)df di f D i lF t(ldquo0 00rdquo)

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3558

jTextField2setText(ldquordquo + mi) jTextField3setText(ldquordquo + dffformat(f)) jTextField4setText(ldquordquo + dfangdirfformat(angdirf))

ldquodffrdquo and ldquodfangdirfrdquo are new variables that should be declared and added to format thenumbers into decimal format ie ldquo0E00rdquo to get a scientific format eg ldquo20019E-10rdquo and ldquo000rdquoto get a decimal number eg ldquo-1570rdquo

To use DecimalFormat you have to put ldquoimport javatextDecimalFormatrdquo in the top of thecoding as shown in Figure 8

Figure 8

To reset the calculation you need to put coding into the ldquoresetrdquo button (this is ldquoJButton2rdquo)First click on the ldquoresetrdquo button right click choose Events and then choose Action thenactionPerformed (similar like in Figure 6) It will bring you to where you should insert your coding(Figure 9) Write the following coding (Figure 9)

Figure 9

To exit the program you need to put coding into the ldquoexitrdquo button (this is ldquoJButton3rdquo) Firstclick on the ldquoexitrdquo button right click choose Events and then choose Action then actionPerformed(similar like in Figure 6) It will bring you to where you should insert your coding (Figure 10) Writethe following coding (Figure 10)

Figure 10

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3658

Once you finished now you can run and see the result If everything is fine then the output wilbe look like this (Figure 11)

Figure 11

Remember when you compile and run ldquoGravityAttract07ajavardquo using Javascripts commandline once you compile and run the result was looked like this

On which particle you want to calculate the resulting force 2The resultant force on particle [2] due to the other masses is 30673e-10 N 8837 deg

SEE THE DIFFERENCES AT THE USER INTERFACE

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3758

8th Step

Written by Mohd Khalid Kasmin

Monday 27 August 2007

Programming using command line interface (CLI) has its advantages but todays computer users are so used to graphical user

interface (GUI) that we just can no longer be contented with writing application that do not use the GUI Hence starting from this 8thstep we are going to embark on learning how to make java application that utilises the the GUI

It is possible to write a GUI application with the text based tools that we have been using for these past 7 steps but the job is so

messy that most of you will definitely be turnoff by it So to make life slightly easier well use an integrated development

environment (IDE) known as NetBeans specifically NetBeans 551

0 First ssh to the our server Next once logged in just type nice -n15 netbeans amp on the terminal If you had sshed with the -Y

switch after a few tens of seconds NetBeans should emerge on your display screen

One important point to note Since our server can readily handle up to 100 users (maybe even

more) doing work in CLI mode this machine will certainly be taxed to its maximum capability once

we start using graphical based application NetBeans 551 uses a geat deal of computer memory

and with the 4Gbytes RAM server it can only accomodate up to 25 users concurrently using

NetBeans if the users use the application wisely What is meant by wisely here is that (a) when

starting the NetBeans application please use it together with the nice -n15 command (b) in using

the NetBeans please make sure you only have one project open at a time and (c) please close your

project everytime before quitting the NetBeans

Home Profile Teaching Links Contact Me My Work

Newsflash

Yesterday all servers in the US went out on strike in a bid to get more RAM and better CPUs A spokes person said that the

need for better RAM was due to some fool increasing the front-side bus speed In future busses will be told to slow down in

residential motherboards

search

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3858

1

2

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3958

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4058

[ Back ]

5

Last Updated ( Wednesday 31 October 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4158

8th Step (Cont)

Written by Mohd Khalid Kasmin

Monday 27 August 2007

Up to this point we have define the name of our project Next we construct a class and to follow what we already know we name the class the sam

project name latihan01

6

7

Home Profile Teaching Links Contact Me My Work

Newsflash

Joomla 10 - Experience the Freedom It has never been easier to create your own dynamic site Manage all your content from

the best CMS admin interface

search

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4258

8

9

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4358

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4458

8th Step (Cont still)

Written by Mohd Khalid Kasmin

Monday 27 August 2007

In item (9) we see that Netbeans does not provide us with the main method So to follow what we already know we now put in the

main method

10

Home Profile Teaching Links Contact Me My Work

Newsflash

Aoccdrnig to a rscheearch at an Elingsh uinervtisy it deosnt mttaer in waht oredr the ltteers in a wrod are the olny iprmoetnt

tihng is taht frist and lsat ltteer is at the rghit pclae The rset can be a toatl mses and you can sitll raed it wouthit porbelm Tihs is

bcuseae we do not raed ervey lteter by itslef but the wrod as a wlohe

search

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4558

11

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4658

12

13

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4758

14

15

Last Updated ( Monday 27 August 2007 )

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4858

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4958

9th Step

Thursday 06 September 2007

Step by Step into Java Programming

for

University Physics Student

By

Mohd Khalid Kasmin

A Building graphical user interface - preparing the JFrame Form

At this stage of our programming experience with Java and NetBeans please follow strictly the steps outline in this article First and

make a new GUI project make sure there is no other project that already been loaded by Netbeans Please close any project displayed

1 Create new project

- On the main menu click on File --gt New Project (note Close first any previous project)

- Specify project name amp project location - Untick Create Main Class item

- Click on the Finish button

Home Profile Teaching Links Contact Me My Work

Newsflash

Aoccdrnig to a rscheearch at an Elingsh uinervtisy it deosnt mttaer in waht oredr the ltteers in a wrod are the olny iprmoetnt

tihng is taht frist and lsat ltteer is at the rghit pclae The rset can be a toatl mses and you can sitll raed it wouthit porbelm Tihs is

bcuseae we do not raed ervey lteter by itslef but the wrod as a wlohe

search

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5058

Figure 91

2 Create UI class

- Right click on the project name in the Project pane

on the popup menu select New --gt JFrame Form

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5158

Figure 92

- In the New JFrame Form dialog specify the Class Name and the Package name

eg Class Name multiplierUI Package mymultiplier

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5258

Figure 93

After clicking on the Finish button we should get something like the following

Figure 94

Last Updated ( Tuesday 18 September 2007 )

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5358

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5458

9th Step (Cont)

Written by Mohd Khalid Kasmin

Tuesday 11 September 2007

Step by Step into Java Programming

for University Physics Student

By

Mohd Khalid Kasmin

B Building user interface (UI) - putting in the components

Now that we have a proper blank form we can start filling the form with the suitable graphical components For introduction to

GUI programming an appropriate application to build (for a physics student) is a simple solver So our first exercise is to build an

application that takes two numbers and multiply them and display the result

With GUI we usually give input values to an application via input boxes that are labelled When all inputs are typed in properlywe click on a button to ask the application to execute the job Hence in this exercise well do just that The target now is to

construct a user interface that looks like Figure 95 below

Figure 95

Home Profile Teaching Links Contact Me My Work

Newsflash

Joomla 10 - Experience the Freedom It has never been easier to create your own dynamic site Manage all your content from

the best CMS admin interface

search

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5558

[ Back ]

1 Problemapplication description box

- In the palette pane click on Swing component named JTextPane (Note click just once) Move the mouse pointer on top of

your JFrame (Note just move not drag) When you see a little white box with orange border position it somewhere near the

top part of your JFrame and click once The JTextPane is then attached on the JFrame Addjust its size with the help of the

sizing hook around the perimeter of the JTextPane

- Edit the text property of the JTextPane with the description of your application

2 Input boxes

- In the palette pane click on Swing component named JTextField Next move the mouse pointer on top of your JFrame

When you see a white box with the word jTextField1 position it to the left and under the JTextPane1

- Click on Swing component JTextField in the palette pane again Move the mouse pointer on top of the JFrame you should

get a second JTextField named jTextField2 Position it to the right of jTextField1

Last Updated ( Tuesday 11 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5658

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5758

multiplication if a b and c were first declared as double or float or int variables So the code of the calculation should look like the

following

double a b c

a =

b =

c = a b

Clearly variables a and b need to be assigned with values of typed double Now looking back at inputboxes A and B we made

those boxes using the JTextField component meaning that any data or value written in a JTextField is of typed Text or String even

if the value displayed in the box is ldquo125rdquo But f irst here there is this task of getting to that Text (or String) values ldquo125rdquo and ldquo72rdquo

In Java we can get to a text in JTextField component using the getText() method So to retrieve values in jTextField1 and

jTextField2 we can use

jTextField1getText()

and

jTextField2getText()

But those values are still in text mode and we need another method to convert the data mode f rom text to double For that we can

use the method DoubleparseDouble() Thus the whole process of retrieve text -gt convert text to double -gt assign to double

variable can be done using

a = DoubleparseDouble( jTextField1getText() )

and

b = DoubleparseDouble( jTextField2getText() )

Hence the code for performing the multiplication becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

Up to this point we have the variable c as the result of the multiplication of a and b So the next thing to do is to display this

result on the JLabel that we have already prepared before

A x B = _____

The text A x B = ______ was made using JLabel component and its variable name is jLabel3 Since the variable c is of type

double and text property of JLabel is of type String we need to convert it first to String data type The method to convert a double

to String is DoubletoString() So we can write the code as DoubletoString(c) After getting the text we can now use it to be the

text displayed on jLabel3 To do that we use the method setText() Hence the appropriate code for displaying the result is

jLabel3setText( A x B = + DoubletoString(c) )

And the full code for our calculation becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

jLabel3setText( A x B = + DoubletoString(c) )

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5858

[ Back ]

Last Updated ( Tuesday 18 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

Page 2: Computional Physics

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 258

Example of usage of array variable Calculation of gravitational force on an obejct due to many other

neighboring objects Array of m[ ] and r[ ] Summations of F on m[ i ]

y y x xr ˆˆ111 +=

v

y y x xr ˆˆ222 +=

v

( ) ( ) y y y x x xr r r ˆˆ121212 minus+minus=minus=

vvv

( )

( )12

12

y yr

x xr

y

x

minus=

minus=

yr xr r y xˆˆ +=

v

22

y x r r r +=

X

r x

Y

m1 (x1y1)

m2 (x2y2)

m4 (x4y4)

m5 (x5y5)

1r v

2r v

1212 r r r r

vvvv

minus==

mi (xiyi)m3 (x3y3)

r y

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 358

r r

mGmF ˆ

2

2112 =

v

r r r

v

=ˆ is a unit vector

r r

mGmF

v

v

3

2112 =

( ) yr xr r mGmF y x

ˆˆ3

2112 +=

v

yr r

mGm xr

r

mGmF y x

ˆˆ3

21

3

2112

+

=

v

( ) ( ) yr

r r

mGm xr

r r

mGmF y

y x

x

y x

ˆˆ2

32

322

21

22

2112

+

+

+

=

v

( ) ( ) 23

223

223

y x y x p r r r r r r +=+==

21co mGm=

yr r

xr r

F y

p

x

p

ˆco

ˆco

12

+

=

v

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 458

Components of vector

co

12

= x

p

x r r

F

= y

p

y r r

F co

12

Magnitude of 12F v

( ) ( )2

12

2

12 12 y x F F F +=

Direction

12

121tan x

y

F

F minus

For n total number of particles

nF F F F F F net 1151413121

vvvvvv

++++=

i

n

i

F F net 1

2

1

vv

sum=

=

inet x

n

i

x F F 11

2

sum=

= inet y

n

i

y F F 11

2

sum=

=

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 558

Given m1 to m5 and their coordinates are as above Calculate the gravitational force at m1 dueto m2 m3 m4 and m5

m[1]=25 x[1]=12 y[1]=25m[2]=15 x[2]=15 y[2]=15m[3]=75 x[3]=02 y[3]=55m[4]=92 x[4]=52 y[4]=25m[5]=66 x[5]=42 y[5]=45

The algorithm

1 Declare the classmethod to be used2 Declare and assign all variables to be used3 Initialize value to all respective variables4 Decide on which mass the gravitational force to be calculated

(eg on m j where j =1)5 Set f x =0 and f y =06 Start the loop from j =2 to j =5 to calculate the summation of f x and f y 7 Resolve the gravitational force into unit vector (2D Cartesian coordinate)

8 Calculate the mass vector position ( ) ( ) y y y x x xr r r ˆˆ121212 minus+minus=minus=

vvv

Therefore r x = x [j]-x [1] and r y = y [j]-y [1]9 Defineassign all respective equations to be used

X

Y

m1 (1225)

m2 (1515)

m4 (5225)

m5 (4245)

m3 (0255)75 kg

25 kg

15 kg

92 kg

66 kg

21 3 4 5

1

2

3

4

5

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 658

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 758

Source code GravityAttract05ajava

Java class for calculating the resultant force on an object m1due to for other objects m2 m3 m4 and m5

import static javalangMath

public class GravityAttract05a

public static void main(String args[])

double[] m = new double[6] mass of object double[] x = new double[6] x-coor of object double[] y = new double[6] y-coor of object double f resultant forcedouble fxfy x amp y component of force double rx ry rp co Gint j

G = 667e-11m[1]=25 x[1]=12 y[1]=25m[2]=15 x[2]=15 y[2]=15m[3]=75 x[3]=02 y[3]=55m[4]=92 x[4]=52 y[4]=25m[5]=66 x[5]=42 y[5]=45

fx = 00fy = 00

for(j=2 jlt=5 j++)rx = x[j]-x[1]ry = y[j]-y[1]rp = pow((rxrx+ryry)15) (rx^2 + ry^2) raised to the power of 32 co = Gm[1]m[j]fx = fx + corxrpfy = fy + coryrp

f = sqrt(fxfx + fyfy)Systemoutprintf(ntThe force on m[1] due to the other masses is e Nnnf)

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 858

Source code GravityAttract06ajava

Java class for calculating the resultant force on each object(on m1 through m5)

import static javalangMath

public class GravityAttract06a

public static void main(String args[])

double[] m = new double[6] mass of objectdouble[] x = new double[6] x-coor of objectdouble[] y = new double[6] y-coor of objectdouble f resultant forcedouble fxfy x amp y component of forcedouble rx ry rp co G dirfangdirfint j mi

G = 667e-11m[1]=25 x[1]=12 y[1]=25m[2]=15 x[2]=15 y[2]=15m[3]=75 x[3]=02 y[3]=55m[4]=92 x[4]=52 y[4]=25m[5]=66 x[5]=42 y[5]=45

for (mi=1 milt=5 mi++)

fx = 00fy = 00for(j=1 jlt=5 j++)

if (j=mi)rx = x[j]-x[mi]ry = y[j]-y[mi]rp = pow((rxrx+ryry)15) (rx^2 + ry^2) raised to the power of 32co = Gm[mi]m[j]fx = fx + corxrpfy = fy + coryrp

f = sqrt(fxfx + fyfy)dirf = atan(fyfx)angdirf = (dirf(22070))180Systemoutprintf(The force on m[+ mi +] due to the other masses is eN fdegnfangdirf)

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 958

Source code GravityAttract07ajava

Java class for calculating the resultant force on any object(ie either on m1m2 m3 m4 or m5)

import static javalangMathimport javaiopublic class GravityAttract07a

public static void main(String args[])

double[] m = new double[6] mass of objectdouble[] x = new double[6] x-coor of objectdouble[] y = new double[6] y-coor of objectdouble f resultant forcedouble fxfy x amp y component of forcedouble rx ry rp co G dirfangdirfint j

G = 667e-11m[1]=25 x[1]=12 y[1]=25m[2]=15 x[2]=15 y[2]=15m[3]=75 x[3]=02 y[3]=55m[4]=92 x[4]=52 y[4]=25m[5]=66 x[5]=42 y[5]=45BufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin))String mass=Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))

try mass=dataInreadLine()catch(IOException e)Systemoutprintln(Not a valid input value)int mi = IntegerparseInt(mass)fx = 00fy = 00

for(j=1 jlt=5 j++)

if (j=mi)rx = x[j]-x[mi]ry = y[j]-y[mi]rp = pow((rxrx+ryry)15) (rx^2 + ry^2) raised to the power of 32

co = Gm[mi]m[j]fx = fx + corxrpfy = fy + coryrp

f = sqrt(fxfx + fyfy)dirf = toDegrees(atan(fyfx))Systemoutprintf(The force on m[+ mi +] due to the other masses is eN fdegnfdirf)

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1058

983107983119983117983120983125983124983105983124983113983119983118983105983116

983120983112983129983123983113983107983123 983080983123983123983120983090983089983090983090983081

983109983148983141983139983156983154983151983155983156983137983156983145983139 983152983151983156983141983150983156983145983137983148983138983161

983105983138983140983086 983115983144983137983149983145983149 983113983155983149983137983145983148

983117983151983144983140 983115983144983137983148983145983140 983115983137983155983149983145983150

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1158

1 Calculation of electrostatic potential for a group of point charges

You will learn about

- mesh method

a) using a for() loop inside another for() loop

to generate ordered pair (xy) points in a mesh on x-y plane

b) calculate physical value in this case V(xy) forevery point (xy) generated

- saving calculated values into a file

a) making a sequential text file

b) saving in a format that gnuplot can understand

for making a surface plot

- use gnuplot to visualize potential surface

a) making a surface plot from data points in a text file

b) making contour plot to visualize equipotential lines

c) changing the z-axis range to zoom in or zoom out on the surface

d) labelling the plot

e) save as an image the potential surface amp the contour plotf) save as an eps file the potential surface amp the contour plot

- including an eps file into a document

2 Calculation of electrostatic field for a group of point charges

You will learn again

- using mesh method to generate ordered pair (xy) points

in a mesh on x-y plane

You will also learn how to

- calculate vector field values in this case E(xy) for every

generated (xy) mesh point

- save the calculated vector points into a text file

in a format understood by gnuplot

- make a vector field plot using gnuplot

Source code

import static javalangMath

public class estatpot01b

public static void main(String[] args)

double[] q = new double[100]

double[] xq = new double[100]

double[] yq = new double[100]

double V r rx ry k x y

int i n

q[1] = 20e-6 xq[1] = -10 yq[1] = 10

q[2] =-30e-6 xq[2] = 30 yq[2] =-20

q[3] = 30e-6 xq[3] = -20 yq[3] = 40

q[4] =-50e-6 xq[4] = 20 yq[4] = 50

k = 9e9n = 4

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1258

for (x=-5 xlt=5 x = x+02)

for (y=5 ygt=-5 y = y-02)

V = 00

for(i=1 ilt=n i++)

rx = x - xq[i]

ry = y - yq[i]r = sqrt(rxrx+ryry)

if (rlt1e-6)

if (q[i]lt0) V = -10 else V = 10

break

V = V + q[i]r

V = kV

Systemoutprintf(f f fn x y V)

Systemoutprintf(n)

How to do surface plot using Gnuplot

To do a surface plotgnuplotgt splot epotdat with lineTo zoom in and zoom out z axisgnuplotgt set zrange [-2000020000]To convert the graph into imagegnuplotgt set term pngTerminal type set to pngOptions are small color picsize 640 480 Then typegnuplotgt set output epotpngAgain do splot as followgnuplotgt splot epotdat with lineNow the graph is saved into a file in png format named as epotpngTo go back into terminal ouputgnuplotgt set term x11To set to countor plotgnuplotgt set pm3dTo remove countor meshgnuplotgt unset surf

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1358

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1458

983107983119983117983120983125983124983105983124983113983119983118983105983116 983120983112983129983123983113983107983123

983080983123983123983120983090983089983090983090983081

983109983157983148983137983154 983149983141983156983144983151983140

983138983161

983105983138983140983086 983115983144983137983149983145983149 983113983155983149983137983145983148

983117983151983144983140 983115983144983137983148983145983140 983115983137983155983149983145983150

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1558

Free fall body problem

Consider an object thrown upward under gravitational force ( f=mg) with an initial velocity v0The object goes up and then stop at maximum y position ymax before it start to fall downback to the original position at y0 and then to the ground level (See figure)

ymdt

yd m

dt

dvmmaF ampamp====

2

2

t

Y

v1 (t1y1)

v2 (t2y2)

v4 (t4y4)

v5 (t5y5)

vn (tnyn)

v3 (t3y3)

v0 (t0y0)

∆t∆t ∆t ∆t

y1

y0

∆t ∆t

∆yv0

a = g

y0

t0

t0 t1 t2 t3 t4 t5 tn

ymax

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1658

Exact theoretical solution

( ) 2

21

00 at t v yt y ++=

The y projection of the exact solution can be plotted using the following java source code

Java class for solving free fall object under gravity (g = 98 mss)Exact solution y = y0 + v0t + 12at^2

public class freefallsol

public static void main(String args[])double av0yy0deltt

a = -98y0 = 10v0 = 10delt = 00001for(t=0 tlt=05 t=t+delt)

y = y0 + v0t + 05attSystemoutprintf(84f 84fnty)

Complile freefallsoljava to get the plot of the exact solution and then run and save theoutput into a file name as follows

[khamimsetpar65 ~]$ java freefallsol gtffsoldat

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1758

Euler Method

There is another way to see the projection of y position of the object using a numericalmethod called ldquoEuler methodrdquo See the following derivation

t avt v y

t

va

t

yv

∆=∆∆=∆

∆=

∆=

t v y y y y

t avvvv

t v y y y y

t avvvv

t v y y y y

t avvvv

nnnn

nnn

∆+=∆+=

∆+=∆+=

∆+=∆+=

∆+=∆+=

∆+=∆+=

∆+=∆+=

minus

minusminus

1

11

2112

112

1001

001

M

if ∆∆∆∆t rarrrarrrarrrarr 0 then y will close to the exact solution ( ( ) 2

21

00 at t v yt y ++= )

The algorithm 1 Declare the classmethod to be used

2 Declare and assign all variables to be used av0v1yy0y1deltt and j 3 Initialize value to all respective variables a = -98 y0 = 10 v0 = 10

4 Select appropriate value for ∆t

∆t = 00055 Start the loop to calculate the velocity and position for t=0

6 Calculate velocity v1 = v0 + ∆v = v0 + a ∆t

7 Calculate position y1 = y0 + ∆y = y0 + v1 ∆t8 Print out values of t and y1 9 Swap v0 = v1 and y0 = y1

10 Repeat from step 5 and increase the value of t by ∆t (t =t + ∆t) until stoppingcondition is satisfied at t=05

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1858

The java code

Java class for solving free fall object under gravity (g = 98 mss)by numerical method called ldquoEulers Methodrdquo

public class freefalleuler

public static void main(String args[])double av0v1yy0y1delttint j

a = -98y0 = 10v0 = 10delt = 0005

for(t=0 tlt=05 t =t + delt)v1 = v0 + adelty1 = y0 + v1deltSystemoutprintf(84f 84fnty1)v0 = v1y0 = y1

Complile freefalleulerjava for different delt ie 00005 0005 005 and then run and savethe output into a file name as follows

[khamimsetpar65 ~]$ java freefalleuler gtffeuler00005dat[khamimsetpar65 ~]$ java freefalleuler gtffeuler0005dat[khamimsetpar65 ~]$ java freefalleuler gtffeuler005dat

Plotting the results using gnuplot

Using gnuplot plot all the graphs using the following command and compare the results

gnuplotgt plot ffeuler00005dat using 12 title euler delt=00005 with linegt ffeuler0005dat using 12 title euler delt=0005 with linegt ffeuler005dat using 12 title euler deltt=005 with linegt ffsoldat using 12 title exact solution with line

In MS windows

gnuplotgt plot ffeuler00005dat using 12 title euler delt=00005 with lineffeuler0005dat using 12 title euler delt=0005 with lineffeuler005dat using 12 titleeuler delt=005 with line ffsoldat using 12 title exact solution with line

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1958

Gnuplot

The smaller the delt the closer the curve to the exact solution

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2058

983107983119983117983120983125983124983105983124983113983119983118983105983116 983120983112983129983123983113983107983123

983080983123983123983120983090983089983090983090983081

983113983149983152983154983151983158983141983140 983109983157983148983137983154 983149983141983156983144983151983140

983138983161

983105983138983140983086 983115983144983137983149983145983149 983113983155983149983137983145983148

983117983151983144983140 983115983144983137983148983145983140 983115983137983155983149983145983150

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2158

Improved Euler method

Comparison between analytical solution of F=ma with the Euler method shows that although the

method is able to find approximate solution the error is quite significant if computational step is

reduced The error is also cumulative ie as the iteration of the calculation increases the error also gets

bigger hence value furthest away from initial condition has the largest error

The Euler method gives us a way to approximate positions of object with respect to time but requires

an order of magnitude of computational iterations for each order of magnitude of accuracy For large

number of objects this method will certainly consume a great deal of computation time Hence

students are introduced to Initial Half-Step Method an Euler method with a much improved

computational effeciency

Source code

Study of motion of an object with mass m

experiencing a constant force F using

Improved Euler Method (Initial half step method)

In X and Y dimension

import static javalangMath

public class fma04

public static void main(String args[])

double m fx fy x y vo vx vy ax ay t delt

double pi th theta ymax tmax xmax

pi = 40atan(10)

m = 01 in kg

fy = -02 in N (pulling down force)

fx = 00 in N (retarding force)

ay = fym

ax = fxm

vo = 500 in ms

theta = 300 in degrees

th = theta(pi1800) degrees change to radian

vx = vo cos(th) in ms vy = vo sin(th) in ms

y = 12 in m

x = 00 in m

ymax=y initialize var ymax with initial value of y

xmax=00

tmax=00

delt = 001 in s

for(t=00 tlt=300 t=t+delt)

System outprintf(f f fntxy)

if (t==00)vx = vx + 05axdelt

vy = vy + 05aydelt

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2258

else

vx = vx + axdelt

vy = vy + aydelt

x = x + vxdelt

y = y + vydelt

if (ygtymax)

ymax=y

xmax=xtmax=t

if (ylt=00) break

System outprintf( Value of Y maximum is f happened when t = f

nymaxtmax)

System outprintf( at x = fnxmax)

System outprintf( The object reach the ground at x=f and t=fn xt)

gnuplot result

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2358

983107983119983117983120983125983124983105983124983113983119983118983105983116

983120983112983129983123983113983107983123 983080983123983123983120983090983089983090983090983081

983122983141983155983156983151983154983145983150983143 983110983151983154983139983141

983138983161

983105983138983140983086 983115983144983137983149983145983149 983113983155983149983137983145983148

983117983151983144983140 983115983144983137983148983145983140 983115983137983155983149983145983150

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2458

Damped Harmonic Oscillation

Object m is on a flat surface attached to a spring as in figure above The object is pulled

to position A and released so that it oscillates about x =0 Assuming that the force F=ma= -bv-kx where x is position of the object v is the velocity k the spring constant andb is a constant associated with friction with the flat surface

The algorithm

1 Declare the classmethod to be used2 Declare and assign all variables to be used

x t delt k b A m F a v v0 and x03 Declare and assign constants to the respective variables

k = 02 b = 01 A = 01 m = 054 Select appropriate value for ∆t 5 Initialized initial values xo=A vo=00

6 set x=xo v=vo and t = 07 calculate the value of force F = -kx-bv 8 calculate the value of acceleration a = Fm

9 calculate velocity v = vo + ∆v = vo + a ∆t

10 calculate position x = xo + ∆x = xo + v ∆t

11 increment value of t by ∆t 12 print out values of t and x

13 swap the value of xo=x vo=v 14 repeat from step 4 until stopping condition is satisfied (tlt=600)

The java code

public class simplehar01

public static void main(String args[])double x t deltdouble k b A m F a v v0 x0

k = 02b = 01A = 0 1

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2558

m = 05delt = 0001v0 = 00x0 = Ax = x0 v = v0

t = 00while(tlt=600)

F = -kx-bva = Fmv = v0 + adeltx = x0 + vdeltt = t + deltSystemoutprintf(f fntx)x0 = xv0 = v

gnuplot result

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2658

983107983119983117983120983125983124983105983124983113983119983118983105983116 983120983112983129983123983113983107983123

(983123983123983120983090983089983090983090983081

983106983157983145983148983140983145983150983143 983137 983152983144983161983155983145983139983155 983155983151983148983158983141983154

983157983155983145983150983143 983118983141983156983138983141983137983150983155

983138983161

983105983138983140983086 983115983144983137983149983145983149 983113983155983149983137983145983148

983117983151983144983140 983115983144983137983148983145983140 983115983137983155983149983145983150

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2758

How to create a user interface (UI) using Netbeans

Based on the ldquoGravityAttract07ajavardquo source code (shown below) build a user interface usingNetbeans The design of the UI could be look like this (Figure1)

Figure 1

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2858

ldquoGravityAttract07ajavardquo source code

Java class for calculating the resultant force on any object(ie either on m1m2 m3 m4 or m5)

import static javalangMathimport javaiopublic class GravityAttract07a

public static void main(String args[])

double[] m = new double[6] mass of objectdouble[] x = new double[6] x-coor of objectdouble[] y = new double[6] y-coor of objectdouble f resultant forcedouble fxfy x amp y component of forcedouble rx ry rp co G dirfangdirf

int j

G = 667e-11m[1]=25 x[1]=12 y[1]=25m[2]=15 x[2]=15 y[2]=15m[3]=75 x[3]=02 y[3]=55m[4]=92 x[4]=52 y[4]=25m[5]=66 x[5]=42 y[5]=45BufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin))String mass=Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))try mass=dataInreadLine()

catch(IOException e)Systemoutprintln(Not a valid input value)int mi = IntegerparseInt(mass)fx = 00fy = 00

for(j=1 jlt=5 j++)

if (j=mi)rx = x[j]-x[mi]ry = y[j]-y[mi]rp = pow((rxrx+ryry)15) (rx^2 + ry^2) raised to the power of 32co = Gm[mi]m[j]

fx = fx + corxrpfy = fy + coryrp

f = sqrt(fxfx + fyfy)dirf = toDegrees(atan(fyfx))Systemoutprintf(The force on m[+ mi +] due to the other masses is eN fdegnfdirf)

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2958

This example will not show you step by step details procedure since it already explained inthe class and in the e-learning Please refer to the e-learning about how to create a project how tocreate JFrame Form and as well how to create JButton JTextField JLabel JTextPane and so on

First create a new project and give a name eg ldquogravityrdquo and then create JFrame Form andthe following JButton JTextField JLabel JTextPane etc by using ldquoswing paletterdquo as shown in Figure2

Figure 2

To insert a picture into JLabel2 click on the JLabel2 go to properties and select ldquoiconrdquo Onthe icon editor choose file and then select file to locate the directory of your graphicpicture file(Figure 3) Select the file and click OK when finished Make sure you have already created your ownpicture file and save it as jpg png bmp etc format eg ldquogravityjpgrdquo Use any graphic package todraw your diagrampicture or use drawing tools available in Open Office or Microsoft Word

JTextPane1JLabel1

JLabel2

JLabel3JLabel4

JLabel5

JLabel6 JLabel7

JLabel8

JTextField2 JTextField3 JTextField4

JTextF

JButton1JButton2

JButton3

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3058

Figure 3

To write documentation or question eg in JTextPane1 click on the JTextPane1 go toproperties and select ldquotextrdquo Type the documentation in the text editor as shown in Figure 4 Click OKwhen finished

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3158

To do formatting on the text such as superscript and subscript eg in jLabel3 click on the

JLabel3 go to properties and select ldquotextrdquo Type the documentationmessage on the text editor asshown in Figure 5 as follows

eg

lthtmlgtOn which particle (mltsubgt1ltsubgtmltsubgt5ltsubgt) you want to calculate theresultant force

Use sup instead if you want a superscriptThe result will look like this

Click OK when finished

Figure 5

To do the calculation you need to put the coding similar like ldquoGravityAttract07ajavardquo and put itinto the ldquocalculaterdquo button (this is ldquoJButton1rdquo) You can copy ldquoGravityAttract07a javardquo source code

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3258

paste it and then do a few alterations First click on the ldquocalculaterdquo button right click choose Eventsand then choose Action then actionPerformed (Figure 6) It will bring you to where you should insertyour coding (Figure 7) You can write your own coding or copypaste any coding from somewhereelse (in this case from ldquoGravityAttract07ajavardquo)

Figure 6

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3358

Figure 7

Copy ldquoGravityAttract07ajavardquo source code and paste to the place after ldquo TODO add youhandling code hererdquo see Figure 7 To see which part has been modified the coding in Figure 7 hasbeen color coded for better understanding as follows

private void jButton1ActionPerformed(javaawteventActionEvent evt) TODO add your handling code here

double[] m = new double[6] mass of objectdouble[] x = new double[6] x-coor of objectdouble[] y = new double[6] y-coor of objectdouble f resultant forcedouble fxfy x amp y component of forcedouble rx ry rp co G dirfangdirfDecimalFormat dff dfangdirfint j mi

G = 667e-11m[1]=25 x[1]=12 y[1]=25m[2]=15 x[2]=15 y[2]=15m[3]=75 x[3]=02 y[3]=55m[4]=92 x[4]=52 y[4]=25m[5]=66 x[5]=42 y[5]=45

BufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin)) String mass= Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3458

catch(IOException e)Systemoutprintln(Not a valid input value) int mi = IntegerparseInt(mass)mi = IntegerparseInt(jTextField1getText())fx = 00fy = 00for(j=1 jlt=5 j++)

if (j=mi)rx = x[j]-x[mi]ry = y[j]-y[mi]rp = Mathpow((rxrx+ryry)15) (rx^2 + ry^2) raised to the power of 32co = Gm[mi]m[j]fx = fx + corxrpfy = fy + coryrp

f = Mathsqrt(fxfx + fyfy)

dirf = Mathatan(fyfx)angdirf = (dirf(22070))1800 Systemoutprintf(ldquoThe resultant force on particle [ldquo+ mi +rdquo] due to the other masses is

14e N 22f degnrdquofangdirf)dff = new javatextDecimalFormat(ldquoE00rdquo)dfangdirf = new DecimalFormat(ldquo000rdquo)

jTextField2setText(ldquordquo + mi) jTextField3setText(ldquordquo + dffformat(f)) jTextField4setText(ldquordquo + dfangdirfformat(angdirf))

Note

Brown = automatically generatedBlue = the original code from ldquoGravityAttract07ajavardquoGreen = coding from ldquoGravityAttract07ajavardquo which were being disabled due to

incompatibilities with NetbeansRed = a new inserted coding a few of them is to rephrasesubstitute the green one

Javascripts command line useBufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin))String mass=Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))try mass=dataInreadLine()catch(IOException e)Systemoutprintln(Not a valid input value)

int mi = IntegerparseInt(mass)

to read any input from the keyboard typed by the user while Netbeans uses egldquoIntegerparseInt(jTextField1getText())rdquo etc Javascripts command line uses ldquoSystemoutprintfrdquo to format the printed output whileNetbeans uses more complicated way to do the same job (see below)

Javascript command line

Systemoutprintf(The resultant force on particle [+ mi +] due to the other masses is 14e N22f degnfangdirf)

Netbeansdff = new javatextDecimalFormat(ldquo0E00rdquo)df di f D i lF t(ldquo0 00rdquo)

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3558

jTextField2setText(ldquordquo + mi) jTextField3setText(ldquordquo + dffformat(f)) jTextField4setText(ldquordquo + dfangdirfformat(angdirf))

ldquodffrdquo and ldquodfangdirfrdquo are new variables that should be declared and added to format thenumbers into decimal format ie ldquo0E00rdquo to get a scientific format eg ldquo20019E-10rdquo and ldquo000rdquoto get a decimal number eg ldquo-1570rdquo

To use DecimalFormat you have to put ldquoimport javatextDecimalFormatrdquo in the top of thecoding as shown in Figure 8

Figure 8

To reset the calculation you need to put coding into the ldquoresetrdquo button (this is ldquoJButton2rdquo)First click on the ldquoresetrdquo button right click choose Events and then choose Action thenactionPerformed (similar like in Figure 6) It will bring you to where you should insert your coding(Figure 9) Write the following coding (Figure 9)

Figure 9

To exit the program you need to put coding into the ldquoexitrdquo button (this is ldquoJButton3rdquo) Firstclick on the ldquoexitrdquo button right click choose Events and then choose Action then actionPerformed(similar like in Figure 6) It will bring you to where you should insert your coding (Figure 10) Writethe following coding (Figure 10)

Figure 10

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3658

Once you finished now you can run and see the result If everything is fine then the output wilbe look like this (Figure 11)

Figure 11

Remember when you compile and run ldquoGravityAttract07ajavardquo using Javascripts commandline once you compile and run the result was looked like this

On which particle you want to calculate the resulting force 2The resultant force on particle [2] due to the other masses is 30673e-10 N 8837 deg

SEE THE DIFFERENCES AT THE USER INTERFACE

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3758

8th Step

Written by Mohd Khalid Kasmin

Monday 27 August 2007

Programming using command line interface (CLI) has its advantages but todays computer users are so used to graphical user

interface (GUI) that we just can no longer be contented with writing application that do not use the GUI Hence starting from this 8thstep we are going to embark on learning how to make java application that utilises the the GUI

It is possible to write a GUI application with the text based tools that we have been using for these past 7 steps but the job is so

messy that most of you will definitely be turnoff by it So to make life slightly easier well use an integrated development

environment (IDE) known as NetBeans specifically NetBeans 551

0 First ssh to the our server Next once logged in just type nice -n15 netbeans amp on the terminal If you had sshed with the -Y

switch after a few tens of seconds NetBeans should emerge on your display screen

One important point to note Since our server can readily handle up to 100 users (maybe even

more) doing work in CLI mode this machine will certainly be taxed to its maximum capability once

we start using graphical based application NetBeans 551 uses a geat deal of computer memory

and with the 4Gbytes RAM server it can only accomodate up to 25 users concurrently using

NetBeans if the users use the application wisely What is meant by wisely here is that (a) when

starting the NetBeans application please use it together with the nice -n15 command (b) in using

the NetBeans please make sure you only have one project open at a time and (c) please close your

project everytime before quitting the NetBeans

Home Profile Teaching Links Contact Me My Work

Newsflash

Yesterday all servers in the US went out on strike in a bid to get more RAM and better CPUs A spokes person said that the

need for better RAM was due to some fool increasing the front-side bus speed In future busses will be told to slow down in

residential motherboards

search

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3858

1

2

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3958

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4058

[ Back ]

5

Last Updated ( Wednesday 31 October 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4158

8th Step (Cont)

Written by Mohd Khalid Kasmin

Monday 27 August 2007

Up to this point we have define the name of our project Next we construct a class and to follow what we already know we name the class the sam

project name latihan01

6

7

Home Profile Teaching Links Contact Me My Work

Newsflash

Joomla 10 - Experience the Freedom It has never been easier to create your own dynamic site Manage all your content from

the best CMS admin interface

search

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4258

8

9

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4358

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4458

8th Step (Cont still)

Written by Mohd Khalid Kasmin

Monday 27 August 2007

In item (9) we see that Netbeans does not provide us with the main method So to follow what we already know we now put in the

main method

10

Home Profile Teaching Links Contact Me My Work

Newsflash

Aoccdrnig to a rscheearch at an Elingsh uinervtisy it deosnt mttaer in waht oredr the ltteers in a wrod are the olny iprmoetnt

tihng is taht frist and lsat ltteer is at the rghit pclae The rset can be a toatl mses and you can sitll raed it wouthit porbelm Tihs is

bcuseae we do not raed ervey lteter by itslef but the wrod as a wlohe

search

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4558

11

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4658

12

13

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4758

14

15

Last Updated ( Monday 27 August 2007 )

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4858

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4958

9th Step

Thursday 06 September 2007

Step by Step into Java Programming

for

University Physics Student

By

Mohd Khalid Kasmin

A Building graphical user interface - preparing the JFrame Form

At this stage of our programming experience with Java and NetBeans please follow strictly the steps outline in this article First and

make a new GUI project make sure there is no other project that already been loaded by Netbeans Please close any project displayed

1 Create new project

- On the main menu click on File --gt New Project (note Close first any previous project)

- Specify project name amp project location - Untick Create Main Class item

- Click on the Finish button

Home Profile Teaching Links Contact Me My Work

Newsflash

Aoccdrnig to a rscheearch at an Elingsh uinervtisy it deosnt mttaer in waht oredr the ltteers in a wrod are the olny iprmoetnt

tihng is taht frist and lsat ltteer is at the rghit pclae The rset can be a toatl mses and you can sitll raed it wouthit porbelm Tihs is

bcuseae we do not raed ervey lteter by itslef but the wrod as a wlohe

search

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5058

Figure 91

2 Create UI class

- Right click on the project name in the Project pane

on the popup menu select New --gt JFrame Form

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5158

Figure 92

- In the New JFrame Form dialog specify the Class Name and the Package name

eg Class Name multiplierUI Package mymultiplier

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5258

Figure 93

After clicking on the Finish button we should get something like the following

Figure 94

Last Updated ( Tuesday 18 September 2007 )

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5358

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5458

9th Step (Cont)

Written by Mohd Khalid Kasmin

Tuesday 11 September 2007

Step by Step into Java Programming

for University Physics Student

By

Mohd Khalid Kasmin

B Building user interface (UI) - putting in the components

Now that we have a proper blank form we can start filling the form with the suitable graphical components For introduction to

GUI programming an appropriate application to build (for a physics student) is a simple solver So our first exercise is to build an

application that takes two numbers and multiply them and display the result

With GUI we usually give input values to an application via input boxes that are labelled When all inputs are typed in properlywe click on a button to ask the application to execute the job Hence in this exercise well do just that The target now is to

construct a user interface that looks like Figure 95 below

Figure 95

Home Profile Teaching Links Contact Me My Work

Newsflash

Joomla 10 - Experience the Freedom It has never been easier to create your own dynamic site Manage all your content from

the best CMS admin interface

search

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5558

[ Back ]

1 Problemapplication description box

- In the palette pane click on Swing component named JTextPane (Note click just once) Move the mouse pointer on top of

your JFrame (Note just move not drag) When you see a little white box with orange border position it somewhere near the

top part of your JFrame and click once The JTextPane is then attached on the JFrame Addjust its size with the help of the

sizing hook around the perimeter of the JTextPane

- Edit the text property of the JTextPane with the description of your application

2 Input boxes

- In the palette pane click on Swing component named JTextField Next move the mouse pointer on top of your JFrame

When you see a white box with the word jTextField1 position it to the left and under the JTextPane1

- Click on Swing component JTextField in the palette pane again Move the mouse pointer on top of the JFrame you should

get a second JTextField named jTextField2 Position it to the right of jTextField1

Last Updated ( Tuesday 11 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5658

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5758

multiplication if a b and c were first declared as double or float or int variables So the code of the calculation should look like the

following

double a b c

a =

b =

c = a b

Clearly variables a and b need to be assigned with values of typed double Now looking back at inputboxes A and B we made

those boxes using the JTextField component meaning that any data or value written in a JTextField is of typed Text or String even

if the value displayed in the box is ldquo125rdquo But f irst here there is this task of getting to that Text (or String) values ldquo125rdquo and ldquo72rdquo

In Java we can get to a text in JTextField component using the getText() method So to retrieve values in jTextField1 and

jTextField2 we can use

jTextField1getText()

and

jTextField2getText()

But those values are still in text mode and we need another method to convert the data mode f rom text to double For that we can

use the method DoubleparseDouble() Thus the whole process of retrieve text -gt convert text to double -gt assign to double

variable can be done using

a = DoubleparseDouble( jTextField1getText() )

and

b = DoubleparseDouble( jTextField2getText() )

Hence the code for performing the multiplication becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

Up to this point we have the variable c as the result of the multiplication of a and b So the next thing to do is to display this

result on the JLabel that we have already prepared before

A x B = _____

The text A x B = ______ was made using JLabel component and its variable name is jLabel3 Since the variable c is of type

double and text property of JLabel is of type String we need to convert it first to String data type The method to convert a double

to String is DoubletoString() So we can write the code as DoubletoString(c) After getting the text we can now use it to be the

text displayed on jLabel3 To do that we use the method setText() Hence the appropriate code for displaying the result is

jLabel3setText( A x B = + DoubletoString(c) )

And the full code for our calculation becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

jLabel3setText( A x B = + DoubletoString(c) )

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5858

[ Back ]

Last Updated ( Tuesday 18 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

Page 3: Computional Physics

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 358

r r

mGmF ˆ

2

2112 =

v

r r r

v

=ˆ is a unit vector

r r

mGmF

v

v

3

2112 =

( ) yr xr r mGmF y x

ˆˆ3

2112 +=

v

yr r

mGm xr

r

mGmF y x

ˆˆ3

21

3

2112

+

=

v

( ) ( ) yr

r r

mGm xr

r r

mGmF y

y x

x

y x

ˆˆ2

32

322

21

22

2112

+

+

+

=

v

( ) ( ) 23

223

223

y x y x p r r r r r r +=+==

21co mGm=

yr r

xr r

F y

p

x

p

ˆco

ˆco

12

+

=

v

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 458

Components of vector

co

12

= x

p

x r r

F

= y

p

y r r

F co

12

Magnitude of 12F v

( ) ( )2

12

2

12 12 y x F F F +=

Direction

12

121tan x

y

F

F minus

For n total number of particles

nF F F F F F net 1151413121

vvvvvv

++++=

i

n

i

F F net 1

2

1

vv

sum=

=

inet x

n

i

x F F 11

2

sum=

= inet y

n

i

y F F 11

2

sum=

=

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 558

Given m1 to m5 and their coordinates are as above Calculate the gravitational force at m1 dueto m2 m3 m4 and m5

m[1]=25 x[1]=12 y[1]=25m[2]=15 x[2]=15 y[2]=15m[3]=75 x[3]=02 y[3]=55m[4]=92 x[4]=52 y[4]=25m[5]=66 x[5]=42 y[5]=45

The algorithm

1 Declare the classmethod to be used2 Declare and assign all variables to be used3 Initialize value to all respective variables4 Decide on which mass the gravitational force to be calculated

(eg on m j where j =1)5 Set f x =0 and f y =06 Start the loop from j =2 to j =5 to calculate the summation of f x and f y 7 Resolve the gravitational force into unit vector (2D Cartesian coordinate)

8 Calculate the mass vector position ( ) ( ) y y y x x xr r r ˆˆ121212 minus+minus=minus=

vvv

Therefore r x = x [j]-x [1] and r y = y [j]-y [1]9 Defineassign all respective equations to be used

X

Y

m1 (1225)

m2 (1515)

m4 (5225)

m5 (4245)

m3 (0255)75 kg

25 kg

15 kg

92 kg

66 kg

21 3 4 5

1

2

3

4

5

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 658

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 758

Source code GravityAttract05ajava

Java class for calculating the resultant force on an object m1due to for other objects m2 m3 m4 and m5

import static javalangMath

public class GravityAttract05a

public static void main(String args[])

double[] m = new double[6] mass of object double[] x = new double[6] x-coor of object double[] y = new double[6] y-coor of object double f resultant forcedouble fxfy x amp y component of force double rx ry rp co Gint j

G = 667e-11m[1]=25 x[1]=12 y[1]=25m[2]=15 x[2]=15 y[2]=15m[3]=75 x[3]=02 y[3]=55m[4]=92 x[4]=52 y[4]=25m[5]=66 x[5]=42 y[5]=45

fx = 00fy = 00

for(j=2 jlt=5 j++)rx = x[j]-x[1]ry = y[j]-y[1]rp = pow((rxrx+ryry)15) (rx^2 + ry^2) raised to the power of 32 co = Gm[1]m[j]fx = fx + corxrpfy = fy + coryrp

f = sqrt(fxfx + fyfy)Systemoutprintf(ntThe force on m[1] due to the other masses is e Nnnf)

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 858

Source code GravityAttract06ajava

Java class for calculating the resultant force on each object(on m1 through m5)

import static javalangMath

public class GravityAttract06a

public static void main(String args[])

double[] m = new double[6] mass of objectdouble[] x = new double[6] x-coor of objectdouble[] y = new double[6] y-coor of objectdouble f resultant forcedouble fxfy x amp y component of forcedouble rx ry rp co G dirfangdirfint j mi

G = 667e-11m[1]=25 x[1]=12 y[1]=25m[2]=15 x[2]=15 y[2]=15m[3]=75 x[3]=02 y[3]=55m[4]=92 x[4]=52 y[4]=25m[5]=66 x[5]=42 y[5]=45

for (mi=1 milt=5 mi++)

fx = 00fy = 00for(j=1 jlt=5 j++)

if (j=mi)rx = x[j]-x[mi]ry = y[j]-y[mi]rp = pow((rxrx+ryry)15) (rx^2 + ry^2) raised to the power of 32co = Gm[mi]m[j]fx = fx + corxrpfy = fy + coryrp

f = sqrt(fxfx + fyfy)dirf = atan(fyfx)angdirf = (dirf(22070))180Systemoutprintf(The force on m[+ mi +] due to the other masses is eN fdegnfangdirf)

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 958

Source code GravityAttract07ajava

Java class for calculating the resultant force on any object(ie either on m1m2 m3 m4 or m5)

import static javalangMathimport javaiopublic class GravityAttract07a

public static void main(String args[])

double[] m = new double[6] mass of objectdouble[] x = new double[6] x-coor of objectdouble[] y = new double[6] y-coor of objectdouble f resultant forcedouble fxfy x amp y component of forcedouble rx ry rp co G dirfangdirfint j

G = 667e-11m[1]=25 x[1]=12 y[1]=25m[2]=15 x[2]=15 y[2]=15m[3]=75 x[3]=02 y[3]=55m[4]=92 x[4]=52 y[4]=25m[5]=66 x[5]=42 y[5]=45BufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin))String mass=Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))

try mass=dataInreadLine()catch(IOException e)Systemoutprintln(Not a valid input value)int mi = IntegerparseInt(mass)fx = 00fy = 00

for(j=1 jlt=5 j++)

if (j=mi)rx = x[j]-x[mi]ry = y[j]-y[mi]rp = pow((rxrx+ryry)15) (rx^2 + ry^2) raised to the power of 32

co = Gm[mi]m[j]fx = fx + corxrpfy = fy + coryrp

f = sqrt(fxfx + fyfy)dirf = toDegrees(atan(fyfx))Systemoutprintf(The force on m[+ mi +] due to the other masses is eN fdegnfdirf)

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1058

983107983119983117983120983125983124983105983124983113983119983118983105983116

983120983112983129983123983113983107983123 983080983123983123983120983090983089983090983090983081

983109983148983141983139983156983154983151983155983156983137983156983145983139 983152983151983156983141983150983156983145983137983148983138983161

983105983138983140983086 983115983144983137983149983145983149 983113983155983149983137983145983148

983117983151983144983140 983115983144983137983148983145983140 983115983137983155983149983145983150

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1158

1 Calculation of electrostatic potential for a group of point charges

You will learn about

- mesh method

a) using a for() loop inside another for() loop

to generate ordered pair (xy) points in a mesh on x-y plane

b) calculate physical value in this case V(xy) forevery point (xy) generated

- saving calculated values into a file

a) making a sequential text file

b) saving in a format that gnuplot can understand

for making a surface plot

- use gnuplot to visualize potential surface

a) making a surface plot from data points in a text file

b) making contour plot to visualize equipotential lines

c) changing the z-axis range to zoom in or zoom out on the surface

d) labelling the plot

e) save as an image the potential surface amp the contour plotf) save as an eps file the potential surface amp the contour plot

- including an eps file into a document

2 Calculation of electrostatic field for a group of point charges

You will learn again

- using mesh method to generate ordered pair (xy) points

in a mesh on x-y plane

You will also learn how to

- calculate vector field values in this case E(xy) for every

generated (xy) mesh point

- save the calculated vector points into a text file

in a format understood by gnuplot

- make a vector field plot using gnuplot

Source code

import static javalangMath

public class estatpot01b

public static void main(String[] args)

double[] q = new double[100]

double[] xq = new double[100]

double[] yq = new double[100]

double V r rx ry k x y

int i n

q[1] = 20e-6 xq[1] = -10 yq[1] = 10

q[2] =-30e-6 xq[2] = 30 yq[2] =-20

q[3] = 30e-6 xq[3] = -20 yq[3] = 40

q[4] =-50e-6 xq[4] = 20 yq[4] = 50

k = 9e9n = 4

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1258

for (x=-5 xlt=5 x = x+02)

for (y=5 ygt=-5 y = y-02)

V = 00

for(i=1 ilt=n i++)

rx = x - xq[i]

ry = y - yq[i]r = sqrt(rxrx+ryry)

if (rlt1e-6)

if (q[i]lt0) V = -10 else V = 10

break

V = V + q[i]r

V = kV

Systemoutprintf(f f fn x y V)

Systemoutprintf(n)

How to do surface plot using Gnuplot

To do a surface plotgnuplotgt splot epotdat with lineTo zoom in and zoom out z axisgnuplotgt set zrange [-2000020000]To convert the graph into imagegnuplotgt set term pngTerminal type set to pngOptions are small color picsize 640 480 Then typegnuplotgt set output epotpngAgain do splot as followgnuplotgt splot epotdat with lineNow the graph is saved into a file in png format named as epotpngTo go back into terminal ouputgnuplotgt set term x11To set to countor plotgnuplotgt set pm3dTo remove countor meshgnuplotgt unset surf

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1358

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1458

983107983119983117983120983125983124983105983124983113983119983118983105983116 983120983112983129983123983113983107983123

983080983123983123983120983090983089983090983090983081

983109983157983148983137983154 983149983141983156983144983151983140

983138983161

983105983138983140983086 983115983144983137983149983145983149 983113983155983149983137983145983148

983117983151983144983140 983115983144983137983148983145983140 983115983137983155983149983145983150

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1558

Free fall body problem

Consider an object thrown upward under gravitational force ( f=mg) with an initial velocity v0The object goes up and then stop at maximum y position ymax before it start to fall downback to the original position at y0 and then to the ground level (See figure)

ymdt

yd m

dt

dvmmaF ampamp====

2

2

t

Y

v1 (t1y1)

v2 (t2y2)

v4 (t4y4)

v5 (t5y5)

vn (tnyn)

v3 (t3y3)

v0 (t0y0)

∆t∆t ∆t ∆t

y1

y0

∆t ∆t

∆yv0

a = g

y0

t0

t0 t1 t2 t3 t4 t5 tn

ymax

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1658

Exact theoretical solution

( ) 2

21

00 at t v yt y ++=

The y projection of the exact solution can be plotted using the following java source code

Java class for solving free fall object under gravity (g = 98 mss)Exact solution y = y0 + v0t + 12at^2

public class freefallsol

public static void main(String args[])double av0yy0deltt

a = -98y0 = 10v0 = 10delt = 00001for(t=0 tlt=05 t=t+delt)

y = y0 + v0t + 05attSystemoutprintf(84f 84fnty)

Complile freefallsoljava to get the plot of the exact solution and then run and save theoutput into a file name as follows

[khamimsetpar65 ~]$ java freefallsol gtffsoldat

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1758

Euler Method

There is another way to see the projection of y position of the object using a numericalmethod called ldquoEuler methodrdquo See the following derivation

t avt v y

t

va

t

yv

∆=∆∆=∆

∆=

∆=

t v y y y y

t avvvv

t v y y y y

t avvvv

t v y y y y

t avvvv

nnnn

nnn

∆+=∆+=

∆+=∆+=

∆+=∆+=

∆+=∆+=

∆+=∆+=

∆+=∆+=

minus

minusminus

1

11

2112

112

1001

001

M

if ∆∆∆∆t rarrrarrrarrrarr 0 then y will close to the exact solution ( ( ) 2

21

00 at t v yt y ++= )

The algorithm 1 Declare the classmethod to be used

2 Declare and assign all variables to be used av0v1yy0y1deltt and j 3 Initialize value to all respective variables a = -98 y0 = 10 v0 = 10

4 Select appropriate value for ∆t

∆t = 00055 Start the loop to calculate the velocity and position for t=0

6 Calculate velocity v1 = v0 + ∆v = v0 + a ∆t

7 Calculate position y1 = y0 + ∆y = y0 + v1 ∆t8 Print out values of t and y1 9 Swap v0 = v1 and y0 = y1

10 Repeat from step 5 and increase the value of t by ∆t (t =t + ∆t) until stoppingcondition is satisfied at t=05

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1858

The java code

Java class for solving free fall object under gravity (g = 98 mss)by numerical method called ldquoEulers Methodrdquo

public class freefalleuler

public static void main(String args[])double av0v1yy0y1delttint j

a = -98y0 = 10v0 = 10delt = 0005

for(t=0 tlt=05 t =t + delt)v1 = v0 + adelty1 = y0 + v1deltSystemoutprintf(84f 84fnty1)v0 = v1y0 = y1

Complile freefalleulerjava for different delt ie 00005 0005 005 and then run and savethe output into a file name as follows

[khamimsetpar65 ~]$ java freefalleuler gtffeuler00005dat[khamimsetpar65 ~]$ java freefalleuler gtffeuler0005dat[khamimsetpar65 ~]$ java freefalleuler gtffeuler005dat

Plotting the results using gnuplot

Using gnuplot plot all the graphs using the following command and compare the results

gnuplotgt plot ffeuler00005dat using 12 title euler delt=00005 with linegt ffeuler0005dat using 12 title euler delt=0005 with linegt ffeuler005dat using 12 title euler deltt=005 with linegt ffsoldat using 12 title exact solution with line

In MS windows

gnuplotgt plot ffeuler00005dat using 12 title euler delt=00005 with lineffeuler0005dat using 12 title euler delt=0005 with lineffeuler005dat using 12 titleeuler delt=005 with line ffsoldat using 12 title exact solution with line

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1958

Gnuplot

The smaller the delt the closer the curve to the exact solution

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2058

983107983119983117983120983125983124983105983124983113983119983118983105983116 983120983112983129983123983113983107983123

983080983123983123983120983090983089983090983090983081

983113983149983152983154983151983158983141983140 983109983157983148983137983154 983149983141983156983144983151983140

983138983161

983105983138983140983086 983115983144983137983149983145983149 983113983155983149983137983145983148

983117983151983144983140 983115983144983137983148983145983140 983115983137983155983149983145983150

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2158

Improved Euler method

Comparison between analytical solution of F=ma with the Euler method shows that although the

method is able to find approximate solution the error is quite significant if computational step is

reduced The error is also cumulative ie as the iteration of the calculation increases the error also gets

bigger hence value furthest away from initial condition has the largest error

The Euler method gives us a way to approximate positions of object with respect to time but requires

an order of magnitude of computational iterations for each order of magnitude of accuracy For large

number of objects this method will certainly consume a great deal of computation time Hence

students are introduced to Initial Half-Step Method an Euler method with a much improved

computational effeciency

Source code

Study of motion of an object with mass m

experiencing a constant force F using

Improved Euler Method (Initial half step method)

In X and Y dimension

import static javalangMath

public class fma04

public static void main(String args[])

double m fx fy x y vo vx vy ax ay t delt

double pi th theta ymax tmax xmax

pi = 40atan(10)

m = 01 in kg

fy = -02 in N (pulling down force)

fx = 00 in N (retarding force)

ay = fym

ax = fxm

vo = 500 in ms

theta = 300 in degrees

th = theta(pi1800) degrees change to radian

vx = vo cos(th) in ms vy = vo sin(th) in ms

y = 12 in m

x = 00 in m

ymax=y initialize var ymax with initial value of y

xmax=00

tmax=00

delt = 001 in s

for(t=00 tlt=300 t=t+delt)

System outprintf(f f fntxy)

if (t==00)vx = vx + 05axdelt

vy = vy + 05aydelt

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2258

else

vx = vx + axdelt

vy = vy + aydelt

x = x + vxdelt

y = y + vydelt

if (ygtymax)

ymax=y

xmax=xtmax=t

if (ylt=00) break

System outprintf( Value of Y maximum is f happened when t = f

nymaxtmax)

System outprintf( at x = fnxmax)

System outprintf( The object reach the ground at x=f and t=fn xt)

gnuplot result

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2358

983107983119983117983120983125983124983105983124983113983119983118983105983116

983120983112983129983123983113983107983123 983080983123983123983120983090983089983090983090983081

983122983141983155983156983151983154983145983150983143 983110983151983154983139983141

983138983161

983105983138983140983086 983115983144983137983149983145983149 983113983155983149983137983145983148

983117983151983144983140 983115983144983137983148983145983140 983115983137983155983149983145983150

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2458

Damped Harmonic Oscillation

Object m is on a flat surface attached to a spring as in figure above The object is pulled

to position A and released so that it oscillates about x =0 Assuming that the force F=ma= -bv-kx where x is position of the object v is the velocity k the spring constant andb is a constant associated with friction with the flat surface

The algorithm

1 Declare the classmethod to be used2 Declare and assign all variables to be used

x t delt k b A m F a v v0 and x03 Declare and assign constants to the respective variables

k = 02 b = 01 A = 01 m = 054 Select appropriate value for ∆t 5 Initialized initial values xo=A vo=00

6 set x=xo v=vo and t = 07 calculate the value of force F = -kx-bv 8 calculate the value of acceleration a = Fm

9 calculate velocity v = vo + ∆v = vo + a ∆t

10 calculate position x = xo + ∆x = xo + v ∆t

11 increment value of t by ∆t 12 print out values of t and x

13 swap the value of xo=x vo=v 14 repeat from step 4 until stopping condition is satisfied (tlt=600)

The java code

public class simplehar01

public static void main(String args[])double x t deltdouble k b A m F a v v0 x0

k = 02b = 01A = 0 1

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2558

m = 05delt = 0001v0 = 00x0 = Ax = x0 v = v0

t = 00while(tlt=600)

F = -kx-bva = Fmv = v0 + adeltx = x0 + vdeltt = t + deltSystemoutprintf(f fntx)x0 = xv0 = v

gnuplot result

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2658

983107983119983117983120983125983124983105983124983113983119983118983105983116 983120983112983129983123983113983107983123

(983123983123983120983090983089983090983090983081

983106983157983145983148983140983145983150983143 983137 983152983144983161983155983145983139983155 983155983151983148983158983141983154

983157983155983145983150983143 983118983141983156983138983141983137983150983155

983138983161

983105983138983140983086 983115983144983137983149983145983149 983113983155983149983137983145983148

983117983151983144983140 983115983144983137983148983145983140 983115983137983155983149983145983150

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2758

How to create a user interface (UI) using Netbeans

Based on the ldquoGravityAttract07ajavardquo source code (shown below) build a user interface usingNetbeans The design of the UI could be look like this (Figure1)

Figure 1

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2858

ldquoGravityAttract07ajavardquo source code

Java class for calculating the resultant force on any object(ie either on m1m2 m3 m4 or m5)

import static javalangMathimport javaiopublic class GravityAttract07a

public static void main(String args[])

double[] m = new double[6] mass of objectdouble[] x = new double[6] x-coor of objectdouble[] y = new double[6] y-coor of objectdouble f resultant forcedouble fxfy x amp y component of forcedouble rx ry rp co G dirfangdirf

int j

G = 667e-11m[1]=25 x[1]=12 y[1]=25m[2]=15 x[2]=15 y[2]=15m[3]=75 x[3]=02 y[3]=55m[4]=92 x[4]=52 y[4]=25m[5]=66 x[5]=42 y[5]=45BufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin))String mass=Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))try mass=dataInreadLine()

catch(IOException e)Systemoutprintln(Not a valid input value)int mi = IntegerparseInt(mass)fx = 00fy = 00

for(j=1 jlt=5 j++)

if (j=mi)rx = x[j]-x[mi]ry = y[j]-y[mi]rp = pow((rxrx+ryry)15) (rx^2 + ry^2) raised to the power of 32co = Gm[mi]m[j]

fx = fx + corxrpfy = fy + coryrp

f = sqrt(fxfx + fyfy)dirf = toDegrees(atan(fyfx))Systemoutprintf(The force on m[+ mi +] due to the other masses is eN fdegnfdirf)

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2958

This example will not show you step by step details procedure since it already explained inthe class and in the e-learning Please refer to the e-learning about how to create a project how tocreate JFrame Form and as well how to create JButton JTextField JLabel JTextPane and so on

First create a new project and give a name eg ldquogravityrdquo and then create JFrame Form andthe following JButton JTextField JLabel JTextPane etc by using ldquoswing paletterdquo as shown in Figure2

Figure 2

To insert a picture into JLabel2 click on the JLabel2 go to properties and select ldquoiconrdquo Onthe icon editor choose file and then select file to locate the directory of your graphicpicture file(Figure 3) Select the file and click OK when finished Make sure you have already created your ownpicture file and save it as jpg png bmp etc format eg ldquogravityjpgrdquo Use any graphic package todraw your diagrampicture or use drawing tools available in Open Office or Microsoft Word

JTextPane1JLabel1

JLabel2

JLabel3JLabel4

JLabel5

JLabel6 JLabel7

JLabel8

JTextField2 JTextField3 JTextField4

JTextF

JButton1JButton2

JButton3

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3058

Figure 3

To write documentation or question eg in JTextPane1 click on the JTextPane1 go toproperties and select ldquotextrdquo Type the documentation in the text editor as shown in Figure 4 Click OKwhen finished

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3158

To do formatting on the text such as superscript and subscript eg in jLabel3 click on the

JLabel3 go to properties and select ldquotextrdquo Type the documentationmessage on the text editor asshown in Figure 5 as follows

eg

lthtmlgtOn which particle (mltsubgt1ltsubgtmltsubgt5ltsubgt) you want to calculate theresultant force

Use sup instead if you want a superscriptThe result will look like this

Click OK when finished

Figure 5

To do the calculation you need to put the coding similar like ldquoGravityAttract07ajavardquo and put itinto the ldquocalculaterdquo button (this is ldquoJButton1rdquo) You can copy ldquoGravityAttract07a javardquo source code

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3258

paste it and then do a few alterations First click on the ldquocalculaterdquo button right click choose Eventsand then choose Action then actionPerformed (Figure 6) It will bring you to where you should insertyour coding (Figure 7) You can write your own coding or copypaste any coding from somewhereelse (in this case from ldquoGravityAttract07ajavardquo)

Figure 6

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3358

Figure 7

Copy ldquoGravityAttract07ajavardquo source code and paste to the place after ldquo TODO add youhandling code hererdquo see Figure 7 To see which part has been modified the coding in Figure 7 hasbeen color coded for better understanding as follows

private void jButton1ActionPerformed(javaawteventActionEvent evt) TODO add your handling code here

double[] m = new double[6] mass of objectdouble[] x = new double[6] x-coor of objectdouble[] y = new double[6] y-coor of objectdouble f resultant forcedouble fxfy x amp y component of forcedouble rx ry rp co G dirfangdirfDecimalFormat dff dfangdirfint j mi

G = 667e-11m[1]=25 x[1]=12 y[1]=25m[2]=15 x[2]=15 y[2]=15m[3]=75 x[3]=02 y[3]=55m[4]=92 x[4]=52 y[4]=25m[5]=66 x[5]=42 y[5]=45

BufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin)) String mass= Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3458

catch(IOException e)Systemoutprintln(Not a valid input value) int mi = IntegerparseInt(mass)mi = IntegerparseInt(jTextField1getText())fx = 00fy = 00for(j=1 jlt=5 j++)

if (j=mi)rx = x[j]-x[mi]ry = y[j]-y[mi]rp = Mathpow((rxrx+ryry)15) (rx^2 + ry^2) raised to the power of 32co = Gm[mi]m[j]fx = fx + corxrpfy = fy + coryrp

f = Mathsqrt(fxfx + fyfy)

dirf = Mathatan(fyfx)angdirf = (dirf(22070))1800 Systemoutprintf(ldquoThe resultant force on particle [ldquo+ mi +rdquo] due to the other masses is

14e N 22f degnrdquofangdirf)dff = new javatextDecimalFormat(ldquoE00rdquo)dfangdirf = new DecimalFormat(ldquo000rdquo)

jTextField2setText(ldquordquo + mi) jTextField3setText(ldquordquo + dffformat(f)) jTextField4setText(ldquordquo + dfangdirfformat(angdirf))

Note

Brown = automatically generatedBlue = the original code from ldquoGravityAttract07ajavardquoGreen = coding from ldquoGravityAttract07ajavardquo which were being disabled due to

incompatibilities with NetbeansRed = a new inserted coding a few of them is to rephrasesubstitute the green one

Javascripts command line useBufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin))String mass=Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))try mass=dataInreadLine()catch(IOException e)Systemoutprintln(Not a valid input value)

int mi = IntegerparseInt(mass)

to read any input from the keyboard typed by the user while Netbeans uses egldquoIntegerparseInt(jTextField1getText())rdquo etc Javascripts command line uses ldquoSystemoutprintfrdquo to format the printed output whileNetbeans uses more complicated way to do the same job (see below)

Javascript command line

Systemoutprintf(The resultant force on particle [+ mi +] due to the other masses is 14e N22f degnfangdirf)

Netbeansdff = new javatextDecimalFormat(ldquo0E00rdquo)df di f D i lF t(ldquo0 00rdquo)

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3558

jTextField2setText(ldquordquo + mi) jTextField3setText(ldquordquo + dffformat(f)) jTextField4setText(ldquordquo + dfangdirfformat(angdirf))

ldquodffrdquo and ldquodfangdirfrdquo are new variables that should be declared and added to format thenumbers into decimal format ie ldquo0E00rdquo to get a scientific format eg ldquo20019E-10rdquo and ldquo000rdquoto get a decimal number eg ldquo-1570rdquo

To use DecimalFormat you have to put ldquoimport javatextDecimalFormatrdquo in the top of thecoding as shown in Figure 8

Figure 8

To reset the calculation you need to put coding into the ldquoresetrdquo button (this is ldquoJButton2rdquo)First click on the ldquoresetrdquo button right click choose Events and then choose Action thenactionPerformed (similar like in Figure 6) It will bring you to where you should insert your coding(Figure 9) Write the following coding (Figure 9)

Figure 9

To exit the program you need to put coding into the ldquoexitrdquo button (this is ldquoJButton3rdquo) Firstclick on the ldquoexitrdquo button right click choose Events and then choose Action then actionPerformed(similar like in Figure 6) It will bring you to where you should insert your coding (Figure 10) Writethe following coding (Figure 10)

Figure 10

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3658

Once you finished now you can run and see the result If everything is fine then the output wilbe look like this (Figure 11)

Figure 11

Remember when you compile and run ldquoGravityAttract07ajavardquo using Javascripts commandline once you compile and run the result was looked like this

On which particle you want to calculate the resulting force 2The resultant force on particle [2] due to the other masses is 30673e-10 N 8837 deg

SEE THE DIFFERENCES AT THE USER INTERFACE

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3758

8th Step

Written by Mohd Khalid Kasmin

Monday 27 August 2007

Programming using command line interface (CLI) has its advantages but todays computer users are so used to graphical user

interface (GUI) that we just can no longer be contented with writing application that do not use the GUI Hence starting from this 8thstep we are going to embark on learning how to make java application that utilises the the GUI

It is possible to write a GUI application with the text based tools that we have been using for these past 7 steps but the job is so

messy that most of you will definitely be turnoff by it So to make life slightly easier well use an integrated development

environment (IDE) known as NetBeans specifically NetBeans 551

0 First ssh to the our server Next once logged in just type nice -n15 netbeans amp on the terminal If you had sshed with the -Y

switch after a few tens of seconds NetBeans should emerge on your display screen

One important point to note Since our server can readily handle up to 100 users (maybe even

more) doing work in CLI mode this machine will certainly be taxed to its maximum capability once

we start using graphical based application NetBeans 551 uses a geat deal of computer memory

and with the 4Gbytes RAM server it can only accomodate up to 25 users concurrently using

NetBeans if the users use the application wisely What is meant by wisely here is that (a) when

starting the NetBeans application please use it together with the nice -n15 command (b) in using

the NetBeans please make sure you only have one project open at a time and (c) please close your

project everytime before quitting the NetBeans

Home Profile Teaching Links Contact Me My Work

Newsflash

Yesterday all servers in the US went out on strike in a bid to get more RAM and better CPUs A spokes person said that the

need for better RAM was due to some fool increasing the front-side bus speed In future busses will be told to slow down in

residential motherboards

search

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3858

1

2

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3958

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4058

[ Back ]

5

Last Updated ( Wednesday 31 October 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4158

8th Step (Cont)

Written by Mohd Khalid Kasmin

Monday 27 August 2007

Up to this point we have define the name of our project Next we construct a class and to follow what we already know we name the class the sam

project name latihan01

6

7

Home Profile Teaching Links Contact Me My Work

Newsflash

Joomla 10 - Experience the Freedom It has never been easier to create your own dynamic site Manage all your content from

the best CMS admin interface

search

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4258

8

9

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4358

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4458

8th Step (Cont still)

Written by Mohd Khalid Kasmin

Monday 27 August 2007

In item (9) we see that Netbeans does not provide us with the main method So to follow what we already know we now put in the

main method

10

Home Profile Teaching Links Contact Me My Work

Newsflash

Aoccdrnig to a rscheearch at an Elingsh uinervtisy it deosnt mttaer in waht oredr the ltteers in a wrod are the olny iprmoetnt

tihng is taht frist and lsat ltteer is at the rghit pclae The rset can be a toatl mses and you can sitll raed it wouthit porbelm Tihs is

bcuseae we do not raed ervey lteter by itslef but the wrod as a wlohe

search

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4558

11

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4658

12

13

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4758

14

15

Last Updated ( Monday 27 August 2007 )

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4858

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4958

9th Step

Thursday 06 September 2007

Step by Step into Java Programming

for

University Physics Student

By

Mohd Khalid Kasmin

A Building graphical user interface - preparing the JFrame Form

At this stage of our programming experience with Java and NetBeans please follow strictly the steps outline in this article First and

make a new GUI project make sure there is no other project that already been loaded by Netbeans Please close any project displayed

1 Create new project

- On the main menu click on File --gt New Project (note Close first any previous project)

- Specify project name amp project location - Untick Create Main Class item

- Click on the Finish button

Home Profile Teaching Links Contact Me My Work

Newsflash

Aoccdrnig to a rscheearch at an Elingsh uinervtisy it deosnt mttaer in waht oredr the ltteers in a wrod are the olny iprmoetnt

tihng is taht frist and lsat ltteer is at the rghit pclae The rset can be a toatl mses and you can sitll raed it wouthit porbelm Tihs is

bcuseae we do not raed ervey lteter by itslef but the wrod as a wlohe

search

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5058

Figure 91

2 Create UI class

- Right click on the project name in the Project pane

on the popup menu select New --gt JFrame Form

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5158

Figure 92

- In the New JFrame Form dialog specify the Class Name and the Package name

eg Class Name multiplierUI Package mymultiplier

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5258

Figure 93

After clicking on the Finish button we should get something like the following

Figure 94

Last Updated ( Tuesday 18 September 2007 )

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5358

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5458

9th Step (Cont)

Written by Mohd Khalid Kasmin

Tuesday 11 September 2007

Step by Step into Java Programming

for University Physics Student

By

Mohd Khalid Kasmin

B Building user interface (UI) - putting in the components

Now that we have a proper blank form we can start filling the form with the suitable graphical components For introduction to

GUI programming an appropriate application to build (for a physics student) is a simple solver So our first exercise is to build an

application that takes two numbers and multiply them and display the result

With GUI we usually give input values to an application via input boxes that are labelled When all inputs are typed in properlywe click on a button to ask the application to execute the job Hence in this exercise well do just that The target now is to

construct a user interface that looks like Figure 95 below

Figure 95

Home Profile Teaching Links Contact Me My Work

Newsflash

Joomla 10 - Experience the Freedom It has never been easier to create your own dynamic site Manage all your content from

the best CMS admin interface

search

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5558

[ Back ]

1 Problemapplication description box

- In the palette pane click on Swing component named JTextPane (Note click just once) Move the mouse pointer on top of

your JFrame (Note just move not drag) When you see a little white box with orange border position it somewhere near the

top part of your JFrame and click once The JTextPane is then attached on the JFrame Addjust its size with the help of the

sizing hook around the perimeter of the JTextPane

- Edit the text property of the JTextPane with the description of your application

2 Input boxes

- In the palette pane click on Swing component named JTextField Next move the mouse pointer on top of your JFrame

When you see a white box with the word jTextField1 position it to the left and under the JTextPane1

- Click on Swing component JTextField in the palette pane again Move the mouse pointer on top of the JFrame you should

get a second JTextField named jTextField2 Position it to the right of jTextField1

Last Updated ( Tuesday 11 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5658

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5758

multiplication if a b and c were first declared as double or float or int variables So the code of the calculation should look like the

following

double a b c

a =

b =

c = a b

Clearly variables a and b need to be assigned with values of typed double Now looking back at inputboxes A and B we made

those boxes using the JTextField component meaning that any data or value written in a JTextField is of typed Text or String even

if the value displayed in the box is ldquo125rdquo But f irst here there is this task of getting to that Text (or String) values ldquo125rdquo and ldquo72rdquo

In Java we can get to a text in JTextField component using the getText() method So to retrieve values in jTextField1 and

jTextField2 we can use

jTextField1getText()

and

jTextField2getText()

But those values are still in text mode and we need another method to convert the data mode f rom text to double For that we can

use the method DoubleparseDouble() Thus the whole process of retrieve text -gt convert text to double -gt assign to double

variable can be done using

a = DoubleparseDouble( jTextField1getText() )

and

b = DoubleparseDouble( jTextField2getText() )

Hence the code for performing the multiplication becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

Up to this point we have the variable c as the result of the multiplication of a and b So the next thing to do is to display this

result on the JLabel that we have already prepared before

A x B = _____

The text A x B = ______ was made using JLabel component and its variable name is jLabel3 Since the variable c is of type

double and text property of JLabel is of type String we need to convert it first to String data type The method to convert a double

to String is DoubletoString() So we can write the code as DoubletoString(c) After getting the text we can now use it to be the

text displayed on jLabel3 To do that we use the method setText() Hence the appropriate code for displaying the result is

jLabel3setText( A x B = + DoubletoString(c) )

And the full code for our calculation becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

jLabel3setText( A x B = + DoubletoString(c) )

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5858

[ Back ]

Last Updated ( Tuesday 18 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

Page 4: Computional Physics

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 458

Components of vector

co

12

= x

p

x r r

F

= y

p

y r r

F co

12

Magnitude of 12F v

( ) ( )2

12

2

12 12 y x F F F +=

Direction

12

121tan x

y

F

F minus

For n total number of particles

nF F F F F F net 1151413121

vvvvvv

++++=

i

n

i

F F net 1

2

1

vv

sum=

=

inet x

n

i

x F F 11

2

sum=

= inet y

n

i

y F F 11

2

sum=

=

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 558

Given m1 to m5 and their coordinates are as above Calculate the gravitational force at m1 dueto m2 m3 m4 and m5

m[1]=25 x[1]=12 y[1]=25m[2]=15 x[2]=15 y[2]=15m[3]=75 x[3]=02 y[3]=55m[4]=92 x[4]=52 y[4]=25m[5]=66 x[5]=42 y[5]=45

The algorithm

1 Declare the classmethod to be used2 Declare and assign all variables to be used3 Initialize value to all respective variables4 Decide on which mass the gravitational force to be calculated

(eg on m j where j =1)5 Set f x =0 and f y =06 Start the loop from j =2 to j =5 to calculate the summation of f x and f y 7 Resolve the gravitational force into unit vector (2D Cartesian coordinate)

8 Calculate the mass vector position ( ) ( ) y y y x x xr r r ˆˆ121212 minus+minus=minus=

vvv

Therefore r x = x [j]-x [1] and r y = y [j]-y [1]9 Defineassign all respective equations to be used

X

Y

m1 (1225)

m2 (1515)

m4 (5225)

m5 (4245)

m3 (0255)75 kg

25 kg

15 kg

92 kg

66 kg

21 3 4 5

1

2

3

4

5

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 658

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 758

Source code GravityAttract05ajava

Java class for calculating the resultant force on an object m1due to for other objects m2 m3 m4 and m5

import static javalangMath

public class GravityAttract05a

public static void main(String args[])

double[] m = new double[6] mass of object double[] x = new double[6] x-coor of object double[] y = new double[6] y-coor of object double f resultant forcedouble fxfy x amp y component of force double rx ry rp co Gint j

G = 667e-11m[1]=25 x[1]=12 y[1]=25m[2]=15 x[2]=15 y[2]=15m[3]=75 x[3]=02 y[3]=55m[4]=92 x[4]=52 y[4]=25m[5]=66 x[5]=42 y[5]=45

fx = 00fy = 00

for(j=2 jlt=5 j++)rx = x[j]-x[1]ry = y[j]-y[1]rp = pow((rxrx+ryry)15) (rx^2 + ry^2) raised to the power of 32 co = Gm[1]m[j]fx = fx + corxrpfy = fy + coryrp

f = sqrt(fxfx + fyfy)Systemoutprintf(ntThe force on m[1] due to the other masses is e Nnnf)

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 858

Source code GravityAttract06ajava

Java class for calculating the resultant force on each object(on m1 through m5)

import static javalangMath

public class GravityAttract06a

public static void main(String args[])

double[] m = new double[6] mass of objectdouble[] x = new double[6] x-coor of objectdouble[] y = new double[6] y-coor of objectdouble f resultant forcedouble fxfy x amp y component of forcedouble rx ry rp co G dirfangdirfint j mi

G = 667e-11m[1]=25 x[1]=12 y[1]=25m[2]=15 x[2]=15 y[2]=15m[3]=75 x[3]=02 y[3]=55m[4]=92 x[4]=52 y[4]=25m[5]=66 x[5]=42 y[5]=45

for (mi=1 milt=5 mi++)

fx = 00fy = 00for(j=1 jlt=5 j++)

if (j=mi)rx = x[j]-x[mi]ry = y[j]-y[mi]rp = pow((rxrx+ryry)15) (rx^2 + ry^2) raised to the power of 32co = Gm[mi]m[j]fx = fx + corxrpfy = fy + coryrp

f = sqrt(fxfx + fyfy)dirf = atan(fyfx)angdirf = (dirf(22070))180Systemoutprintf(The force on m[+ mi +] due to the other masses is eN fdegnfangdirf)

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 958

Source code GravityAttract07ajava

Java class for calculating the resultant force on any object(ie either on m1m2 m3 m4 or m5)

import static javalangMathimport javaiopublic class GravityAttract07a

public static void main(String args[])

double[] m = new double[6] mass of objectdouble[] x = new double[6] x-coor of objectdouble[] y = new double[6] y-coor of objectdouble f resultant forcedouble fxfy x amp y component of forcedouble rx ry rp co G dirfangdirfint j

G = 667e-11m[1]=25 x[1]=12 y[1]=25m[2]=15 x[2]=15 y[2]=15m[3]=75 x[3]=02 y[3]=55m[4]=92 x[4]=52 y[4]=25m[5]=66 x[5]=42 y[5]=45BufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin))String mass=Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))

try mass=dataInreadLine()catch(IOException e)Systemoutprintln(Not a valid input value)int mi = IntegerparseInt(mass)fx = 00fy = 00

for(j=1 jlt=5 j++)

if (j=mi)rx = x[j]-x[mi]ry = y[j]-y[mi]rp = pow((rxrx+ryry)15) (rx^2 + ry^2) raised to the power of 32

co = Gm[mi]m[j]fx = fx + corxrpfy = fy + coryrp

f = sqrt(fxfx + fyfy)dirf = toDegrees(atan(fyfx))Systemoutprintf(The force on m[+ mi +] due to the other masses is eN fdegnfdirf)

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1058

983107983119983117983120983125983124983105983124983113983119983118983105983116

983120983112983129983123983113983107983123 983080983123983123983120983090983089983090983090983081

983109983148983141983139983156983154983151983155983156983137983156983145983139 983152983151983156983141983150983156983145983137983148983138983161

983105983138983140983086 983115983144983137983149983145983149 983113983155983149983137983145983148

983117983151983144983140 983115983144983137983148983145983140 983115983137983155983149983145983150

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1158

1 Calculation of electrostatic potential for a group of point charges

You will learn about

- mesh method

a) using a for() loop inside another for() loop

to generate ordered pair (xy) points in a mesh on x-y plane

b) calculate physical value in this case V(xy) forevery point (xy) generated

- saving calculated values into a file

a) making a sequential text file

b) saving in a format that gnuplot can understand

for making a surface plot

- use gnuplot to visualize potential surface

a) making a surface plot from data points in a text file

b) making contour plot to visualize equipotential lines

c) changing the z-axis range to zoom in or zoom out on the surface

d) labelling the plot

e) save as an image the potential surface amp the contour plotf) save as an eps file the potential surface amp the contour plot

- including an eps file into a document

2 Calculation of electrostatic field for a group of point charges

You will learn again

- using mesh method to generate ordered pair (xy) points

in a mesh on x-y plane

You will also learn how to

- calculate vector field values in this case E(xy) for every

generated (xy) mesh point

- save the calculated vector points into a text file

in a format understood by gnuplot

- make a vector field plot using gnuplot

Source code

import static javalangMath

public class estatpot01b

public static void main(String[] args)

double[] q = new double[100]

double[] xq = new double[100]

double[] yq = new double[100]

double V r rx ry k x y

int i n

q[1] = 20e-6 xq[1] = -10 yq[1] = 10

q[2] =-30e-6 xq[2] = 30 yq[2] =-20

q[3] = 30e-6 xq[3] = -20 yq[3] = 40

q[4] =-50e-6 xq[4] = 20 yq[4] = 50

k = 9e9n = 4

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1258

for (x=-5 xlt=5 x = x+02)

for (y=5 ygt=-5 y = y-02)

V = 00

for(i=1 ilt=n i++)

rx = x - xq[i]

ry = y - yq[i]r = sqrt(rxrx+ryry)

if (rlt1e-6)

if (q[i]lt0) V = -10 else V = 10

break

V = V + q[i]r

V = kV

Systemoutprintf(f f fn x y V)

Systemoutprintf(n)

How to do surface plot using Gnuplot

To do a surface plotgnuplotgt splot epotdat with lineTo zoom in and zoom out z axisgnuplotgt set zrange [-2000020000]To convert the graph into imagegnuplotgt set term pngTerminal type set to pngOptions are small color picsize 640 480 Then typegnuplotgt set output epotpngAgain do splot as followgnuplotgt splot epotdat with lineNow the graph is saved into a file in png format named as epotpngTo go back into terminal ouputgnuplotgt set term x11To set to countor plotgnuplotgt set pm3dTo remove countor meshgnuplotgt unset surf

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1358

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1458

983107983119983117983120983125983124983105983124983113983119983118983105983116 983120983112983129983123983113983107983123

983080983123983123983120983090983089983090983090983081

983109983157983148983137983154 983149983141983156983144983151983140

983138983161

983105983138983140983086 983115983144983137983149983145983149 983113983155983149983137983145983148

983117983151983144983140 983115983144983137983148983145983140 983115983137983155983149983145983150

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1558

Free fall body problem

Consider an object thrown upward under gravitational force ( f=mg) with an initial velocity v0The object goes up and then stop at maximum y position ymax before it start to fall downback to the original position at y0 and then to the ground level (See figure)

ymdt

yd m

dt

dvmmaF ampamp====

2

2

t

Y

v1 (t1y1)

v2 (t2y2)

v4 (t4y4)

v5 (t5y5)

vn (tnyn)

v3 (t3y3)

v0 (t0y0)

∆t∆t ∆t ∆t

y1

y0

∆t ∆t

∆yv0

a = g

y0

t0

t0 t1 t2 t3 t4 t5 tn

ymax

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1658

Exact theoretical solution

( ) 2

21

00 at t v yt y ++=

The y projection of the exact solution can be plotted using the following java source code

Java class for solving free fall object under gravity (g = 98 mss)Exact solution y = y0 + v0t + 12at^2

public class freefallsol

public static void main(String args[])double av0yy0deltt

a = -98y0 = 10v0 = 10delt = 00001for(t=0 tlt=05 t=t+delt)

y = y0 + v0t + 05attSystemoutprintf(84f 84fnty)

Complile freefallsoljava to get the plot of the exact solution and then run and save theoutput into a file name as follows

[khamimsetpar65 ~]$ java freefallsol gtffsoldat

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1758

Euler Method

There is another way to see the projection of y position of the object using a numericalmethod called ldquoEuler methodrdquo See the following derivation

t avt v y

t

va

t

yv

∆=∆∆=∆

∆=

∆=

t v y y y y

t avvvv

t v y y y y

t avvvv

t v y y y y

t avvvv

nnnn

nnn

∆+=∆+=

∆+=∆+=

∆+=∆+=

∆+=∆+=

∆+=∆+=

∆+=∆+=

minus

minusminus

1

11

2112

112

1001

001

M

if ∆∆∆∆t rarrrarrrarrrarr 0 then y will close to the exact solution ( ( ) 2

21

00 at t v yt y ++= )

The algorithm 1 Declare the classmethod to be used

2 Declare and assign all variables to be used av0v1yy0y1deltt and j 3 Initialize value to all respective variables a = -98 y0 = 10 v0 = 10

4 Select appropriate value for ∆t

∆t = 00055 Start the loop to calculate the velocity and position for t=0

6 Calculate velocity v1 = v0 + ∆v = v0 + a ∆t

7 Calculate position y1 = y0 + ∆y = y0 + v1 ∆t8 Print out values of t and y1 9 Swap v0 = v1 and y0 = y1

10 Repeat from step 5 and increase the value of t by ∆t (t =t + ∆t) until stoppingcondition is satisfied at t=05

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1858

The java code

Java class for solving free fall object under gravity (g = 98 mss)by numerical method called ldquoEulers Methodrdquo

public class freefalleuler

public static void main(String args[])double av0v1yy0y1delttint j

a = -98y0 = 10v0 = 10delt = 0005

for(t=0 tlt=05 t =t + delt)v1 = v0 + adelty1 = y0 + v1deltSystemoutprintf(84f 84fnty1)v0 = v1y0 = y1

Complile freefalleulerjava for different delt ie 00005 0005 005 and then run and savethe output into a file name as follows

[khamimsetpar65 ~]$ java freefalleuler gtffeuler00005dat[khamimsetpar65 ~]$ java freefalleuler gtffeuler0005dat[khamimsetpar65 ~]$ java freefalleuler gtffeuler005dat

Plotting the results using gnuplot

Using gnuplot plot all the graphs using the following command and compare the results

gnuplotgt plot ffeuler00005dat using 12 title euler delt=00005 with linegt ffeuler0005dat using 12 title euler delt=0005 with linegt ffeuler005dat using 12 title euler deltt=005 with linegt ffsoldat using 12 title exact solution with line

In MS windows

gnuplotgt plot ffeuler00005dat using 12 title euler delt=00005 with lineffeuler0005dat using 12 title euler delt=0005 with lineffeuler005dat using 12 titleeuler delt=005 with line ffsoldat using 12 title exact solution with line

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1958

Gnuplot

The smaller the delt the closer the curve to the exact solution

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2058

983107983119983117983120983125983124983105983124983113983119983118983105983116 983120983112983129983123983113983107983123

983080983123983123983120983090983089983090983090983081

983113983149983152983154983151983158983141983140 983109983157983148983137983154 983149983141983156983144983151983140

983138983161

983105983138983140983086 983115983144983137983149983145983149 983113983155983149983137983145983148

983117983151983144983140 983115983144983137983148983145983140 983115983137983155983149983145983150

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2158

Improved Euler method

Comparison between analytical solution of F=ma with the Euler method shows that although the

method is able to find approximate solution the error is quite significant if computational step is

reduced The error is also cumulative ie as the iteration of the calculation increases the error also gets

bigger hence value furthest away from initial condition has the largest error

The Euler method gives us a way to approximate positions of object with respect to time but requires

an order of magnitude of computational iterations for each order of magnitude of accuracy For large

number of objects this method will certainly consume a great deal of computation time Hence

students are introduced to Initial Half-Step Method an Euler method with a much improved

computational effeciency

Source code

Study of motion of an object with mass m

experiencing a constant force F using

Improved Euler Method (Initial half step method)

In X and Y dimension

import static javalangMath

public class fma04

public static void main(String args[])

double m fx fy x y vo vx vy ax ay t delt

double pi th theta ymax tmax xmax

pi = 40atan(10)

m = 01 in kg

fy = -02 in N (pulling down force)

fx = 00 in N (retarding force)

ay = fym

ax = fxm

vo = 500 in ms

theta = 300 in degrees

th = theta(pi1800) degrees change to radian

vx = vo cos(th) in ms vy = vo sin(th) in ms

y = 12 in m

x = 00 in m

ymax=y initialize var ymax with initial value of y

xmax=00

tmax=00

delt = 001 in s

for(t=00 tlt=300 t=t+delt)

System outprintf(f f fntxy)

if (t==00)vx = vx + 05axdelt

vy = vy + 05aydelt

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2258

else

vx = vx + axdelt

vy = vy + aydelt

x = x + vxdelt

y = y + vydelt

if (ygtymax)

ymax=y

xmax=xtmax=t

if (ylt=00) break

System outprintf( Value of Y maximum is f happened when t = f

nymaxtmax)

System outprintf( at x = fnxmax)

System outprintf( The object reach the ground at x=f and t=fn xt)

gnuplot result

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2358

983107983119983117983120983125983124983105983124983113983119983118983105983116

983120983112983129983123983113983107983123 983080983123983123983120983090983089983090983090983081

983122983141983155983156983151983154983145983150983143 983110983151983154983139983141

983138983161

983105983138983140983086 983115983144983137983149983145983149 983113983155983149983137983145983148

983117983151983144983140 983115983144983137983148983145983140 983115983137983155983149983145983150

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2458

Damped Harmonic Oscillation

Object m is on a flat surface attached to a spring as in figure above The object is pulled

to position A and released so that it oscillates about x =0 Assuming that the force F=ma= -bv-kx where x is position of the object v is the velocity k the spring constant andb is a constant associated with friction with the flat surface

The algorithm

1 Declare the classmethod to be used2 Declare and assign all variables to be used

x t delt k b A m F a v v0 and x03 Declare and assign constants to the respective variables

k = 02 b = 01 A = 01 m = 054 Select appropriate value for ∆t 5 Initialized initial values xo=A vo=00

6 set x=xo v=vo and t = 07 calculate the value of force F = -kx-bv 8 calculate the value of acceleration a = Fm

9 calculate velocity v = vo + ∆v = vo + a ∆t

10 calculate position x = xo + ∆x = xo + v ∆t

11 increment value of t by ∆t 12 print out values of t and x

13 swap the value of xo=x vo=v 14 repeat from step 4 until stopping condition is satisfied (tlt=600)

The java code

public class simplehar01

public static void main(String args[])double x t deltdouble k b A m F a v v0 x0

k = 02b = 01A = 0 1

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2558

m = 05delt = 0001v0 = 00x0 = Ax = x0 v = v0

t = 00while(tlt=600)

F = -kx-bva = Fmv = v0 + adeltx = x0 + vdeltt = t + deltSystemoutprintf(f fntx)x0 = xv0 = v

gnuplot result

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2658

983107983119983117983120983125983124983105983124983113983119983118983105983116 983120983112983129983123983113983107983123

(983123983123983120983090983089983090983090983081

983106983157983145983148983140983145983150983143 983137 983152983144983161983155983145983139983155 983155983151983148983158983141983154

983157983155983145983150983143 983118983141983156983138983141983137983150983155

983138983161

983105983138983140983086 983115983144983137983149983145983149 983113983155983149983137983145983148

983117983151983144983140 983115983144983137983148983145983140 983115983137983155983149983145983150

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2758

How to create a user interface (UI) using Netbeans

Based on the ldquoGravityAttract07ajavardquo source code (shown below) build a user interface usingNetbeans The design of the UI could be look like this (Figure1)

Figure 1

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2858

ldquoGravityAttract07ajavardquo source code

Java class for calculating the resultant force on any object(ie either on m1m2 m3 m4 or m5)

import static javalangMathimport javaiopublic class GravityAttract07a

public static void main(String args[])

double[] m = new double[6] mass of objectdouble[] x = new double[6] x-coor of objectdouble[] y = new double[6] y-coor of objectdouble f resultant forcedouble fxfy x amp y component of forcedouble rx ry rp co G dirfangdirf

int j

G = 667e-11m[1]=25 x[1]=12 y[1]=25m[2]=15 x[2]=15 y[2]=15m[3]=75 x[3]=02 y[3]=55m[4]=92 x[4]=52 y[4]=25m[5]=66 x[5]=42 y[5]=45BufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin))String mass=Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))try mass=dataInreadLine()

catch(IOException e)Systemoutprintln(Not a valid input value)int mi = IntegerparseInt(mass)fx = 00fy = 00

for(j=1 jlt=5 j++)

if (j=mi)rx = x[j]-x[mi]ry = y[j]-y[mi]rp = pow((rxrx+ryry)15) (rx^2 + ry^2) raised to the power of 32co = Gm[mi]m[j]

fx = fx + corxrpfy = fy + coryrp

f = sqrt(fxfx + fyfy)dirf = toDegrees(atan(fyfx))Systemoutprintf(The force on m[+ mi +] due to the other masses is eN fdegnfdirf)

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2958

This example will not show you step by step details procedure since it already explained inthe class and in the e-learning Please refer to the e-learning about how to create a project how tocreate JFrame Form and as well how to create JButton JTextField JLabel JTextPane and so on

First create a new project and give a name eg ldquogravityrdquo and then create JFrame Form andthe following JButton JTextField JLabel JTextPane etc by using ldquoswing paletterdquo as shown in Figure2

Figure 2

To insert a picture into JLabel2 click on the JLabel2 go to properties and select ldquoiconrdquo Onthe icon editor choose file and then select file to locate the directory of your graphicpicture file(Figure 3) Select the file and click OK when finished Make sure you have already created your ownpicture file and save it as jpg png bmp etc format eg ldquogravityjpgrdquo Use any graphic package todraw your diagrampicture or use drawing tools available in Open Office or Microsoft Word

JTextPane1JLabel1

JLabel2

JLabel3JLabel4

JLabel5

JLabel6 JLabel7

JLabel8

JTextField2 JTextField3 JTextField4

JTextF

JButton1JButton2

JButton3

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3058

Figure 3

To write documentation or question eg in JTextPane1 click on the JTextPane1 go toproperties and select ldquotextrdquo Type the documentation in the text editor as shown in Figure 4 Click OKwhen finished

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3158

To do formatting on the text such as superscript and subscript eg in jLabel3 click on the

JLabel3 go to properties and select ldquotextrdquo Type the documentationmessage on the text editor asshown in Figure 5 as follows

eg

lthtmlgtOn which particle (mltsubgt1ltsubgtmltsubgt5ltsubgt) you want to calculate theresultant force

Use sup instead if you want a superscriptThe result will look like this

Click OK when finished

Figure 5

To do the calculation you need to put the coding similar like ldquoGravityAttract07ajavardquo and put itinto the ldquocalculaterdquo button (this is ldquoJButton1rdquo) You can copy ldquoGravityAttract07a javardquo source code

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3258

paste it and then do a few alterations First click on the ldquocalculaterdquo button right click choose Eventsand then choose Action then actionPerformed (Figure 6) It will bring you to where you should insertyour coding (Figure 7) You can write your own coding or copypaste any coding from somewhereelse (in this case from ldquoGravityAttract07ajavardquo)

Figure 6

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3358

Figure 7

Copy ldquoGravityAttract07ajavardquo source code and paste to the place after ldquo TODO add youhandling code hererdquo see Figure 7 To see which part has been modified the coding in Figure 7 hasbeen color coded for better understanding as follows

private void jButton1ActionPerformed(javaawteventActionEvent evt) TODO add your handling code here

double[] m = new double[6] mass of objectdouble[] x = new double[6] x-coor of objectdouble[] y = new double[6] y-coor of objectdouble f resultant forcedouble fxfy x amp y component of forcedouble rx ry rp co G dirfangdirfDecimalFormat dff dfangdirfint j mi

G = 667e-11m[1]=25 x[1]=12 y[1]=25m[2]=15 x[2]=15 y[2]=15m[3]=75 x[3]=02 y[3]=55m[4]=92 x[4]=52 y[4]=25m[5]=66 x[5]=42 y[5]=45

BufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin)) String mass= Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3458

catch(IOException e)Systemoutprintln(Not a valid input value) int mi = IntegerparseInt(mass)mi = IntegerparseInt(jTextField1getText())fx = 00fy = 00for(j=1 jlt=5 j++)

if (j=mi)rx = x[j]-x[mi]ry = y[j]-y[mi]rp = Mathpow((rxrx+ryry)15) (rx^2 + ry^2) raised to the power of 32co = Gm[mi]m[j]fx = fx + corxrpfy = fy + coryrp

f = Mathsqrt(fxfx + fyfy)

dirf = Mathatan(fyfx)angdirf = (dirf(22070))1800 Systemoutprintf(ldquoThe resultant force on particle [ldquo+ mi +rdquo] due to the other masses is

14e N 22f degnrdquofangdirf)dff = new javatextDecimalFormat(ldquoE00rdquo)dfangdirf = new DecimalFormat(ldquo000rdquo)

jTextField2setText(ldquordquo + mi) jTextField3setText(ldquordquo + dffformat(f)) jTextField4setText(ldquordquo + dfangdirfformat(angdirf))

Note

Brown = automatically generatedBlue = the original code from ldquoGravityAttract07ajavardquoGreen = coding from ldquoGravityAttract07ajavardquo which were being disabled due to

incompatibilities with NetbeansRed = a new inserted coding a few of them is to rephrasesubstitute the green one

Javascripts command line useBufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin))String mass=Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))try mass=dataInreadLine()catch(IOException e)Systemoutprintln(Not a valid input value)

int mi = IntegerparseInt(mass)

to read any input from the keyboard typed by the user while Netbeans uses egldquoIntegerparseInt(jTextField1getText())rdquo etc Javascripts command line uses ldquoSystemoutprintfrdquo to format the printed output whileNetbeans uses more complicated way to do the same job (see below)

Javascript command line

Systemoutprintf(The resultant force on particle [+ mi +] due to the other masses is 14e N22f degnfangdirf)

Netbeansdff = new javatextDecimalFormat(ldquo0E00rdquo)df di f D i lF t(ldquo0 00rdquo)

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3558

jTextField2setText(ldquordquo + mi) jTextField3setText(ldquordquo + dffformat(f)) jTextField4setText(ldquordquo + dfangdirfformat(angdirf))

ldquodffrdquo and ldquodfangdirfrdquo are new variables that should be declared and added to format thenumbers into decimal format ie ldquo0E00rdquo to get a scientific format eg ldquo20019E-10rdquo and ldquo000rdquoto get a decimal number eg ldquo-1570rdquo

To use DecimalFormat you have to put ldquoimport javatextDecimalFormatrdquo in the top of thecoding as shown in Figure 8

Figure 8

To reset the calculation you need to put coding into the ldquoresetrdquo button (this is ldquoJButton2rdquo)First click on the ldquoresetrdquo button right click choose Events and then choose Action thenactionPerformed (similar like in Figure 6) It will bring you to where you should insert your coding(Figure 9) Write the following coding (Figure 9)

Figure 9

To exit the program you need to put coding into the ldquoexitrdquo button (this is ldquoJButton3rdquo) Firstclick on the ldquoexitrdquo button right click choose Events and then choose Action then actionPerformed(similar like in Figure 6) It will bring you to where you should insert your coding (Figure 10) Writethe following coding (Figure 10)

Figure 10

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3658

Once you finished now you can run and see the result If everything is fine then the output wilbe look like this (Figure 11)

Figure 11

Remember when you compile and run ldquoGravityAttract07ajavardquo using Javascripts commandline once you compile and run the result was looked like this

On which particle you want to calculate the resulting force 2The resultant force on particle [2] due to the other masses is 30673e-10 N 8837 deg

SEE THE DIFFERENCES AT THE USER INTERFACE

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3758

8th Step

Written by Mohd Khalid Kasmin

Monday 27 August 2007

Programming using command line interface (CLI) has its advantages but todays computer users are so used to graphical user

interface (GUI) that we just can no longer be contented with writing application that do not use the GUI Hence starting from this 8thstep we are going to embark on learning how to make java application that utilises the the GUI

It is possible to write a GUI application with the text based tools that we have been using for these past 7 steps but the job is so

messy that most of you will definitely be turnoff by it So to make life slightly easier well use an integrated development

environment (IDE) known as NetBeans specifically NetBeans 551

0 First ssh to the our server Next once logged in just type nice -n15 netbeans amp on the terminal If you had sshed with the -Y

switch after a few tens of seconds NetBeans should emerge on your display screen

One important point to note Since our server can readily handle up to 100 users (maybe even

more) doing work in CLI mode this machine will certainly be taxed to its maximum capability once

we start using graphical based application NetBeans 551 uses a geat deal of computer memory

and with the 4Gbytes RAM server it can only accomodate up to 25 users concurrently using

NetBeans if the users use the application wisely What is meant by wisely here is that (a) when

starting the NetBeans application please use it together with the nice -n15 command (b) in using

the NetBeans please make sure you only have one project open at a time and (c) please close your

project everytime before quitting the NetBeans

Home Profile Teaching Links Contact Me My Work

Newsflash

Yesterday all servers in the US went out on strike in a bid to get more RAM and better CPUs A spokes person said that the

need for better RAM was due to some fool increasing the front-side bus speed In future busses will be told to slow down in

residential motherboards

search

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3858

1

2

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3958

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4058

[ Back ]

5

Last Updated ( Wednesday 31 October 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4158

8th Step (Cont)

Written by Mohd Khalid Kasmin

Monday 27 August 2007

Up to this point we have define the name of our project Next we construct a class and to follow what we already know we name the class the sam

project name latihan01

6

7

Home Profile Teaching Links Contact Me My Work

Newsflash

Joomla 10 - Experience the Freedom It has never been easier to create your own dynamic site Manage all your content from

the best CMS admin interface

search

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4258

8

9

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4358

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4458

8th Step (Cont still)

Written by Mohd Khalid Kasmin

Monday 27 August 2007

In item (9) we see that Netbeans does not provide us with the main method So to follow what we already know we now put in the

main method

10

Home Profile Teaching Links Contact Me My Work

Newsflash

Aoccdrnig to a rscheearch at an Elingsh uinervtisy it deosnt mttaer in waht oredr the ltteers in a wrod are the olny iprmoetnt

tihng is taht frist and lsat ltteer is at the rghit pclae The rset can be a toatl mses and you can sitll raed it wouthit porbelm Tihs is

bcuseae we do not raed ervey lteter by itslef but the wrod as a wlohe

search

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4558

11

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4658

12

13

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4758

14

15

Last Updated ( Monday 27 August 2007 )

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4858

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4958

9th Step

Thursday 06 September 2007

Step by Step into Java Programming

for

University Physics Student

By

Mohd Khalid Kasmin

A Building graphical user interface - preparing the JFrame Form

At this stage of our programming experience with Java and NetBeans please follow strictly the steps outline in this article First and

make a new GUI project make sure there is no other project that already been loaded by Netbeans Please close any project displayed

1 Create new project

- On the main menu click on File --gt New Project (note Close first any previous project)

- Specify project name amp project location - Untick Create Main Class item

- Click on the Finish button

Home Profile Teaching Links Contact Me My Work

Newsflash

Aoccdrnig to a rscheearch at an Elingsh uinervtisy it deosnt mttaer in waht oredr the ltteers in a wrod are the olny iprmoetnt

tihng is taht frist and lsat ltteer is at the rghit pclae The rset can be a toatl mses and you can sitll raed it wouthit porbelm Tihs is

bcuseae we do not raed ervey lteter by itslef but the wrod as a wlohe

search

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5058

Figure 91

2 Create UI class

- Right click on the project name in the Project pane

on the popup menu select New --gt JFrame Form

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5158

Figure 92

- In the New JFrame Form dialog specify the Class Name and the Package name

eg Class Name multiplierUI Package mymultiplier

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5258

Figure 93

After clicking on the Finish button we should get something like the following

Figure 94

Last Updated ( Tuesday 18 September 2007 )

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5358

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5458

9th Step (Cont)

Written by Mohd Khalid Kasmin

Tuesday 11 September 2007

Step by Step into Java Programming

for University Physics Student

By

Mohd Khalid Kasmin

B Building user interface (UI) - putting in the components

Now that we have a proper blank form we can start filling the form with the suitable graphical components For introduction to

GUI programming an appropriate application to build (for a physics student) is a simple solver So our first exercise is to build an

application that takes two numbers and multiply them and display the result

With GUI we usually give input values to an application via input boxes that are labelled When all inputs are typed in properlywe click on a button to ask the application to execute the job Hence in this exercise well do just that The target now is to

construct a user interface that looks like Figure 95 below

Figure 95

Home Profile Teaching Links Contact Me My Work

Newsflash

Joomla 10 - Experience the Freedom It has never been easier to create your own dynamic site Manage all your content from

the best CMS admin interface

search

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5558

[ Back ]

1 Problemapplication description box

- In the palette pane click on Swing component named JTextPane (Note click just once) Move the mouse pointer on top of

your JFrame (Note just move not drag) When you see a little white box with orange border position it somewhere near the

top part of your JFrame and click once The JTextPane is then attached on the JFrame Addjust its size with the help of the

sizing hook around the perimeter of the JTextPane

- Edit the text property of the JTextPane with the description of your application

2 Input boxes

- In the palette pane click on Swing component named JTextField Next move the mouse pointer on top of your JFrame

When you see a white box with the word jTextField1 position it to the left and under the JTextPane1

- Click on Swing component JTextField in the palette pane again Move the mouse pointer on top of the JFrame you should

get a second JTextField named jTextField2 Position it to the right of jTextField1

Last Updated ( Tuesday 11 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5658

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5758

multiplication if a b and c were first declared as double or float or int variables So the code of the calculation should look like the

following

double a b c

a =

b =

c = a b

Clearly variables a and b need to be assigned with values of typed double Now looking back at inputboxes A and B we made

those boxes using the JTextField component meaning that any data or value written in a JTextField is of typed Text or String even

if the value displayed in the box is ldquo125rdquo But f irst here there is this task of getting to that Text (or String) values ldquo125rdquo and ldquo72rdquo

In Java we can get to a text in JTextField component using the getText() method So to retrieve values in jTextField1 and

jTextField2 we can use

jTextField1getText()

and

jTextField2getText()

But those values are still in text mode and we need another method to convert the data mode f rom text to double For that we can

use the method DoubleparseDouble() Thus the whole process of retrieve text -gt convert text to double -gt assign to double

variable can be done using

a = DoubleparseDouble( jTextField1getText() )

and

b = DoubleparseDouble( jTextField2getText() )

Hence the code for performing the multiplication becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

Up to this point we have the variable c as the result of the multiplication of a and b So the next thing to do is to display this

result on the JLabel that we have already prepared before

A x B = _____

The text A x B = ______ was made using JLabel component and its variable name is jLabel3 Since the variable c is of type

double and text property of JLabel is of type String we need to convert it first to String data type The method to convert a double

to String is DoubletoString() So we can write the code as DoubletoString(c) After getting the text we can now use it to be the

text displayed on jLabel3 To do that we use the method setText() Hence the appropriate code for displaying the result is

jLabel3setText( A x B = + DoubletoString(c) )

And the full code for our calculation becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

jLabel3setText( A x B = + DoubletoString(c) )

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5858

[ Back ]

Last Updated ( Tuesday 18 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

Page 5: Computional Physics

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 558

Given m1 to m5 and their coordinates are as above Calculate the gravitational force at m1 dueto m2 m3 m4 and m5

m[1]=25 x[1]=12 y[1]=25m[2]=15 x[2]=15 y[2]=15m[3]=75 x[3]=02 y[3]=55m[4]=92 x[4]=52 y[4]=25m[5]=66 x[5]=42 y[5]=45

The algorithm

1 Declare the classmethod to be used2 Declare and assign all variables to be used3 Initialize value to all respective variables4 Decide on which mass the gravitational force to be calculated

(eg on m j where j =1)5 Set f x =0 and f y =06 Start the loop from j =2 to j =5 to calculate the summation of f x and f y 7 Resolve the gravitational force into unit vector (2D Cartesian coordinate)

8 Calculate the mass vector position ( ) ( ) y y y x x xr r r ˆˆ121212 minus+minus=minus=

vvv

Therefore r x = x [j]-x [1] and r y = y [j]-y [1]9 Defineassign all respective equations to be used

X

Y

m1 (1225)

m2 (1515)

m4 (5225)

m5 (4245)

m3 (0255)75 kg

25 kg

15 kg

92 kg

66 kg

21 3 4 5

1

2

3

4

5

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 658

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 758

Source code GravityAttract05ajava

Java class for calculating the resultant force on an object m1due to for other objects m2 m3 m4 and m5

import static javalangMath

public class GravityAttract05a

public static void main(String args[])

double[] m = new double[6] mass of object double[] x = new double[6] x-coor of object double[] y = new double[6] y-coor of object double f resultant forcedouble fxfy x amp y component of force double rx ry rp co Gint j

G = 667e-11m[1]=25 x[1]=12 y[1]=25m[2]=15 x[2]=15 y[2]=15m[3]=75 x[3]=02 y[3]=55m[4]=92 x[4]=52 y[4]=25m[5]=66 x[5]=42 y[5]=45

fx = 00fy = 00

for(j=2 jlt=5 j++)rx = x[j]-x[1]ry = y[j]-y[1]rp = pow((rxrx+ryry)15) (rx^2 + ry^2) raised to the power of 32 co = Gm[1]m[j]fx = fx + corxrpfy = fy + coryrp

f = sqrt(fxfx + fyfy)Systemoutprintf(ntThe force on m[1] due to the other masses is e Nnnf)

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 858

Source code GravityAttract06ajava

Java class for calculating the resultant force on each object(on m1 through m5)

import static javalangMath

public class GravityAttract06a

public static void main(String args[])

double[] m = new double[6] mass of objectdouble[] x = new double[6] x-coor of objectdouble[] y = new double[6] y-coor of objectdouble f resultant forcedouble fxfy x amp y component of forcedouble rx ry rp co G dirfangdirfint j mi

G = 667e-11m[1]=25 x[1]=12 y[1]=25m[2]=15 x[2]=15 y[2]=15m[3]=75 x[3]=02 y[3]=55m[4]=92 x[4]=52 y[4]=25m[5]=66 x[5]=42 y[5]=45

for (mi=1 milt=5 mi++)

fx = 00fy = 00for(j=1 jlt=5 j++)

if (j=mi)rx = x[j]-x[mi]ry = y[j]-y[mi]rp = pow((rxrx+ryry)15) (rx^2 + ry^2) raised to the power of 32co = Gm[mi]m[j]fx = fx + corxrpfy = fy + coryrp

f = sqrt(fxfx + fyfy)dirf = atan(fyfx)angdirf = (dirf(22070))180Systemoutprintf(The force on m[+ mi +] due to the other masses is eN fdegnfangdirf)

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 958

Source code GravityAttract07ajava

Java class for calculating the resultant force on any object(ie either on m1m2 m3 m4 or m5)

import static javalangMathimport javaiopublic class GravityAttract07a

public static void main(String args[])

double[] m = new double[6] mass of objectdouble[] x = new double[6] x-coor of objectdouble[] y = new double[6] y-coor of objectdouble f resultant forcedouble fxfy x amp y component of forcedouble rx ry rp co G dirfangdirfint j

G = 667e-11m[1]=25 x[1]=12 y[1]=25m[2]=15 x[2]=15 y[2]=15m[3]=75 x[3]=02 y[3]=55m[4]=92 x[4]=52 y[4]=25m[5]=66 x[5]=42 y[5]=45BufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin))String mass=Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))

try mass=dataInreadLine()catch(IOException e)Systemoutprintln(Not a valid input value)int mi = IntegerparseInt(mass)fx = 00fy = 00

for(j=1 jlt=5 j++)

if (j=mi)rx = x[j]-x[mi]ry = y[j]-y[mi]rp = pow((rxrx+ryry)15) (rx^2 + ry^2) raised to the power of 32

co = Gm[mi]m[j]fx = fx + corxrpfy = fy + coryrp

f = sqrt(fxfx + fyfy)dirf = toDegrees(atan(fyfx))Systemoutprintf(The force on m[+ mi +] due to the other masses is eN fdegnfdirf)

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1058

983107983119983117983120983125983124983105983124983113983119983118983105983116

983120983112983129983123983113983107983123 983080983123983123983120983090983089983090983090983081

983109983148983141983139983156983154983151983155983156983137983156983145983139 983152983151983156983141983150983156983145983137983148983138983161

983105983138983140983086 983115983144983137983149983145983149 983113983155983149983137983145983148

983117983151983144983140 983115983144983137983148983145983140 983115983137983155983149983145983150

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1158

1 Calculation of electrostatic potential for a group of point charges

You will learn about

- mesh method

a) using a for() loop inside another for() loop

to generate ordered pair (xy) points in a mesh on x-y plane

b) calculate physical value in this case V(xy) forevery point (xy) generated

- saving calculated values into a file

a) making a sequential text file

b) saving in a format that gnuplot can understand

for making a surface plot

- use gnuplot to visualize potential surface

a) making a surface plot from data points in a text file

b) making contour plot to visualize equipotential lines

c) changing the z-axis range to zoom in or zoom out on the surface

d) labelling the plot

e) save as an image the potential surface amp the contour plotf) save as an eps file the potential surface amp the contour plot

- including an eps file into a document

2 Calculation of electrostatic field for a group of point charges

You will learn again

- using mesh method to generate ordered pair (xy) points

in a mesh on x-y plane

You will also learn how to

- calculate vector field values in this case E(xy) for every

generated (xy) mesh point

- save the calculated vector points into a text file

in a format understood by gnuplot

- make a vector field plot using gnuplot

Source code

import static javalangMath

public class estatpot01b

public static void main(String[] args)

double[] q = new double[100]

double[] xq = new double[100]

double[] yq = new double[100]

double V r rx ry k x y

int i n

q[1] = 20e-6 xq[1] = -10 yq[1] = 10

q[2] =-30e-6 xq[2] = 30 yq[2] =-20

q[3] = 30e-6 xq[3] = -20 yq[3] = 40

q[4] =-50e-6 xq[4] = 20 yq[4] = 50

k = 9e9n = 4

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1258

for (x=-5 xlt=5 x = x+02)

for (y=5 ygt=-5 y = y-02)

V = 00

for(i=1 ilt=n i++)

rx = x - xq[i]

ry = y - yq[i]r = sqrt(rxrx+ryry)

if (rlt1e-6)

if (q[i]lt0) V = -10 else V = 10

break

V = V + q[i]r

V = kV

Systemoutprintf(f f fn x y V)

Systemoutprintf(n)

How to do surface plot using Gnuplot

To do a surface plotgnuplotgt splot epotdat with lineTo zoom in and zoom out z axisgnuplotgt set zrange [-2000020000]To convert the graph into imagegnuplotgt set term pngTerminal type set to pngOptions are small color picsize 640 480 Then typegnuplotgt set output epotpngAgain do splot as followgnuplotgt splot epotdat with lineNow the graph is saved into a file in png format named as epotpngTo go back into terminal ouputgnuplotgt set term x11To set to countor plotgnuplotgt set pm3dTo remove countor meshgnuplotgt unset surf

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1358

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1458

983107983119983117983120983125983124983105983124983113983119983118983105983116 983120983112983129983123983113983107983123

983080983123983123983120983090983089983090983090983081

983109983157983148983137983154 983149983141983156983144983151983140

983138983161

983105983138983140983086 983115983144983137983149983145983149 983113983155983149983137983145983148

983117983151983144983140 983115983144983137983148983145983140 983115983137983155983149983145983150

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1558

Free fall body problem

Consider an object thrown upward under gravitational force ( f=mg) with an initial velocity v0The object goes up and then stop at maximum y position ymax before it start to fall downback to the original position at y0 and then to the ground level (See figure)

ymdt

yd m

dt

dvmmaF ampamp====

2

2

t

Y

v1 (t1y1)

v2 (t2y2)

v4 (t4y4)

v5 (t5y5)

vn (tnyn)

v3 (t3y3)

v0 (t0y0)

∆t∆t ∆t ∆t

y1

y0

∆t ∆t

∆yv0

a = g

y0

t0

t0 t1 t2 t3 t4 t5 tn

ymax

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1658

Exact theoretical solution

( ) 2

21

00 at t v yt y ++=

The y projection of the exact solution can be plotted using the following java source code

Java class for solving free fall object under gravity (g = 98 mss)Exact solution y = y0 + v0t + 12at^2

public class freefallsol

public static void main(String args[])double av0yy0deltt

a = -98y0 = 10v0 = 10delt = 00001for(t=0 tlt=05 t=t+delt)

y = y0 + v0t + 05attSystemoutprintf(84f 84fnty)

Complile freefallsoljava to get the plot of the exact solution and then run and save theoutput into a file name as follows

[khamimsetpar65 ~]$ java freefallsol gtffsoldat

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1758

Euler Method

There is another way to see the projection of y position of the object using a numericalmethod called ldquoEuler methodrdquo See the following derivation

t avt v y

t

va

t

yv

∆=∆∆=∆

∆=

∆=

t v y y y y

t avvvv

t v y y y y

t avvvv

t v y y y y

t avvvv

nnnn

nnn

∆+=∆+=

∆+=∆+=

∆+=∆+=

∆+=∆+=

∆+=∆+=

∆+=∆+=

minus

minusminus

1

11

2112

112

1001

001

M

if ∆∆∆∆t rarrrarrrarrrarr 0 then y will close to the exact solution ( ( ) 2

21

00 at t v yt y ++= )

The algorithm 1 Declare the classmethod to be used

2 Declare and assign all variables to be used av0v1yy0y1deltt and j 3 Initialize value to all respective variables a = -98 y0 = 10 v0 = 10

4 Select appropriate value for ∆t

∆t = 00055 Start the loop to calculate the velocity and position for t=0

6 Calculate velocity v1 = v0 + ∆v = v0 + a ∆t

7 Calculate position y1 = y0 + ∆y = y0 + v1 ∆t8 Print out values of t and y1 9 Swap v0 = v1 and y0 = y1

10 Repeat from step 5 and increase the value of t by ∆t (t =t + ∆t) until stoppingcondition is satisfied at t=05

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1858

The java code

Java class for solving free fall object under gravity (g = 98 mss)by numerical method called ldquoEulers Methodrdquo

public class freefalleuler

public static void main(String args[])double av0v1yy0y1delttint j

a = -98y0 = 10v0 = 10delt = 0005

for(t=0 tlt=05 t =t + delt)v1 = v0 + adelty1 = y0 + v1deltSystemoutprintf(84f 84fnty1)v0 = v1y0 = y1

Complile freefalleulerjava for different delt ie 00005 0005 005 and then run and savethe output into a file name as follows

[khamimsetpar65 ~]$ java freefalleuler gtffeuler00005dat[khamimsetpar65 ~]$ java freefalleuler gtffeuler0005dat[khamimsetpar65 ~]$ java freefalleuler gtffeuler005dat

Plotting the results using gnuplot

Using gnuplot plot all the graphs using the following command and compare the results

gnuplotgt plot ffeuler00005dat using 12 title euler delt=00005 with linegt ffeuler0005dat using 12 title euler delt=0005 with linegt ffeuler005dat using 12 title euler deltt=005 with linegt ffsoldat using 12 title exact solution with line

In MS windows

gnuplotgt plot ffeuler00005dat using 12 title euler delt=00005 with lineffeuler0005dat using 12 title euler delt=0005 with lineffeuler005dat using 12 titleeuler delt=005 with line ffsoldat using 12 title exact solution with line

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1958

Gnuplot

The smaller the delt the closer the curve to the exact solution

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2058

983107983119983117983120983125983124983105983124983113983119983118983105983116 983120983112983129983123983113983107983123

983080983123983123983120983090983089983090983090983081

983113983149983152983154983151983158983141983140 983109983157983148983137983154 983149983141983156983144983151983140

983138983161

983105983138983140983086 983115983144983137983149983145983149 983113983155983149983137983145983148

983117983151983144983140 983115983144983137983148983145983140 983115983137983155983149983145983150

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2158

Improved Euler method

Comparison between analytical solution of F=ma with the Euler method shows that although the

method is able to find approximate solution the error is quite significant if computational step is

reduced The error is also cumulative ie as the iteration of the calculation increases the error also gets

bigger hence value furthest away from initial condition has the largest error

The Euler method gives us a way to approximate positions of object with respect to time but requires

an order of magnitude of computational iterations for each order of magnitude of accuracy For large

number of objects this method will certainly consume a great deal of computation time Hence

students are introduced to Initial Half-Step Method an Euler method with a much improved

computational effeciency

Source code

Study of motion of an object with mass m

experiencing a constant force F using

Improved Euler Method (Initial half step method)

In X and Y dimension

import static javalangMath

public class fma04

public static void main(String args[])

double m fx fy x y vo vx vy ax ay t delt

double pi th theta ymax tmax xmax

pi = 40atan(10)

m = 01 in kg

fy = -02 in N (pulling down force)

fx = 00 in N (retarding force)

ay = fym

ax = fxm

vo = 500 in ms

theta = 300 in degrees

th = theta(pi1800) degrees change to radian

vx = vo cos(th) in ms vy = vo sin(th) in ms

y = 12 in m

x = 00 in m

ymax=y initialize var ymax with initial value of y

xmax=00

tmax=00

delt = 001 in s

for(t=00 tlt=300 t=t+delt)

System outprintf(f f fntxy)

if (t==00)vx = vx + 05axdelt

vy = vy + 05aydelt

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2258

else

vx = vx + axdelt

vy = vy + aydelt

x = x + vxdelt

y = y + vydelt

if (ygtymax)

ymax=y

xmax=xtmax=t

if (ylt=00) break

System outprintf( Value of Y maximum is f happened when t = f

nymaxtmax)

System outprintf( at x = fnxmax)

System outprintf( The object reach the ground at x=f and t=fn xt)

gnuplot result

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2358

983107983119983117983120983125983124983105983124983113983119983118983105983116

983120983112983129983123983113983107983123 983080983123983123983120983090983089983090983090983081

983122983141983155983156983151983154983145983150983143 983110983151983154983139983141

983138983161

983105983138983140983086 983115983144983137983149983145983149 983113983155983149983137983145983148

983117983151983144983140 983115983144983137983148983145983140 983115983137983155983149983145983150

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2458

Damped Harmonic Oscillation

Object m is on a flat surface attached to a spring as in figure above The object is pulled

to position A and released so that it oscillates about x =0 Assuming that the force F=ma= -bv-kx where x is position of the object v is the velocity k the spring constant andb is a constant associated with friction with the flat surface

The algorithm

1 Declare the classmethod to be used2 Declare and assign all variables to be used

x t delt k b A m F a v v0 and x03 Declare and assign constants to the respective variables

k = 02 b = 01 A = 01 m = 054 Select appropriate value for ∆t 5 Initialized initial values xo=A vo=00

6 set x=xo v=vo and t = 07 calculate the value of force F = -kx-bv 8 calculate the value of acceleration a = Fm

9 calculate velocity v = vo + ∆v = vo + a ∆t

10 calculate position x = xo + ∆x = xo + v ∆t

11 increment value of t by ∆t 12 print out values of t and x

13 swap the value of xo=x vo=v 14 repeat from step 4 until stopping condition is satisfied (tlt=600)

The java code

public class simplehar01

public static void main(String args[])double x t deltdouble k b A m F a v v0 x0

k = 02b = 01A = 0 1

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2558

m = 05delt = 0001v0 = 00x0 = Ax = x0 v = v0

t = 00while(tlt=600)

F = -kx-bva = Fmv = v0 + adeltx = x0 + vdeltt = t + deltSystemoutprintf(f fntx)x0 = xv0 = v

gnuplot result

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2658

983107983119983117983120983125983124983105983124983113983119983118983105983116 983120983112983129983123983113983107983123

(983123983123983120983090983089983090983090983081

983106983157983145983148983140983145983150983143 983137 983152983144983161983155983145983139983155 983155983151983148983158983141983154

983157983155983145983150983143 983118983141983156983138983141983137983150983155

983138983161

983105983138983140983086 983115983144983137983149983145983149 983113983155983149983137983145983148

983117983151983144983140 983115983144983137983148983145983140 983115983137983155983149983145983150

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2758

How to create a user interface (UI) using Netbeans

Based on the ldquoGravityAttract07ajavardquo source code (shown below) build a user interface usingNetbeans The design of the UI could be look like this (Figure1)

Figure 1

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2858

ldquoGravityAttract07ajavardquo source code

Java class for calculating the resultant force on any object(ie either on m1m2 m3 m4 or m5)

import static javalangMathimport javaiopublic class GravityAttract07a

public static void main(String args[])

double[] m = new double[6] mass of objectdouble[] x = new double[6] x-coor of objectdouble[] y = new double[6] y-coor of objectdouble f resultant forcedouble fxfy x amp y component of forcedouble rx ry rp co G dirfangdirf

int j

G = 667e-11m[1]=25 x[1]=12 y[1]=25m[2]=15 x[2]=15 y[2]=15m[3]=75 x[3]=02 y[3]=55m[4]=92 x[4]=52 y[4]=25m[5]=66 x[5]=42 y[5]=45BufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin))String mass=Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))try mass=dataInreadLine()

catch(IOException e)Systemoutprintln(Not a valid input value)int mi = IntegerparseInt(mass)fx = 00fy = 00

for(j=1 jlt=5 j++)

if (j=mi)rx = x[j]-x[mi]ry = y[j]-y[mi]rp = pow((rxrx+ryry)15) (rx^2 + ry^2) raised to the power of 32co = Gm[mi]m[j]

fx = fx + corxrpfy = fy + coryrp

f = sqrt(fxfx + fyfy)dirf = toDegrees(atan(fyfx))Systemoutprintf(The force on m[+ mi +] due to the other masses is eN fdegnfdirf)

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2958

This example will not show you step by step details procedure since it already explained inthe class and in the e-learning Please refer to the e-learning about how to create a project how tocreate JFrame Form and as well how to create JButton JTextField JLabel JTextPane and so on

First create a new project and give a name eg ldquogravityrdquo and then create JFrame Form andthe following JButton JTextField JLabel JTextPane etc by using ldquoswing paletterdquo as shown in Figure2

Figure 2

To insert a picture into JLabel2 click on the JLabel2 go to properties and select ldquoiconrdquo Onthe icon editor choose file and then select file to locate the directory of your graphicpicture file(Figure 3) Select the file and click OK when finished Make sure you have already created your ownpicture file and save it as jpg png bmp etc format eg ldquogravityjpgrdquo Use any graphic package todraw your diagrampicture or use drawing tools available in Open Office or Microsoft Word

JTextPane1JLabel1

JLabel2

JLabel3JLabel4

JLabel5

JLabel6 JLabel7

JLabel8

JTextField2 JTextField3 JTextField4

JTextF

JButton1JButton2

JButton3

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3058

Figure 3

To write documentation or question eg in JTextPane1 click on the JTextPane1 go toproperties and select ldquotextrdquo Type the documentation in the text editor as shown in Figure 4 Click OKwhen finished

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3158

To do formatting on the text such as superscript and subscript eg in jLabel3 click on the

JLabel3 go to properties and select ldquotextrdquo Type the documentationmessage on the text editor asshown in Figure 5 as follows

eg

lthtmlgtOn which particle (mltsubgt1ltsubgtmltsubgt5ltsubgt) you want to calculate theresultant force

Use sup instead if you want a superscriptThe result will look like this

Click OK when finished

Figure 5

To do the calculation you need to put the coding similar like ldquoGravityAttract07ajavardquo and put itinto the ldquocalculaterdquo button (this is ldquoJButton1rdquo) You can copy ldquoGravityAttract07a javardquo source code

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3258

paste it and then do a few alterations First click on the ldquocalculaterdquo button right click choose Eventsand then choose Action then actionPerformed (Figure 6) It will bring you to where you should insertyour coding (Figure 7) You can write your own coding or copypaste any coding from somewhereelse (in this case from ldquoGravityAttract07ajavardquo)

Figure 6

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3358

Figure 7

Copy ldquoGravityAttract07ajavardquo source code and paste to the place after ldquo TODO add youhandling code hererdquo see Figure 7 To see which part has been modified the coding in Figure 7 hasbeen color coded for better understanding as follows

private void jButton1ActionPerformed(javaawteventActionEvent evt) TODO add your handling code here

double[] m = new double[6] mass of objectdouble[] x = new double[6] x-coor of objectdouble[] y = new double[6] y-coor of objectdouble f resultant forcedouble fxfy x amp y component of forcedouble rx ry rp co G dirfangdirfDecimalFormat dff dfangdirfint j mi

G = 667e-11m[1]=25 x[1]=12 y[1]=25m[2]=15 x[2]=15 y[2]=15m[3]=75 x[3]=02 y[3]=55m[4]=92 x[4]=52 y[4]=25m[5]=66 x[5]=42 y[5]=45

BufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin)) String mass= Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3458

catch(IOException e)Systemoutprintln(Not a valid input value) int mi = IntegerparseInt(mass)mi = IntegerparseInt(jTextField1getText())fx = 00fy = 00for(j=1 jlt=5 j++)

if (j=mi)rx = x[j]-x[mi]ry = y[j]-y[mi]rp = Mathpow((rxrx+ryry)15) (rx^2 + ry^2) raised to the power of 32co = Gm[mi]m[j]fx = fx + corxrpfy = fy + coryrp

f = Mathsqrt(fxfx + fyfy)

dirf = Mathatan(fyfx)angdirf = (dirf(22070))1800 Systemoutprintf(ldquoThe resultant force on particle [ldquo+ mi +rdquo] due to the other masses is

14e N 22f degnrdquofangdirf)dff = new javatextDecimalFormat(ldquoE00rdquo)dfangdirf = new DecimalFormat(ldquo000rdquo)

jTextField2setText(ldquordquo + mi) jTextField3setText(ldquordquo + dffformat(f)) jTextField4setText(ldquordquo + dfangdirfformat(angdirf))

Note

Brown = automatically generatedBlue = the original code from ldquoGravityAttract07ajavardquoGreen = coding from ldquoGravityAttract07ajavardquo which were being disabled due to

incompatibilities with NetbeansRed = a new inserted coding a few of them is to rephrasesubstitute the green one

Javascripts command line useBufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin))String mass=Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))try mass=dataInreadLine()catch(IOException e)Systemoutprintln(Not a valid input value)

int mi = IntegerparseInt(mass)

to read any input from the keyboard typed by the user while Netbeans uses egldquoIntegerparseInt(jTextField1getText())rdquo etc Javascripts command line uses ldquoSystemoutprintfrdquo to format the printed output whileNetbeans uses more complicated way to do the same job (see below)

Javascript command line

Systemoutprintf(The resultant force on particle [+ mi +] due to the other masses is 14e N22f degnfangdirf)

Netbeansdff = new javatextDecimalFormat(ldquo0E00rdquo)df di f D i lF t(ldquo0 00rdquo)

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3558

jTextField2setText(ldquordquo + mi) jTextField3setText(ldquordquo + dffformat(f)) jTextField4setText(ldquordquo + dfangdirfformat(angdirf))

ldquodffrdquo and ldquodfangdirfrdquo are new variables that should be declared and added to format thenumbers into decimal format ie ldquo0E00rdquo to get a scientific format eg ldquo20019E-10rdquo and ldquo000rdquoto get a decimal number eg ldquo-1570rdquo

To use DecimalFormat you have to put ldquoimport javatextDecimalFormatrdquo in the top of thecoding as shown in Figure 8

Figure 8

To reset the calculation you need to put coding into the ldquoresetrdquo button (this is ldquoJButton2rdquo)First click on the ldquoresetrdquo button right click choose Events and then choose Action thenactionPerformed (similar like in Figure 6) It will bring you to where you should insert your coding(Figure 9) Write the following coding (Figure 9)

Figure 9

To exit the program you need to put coding into the ldquoexitrdquo button (this is ldquoJButton3rdquo) Firstclick on the ldquoexitrdquo button right click choose Events and then choose Action then actionPerformed(similar like in Figure 6) It will bring you to where you should insert your coding (Figure 10) Writethe following coding (Figure 10)

Figure 10

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3658

Once you finished now you can run and see the result If everything is fine then the output wilbe look like this (Figure 11)

Figure 11

Remember when you compile and run ldquoGravityAttract07ajavardquo using Javascripts commandline once you compile and run the result was looked like this

On which particle you want to calculate the resulting force 2The resultant force on particle [2] due to the other masses is 30673e-10 N 8837 deg

SEE THE DIFFERENCES AT THE USER INTERFACE

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3758

8th Step

Written by Mohd Khalid Kasmin

Monday 27 August 2007

Programming using command line interface (CLI) has its advantages but todays computer users are so used to graphical user

interface (GUI) that we just can no longer be contented with writing application that do not use the GUI Hence starting from this 8thstep we are going to embark on learning how to make java application that utilises the the GUI

It is possible to write a GUI application with the text based tools that we have been using for these past 7 steps but the job is so

messy that most of you will definitely be turnoff by it So to make life slightly easier well use an integrated development

environment (IDE) known as NetBeans specifically NetBeans 551

0 First ssh to the our server Next once logged in just type nice -n15 netbeans amp on the terminal If you had sshed with the -Y

switch after a few tens of seconds NetBeans should emerge on your display screen

One important point to note Since our server can readily handle up to 100 users (maybe even

more) doing work in CLI mode this machine will certainly be taxed to its maximum capability once

we start using graphical based application NetBeans 551 uses a geat deal of computer memory

and with the 4Gbytes RAM server it can only accomodate up to 25 users concurrently using

NetBeans if the users use the application wisely What is meant by wisely here is that (a) when

starting the NetBeans application please use it together with the nice -n15 command (b) in using

the NetBeans please make sure you only have one project open at a time and (c) please close your

project everytime before quitting the NetBeans

Home Profile Teaching Links Contact Me My Work

Newsflash

Yesterday all servers in the US went out on strike in a bid to get more RAM and better CPUs A spokes person said that the

need for better RAM was due to some fool increasing the front-side bus speed In future busses will be told to slow down in

residential motherboards

search

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3858

1

2

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3958

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4058

[ Back ]

5

Last Updated ( Wednesday 31 October 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4158

8th Step (Cont)

Written by Mohd Khalid Kasmin

Monday 27 August 2007

Up to this point we have define the name of our project Next we construct a class and to follow what we already know we name the class the sam

project name latihan01

6

7

Home Profile Teaching Links Contact Me My Work

Newsflash

Joomla 10 - Experience the Freedom It has never been easier to create your own dynamic site Manage all your content from

the best CMS admin interface

search

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4258

8

9

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4358

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4458

8th Step (Cont still)

Written by Mohd Khalid Kasmin

Monday 27 August 2007

In item (9) we see that Netbeans does not provide us with the main method So to follow what we already know we now put in the

main method

10

Home Profile Teaching Links Contact Me My Work

Newsflash

Aoccdrnig to a rscheearch at an Elingsh uinervtisy it deosnt mttaer in waht oredr the ltteers in a wrod are the olny iprmoetnt

tihng is taht frist and lsat ltteer is at the rghit pclae The rset can be a toatl mses and you can sitll raed it wouthit porbelm Tihs is

bcuseae we do not raed ervey lteter by itslef but the wrod as a wlohe

search

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4558

11

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4658

12

13

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4758

14

15

Last Updated ( Monday 27 August 2007 )

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4858

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4958

9th Step

Thursday 06 September 2007

Step by Step into Java Programming

for

University Physics Student

By

Mohd Khalid Kasmin

A Building graphical user interface - preparing the JFrame Form

At this stage of our programming experience with Java and NetBeans please follow strictly the steps outline in this article First and

make a new GUI project make sure there is no other project that already been loaded by Netbeans Please close any project displayed

1 Create new project

- On the main menu click on File --gt New Project (note Close first any previous project)

- Specify project name amp project location - Untick Create Main Class item

- Click on the Finish button

Home Profile Teaching Links Contact Me My Work

Newsflash

Aoccdrnig to a rscheearch at an Elingsh uinervtisy it deosnt mttaer in waht oredr the ltteers in a wrod are the olny iprmoetnt

tihng is taht frist and lsat ltteer is at the rghit pclae The rset can be a toatl mses and you can sitll raed it wouthit porbelm Tihs is

bcuseae we do not raed ervey lteter by itslef but the wrod as a wlohe

search

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5058

Figure 91

2 Create UI class

- Right click on the project name in the Project pane

on the popup menu select New --gt JFrame Form

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5158

Figure 92

- In the New JFrame Form dialog specify the Class Name and the Package name

eg Class Name multiplierUI Package mymultiplier

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5258

Figure 93

After clicking on the Finish button we should get something like the following

Figure 94

Last Updated ( Tuesday 18 September 2007 )

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5358

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5458

9th Step (Cont)

Written by Mohd Khalid Kasmin

Tuesday 11 September 2007

Step by Step into Java Programming

for University Physics Student

By

Mohd Khalid Kasmin

B Building user interface (UI) - putting in the components

Now that we have a proper blank form we can start filling the form with the suitable graphical components For introduction to

GUI programming an appropriate application to build (for a physics student) is a simple solver So our first exercise is to build an

application that takes two numbers and multiply them and display the result

With GUI we usually give input values to an application via input boxes that are labelled When all inputs are typed in properlywe click on a button to ask the application to execute the job Hence in this exercise well do just that The target now is to

construct a user interface that looks like Figure 95 below

Figure 95

Home Profile Teaching Links Contact Me My Work

Newsflash

Joomla 10 - Experience the Freedom It has never been easier to create your own dynamic site Manage all your content from

the best CMS admin interface

search

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5558

[ Back ]

1 Problemapplication description box

- In the palette pane click on Swing component named JTextPane (Note click just once) Move the mouse pointer on top of

your JFrame (Note just move not drag) When you see a little white box with orange border position it somewhere near the

top part of your JFrame and click once The JTextPane is then attached on the JFrame Addjust its size with the help of the

sizing hook around the perimeter of the JTextPane

- Edit the text property of the JTextPane with the description of your application

2 Input boxes

- In the palette pane click on Swing component named JTextField Next move the mouse pointer on top of your JFrame

When you see a white box with the word jTextField1 position it to the left and under the JTextPane1

- Click on Swing component JTextField in the palette pane again Move the mouse pointer on top of the JFrame you should

get a second JTextField named jTextField2 Position it to the right of jTextField1

Last Updated ( Tuesday 11 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5658

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5758

multiplication if a b and c were first declared as double or float or int variables So the code of the calculation should look like the

following

double a b c

a =

b =

c = a b

Clearly variables a and b need to be assigned with values of typed double Now looking back at inputboxes A and B we made

those boxes using the JTextField component meaning that any data or value written in a JTextField is of typed Text or String even

if the value displayed in the box is ldquo125rdquo But f irst here there is this task of getting to that Text (or String) values ldquo125rdquo and ldquo72rdquo

In Java we can get to a text in JTextField component using the getText() method So to retrieve values in jTextField1 and

jTextField2 we can use

jTextField1getText()

and

jTextField2getText()

But those values are still in text mode and we need another method to convert the data mode f rom text to double For that we can

use the method DoubleparseDouble() Thus the whole process of retrieve text -gt convert text to double -gt assign to double

variable can be done using

a = DoubleparseDouble( jTextField1getText() )

and

b = DoubleparseDouble( jTextField2getText() )

Hence the code for performing the multiplication becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

Up to this point we have the variable c as the result of the multiplication of a and b So the next thing to do is to display this

result on the JLabel that we have already prepared before

A x B = _____

The text A x B = ______ was made using JLabel component and its variable name is jLabel3 Since the variable c is of type

double and text property of JLabel is of type String we need to convert it first to String data type The method to convert a double

to String is DoubletoString() So we can write the code as DoubletoString(c) After getting the text we can now use it to be the

text displayed on jLabel3 To do that we use the method setText() Hence the appropriate code for displaying the result is

jLabel3setText( A x B = + DoubletoString(c) )

And the full code for our calculation becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

jLabel3setText( A x B = + DoubletoString(c) )

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5858

[ Back ]

Last Updated ( Tuesday 18 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

Page 6: Computional Physics

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 658

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 758

Source code GravityAttract05ajava

Java class for calculating the resultant force on an object m1due to for other objects m2 m3 m4 and m5

import static javalangMath

public class GravityAttract05a

public static void main(String args[])

double[] m = new double[6] mass of object double[] x = new double[6] x-coor of object double[] y = new double[6] y-coor of object double f resultant forcedouble fxfy x amp y component of force double rx ry rp co Gint j

G = 667e-11m[1]=25 x[1]=12 y[1]=25m[2]=15 x[2]=15 y[2]=15m[3]=75 x[3]=02 y[3]=55m[4]=92 x[4]=52 y[4]=25m[5]=66 x[5]=42 y[5]=45

fx = 00fy = 00

for(j=2 jlt=5 j++)rx = x[j]-x[1]ry = y[j]-y[1]rp = pow((rxrx+ryry)15) (rx^2 + ry^2) raised to the power of 32 co = Gm[1]m[j]fx = fx + corxrpfy = fy + coryrp

f = sqrt(fxfx + fyfy)Systemoutprintf(ntThe force on m[1] due to the other masses is e Nnnf)

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 858

Source code GravityAttract06ajava

Java class for calculating the resultant force on each object(on m1 through m5)

import static javalangMath

public class GravityAttract06a

public static void main(String args[])

double[] m = new double[6] mass of objectdouble[] x = new double[6] x-coor of objectdouble[] y = new double[6] y-coor of objectdouble f resultant forcedouble fxfy x amp y component of forcedouble rx ry rp co G dirfangdirfint j mi

G = 667e-11m[1]=25 x[1]=12 y[1]=25m[2]=15 x[2]=15 y[2]=15m[3]=75 x[3]=02 y[3]=55m[4]=92 x[4]=52 y[4]=25m[5]=66 x[5]=42 y[5]=45

for (mi=1 milt=5 mi++)

fx = 00fy = 00for(j=1 jlt=5 j++)

if (j=mi)rx = x[j]-x[mi]ry = y[j]-y[mi]rp = pow((rxrx+ryry)15) (rx^2 + ry^2) raised to the power of 32co = Gm[mi]m[j]fx = fx + corxrpfy = fy + coryrp

f = sqrt(fxfx + fyfy)dirf = atan(fyfx)angdirf = (dirf(22070))180Systemoutprintf(The force on m[+ mi +] due to the other masses is eN fdegnfangdirf)

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 958

Source code GravityAttract07ajava

Java class for calculating the resultant force on any object(ie either on m1m2 m3 m4 or m5)

import static javalangMathimport javaiopublic class GravityAttract07a

public static void main(String args[])

double[] m = new double[6] mass of objectdouble[] x = new double[6] x-coor of objectdouble[] y = new double[6] y-coor of objectdouble f resultant forcedouble fxfy x amp y component of forcedouble rx ry rp co G dirfangdirfint j

G = 667e-11m[1]=25 x[1]=12 y[1]=25m[2]=15 x[2]=15 y[2]=15m[3]=75 x[3]=02 y[3]=55m[4]=92 x[4]=52 y[4]=25m[5]=66 x[5]=42 y[5]=45BufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin))String mass=Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))

try mass=dataInreadLine()catch(IOException e)Systemoutprintln(Not a valid input value)int mi = IntegerparseInt(mass)fx = 00fy = 00

for(j=1 jlt=5 j++)

if (j=mi)rx = x[j]-x[mi]ry = y[j]-y[mi]rp = pow((rxrx+ryry)15) (rx^2 + ry^2) raised to the power of 32

co = Gm[mi]m[j]fx = fx + corxrpfy = fy + coryrp

f = sqrt(fxfx + fyfy)dirf = toDegrees(atan(fyfx))Systemoutprintf(The force on m[+ mi +] due to the other masses is eN fdegnfdirf)

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1058

983107983119983117983120983125983124983105983124983113983119983118983105983116

983120983112983129983123983113983107983123 983080983123983123983120983090983089983090983090983081

983109983148983141983139983156983154983151983155983156983137983156983145983139 983152983151983156983141983150983156983145983137983148983138983161

983105983138983140983086 983115983144983137983149983145983149 983113983155983149983137983145983148

983117983151983144983140 983115983144983137983148983145983140 983115983137983155983149983145983150

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1158

1 Calculation of electrostatic potential for a group of point charges

You will learn about

- mesh method

a) using a for() loop inside another for() loop

to generate ordered pair (xy) points in a mesh on x-y plane

b) calculate physical value in this case V(xy) forevery point (xy) generated

- saving calculated values into a file

a) making a sequential text file

b) saving in a format that gnuplot can understand

for making a surface plot

- use gnuplot to visualize potential surface

a) making a surface plot from data points in a text file

b) making contour plot to visualize equipotential lines

c) changing the z-axis range to zoom in or zoom out on the surface

d) labelling the plot

e) save as an image the potential surface amp the contour plotf) save as an eps file the potential surface amp the contour plot

- including an eps file into a document

2 Calculation of electrostatic field for a group of point charges

You will learn again

- using mesh method to generate ordered pair (xy) points

in a mesh on x-y plane

You will also learn how to

- calculate vector field values in this case E(xy) for every

generated (xy) mesh point

- save the calculated vector points into a text file

in a format understood by gnuplot

- make a vector field plot using gnuplot

Source code

import static javalangMath

public class estatpot01b

public static void main(String[] args)

double[] q = new double[100]

double[] xq = new double[100]

double[] yq = new double[100]

double V r rx ry k x y

int i n

q[1] = 20e-6 xq[1] = -10 yq[1] = 10

q[2] =-30e-6 xq[2] = 30 yq[2] =-20

q[3] = 30e-6 xq[3] = -20 yq[3] = 40

q[4] =-50e-6 xq[4] = 20 yq[4] = 50

k = 9e9n = 4

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1258

for (x=-5 xlt=5 x = x+02)

for (y=5 ygt=-5 y = y-02)

V = 00

for(i=1 ilt=n i++)

rx = x - xq[i]

ry = y - yq[i]r = sqrt(rxrx+ryry)

if (rlt1e-6)

if (q[i]lt0) V = -10 else V = 10

break

V = V + q[i]r

V = kV

Systemoutprintf(f f fn x y V)

Systemoutprintf(n)

How to do surface plot using Gnuplot

To do a surface plotgnuplotgt splot epotdat with lineTo zoom in and zoom out z axisgnuplotgt set zrange [-2000020000]To convert the graph into imagegnuplotgt set term pngTerminal type set to pngOptions are small color picsize 640 480 Then typegnuplotgt set output epotpngAgain do splot as followgnuplotgt splot epotdat with lineNow the graph is saved into a file in png format named as epotpngTo go back into terminal ouputgnuplotgt set term x11To set to countor plotgnuplotgt set pm3dTo remove countor meshgnuplotgt unset surf

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1358

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1458

983107983119983117983120983125983124983105983124983113983119983118983105983116 983120983112983129983123983113983107983123

983080983123983123983120983090983089983090983090983081

983109983157983148983137983154 983149983141983156983144983151983140

983138983161

983105983138983140983086 983115983144983137983149983145983149 983113983155983149983137983145983148

983117983151983144983140 983115983144983137983148983145983140 983115983137983155983149983145983150

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1558

Free fall body problem

Consider an object thrown upward under gravitational force ( f=mg) with an initial velocity v0The object goes up and then stop at maximum y position ymax before it start to fall downback to the original position at y0 and then to the ground level (See figure)

ymdt

yd m

dt

dvmmaF ampamp====

2

2

t

Y

v1 (t1y1)

v2 (t2y2)

v4 (t4y4)

v5 (t5y5)

vn (tnyn)

v3 (t3y3)

v0 (t0y0)

∆t∆t ∆t ∆t

y1

y0

∆t ∆t

∆yv0

a = g

y0

t0

t0 t1 t2 t3 t4 t5 tn

ymax

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1658

Exact theoretical solution

( ) 2

21

00 at t v yt y ++=

The y projection of the exact solution can be plotted using the following java source code

Java class for solving free fall object under gravity (g = 98 mss)Exact solution y = y0 + v0t + 12at^2

public class freefallsol

public static void main(String args[])double av0yy0deltt

a = -98y0 = 10v0 = 10delt = 00001for(t=0 tlt=05 t=t+delt)

y = y0 + v0t + 05attSystemoutprintf(84f 84fnty)

Complile freefallsoljava to get the plot of the exact solution and then run and save theoutput into a file name as follows

[khamimsetpar65 ~]$ java freefallsol gtffsoldat

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1758

Euler Method

There is another way to see the projection of y position of the object using a numericalmethod called ldquoEuler methodrdquo See the following derivation

t avt v y

t

va

t

yv

∆=∆∆=∆

∆=

∆=

t v y y y y

t avvvv

t v y y y y

t avvvv

t v y y y y

t avvvv

nnnn

nnn

∆+=∆+=

∆+=∆+=

∆+=∆+=

∆+=∆+=

∆+=∆+=

∆+=∆+=

minus

minusminus

1

11

2112

112

1001

001

M

if ∆∆∆∆t rarrrarrrarrrarr 0 then y will close to the exact solution ( ( ) 2

21

00 at t v yt y ++= )

The algorithm 1 Declare the classmethod to be used

2 Declare and assign all variables to be used av0v1yy0y1deltt and j 3 Initialize value to all respective variables a = -98 y0 = 10 v0 = 10

4 Select appropriate value for ∆t

∆t = 00055 Start the loop to calculate the velocity and position for t=0

6 Calculate velocity v1 = v0 + ∆v = v0 + a ∆t

7 Calculate position y1 = y0 + ∆y = y0 + v1 ∆t8 Print out values of t and y1 9 Swap v0 = v1 and y0 = y1

10 Repeat from step 5 and increase the value of t by ∆t (t =t + ∆t) until stoppingcondition is satisfied at t=05

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1858

The java code

Java class for solving free fall object under gravity (g = 98 mss)by numerical method called ldquoEulers Methodrdquo

public class freefalleuler

public static void main(String args[])double av0v1yy0y1delttint j

a = -98y0 = 10v0 = 10delt = 0005

for(t=0 tlt=05 t =t + delt)v1 = v0 + adelty1 = y0 + v1deltSystemoutprintf(84f 84fnty1)v0 = v1y0 = y1

Complile freefalleulerjava for different delt ie 00005 0005 005 and then run and savethe output into a file name as follows

[khamimsetpar65 ~]$ java freefalleuler gtffeuler00005dat[khamimsetpar65 ~]$ java freefalleuler gtffeuler0005dat[khamimsetpar65 ~]$ java freefalleuler gtffeuler005dat

Plotting the results using gnuplot

Using gnuplot plot all the graphs using the following command and compare the results

gnuplotgt plot ffeuler00005dat using 12 title euler delt=00005 with linegt ffeuler0005dat using 12 title euler delt=0005 with linegt ffeuler005dat using 12 title euler deltt=005 with linegt ffsoldat using 12 title exact solution with line

In MS windows

gnuplotgt plot ffeuler00005dat using 12 title euler delt=00005 with lineffeuler0005dat using 12 title euler delt=0005 with lineffeuler005dat using 12 titleeuler delt=005 with line ffsoldat using 12 title exact solution with line

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1958

Gnuplot

The smaller the delt the closer the curve to the exact solution

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2058

983107983119983117983120983125983124983105983124983113983119983118983105983116 983120983112983129983123983113983107983123

983080983123983123983120983090983089983090983090983081

983113983149983152983154983151983158983141983140 983109983157983148983137983154 983149983141983156983144983151983140

983138983161

983105983138983140983086 983115983144983137983149983145983149 983113983155983149983137983145983148

983117983151983144983140 983115983144983137983148983145983140 983115983137983155983149983145983150

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2158

Improved Euler method

Comparison between analytical solution of F=ma with the Euler method shows that although the

method is able to find approximate solution the error is quite significant if computational step is

reduced The error is also cumulative ie as the iteration of the calculation increases the error also gets

bigger hence value furthest away from initial condition has the largest error

The Euler method gives us a way to approximate positions of object with respect to time but requires

an order of magnitude of computational iterations for each order of magnitude of accuracy For large

number of objects this method will certainly consume a great deal of computation time Hence

students are introduced to Initial Half-Step Method an Euler method with a much improved

computational effeciency

Source code

Study of motion of an object with mass m

experiencing a constant force F using

Improved Euler Method (Initial half step method)

In X and Y dimension

import static javalangMath

public class fma04

public static void main(String args[])

double m fx fy x y vo vx vy ax ay t delt

double pi th theta ymax tmax xmax

pi = 40atan(10)

m = 01 in kg

fy = -02 in N (pulling down force)

fx = 00 in N (retarding force)

ay = fym

ax = fxm

vo = 500 in ms

theta = 300 in degrees

th = theta(pi1800) degrees change to radian

vx = vo cos(th) in ms vy = vo sin(th) in ms

y = 12 in m

x = 00 in m

ymax=y initialize var ymax with initial value of y

xmax=00

tmax=00

delt = 001 in s

for(t=00 tlt=300 t=t+delt)

System outprintf(f f fntxy)

if (t==00)vx = vx + 05axdelt

vy = vy + 05aydelt

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2258

else

vx = vx + axdelt

vy = vy + aydelt

x = x + vxdelt

y = y + vydelt

if (ygtymax)

ymax=y

xmax=xtmax=t

if (ylt=00) break

System outprintf( Value of Y maximum is f happened when t = f

nymaxtmax)

System outprintf( at x = fnxmax)

System outprintf( The object reach the ground at x=f and t=fn xt)

gnuplot result

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2358

983107983119983117983120983125983124983105983124983113983119983118983105983116

983120983112983129983123983113983107983123 983080983123983123983120983090983089983090983090983081

983122983141983155983156983151983154983145983150983143 983110983151983154983139983141

983138983161

983105983138983140983086 983115983144983137983149983145983149 983113983155983149983137983145983148

983117983151983144983140 983115983144983137983148983145983140 983115983137983155983149983145983150

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2458

Damped Harmonic Oscillation

Object m is on a flat surface attached to a spring as in figure above The object is pulled

to position A and released so that it oscillates about x =0 Assuming that the force F=ma= -bv-kx where x is position of the object v is the velocity k the spring constant andb is a constant associated with friction with the flat surface

The algorithm

1 Declare the classmethod to be used2 Declare and assign all variables to be used

x t delt k b A m F a v v0 and x03 Declare and assign constants to the respective variables

k = 02 b = 01 A = 01 m = 054 Select appropriate value for ∆t 5 Initialized initial values xo=A vo=00

6 set x=xo v=vo and t = 07 calculate the value of force F = -kx-bv 8 calculate the value of acceleration a = Fm

9 calculate velocity v = vo + ∆v = vo + a ∆t

10 calculate position x = xo + ∆x = xo + v ∆t

11 increment value of t by ∆t 12 print out values of t and x

13 swap the value of xo=x vo=v 14 repeat from step 4 until stopping condition is satisfied (tlt=600)

The java code

public class simplehar01

public static void main(String args[])double x t deltdouble k b A m F a v v0 x0

k = 02b = 01A = 0 1

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2558

m = 05delt = 0001v0 = 00x0 = Ax = x0 v = v0

t = 00while(tlt=600)

F = -kx-bva = Fmv = v0 + adeltx = x0 + vdeltt = t + deltSystemoutprintf(f fntx)x0 = xv0 = v

gnuplot result

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2658

983107983119983117983120983125983124983105983124983113983119983118983105983116 983120983112983129983123983113983107983123

(983123983123983120983090983089983090983090983081

983106983157983145983148983140983145983150983143 983137 983152983144983161983155983145983139983155 983155983151983148983158983141983154

983157983155983145983150983143 983118983141983156983138983141983137983150983155

983138983161

983105983138983140983086 983115983144983137983149983145983149 983113983155983149983137983145983148

983117983151983144983140 983115983144983137983148983145983140 983115983137983155983149983145983150

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2758

How to create a user interface (UI) using Netbeans

Based on the ldquoGravityAttract07ajavardquo source code (shown below) build a user interface usingNetbeans The design of the UI could be look like this (Figure1)

Figure 1

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2858

ldquoGravityAttract07ajavardquo source code

Java class for calculating the resultant force on any object(ie either on m1m2 m3 m4 or m5)

import static javalangMathimport javaiopublic class GravityAttract07a

public static void main(String args[])

double[] m = new double[6] mass of objectdouble[] x = new double[6] x-coor of objectdouble[] y = new double[6] y-coor of objectdouble f resultant forcedouble fxfy x amp y component of forcedouble rx ry rp co G dirfangdirf

int j

G = 667e-11m[1]=25 x[1]=12 y[1]=25m[2]=15 x[2]=15 y[2]=15m[3]=75 x[3]=02 y[3]=55m[4]=92 x[4]=52 y[4]=25m[5]=66 x[5]=42 y[5]=45BufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin))String mass=Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))try mass=dataInreadLine()

catch(IOException e)Systemoutprintln(Not a valid input value)int mi = IntegerparseInt(mass)fx = 00fy = 00

for(j=1 jlt=5 j++)

if (j=mi)rx = x[j]-x[mi]ry = y[j]-y[mi]rp = pow((rxrx+ryry)15) (rx^2 + ry^2) raised to the power of 32co = Gm[mi]m[j]

fx = fx + corxrpfy = fy + coryrp

f = sqrt(fxfx + fyfy)dirf = toDegrees(atan(fyfx))Systemoutprintf(The force on m[+ mi +] due to the other masses is eN fdegnfdirf)

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2958

This example will not show you step by step details procedure since it already explained inthe class and in the e-learning Please refer to the e-learning about how to create a project how tocreate JFrame Form and as well how to create JButton JTextField JLabel JTextPane and so on

First create a new project and give a name eg ldquogravityrdquo and then create JFrame Form andthe following JButton JTextField JLabel JTextPane etc by using ldquoswing paletterdquo as shown in Figure2

Figure 2

To insert a picture into JLabel2 click on the JLabel2 go to properties and select ldquoiconrdquo Onthe icon editor choose file and then select file to locate the directory of your graphicpicture file(Figure 3) Select the file and click OK when finished Make sure you have already created your ownpicture file and save it as jpg png bmp etc format eg ldquogravityjpgrdquo Use any graphic package todraw your diagrampicture or use drawing tools available in Open Office or Microsoft Word

JTextPane1JLabel1

JLabel2

JLabel3JLabel4

JLabel5

JLabel6 JLabel7

JLabel8

JTextField2 JTextField3 JTextField4

JTextF

JButton1JButton2

JButton3

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3058

Figure 3

To write documentation or question eg in JTextPane1 click on the JTextPane1 go toproperties and select ldquotextrdquo Type the documentation in the text editor as shown in Figure 4 Click OKwhen finished

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3158

To do formatting on the text such as superscript and subscript eg in jLabel3 click on the

JLabel3 go to properties and select ldquotextrdquo Type the documentationmessage on the text editor asshown in Figure 5 as follows

eg

lthtmlgtOn which particle (mltsubgt1ltsubgtmltsubgt5ltsubgt) you want to calculate theresultant force

Use sup instead if you want a superscriptThe result will look like this

Click OK when finished

Figure 5

To do the calculation you need to put the coding similar like ldquoGravityAttract07ajavardquo and put itinto the ldquocalculaterdquo button (this is ldquoJButton1rdquo) You can copy ldquoGravityAttract07a javardquo source code

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3258

paste it and then do a few alterations First click on the ldquocalculaterdquo button right click choose Eventsand then choose Action then actionPerformed (Figure 6) It will bring you to where you should insertyour coding (Figure 7) You can write your own coding or copypaste any coding from somewhereelse (in this case from ldquoGravityAttract07ajavardquo)

Figure 6

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3358

Figure 7

Copy ldquoGravityAttract07ajavardquo source code and paste to the place after ldquo TODO add youhandling code hererdquo see Figure 7 To see which part has been modified the coding in Figure 7 hasbeen color coded for better understanding as follows

private void jButton1ActionPerformed(javaawteventActionEvent evt) TODO add your handling code here

double[] m = new double[6] mass of objectdouble[] x = new double[6] x-coor of objectdouble[] y = new double[6] y-coor of objectdouble f resultant forcedouble fxfy x amp y component of forcedouble rx ry rp co G dirfangdirfDecimalFormat dff dfangdirfint j mi

G = 667e-11m[1]=25 x[1]=12 y[1]=25m[2]=15 x[2]=15 y[2]=15m[3]=75 x[3]=02 y[3]=55m[4]=92 x[4]=52 y[4]=25m[5]=66 x[5]=42 y[5]=45

BufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin)) String mass= Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3458

catch(IOException e)Systemoutprintln(Not a valid input value) int mi = IntegerparseInt(mass)mi = IntegerparseInt(jTextField1getText())fx = 00fy = 00for(j=1 jlt=5 j++)

if (j=mi)rx = x[j]-x[mi]ry = y[j]-y[mi]rp = Mathpow((rxrx+ryry)15) (rx^2 + ry^2) raised to the power of 32co = Gm[mi]m[j]fx = fx + corxrpfy = fy + coryrp

f = Mathsqrt(fxfx + fyfy)

dirf = Mathatan(fyfx)angdirf = (dirf(22070))1800 Systemoutprintf(ldquoThe resultant force on particle [ldquo+ mi +rdquo] due to the other masses is

14e N 22f degnrdquofangdirf)dff = new javatextDecimalFormat(ldquoE00rdquo)dfangdirf = new DecimalFormat(ldquo000rdquo)

jTextField2setText(ldquordquo + mi) jTextField3setText(ldquordquo + dffformat(f)) jTextField4setText(ldquordquo + dfangdirfformat(angdirf))

Note

Brown = automatically generatedBlue = the original code from ldquoGravityAttract07ajavardquoGreen = coding from ldquoGravityAttract07ajavardquo which were being disabled due to

incompatibilities with NetbeansRed = a new inserted coding a few of them is to rephrasesubstitute the green one

Javascripts command line useBufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin))String mass=Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))try mass=dataInreadLine()catch(IOException e)Systemoutprintln(Not a valid input value)

int mi = IntegerparseInt(mass)

to read any input from the keyboard typed by the user while Netbeans uses egldquoIntegerparseInt(jTextField1getText())rdquo etc Javascripts command line uses ldquoSystemoutprintfrdquo to format the printed output whileNetbeans uses more complicated way to do the same job (see below)

Javascript command line

Systemoutprintf(The resultant force on particle [+ mi +] due to the other masses is 14e N22f degnfangdirf)

Netbeansdff = new javatextDecimalFormat(ldquo0E00rdquo)df di f D i lF t(ldquo0 00rdquo)

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3558

jTextField2setText(ldquordquo + mi) jTextField3setText(ldquordquo + dffformat(f)) jTextField4setText(ldquordquo + dfangdirfformat(angdirf))

ldquodffrdquo and ldquodfangdirfrdquo are new variables that should be declared and added to format thenumbers into decimal format ie ldquo0E00rdquo to get a scientific format eg ldquo20019E-10rdquo and ldquo000rdquoto get a decimal number eg ldquo-1570rdquo

To use DecimalFormat you have to put ldquoimport javatextDecimalFormatrdquo in the top of thecoding as shown in Figure 8

Figure 8

To reset the calculation you need to put coding into the ldquoresetrdquo button (this is ldquoJButton2rdquo)First click on the ldquoresetrdquo button right click choose Events and then choose Action thenactionPerformed (similar like in Figure 6) It will bring you to where you should insert your coding(Figure 9) Write the following coding (Figure 9)

Figure 9

To exit the program you need to put coding into the ldquoexitrdquo button (this is ldquoJButton3rdquo) Firstclick on the ldquoexitrdquo button right click choose Events and then choose Action then actionPerformed(similar like in Figure 6) It will bring you to where you should insert your coding (Figure 10) Writethe following coding (Figure 10)

Figure 10

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3658

Once you finished now you can run and see the result If everything is fine then the output wilbe look like this (Figure 11)

Figure 11

Remember when you compile and run ldquoGravityAttract07ajavardquo using Javascripts commandline once you compile and run the result was looked like this

On which particle you want to calculate the resulting force 2The resultant force on particle [2] due to the other masses is 30673e-10 N 8837 deg

SEE THE DIFFERENCES AT THE USER INTERFACE

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3758

8th Step

Written by Mohd Khalid Kasmin

Monday 27 August 2007

Programming using command line interface (CLI) has its advantages but todays computer users are so used to graphical user

interface (GUI) that we just can no longer be contented with writing application that do not use the GUI Hence starting from this 8thstep we are going to embark on learning how to make java application that utilises the the GUI

It is possible to write a GUI application with the text based tools that we have been using for these past 7 steps but the job is so

messy that most of you will definitely be turnoff by it So to make life slightly easier well use an integrated development

environment (IDE) known as NetBeans specifically NetBeans 551

0 First ssh to the our server Next once logged in just type nice -n15 netbeans amp on the terminal If you had sshed with the -Y

switch after a few tens of seconds NetBeans should emerge on your display screen

One important point to note Since our server can readily handle up to 100 users (maybe even

more) doing work in CLI mode this machine will certainly be taxed to its maximum capability once

we start using graphical based application NetBeans 551 uses a geat deal of computer memory

and with the 4Gbytes RAM server it can only accomodate up to 25 users concurrently using

NetBeans if the users use the application wisely What is meant by wisely here is that (a) when

starting the NetBeans application please use it together with the nice -n15 command (b) in using

the NetBeans please make sure you only have one project open at a time and (c) please close your

project everytime before quitting the NetBeans

Home Profile Teaching Links Contact Me My Work

Newsflash

Yesterday all servers in the US went out on strike in a bid to get more RAM and better CPUs A spokes person said that the

need for better RAM was due to some fool increasing the front-side bus speed In future busses will be told to slow down in

residential motherboards

search

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3858

1

2

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3958

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4058

[ Back ]

5

Last Updated ( Wednesday 31 October 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4158

8th Step (Cont)

Written by Mohd Khalid Kasmin

Monday 27 August 2007

Up to this point we have define the name of our project Next we construct a class and to follow what we already know we name the class the sam

project name latihan01

6

7

Home Profile Teaching Links Contact Me My Work

Newsflash

Joomla 10 - Experience the Freedom It has never been easier to create your own dynamic site Manage all your content from

the best CMS admin interface

search

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4258

8

9

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4358

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4458

8th Step (Cont still)

Written by Mohd Khalid Kasmin

Monday 27 August 2007

In item (9) we see that Netbeans does not provide us with the main method So to follow what we already know we now put in the

main method

10

Home Profile Teaching Links Contact Me My Work

Newsflash

Aoccdrnig to a rscheearch at an Elingsh uinervtisy it deosnt mttaer in waht oredr the ltteers in a wrod are the olny iprmoetnt

tihng is taht frist and lsat ltteer is at the rghit pclae The rset can be a toatl mses and you can sitll raed it wouthit porbelm Tihs is

bcuseae we do not raed ervey lteter by itslef but the wrod as a wlohe

search

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4558

11

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4658

12

13

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4758

14

15

Last Updated ( Monday 27 August 2007 )

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4858

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4958

9th Step

Thursday 06 September 2007

Step by Step into Java Programming

for

University Physics Student

By

Mohd Khalid Kasmin

A Building graphical user interface - preparing the JFrame Form

At this stage of our programming experience with Java and NetBeans please follow strictly the steps outline in this article First and

make a new GUI project make sure there is no other project that already been loaded by Netbeans Please close any project displayed

1 Create new project

- On the main menu click on File --gt New Project (note Close first any previous project)

- Specify project name amp project location - Untick Create Main Class item

- Click on the Finish button

Home Profile Teaching Links Contact Me My Work

Newsflash

Aoccdrnig to a rscheearch at an Elingsh uinervtisy it deosnt mttaer in waht oredr the ltteers in a wrod are the olny iprmoetnt

tihng is taht frist and lsat ltteer is at the rghit pclae The rset can be a toatl mses and you can sitll raed it wouthit porbelm Tihs is

bcuseae we do not raed ervey lteter by itslef but the wrod as a wlohe

search

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5058

Figure 91

2 Create UI class

- Right click on the project name in the Project pane

on the popup menu select New --gt JFrame Form

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5158

Figure 92

- In the New JFrame Form dialog specify the Class Name and the Package name

eg Class Name multiplierUI Package mymultiplier

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5258

Figure 93

After clicking on the Finish button we should get something like the following

Figure 94

Last Updated ( Tuesday 18 September 2007 )

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5358

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5458

9th Step (Cont)

Written by Mohd Khalid Kasmin

Tuesday 11 September 2007

Step by Step into Java Programming

for University Physics Student

By

Mohd Khalid Kasmin

B Building user interface (UI) - putting in the components

Now that we have a proper blank form we can start filling the form with the suitable graphical components For introduction to

GUI programming an appropriate application to build (for a physics student) is a simple solver So our first exercise is to build an

application that takes two numbers and multiply them and display the result

With GUI we usually give input values to an application via input boxes that are labelled When all inputs are typed in properlywe click on a button to ask the application to execute the job Hence in this exercise well do just that The target now is to

construct a user interface that looks like Figure 95 below

Figure 95

Home Profile Teaching Links Contact Me My Work

Newsflash

Joomla 10 - Experience the Freedom It has never been easier to create your own dynamic site Manage all your content from

the best CMS admin interface

search

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5558

[ Back ]

1 Problemapplication description box

- In the palette pane click on Swing component named JTextPane (Note click just once) Move the mouse pointer on top of

your JFrame (Note just move not drag) When you see a little white box with orange border position it somewhere near the

top part of your JFrame and click once The JTextPane is then attached on the JFrame Addjust its size with the help of the

sizing hook around the perimeter of the JTextPane

- Edit the text property of the JTextPane with the description of your application

2 Input boxes

- In the palette pane click on Swing component named JTextField Next move the mouse pointer on top of your JFrame

When you see a white box with the word jTextField1 position it to the left and under the JTextPane1

- Click on Swing component JTextField in the palette pane again Move the mouse pointer on top of the JFrame you should

get a second JTextField named jTextField2 Position it to the right of jTextField1

Last Updated ( Tuesday 11 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5658

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5758

multiplication if a b and c were first declared as double or float or int variables So the code of the calculation should look like the

following

double a b c

a =

b =

c = a b

Clearly variables a and b need to be assigned with values of typed double Now looking back at inputboxes A and B we made

those boxes using the JTextField component meaning that any data or value written in a JTextField is of typed Text or String even

if the value displayed in the box is ldquo125rdquo But f irst here there is this task of getting to that Text (or String) values ldquo125rdquo and ldquo72rdquo

In Java we can get to a text in JTextField component using the getText() method So to retrieve values in jTextField1 and

jTextField2 we can use

jTextField1getText()

and

jTextField2getText()

But those values are still in text mode and we need another method to convert the data mode f rom text to double For that we can

use the method DoubleparseDouble() Thus the whole process of retrieve text -gt convert text to double -gt assign to double

variable can be done using

a = DoubleparseDouble( jTextField1getText() )

and

b = DoubleparseDouble( jTextField2getText() )

Hence the code for performing the multiplication becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

Up to this point we have the variable c as the result of the multiplication of a and b So the next thing to do is to display this

result on the JLabel that we have already prepared before

A x B = _____

The text A x B = ______ was made using JLabel component and its variable name is jLabel3 Since the variable c is of type

double and text property of JLabel is of type String we need to convert it first to String data type The method to convert a double

to String is DoubletoString() So we can write the code as DoubletoString(c) After getting the text we can now use it to be the

text displayed on jLabel3 To do that we use the method setText() Hence the appropriate code for displaying the result is

jLabel3setText( A x B = + DoubletoString(c) )

And the full code for our calculation becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

jLabel3setText( A x B = + DoubletoString(c) )

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5858

[ Back ]

Last Updated ( Tuesday 18 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

Page 7: Computional Physics

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 758

Source code GravityAttract05ajava

Java class for calculating the resultant force on an object m1due to for other objects m2 m3 m4 and m5

import static javalangMath

public class GravityAttract05a

public static void main(String args[])

double[] m = new double[6] mass of object double[] x = new double[6] x-coor of object double[] y = new double[6] y-coor of object double f resultant forcedouble fxfy x amp y component of force double rx ry rp co Gint j

G = 667e-11m[1]=25 x[1]=12 y[1]=25m[2]=15 x[2]=15 y[2]=15m[3]=75 x[3]=02 y[3]=55m[4]=92 x[4]=52 y[4]=25m[5]=66 x[5]=42 y[5]=45

fx = 00fy = 00

for(j=2 jlt=5 j++)rx = x[j]-x[1]ry = y[j]-y[1]rp = pow((rxrx+ryry)15) (rx^2 + ry^2) raised to the power of 32 co = Gm[1]m[j]fx = fx + corxrpfy = fy + coryrp

f = sqrt(fxfx + fyfy)Systemoutprintf(ntThe force on m[1] due to the other masses is e Nnnf)

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 858

Source code GravityAttract06ajava

Java class for calculating the resultant force on each object(on m1 through m5)

import static javalangMath

public class GravityAttract06a

public static void main(String args[])

double[] m = new double[6] mass of objectdouble[] x = new double[6] x-coor of objectdouble[] y = new double[6] y-coor of objectdouble f resultant forcedouble fxfy x amp y component of forcedouble rx ry rp co G dirfangdirfint j mi

G = 667e-11m[1]=25 x[1]=12 y[1]=25m[2]=15 x[2]=15 y[2]=15m[3]=75 x[3]=02 y[3]=55m[4]=92 x[4]=52 y[4]=25m[5]=66 x[5]=42 y[5]=45

for (mi=1 milt=5 mi++)

fx = 00fy = 00for(j=1 jlt=5 j++)

if (j=mi)rx = x[j]-x[mi]ry = y[j]-y[mi]rp = pow((rxrx+ryry)15) (rx^2 + ry^2) raised to the power of 32co = Gm[mi]m[j]fx = fx + corxrpfy = fy + coryrp

f = sqrt(fxfx + fyfy)dirf = atan(fyfx)angdirf = (dirf(22070))180Systemoutprintf(The force on m[+ mi +] due to the other masses is eN fdegnfangdirf)

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 958

Source code GravityAttract07ajava

Java class for calculating the resultant force on any object(ie either on m1m2 m3 m4 or m5)

import static javalangMathimport javaiopublic class GravityAttract07a

public static void main(String args[])

double[] m = new double[6] mass of objectdouble[] x = new double[6] x-coor of objectdouble[] y = new double[6] y-coor of objectdouble f resultant forcedouble fxfy x amp y component of forcedouble rx ry rp co G dirfangdirfint j

G = 667e-11m[1]=25 x[1]=12 y[1]=25m[2]=15 x[2]=15 y[2]=15m[3]=75 x[3]=02 y[3]=55m[4]=92 x[4]=52 y[4]=25m[5]=66 x[5]=42 y[5]=45BufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin))String mass=Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))

try mass=dataInreadLine()catch(IOException e)Systemoutprintln(Not a valid input value)int mi = IntegerparseInt(mass)fx = 00fy = 00

for(j=1 jlt=5 j++)

if (j=mi)rx = x[j]-x[mi]ry = y[j]-y[mi]rp = pow((rxrx+ryry)15) (rx^2 + ry^2) raised to the power of 32

co = Gm[mi]m[j]fx = fx + corxrpfy = fy + coryrp

f = sqrt(fxfx + fyfy)dirf = toDegrees(atan(fyfx))Systemoutprintf(The force on m[+ mi +] due to the other masses is eN fdegnfdirf)

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1058

983107983119983117983120983125983124983105983124983113983119983118983105983116

983120983112983129983123983113983107983123 983080983123983123983120983090983089983090983090983081

983109983148983141983139983156983154983151983155983156983137983156983145983139 983152983151983156983141983150983156983145983137983148983138983161

983105983138983140983086 983115983144983137983149983145983149 983113983155983149983137983145983148

983117983151983144983140 983115983144983137983148983145983140 983115983137983155983149983145983150

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1158

1 Calculation of electrostatic potential for a group of point charges

You will learn about

- mesh method

a) using a for() loop inside another for() loop

to generate ordered pair (xy) points in a mesh on x-y plane

b) calculate physical value in this case V(xy) forevery point (xy) generated

- saving calculated values into a file

a) making a sequential text file

b) saving in a format that gnuplot can understand

for making a surface plot

- use gnuplot to visualize potential surface

a) making a surface plot from data points in a text file

b) making contour plot to visualize equipotential lines

c) changing the z-axis range to zoom in or zoom out on the surface

d) labelling the plot

e) save as an image the potential surface amp the contour plotf) save as an eps file the potential surface amp the contour plot

- including an eps file into a document

2 Calculation of electrostatic field for a group of point charges

You will learn again

- using mesh method to generate ordered pair (xy) points

in a mesh on x-y plane

You will also learn how to

- calculate vector field values in this case E(xy) for every

generated (xy) mesh point

- save the calculated vector points into a text file

in a format understood by gnuplot

- make a vector field plot using gnuplot

Source code

import static javalangMath

public class estatpot01b

public static void main(String[] args)

double[] q = new double[100]

double[] xq = new double[100]

double[] yq = new double[100]

double V r rx ry k x y

int i n

q[1] = 20e-6 xq[1] = -10 yq[1] = 10

q[2] =-30e-6 xq[2] = 30 yq[2] =-20

q[3] = 30e-6 xq[3] = -20 yq[3] = 40

q[4] =-50e-6 xq[4] = 20 yq[4] = 50

k = 9e9n = 4

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1258

for (x=-5 xlt=5 x = x+02)

for (y=5 ygt=-5 y = y-02)

V = 00

for(i=1 ilt=n i++)

rx = x - xq[i]

ry = y - yq[i]r = sqrt(rxrx+ryry)

if (rlt1e-6)

if (q[i]lt0) V = -10 else V = 10

break

V = V + q[i]r

V = kV

Systemoutprintf(f f fn x y V)

Systemoutprintf(n)

How to do surface plot using Gnuplot

To do a surface plotgnuplotgt splot epotdat with lineTo zoom in and zoom out z axisgnuplotgt set zrange [-2000020000]To convert the graph into imagegnuplotgt set term pngTerminal type set to pngOptions are small color picsize 640 480 Then typegnuplotgt set output epotpngAgain do splot as followgnuplotgt splot epotdat with lineNow the graph is saved into a file in png format named as epotpngTo go back into terminal ouputgnuplotgt set term x11To set to countor plotgnuplotgt set pm3dTo remove countor meshgnuplotgt unset surf

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1358

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1458

983107983119983117983120983125983124983105983124983113983119983118983105983116 983120983112983129983123983113983107983123

983080983123983123983120983090983089983090983090983081

983109983157983148983137983154 983149983141983156983144983151983140

983138983161

983105983138983140983086 983115983144983137983149983145983149 983113983155983149983137983145983148

983117983151983144983140 983115983144983137983148983145983140 983115983137983155983149983145983150

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1558

Free fall body problem

Consider an object thrown upward under gravitational force ( f=mg) with an initial velocity v0The object goes up and then stop at maximum y position ymax before it start to fall downback to the original position at y0 and then to the ground level (See figure)

ymdt

yd m

dt

dvmmaF ampamp====

2

2

t

Y

v1 (t1y1)

v2 (t2y2)

v4 (t4y4)

v5 (t5y5)

vn (tnyn)

v3 (t3y3)

v0 (t0y0)

∆t∆t ∆t ∆t

y1

y0

∆t ∆t

∆yv0

a = g

y0

t0

t0 t1 t2 t3 t4 t5 tn

ymax

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1658

Exact theoretical solution

( ) 2

21

00 at t v yt y ++=

The y projection of the exact solution can be plotted using the following java source code

Java class for solving free fall object under gravity (g = 98 mss)Exact solution y = y0 + v0t + 12at^2

public class freefallsol

public static void main(String args[])double av0yy0deltt

a = -98y0 = 10v0 = 10delt = 00001for(t=0 tlt=05 t=t+delt)

y = y0 + v0t + 05attSystemoutprintf(84f 84fnty)

Complile freefallsoljava to get the plot of the exact solution and then run and save theoutput into a file name as follows

[khamimsetpar65 ~]$ java freefallsol gtffsoldat

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1758

Euler Method

There is another way to see the projection of y position of the object using a numericalmethod called ldquoEuler methodrdquo See the following derivation

t avt v y

t

va

t

yv

∆=∆∆=∆

∆=

∆=

t v y y y y

t avvvv

t v y y y y

t avvvv

t v y y y y

t avvvv

nnnn

nnn

∆+=∆+=

∆+=∆+=

∆+=∆+=

∆+=∆+=

∆+=∆+=

∆+=∆+=

minus

minusminus

1

11

2112

112

1001

001

M

if ∆∆∆∆t rarrrarrrarrrarr 0 then y will close to the exact solution ( ( ) 2

21

00 at t v yt y ++= )

The algorithm 1 Declare the classmethod to be used

2 Declare and assign all variables to be used av0v1yy0y1deltt and j 3 Initialize value to all respective variables a = -98 y0 = 10 v0 = 10

4 Select appropriate value for ∆t

∆t = 00055 Start the loop to calculate the velocity and position for t=0

6 Calculate velocity v1 = v0 + ∆v = v0 + a ∆t

7 Calculate position y1 = y0 + ∆y = y0 + v1 ∆t8 Print out values of t and y1 9 Swap v0 = v1 and y0 = y1

10 Repeat from step 5 and increase the value of t by ∆t (t =t + ∆t) until stoppingcondition is satisfied at t=05

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1858

The java code

Java class for solving free fall object under gravity (g = 98 mss)by numerical method called ldquoEulers Methodrdquo

public class freefalleuler

public static void main(String args[])double av0v1yy0y1delttint j

a = -98y0 = 10v0 = 10delt = 0005

for(t=0 tlt=05 t =t + delt)v1 = v0 + adelty1 = y0 + v1deltSystemoutprintf(84f 84fnty1)v0 = v1y0 = y1

Complile freefalleulerjava for different delt ie 00005 0005 005 and then run and savethe output into a file name as follows

[khamimsetpar65 ~]$ java freefalleuler gtffeuler00005dat[khamimsetpar65 ~]$ java freefalleuler gtffeuler0005dat[khamimsetpar65 ~]$ java freefalleuler gtffeuler005dat

Plotting the results using gnuplot

Using gnuplot plot all the graphs using the following command and compare the results

gnuplotgt plot ffeuler00005dat using 12 title euler delt=00005 with linegt ffeuler0005dat using 12 title euler delt=0005 with linegt ffeuler005dat using 12 title euler deltt=005 with linegt ffsoldat using 12 title exact solution with line

In MS windows

gnuplotgt plot ffeuler00005dat using 12 title euler delt=00005 with lineffeuler0005dat using 12 title euler delt=0005 with lineffeuler005dat using 12 titleeuler delt=005 with line ffsoldat using 12 title exact solution with line

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1958

Gnuplot

The smaller the delt the closer the curve to the exact solution

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2058

983107983119983117983120983125983124983105983124983113983119983118983105983116 983120983112983129983123983113983107983123

983080983123983123983120983090983089983090983090983081

983113983149983152983154983151983158983141983140 983109983157983148983137983154 983149983141983156983144983151983140

983138983161

983105983138983140983086 983115983144983137983149983145983149 983113983155983149983137983145983148

983117983151983144983140 983115983144983137983148983145983140 983115983137983155983149983145983150

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2158

Improved Euler method

Comparison between analytical solution of F=ma with the Euler method shows that although the

method is able to find approximate solution the error is quite significant if computational step is

reduced The error is also cumulative ie as the iteration of the calculation increases the error also gets

bigger hence value furthest away from initial condition has the largest error

The Euler method gives us a way to approximate positions of object with respect to time but requires

an order of magnitude of computational iterations for each order of magnitude of accuracy For large

number of objects this method will certainly consume a great deal of computation time Hence

students are introduced to Initial Half-Step Method an Euler method with a much improved

computational effeciency

Source code

Study of motion of an object with mass m

experiencing a constant force F using

Improved Euler Method (Initial half step method)

In X and Y dimension

import static javalangMath

public class fma04

public static void main(String args[])

double m fx fy x y vo vx vy ax ay t delt

double pi th theta ymax tmax xmax

pi = 40atan(10)

m = 01 in kg

fy = -02 in N (pulling down force)

fx = 00 in N (retarding force)

ay = fym

ax = fxm

vo = 500 in ms

theta = 300 in degrees

th = theta(pi1800) degrees change to radian

vx = vo cos(th) in ms vy = vo sin(th) in ms

y = 12 in m

x = 00 in m

ymax=y initialize var ymax with initial value of y

xmax=00

tmax=00

delt = 001 in s

for(t=00 tlt=300 t=t+delt)

System outprintf(f f fntxy)

if (t==00)vx = vx + 05axdelt

vy = vy + 05aydelt

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2258

else

vx = vx + axdelt

vy = vy + aydelt

x = x + vxdelt

y = y + vydelt

if (ygtymax)

ymax=y

xmax=xtmax=t

if (ylt=00) break

System outprintf( Value of Y maximum is f happened when t = f

nymaxtmax)

System outprintf( at x = fnxmax)

System outprintf( The object reach the ground at x=f and t=fn xt)

gnuplot result

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2358

983107983119983117983120983125983124983105983124983113983119983118983105983116

983120983112983129983123983113983107983123 983080983123983123983120983090983089983090983090983081

983122983141983155983156983151983154983145983150983143 983110983151983154983139983141

983138983161

983105983138983140983086 983115983144983137983149983145983149 983113983155983149983137983145983148

983117983151983144983140 983115983144983137983148983145983140 983115983137983155983149983145983150

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2458

Damped Harmonic Oscillation

Object m is on a flat surface attached to a spring as in figure above The object is pulled

to position A and released so that it oscillates about x =0 Assuming that the force F=ma= -bv-kx where x is position of the object v is the velocity k the spring constant andb is a constant associated with friction with the flat surface

The algorithm

1 Declare the classmethod to be used2 Declare and assign all variables to be used

x t delt k b A m F a v v0 and x03 Declare and assign constants to the respective variables

k = 02 b = 01 A = 01 m = 054 Select appropriate value for ∆t 5 Initialized initial values xo=A vo=00

6 set x=xo v=vo and t = 07 calculate the value of force F = -kx-bv 8 calculate the value of acceleration a = Fm

9 calculate velocity v = vo + ∆v = vo + a ∆t

10 calculate position x = xo + ∆x = xo + v ∆t

11 increment value of t by ∆t 12 print out values of t and x

13 swap the value of xo=x vo=v 14 repeat from step 4 until stopping condition is satisfied (tlt=600)

The java code

public class simplehar01

public static void main(String args[])double x t deltdouble k b A m F a v v0 x0

k = 02b = 01A = 0 1

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2558

m = 05delt = 0001v0 = 00x0 = Ax = x0 v = v0

t = 00while(tlt=600)

F = -kx-bva = Fmv = v0 + adeltx = x0 + vdeltt = t + deltSystemoutprintf(f fntx)x0 = xv0 = v

gnuplot result

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2658

983107983119983117983120983125983124983105983124983113983119983118983105983116 983120983112983129983123983113983107983123

(983123983123983120983090983089983090983090983081

983106983157983145983148983140983145983150983143 983137 983152983144983161983155983145983139983155 983155983151983148983158983141983154

983157983155983145983150983143 983118983141983156983138983141983137983150983155

983138983161

983105983138983140983086 983115983144983137983149983145983149 983113983155983149983137983145983148

983117983151983144983140 983115983144983137983148983145983140 983115983137983155983149983145983150

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2758

How to create a user interface (UI) using Netbeans

Based on the ldquoGravityAttract07ajavardquo source code (shown below) build a user interface usingNetbeans The design of the UI could be look like this (Figure1)

Figure 1

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2858

ldquoGravityAttract07ajavardquo source code

Java class for calculating the resultant force on any object(ie either on m1m2 m3 m4 or m5)

import static javalangMathimport javaiopublic class GravityAttract07a

public static void main(String args[])

double[] m = new double[6] mass of objectdouble[] x = new double[6] x-coor of objectdouble[] y = new double[6] y-coor of objectdouble f resultant forcedouble fxfy x amp y component of forcedouble rx ry rp co G dirfangdirf

int j

G = 667e-11m[1]=25 x[1]=12 y[1]=25m[2]=15 x[2]=15 y[2]=15m[3]=75 x[3]=02 y[3]=55m[4]=92 x[4]=52 y[4]=25m[5]=66 x[5]=42 y[5]=45BufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin))String mass=Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))try mass=dataInreadLine()

catch(IOException e)Systemoutprintln(Not a valid input value)int mi = IntegerparseInt(mass)fx = 00fy = 00

for(j=1 jlt=5 j++)

if (j=mi)rx = x[j]-x[mi]ry = y[j]-y[mi]rp = pow((rxrx+ryry)15) (rx^2 + ry^2) raised to the power of 32co = Gm[mi]m[j]

fx = fx + corxrpfy = fy + coryrp

f = sqrt(fxfx + fyfy)dirf = toDegrees(atan(fyfx))Systemoutprintf(The force on m[+ mi +] due to the other masses is eN fdegnfdirf)

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2958

This example will not show you step by step details procedure since it already explained inthe class and in the e-learning Please refer to the e-learning about how to create a project how tocreate JFrame Form and as well how to create JButton JTextField JLabel JTextPane and so on

First create a new project and give a name eg ldquogravityrdquo and then create JFrame Form andthe following JButton JTextField JLabel JTextPane etc by using ldquoswing paletterdquo as shown in Figure2

Figure 2

To insert a picture into JLabel2 click on the JLabel2 go to properties and select ldquoiconrdquo Onthe icon editor choose file and then select file to locate the directory of your graphicpicture file(Figure 3) Select the file and click OK when finished Make sure you have already created your ownpicture file and save it as jpg png bmp etc format eg ldquogravityjpgrdquo Use any graphic package todraw your diagrampicture or use drawing tools available in Open Office or Microsoft Word

JTextPane1JLabel1

JLabel2

JLabel3JLabel4

JLabel5

JLabel6 JLabel7

JLabel8

JTextField2 JTextField3 JTextField4

JTextF

JButton1JButton2

JButton3

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3058

Figure 3

To write documentation or question eg in JTextPane1 click on the JTextPane1 go toproperties and select ldquotextrdquo Type the documentation in the text editor as shown in Figure 4 Click OKwhen finished

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3158

To do formatting on the text such as superscript and subscript eg in jLabel3 click on the

JLabel3 go to properties and select ldquotextrdquo Type the documentationmessage on the text editor asshown in Figure 5 as follows

eg

lthtmlgtOn which particle (mltsubgt1ltsubgtmltsubgt5ltsubgt) you want to calculate theresultant force

Use sup instead if you want a superscriptThe result will look like this

Click OK when finished

Figure 5

To do the calculation you need to put the coding similar like ldquoGravityAttract07ajavardquo and put itinto the ldquocalculaterdquo button (this is ldquoJButton1rdquo) You can copy ldquoGravityAttract07a javardquo source code

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3258

paste it and then do a few alterations First click on the ldquocalculaterdquo button right click choose Eventsand then choose Action then actionPerformed (Figure 6) It will bring you to where you should insertyour coding (Figure 7) You can write your own coding or copypaste any coding from somewhereelse (in this case from ldquoGravityAttract07ajavardquo)

Figure 6

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3358

Figure 7

Copy ldquoGravityAttract07ajavardquo source code and paste to the place after ldquo TODO add youhandling code hererdquo see Figure 7 To see which part has been modified the coding in Figure 7 hasbeen color coded for better understanding as follows

private void jButton1ActionPerformed(javaawteventActionEvent evt) TODO add your handling code here

double[] m = new double[6] mass of objectdouble[] x = new double[6] x-coor of objectdouble[] y = new double[6] y-coor of objectdouble f resultant forcedouble fxfy x amp y component of forcedouble rx ry rp co G dirfangdirfDecimalFormat dff dfangdirfint j mi

G = 667e-11m[1]=25 x[1]=12 y[1]=25m[2]=15 x[2]=15 y[2]=15m[3]=75 x[3]=02 y[3]=55m[4]=92 x[4]=52 y[4]=25m[5]=66 x[5]=42 y[5]=45

BufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin)) String mass= Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3458

catch(IOException e)Systemoutprintln(Not a valid input value) int mi = IntegerparseInt(mass)mi = IntegerparseInt(jTextField1getText())fx = 00fy = 00for(j=1 jlt=5 j++)

if (j=mi)rx = x[j]-x[mi]ry = y[j]-y[mi]rp = Mathpow((rxrx+ryry)15) (rx^2 + ry^2) raised to the power of 32co = Gm[mi]m[j]fx = fx + corxrpfy = fy + coryrp

f = Mathsqrt(fxfx + fyfy)

dirf = Mathatan(fyfx)angdirf = (dirf(22070))1800 Systemoutprintf(ldquoThe resultant force on particle [ldquo+ mi +rdquo] due to the other masses is

14e N 22f degnrdquofangdirf)dff = new javatextDecimalFormat(ldquoE00rdquo)dfangdirf = new DecimalFormat(ldquo000rdquo)

jTextField2setText(ldquordquo + mi) jTextField3setText(ldquordquo + dffformat(f)) jTextField4setText(ldquordquo + dfangdirfformat(angdirf))

Note

Brown = automatically generatedBlue = the original code from ldquoGravityAttract07ajavardquoGreen = coding from ldquoGravityAttract07ajavardquo which were being disabled due to

incompatibilities with NetbeansRed = a new inserted coding a few of them is to rephrasesubstitute the green one

Javascripts command line useBufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin))String mass=Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))try mass=dataInreadLine()catch(IOException e)Systemoutprintln(Not a valid input value)

int mi = IntegerparseInt(mass)

to read any input from the keyboard typed by the user while Netbeans uses egldquoIntegerparseInt(jTextField1getText())rdquo etc Javascripts command line uses ldquoSystemoutprintfrdquo to format the printed output whileNetbeans uses more complicated way to do the same job (see below)

Javascript command line

Systemoutprintf(The resultant force on particle [+ mi +] due to the other masses is 14e N22f degnfangdirf)

Netbeansdff = new javatextDecimalFormat(ldquo0E00rdquo)df di f D i lF t(ldquo0 00rdquo)

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3558

jTextField2setText(ldquordquo + mi) jTextField3setText(ldquordquo + dffformat(f)) jTextField4setText(ldquordquo + dfangdirfformat(angdirf))

ldquodffrdquo and ldquodfangdirfrdquo are new variables that should be declared and added to format thenumbers into decimal format ie ldquo0E00rdquo to get a scientific format eg ldquo20019E-10rdquo and ldquo000rdquoto get a decimal number eg ldquo-1570rdquo

To use DecimalFormat you have to put ldquoimport javatextDecimalFormatrdquo in the top of thecoding as shown in Figure 8

Figure 8

To reset the calculation you need to put coding into the ldquoresetrdquo button (this is ldquoJButton2rdquo)First click on the ldquoresetrdquo button right click choose Events and then choose Action thenactionPerformed (similar like in Figure 6) It will bring you to where you should insert your coding(Figure 9) Write the following coding (Figure 9)

Figure 9

To exit the program you need to put coding into the ldquoexitrdquo button (this is ldquoJButton3rdquo) Firstclick on the ldquoexitrdquo button right click choose Events and then choose Action then actionPerformed(similar like in Figure 6) It will bring you to where you should insert your coding (Figure 10) Writethe following coding (Figure 10)

Figure 10

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3658

Once you finished now you can run and see the result If everything is fine then the output wilbe look like this (Figure 11)

Figure 11

Remember when you compile and run ldquoGravityAttract07ajavardquo using Javascripts commandline once you compile and run the result was looked like this

On which particle you want to calculate the resulting force 2The resultant force on particle [2] due to the other masses is 30673e-10 N 8837 deg

SEE THE DIFFERENCES AT THE USER INTERFACE

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3758

8th Step

Written by Mohd Khalid Kasmin

Monday 27 August 2007

Programming using command line interface (CLI) has its advantages but todays computer users are so used to graphical user

interface (GUI) that we just can no longer be contented with writing application that do not use the GUI Hence starting from this 8thstep we are going to embark on learning how to make java application that utilises the the GUI

It is possible to write a GUI application with the text based tools that we have been using for these past 7 steps but the job is so

messy that most of you will definitely be turnoff by it So to make life slightly easier well use an integrated development

environment (IDE) known as NetBeans specifically NetBeans 551

0 First ssh to the our server Next once logged in just type nice -n15 netbeans amp on the terminal If you had sshed with the -Y

switch after a few tens of seconds NetBeans should emerge on your display screen

One important point to note Since our server can readily handle up to 100 users (maybe even

more) doing work in CLI mode this machine will certainly be taxed to its maximum capability once

we start using graphical based application NetBeans 551 uses a geat deal of computer memory

and with the 4Gbytes RAM server it can only accomodate up to 25 users concurrently using

NetBeans if the users use the application wisely What is meant by wisely here is that (a) when

starting the NetBeans application please use it together with the nice -n15 command (b) in using

the NetBeans please make sure you only have one project open at a time and (c) please close your

project everytime before quitting the NetBeans

Home Profile Teaching Links Contact Me My Work

Newsflash

Yesterday all servers in the US went out on strike in a bid to get more RAM and better CPUs A spokes person said that the

need for better RAM was due to some fool increasing the front-side bus speed In future busses will be told to slow down in

residential motherboards

search

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3858

1

2

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3958

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4058

[ Back ]

5

Last Updated ( Wednesday 31 October 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4158

8th Step (Cont)

Written by Mohd Khalid Kasmin

Monday 27 August 2007

Up to this point we have define the name of our project Next we construct a class and to follow what we already know we name the class the sam

project name latihan01

6

7

Home Profile Teaching Links Contact Me My Work

Newsflash

Joomla 10 - Experience the Freedom It has never been easier to create your own dynamic site Manage all your content from

the best CMS admin interface

search

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4258

8

9

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4358

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4458

8th Step (Cont still)

Written by Mohd Khalid Kasmin

Monday 27 August 2007

In item (9) we see that Netbeans does not provide us with the main method So to follow what we already know we now put in the

main method

10

Home Profile Teaching Links Contact Me My Work

Newsflash

Aoccdrnig to a rscheearch at an Elingsh uinervtisy it deosnt mttaer in waht oredr the ltteers in a wrod are the olny iprmoetnt

tihng is taht frist and lsat ltteer is at the rghit pclae The rset can be a toatl mses and you can sitll raed it wouthit porbelm Tihs is

bcuseae we do not raed ervey lteter by itslef but the wrod as a wlohe

search

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4558

11

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4658

12

13

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4758

14

15

Last Updated ( Monday 27 August 2007 )

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4858

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4958

9th Step

Thursday 06 September 2007

Step by Step into Java Programming

for

University Physics Student

By

Mohd Khalid Kasmin

A Building graphical user interface - preparing the JFrame Form

At this stage of our programming experience with Java and NetBeans please follow strictly the steps outline in this article First and

make a new GUI project make sure there is no other project that already been loaded by Netbeans Please close any project displayed

1 Create new project

- On the main menu click on File --gt New Project (note Close first any previous project)

- Specify project name amp project location - Untick Create Main Class item

- Click on the Finish button

Home Profile Teaching Links Contact Me My Work

Newsflash

Aoccdrnig to a rscheearch at an Elingsh uinervtisy it deosnt mttaer in waht oredr the ltteers in a wrod are the olny iprmoetnt

tihng is taht frist and lsat ltteer is at the rghit pclae The rset can be a toatl mses and you can sitll raed it wouthit porbelm Tihs is

bcuseae we do not raed ervey lteter by itslef but the wrod as a wlohe

search

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5058

Figure 91

2 Create UI class

- Right click on the project name in the Project pane

on the popup menu select New --gt JFrame Form

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5158

Figure 92

- In the New JFrame Form dialog specify the Class Name and the Package name

eg Class Name multiplierUI Package mymultiplier

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5258

Figure 93

After clicking on the Finish button we should get something like the following

Figure 94

Last Updated ( Tuesday 18 September 2007 )

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5358

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5458

9th Step (Cont)

Written by Mohd Khalid Kasmin

Tuesday 11 September 2007

Step by Step into Java Programming

for University Physics Student

By

Mohd Khalid Kasmin

B Building user interface (UI) - putting in the components

Now that we have a proper blank form we can start filling the form with the suitable graphical components For introduction to

GUI programming an appropriate application to build (for a physics student) is a simple solver So our first exercise is to build an

application that takes two numbers and multiply them and display the result

With GUI we usually give input values to an application via input boxes that are labelled When all inputs are typed in properlywe click on a button to ask the application to execute the job Hence in this exercise well do just that The target now is to

construct a user interface that looks like Figure 95 below

Figure 95

Home Profile Teaching Links Contact Me My Work

Newsflash

Joomla 10 - Experience the Freedom It has never been easier to create your own dynamic site Manage all your content from

the best CMS admin interface

search

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5558

[ Back ]

1 Problemapplication description box

- In the palette pane click on Swing component named JTextPane (Note click just once) Move the mouse pointer on top of

your JFrame (Note just move not drag) When you see a little white box with orange border position it somewhere near the

top part of your JFrame and click once The JTextPane is then attached on the JFrame Addjust its size with the help of the

sizing hook around the perimeter of the JTextPane

- Edit the text property of the JTextPane with the description of your application

2 Input boxes

- In the palette pane click on Swing component named JTextField Next move the mouse pointer on top of your JFrame

When you see a white box with the word jTextField1 position it to the left and under the JTextPane1

- Click on Swing component JTextField in the palette pane again Move the mouse pointer on top of the JFrame you should

get a second JTextField named jTextField2 Position it to the right of jTextField1

Last Updated ( Tuesday 11 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5658

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5758

multiplication if a b and c were first declared as double or float or int variables So the code of the calculation should look like the

following

double a b c

a =

b =

c = a b

Clearly variables a and b need to be assigned with values of typed double Now looking back at inputboxes A and B we made

those boxes using the JTextField component meaning that any data or value written in a JTextField is of typed Text or String even

if the value displayed in the box is ldquo125rdquo But f irst here there is this task of getting to that Text (or String) values ldquo125rdquo and ldquo72rdquo

In Java we can get to a text in JTextField component using the getText() method So to retrieve values in jTextField1 and

jTextField2 we can use

jTextField1getText()

and

jTextField2getText()

But those values are still in text mode and we need another method to convert the data mode f rom text to double For that we can

use the method DoubleparseDouble() Thus the whole process of retrieve text -gt convert text to double -gt assign to double

variable can be done using

a = DoubleparseDouble( jTextField1getText() )

and

b = DoubleparseDouble( jTextField2getText() )

Hence the code for performing the multiplication becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

Up to this point we have the variable c as the result of the multiplication of a and b So the next thing to do is to display this

result on the JLabel that we have already prepared before

A x B = _____

The text A x B = ______ was made using JLabel component and its variable name is jLabel3 Since the variable c is of type

double and text property of JLabel is of type String we need to convert it first to String data type The method to convert a double

to String is DoubletoString() So we can write the code as DoubletoString(c) After getting the text we can now use it to be the

text displayed on jLabel3 To do that we use the method setText() Hence the appropriate code for displaying the result is

jLabel3setText( A x B = + DoubletoString(c) )

And the full code for our calculation becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

jLabel3setText( A x B = + DoubletoString(c) )

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5858

[ Back ]

Last Updated ( Tuesday 18 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

Page 8: Computional Physics

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 858

Source code GravityAttract06ajava

Java class for calculating the resultant force on each object(on m1 through m5)

import static javalangMath

public class GravityAttract06a

public static void main(String args[])

double[] m = new double[6] mass of objectdouble[] x = new double[6] x-coor of objectdouble[] y = new double[6] y-coor of objectdouble f resultant forcedouble fxfy x amp y component of forcedouble rx ry rp co G dirfangdirfint j mi

G = 667e-11m[1]=25 x[1]=12 y[1]=25m[2]=15 x[2]=15 y[2]=15m[3]=75 x[3]=02 y[3]=55m[4]=92 x[4]=52 y[4]=25m[5]=66 x[5]=42 y[5]=45

for (mi=1 milt=5 mi++)

fx = 00fy = 00for(j=1 jlt=5 j++)

if (j=mi)rx = x[j]-x[mi]ry = y[j]-y[mi]rp = pow((rxrx+ryry)15) (rx^2 + ry^2) raised to the power of 32co = Gm[mi]m[j]fx = fx + corxrpfy = fy + coryrp

f = sqrt(fxfx + fyfy)dirf = atan(fyfx)angdirf = (dirf(22070))180Systemoutprintf(The force on m[+ mi +] due to the other masses is eN fdegnfangdirf)

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 958

Source code GravityAttract07ajava

Java class for calculating the resultant force on any object(ie either on m1m2 m3 m4 or m5)

import static javalangMathimport javaiopublic class GravityAttract07a

public static void main(String args[])

double[] m = new double[6] mass of objectdouble[] x = new double[6] x-coor of objectdouble[] y = new double[6] y-coor of objectdouble f resultant forcedouble fxfy x amp y component of forcedouble rx ry rp co G dirfangdirfint j

G = 667e-11m[1]=25 x[1]=12 y[1]=25m[2]=15 x[2]=15 y[2]=15m[3]=75 x[3]=02 y[3]=55m[4]=92 x[4]=52 y[4]=25m[5]=66 x[5]=42 y[5]=45BufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin))String mass=Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))

try mass=dataInreadLine()catch(IOException e)Systemoutprintln(Not a valid input value)int mi = IntegerparseInt(mass)fx = 00fy = 00

for(j=1 jlt=5 j++)

if (j=mi)rx = x[j]-x[mi]ry = y[j]-y[mi]rp = pow((rxrx+ryry)15) (rx^2 + ry^2) raised to the power of 32

co = Gm[mi]m[j]fx = fx + corxrpfy = fy + coryrp

f = sqrt(fxfx + fyfy)dirf = toDegrees(atan(fyfx))Systemoutprintf(The force on m[+ mi +] due to the other masses is eN fdegnfdirf)

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1058

983107983119983117983120983125983124983105983124983113983119983118983105983116

983120983112983129983123983113983107983123 983080983123983123983120983090983089983090983090983081

983109983148983141983139983156983154983151983155983156983137983156983145983139 983152983151983156983141983150983156983145983137983148983138983161

983105983138983140983086 983115983144983137983149983145983149 983113983155983149983137983145983148

983117983151983144983140 983115983144983137983148983145983140 983115983137983155983149983145983150

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1158

1 Calculation of electrostatic potential for a group of point charges

You will learn about

- mesh method

a) using a for() loop inside another for() loop

to generate ordered pair (xy) points in a mesh on x-y plane

b) calculate physical value in this case V(xy) forevery point (xy) generated

- saving calculated values into a file

a) making a sequential text file

b) saving in a format that gnuplot can understand

for making a surface plot

- use gnuplot to visualize potential surface

a) making a surface plot from data points in a text file

b) making contour plot to visualize equipotential lines

c) changing the z-axis range to zoom in or zoom out on the surface

d) labelling the plot

e) save as an image the potential surface amp the contour plotf) save as an eps file the potential surface amp the contour plot

- including an eps file into a document

2 Calculation of electrostatic field for a group of point charges

You will learn again

- using mesh method to generate ordered pair (xy) points

in a mesh on x-y plane

You will also learn how to

- calculate vector field values in this case E(xy) for every

generated (xy) mesh point

- save the calculated vector points into a text file

in a format understood by gnuplot

- make a vector field plot using gnuplot

Source code

import static javalangMath

public class estatpot01b

public static void main(String[] args)

double[] q = new double[100]

double[] xq = new double[100]

double[] yq = new double[100]

double V r rx ry k x y

int i n

q[1] = 20e-6 xq[1] = -10 yq[1] = 10

q[2] =-30e-6 xq[2] = 30 yq[2] =-20

q[3] = 30e-6 xq[3] = -20 yq[3] = 40

q[4] =-50e-6 xq[4] = 20 yq[4] = 50

k = 9e9n = 4

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1258

for (x=-5 xlt=5 x = x+02)

for (y=5 ygt=-5 y = y-02)

V = 00

for(i=1 ilt=n i++)

rx = x - xq[i]

ry = y - yq[i]r = sqrt(rxrx+ryry)

if (rlt1e-6)

if (q[i]lt0) V = -10 else V = 10

break

V = V + q[i]r

V = kV

Systemoutprintf(f f fn x y V)

Systemoutprintf(n)

How to do surface plot using Gnuplot

To do a surface plotgnuplotgt splot epotdat with lineTo zoom in and zoom out z axisgnuplotgt set zrange [-2000020000]To convert the graph into imagegnuplotgt set term pngTerminal type set to pngOptions are small color picsize 640 480 Then typegnuplotgt set output epotpngAgain do splot as followgnuplotgt splot epotdat with lineNow the graph is saved into a file in png format named as epotpngTo go back into terminal ouputgnuplotgt set term x11To set to countor plotgnuplotgt set pm3dTo remove countor meshgnuplotgt unset surf

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1358

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1458

983107983119983117983120983125983124983105983124983113983119983118983105983116 983120983112983129983123983113983107983123

983080983123983123983120983090983089983090983090983081

983109983157983148983137983154 983149983141983156983144983151983140

983138983161

983105983138983140983086 983115983144983137983149983145983149 983113983155983149983137983145983148

983117983151983144983140 983115983144983137983148983145983140 983115983137983155983149983145983150

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1558

Free fall body problem

Consider an object thrown upward under gravitational force ( f=mg) with an initial velocity v0The object goes up and then stop at maximum y position ymax before it start to fall downback to the original position at y0 and then to the ground level (See figure)

ymdt

yd m

dt

dvmmaF ampamp====

2

2

t

Y

v1 (t1y1)

v2 (t2y2)

v4 (t4y4)

v5 (t5y5)

vn (tnyn)

v3 (t3y3)

v0 (t0y0)

∆t∆t ∆t ∆t

y1

y0

∆t ∆t

∆yv0

a = g

y0

t0

t0 t1 t2 t3 t4 t5 tn

ymax

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1658

Exact theoretical solution

( ) 2

21

00 at t v yt y ++=

The y projection of the exact solution can be plotted using the following java source code

Java class for solving free fall object under gravity (g = 98 mss)Exact solution y = y0 + v0t + 12at^2

public class freefallsol

public static void main(String args[])double av0yy0deltt

a = -98y0 = 10v0 = 10delt = 00001for(t=0 tlt=05 t=t+delt)

y = y0 + v0t + 05attSystemoutprintf(84f 84fnty)

Complile freefallsoljava to get the plot of the exact solution and then run and save theoutput into a file name as follows

[khamimsetpar65 ~]$ java freefallsol gtffsoldat

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1758

Euler Method

There is another way to see the projection of y position of the object using a numericalmethod called ldquoEuler methodrdquo See the following derivation

t avt v y

t

va

t

yv

∆=∆∆=∆

∆=

∆=

t v y y y y

t avvvv

t v y y y y

t avvvv

t v y y y y

t avvvv

nnnn

nnn

∆+=∆+=

∆+=∆+=

∆+=∆+=

∆+=∆+=

∆+=∆+=

∆+=∆+=

minus

minusminus

1

11

2112

112

1001

001

M

if ∆∆∆∆t rarrrarrrarrrarr 0 then y will close to the exact solution ( ( ) 2

21

00 at t v yt y ++= )

The algorithm 1 Declare the classmethod to be used

2 Declare and assign all variables to be used av0v1yy0y1deltt and j 3 Initialize value to all respective variables a = -98 y0 = 10 v0 = 10

4 Select appropriate value for ∆t

∆t = 00055 Start the loop to calculate the velocity and position for t=0

6 Calculate velocity v1 = v0 + ∆v = v0 + a ∆t

7 Calculate position y1 = y0 + ∆y = y0 + v1 ∆t8 Print out values of t and y1 9 Swap v0 = v1 and y0 = y1

10 Repeat from step 5 and increase the value of t by ∆t (t =t + ∆t) until stoppingcondition is satisfied at t=05

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1858

The java code

Java class for solving free fall object under gravity (g = 98 mss)by numerical method called ldquoEulers Methodrdquo

public class freefalleuler

public static void main(String args[])double av0v1yy0y1delttint j

a = -98y0 = 10v0 = 10delt = 0005

for(t=0 tlt=05 t =t + delt)v1 = v0 + adelty1 = y0 + v1deltSystemoutprintf(84f 84fnty1)v0 = v1y0 = y1

Complile freefalleulerjava for different delt ie 00005 0005 005 and then run and savethe output into a file name as follows

[khamimsetpar65 ~]$ java freefalleuler gtffeuler00005dat[khamimsetpar65 ~]$ java freefalleuler gtffeuler0005dat[khamimsetpar65 ~]$ java freefalleuler gtffeuler005dat

Plotting the results using gnuplot

Using gnuplot plot all the graphs using the following command and compare the results

gnuplotgt plot ffeuler00005dat using 12 title euler delt=00005 with linegt ffeuler0005dat using 12 title euler delt=0005 with linegt ffeuler005dat using 12 title euler deltt=005 with linegt ffsoldat using 12 title exact solution with line

In MS windows

gnuplotgt plot ffeuler00005dat using 12 title euler delt=00005 with lineffeuler0005dat using 12 title euler delt=0005 with lineffeuler005dat using 12 titleeuler delt=005 with line ffsoldat using 12 title exact solution with line

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1958

Gnuplot

The smaller the delt the closer the curve to the exact solution

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2058

983107983119983117983120983125983124983105983124983113983119983118983105983116 983120983112983129983123983113983107983123

983080983123983123983120983090983089983090983090983081

983113983149983152983154983151983158983141983140 983109983157983148983137983154 983149983141983156983144983151983140

983138983161

983105983138983140983086 983115983144983137983149983145983149 983113983155983149983137983145983148

983117983151983144983140 983115983144983137983148983145983140 983115983137983155983149983145983150

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2158

Improved Euler method

Comparison between analytical solution of F=ma with the Euler method shows that although the

method is able to find approximate solution the error is quite significant if computational step is

reduced The error is also cumulative ie as the iteration of the calculation increases the error also gets

bigger hence value furthest away from initial condition has the largest error

The Euler method gives us a way to approximate positions of object with respect to time but requires

an order of magnitude of computational iterations for each order of magnitude of accuracy For large

number of objects this method will certainly consume a great deal of computation time Hence

students are introduced to Initial Half-Step Method an Euler method with a much improved

computational effeciency

Source code

Study of motion of an object with mass m

experiencing a constant force F using

Improved Euler Method (Initial half step method)

In X and Y dimension

import static javalangMath

public class fma04

public static void main(String args[])

double m fx fy x y vo vx vy ax ay t delt

double pi th theta ymax tmax xmax

pi = 40atan(10)

m = 01 in kg

fy = -02 in N (pulling down force)

fx = 00 in N (retarding force)

ay = fym

ax = fxm

vo = 500 in ms

theta = 300 in degrees

th = theta(pi1800) degrees change to radian

vx = vo cos(th) in ms vy = vo sin(th) in ms

y = 12 in m

x = 00 in m

ymax=y initialize var ymax with initial value of y

xmax=00

tmax=00

delt = 001 in s

for(t=00 tlt=300 t=t+delt)

System outprintf(f f fntxy)

if (t==00)vx = vx + 05axdelt

vy = vy + 05aydelt

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2258

else

vx = vx + axdelt

vy = vy + aydelt

x = x + vxdelt

y = y + vydelt

if (ygtymax)

ymax=y

xmax=xtmax=t

if (ylt=00) break

System outprintf( Value of Y maximum is f happened when t = f

nymaxtmax)

System outprintf( at x = fnxmax)

System outprintf( The object reach the ground at x=f and t=fn xt)

gnuplot result

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2358

983107983119983117983120983125983124983105983124983113983119983118983105983116

983120983112983129983123983113983107983123 983080983123983123983120983090983089983090983090983081

983122983141983155983156983151983154983145983150983143 983110983151983154983139983141

983138983161

983105983138983140983086 983115983144983137983149983145983149 983113983155983149983137983145983148

983117983151983144983140 983115983144983137983148983145983140 983115983137983155983149983145983150

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2458

Damped Harmonic Oscillation

Object m is on a flat surface attached to a spring as in figure above The object is pulled

to position A and released so that it oscillates about x =0 Assuming that the force F=ma= -bv-kx where x is position of the object v is the velocity k the spring constant andb is a constant associated with friction with the flat surface

The algorithm

1 Declare the classmethod to be used2 Declare and assign all variables to be used

x t delt k b A m F a v v0 and x03 Declare and assign constants to the respective variables

k = 02 b = 01 A = 01 m = 054 Select appropriate value for ∆t 5 Initialized initial values xo=A vo=00

6 set x=xo v=vo and t = 07 calculate the value of force F = -kx-bv 8 calculate the value of acceleration a = Fm

9 calculate velocity v = vo + ∆v = vo + a ∆t

10 calculate position x = xo + ∆x = xo + v ∆t

11 increment value of t by ∆t 12 print out values of t and x

13 swap the value of xo=x vo=v 14 repeat from step 4 until stopping condition is satisfied (tlt=600)

The java code

public class simplehar01

public static void main(String args[])double x t deltdouble k b A m F a v v0 x0

k = 02b = 01A = 0 1

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2558

m = 05delt = 0001v0 = 00x0 = Ax = x0 v = v0

t = 00while(tlt=600)

F = -kx-bva = Fmv = v0 + adeltx = x0 + vdeltt = t + deltSystemoutprintf(f fntx)x0 = xv0 = v

gnuplot result

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2658

983107983119983117983120983125983124983105983124983113983119983118983105983116 983120983112983129983123983113983107983123

(983123983123983120983090983089983090983090983081

983106983157983145983148983140983145983150983143 983137 983152983144983161983155983145983139983155 983155983151983148983158983141983154

983157983155983145983150983143 983118983141983156983138983141983137983150983155

983138983161

983105983138983140983086 983115983144983137983149983145983149 983113983155983149983137983145983148

983117983151983144983140 983115983144983137983148983145983140 983115983137983155983149983145983150

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2758

How to create a user interface (UI) using Netbeans

Based on the ldquoGravityAttract07ajavardquo source code (shown below) build a user interface usingNetbeans The design of the UI could be look like this (Figure1)

Figure 1

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2858

ldquoGravityAttract07ajavardquo source code

Java class for calculating the resultant force on any object(ie either on m1m2 m3 m4 or m5)

import static javalangMathimport javaiopublic class GravityAttract07a

public static void main(String args[])

double[] m = new double[6] mass of objectdouble[] x = new double[6] x-coor of objectdouble[] y = new double[6] y-coor of objectdouble f resultant forcedouble fxfy x amp y component of forcedouble rx ry rp co G dirfangdirf

int j

G = 667e-11m[1]=25 x[1]=12 y[1]=25m[2]=15 x[2]=15 y[2]=15m[3]=75 x[3]=02 y[3]=55m[4]=92 x[4]=52 y[4]=25m[5]=66 x[5]=42 y[5]=45BufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin))String mass=Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))try mass=dataInreadLine()

catch(IOException e)Systemoutprintln(Not a valid input value)int mi = IntegerparseInt(mass)fx = 00fy = 00

for(j=1 jlt=5 j++)

if (j=mi)rx = x[j]-x[mi]ry = y[j]-y[mi]rp = pow((rxrx+ryry)15) (rx^2 + ry^2) raised to the power of 32co = Gm[mi]m[j]

fx = fx + corxrpfy = fy + coryrp

f = sqrt(fxfx + fyfy)dirf = toDegrees(atan(fyfx))Systemoutprintf(The force on m[+ mi +] due to the other masses is eN fdegnfdirf)

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2958

This example will not show you step by step details procedure since it already explained inthe class and in the e-learning Please refer to the e-learning about how to create a project how tocreate JFrame Form and as well how to create JButton JTextField JLabel JTextPane and so on

First create a new project and give a name eg ldquogravityrdquo and then create JFrame Form andthe following JButton JTextField JLabel JTextPane etc by using ldquoswing paletterdquo as shown in Figure2

Figure 2

To insert a picture into JLabel2 click on the JLabel2 go to properties and select ldquoiconrdquo Onthe icon editor choose file and then select file to locate the directory of your graphicpicture file(Figure 3) Select the file and click OK when finished Make sure you have already created your ownpicture file and save it as jpg png bmp etc format eg ldquogravityjpgrdquo Use any graphic package todraw your diagrampicture or use drawing tools available in Open Office or Microsoft Word

JTextPane1JLabel1

JLabel2

JLabel3JLabel4

JLabel5

JLabel6 JLabel7

JLabel8

JTextField2 JTextField3 JTextField4

JTextF

JButton1JButton2

JButton3

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3058

Figure 3

To write documentation or question eg in JTextPane1 click on the JTextPane1 go toproperties and select ldquotextrdquo Type the documentation in the text editor as shown in Figure 4 Click OKwhen finished

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3158

To do formatting on the text such as superscript and subscript eg in jLabel3 click on the

JLabel3 go to properties and select ldquotextrdquo Type the documentationmessage on the text editor asshown in Figure 5 as follows

eg

lthtmlgtOn which particle (mltsubgt1ltsubgtmltsubgt5ltsubgt) you want to calculate theresultant force

Use sup instead if you want a superscriptThe result will look like this

Click OK when finished

Figure 5

To do the calculation you need to put the coding similar like ldquoGravityAttract07ajavardquo and put itinto the ldquocalculaterdquo button (this is ldquoJButton1rdquo) You can copy ldquoGravityAttract07a javardquo source code

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3258

paste it and then do a few alterations First click on the ldquocalculaterdquo button right click choose Eventsand then choose Action then actionPerformed (Figure 6) It will bring you to where you should insertyour coding (Figure 7) You can write your own coding or copypaste any coding from somewhereelse (in this case from ldquoGravityAttract07ajavardquo)

Figure 6

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3358

Figure 7

Copy ldquoGravityAttract07ajavardquo source code and paste to the place after ldquo TODO add youhandling code hererdquo see Figure 7 To see which part has been modified the coding in Figure 7 hasbeen color coded for better understanding as follows

private void jButton1ActionPerformed(javaawteventActionEvent evt) TODO add your handling code here

double[] m = new double[6] mass of objectdouble[] x = new double[6] x-coor of objectdouble[] y = new double[6] y-coor of objectdouble f resultant forcedouble fxfy x amp y component of forcedouble rx ry rp co G dirfangdirfDecimalFormat dff dfangdirfint j mi

G = 667e-11m[1]=25 x[1]=12 y[1]=25m[2]=15 x[2]=15 y[2]=15m[3]=75 x[3]=02 y[3]=55m[4]=92 x[4]=52 y[4]=25m[5]=66 x[5]=42 y[5]=45

BufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin)) String mass= Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3458

catch(IOException e)Systemoutprintln(Not a valid input value) int mi = IntegerparseInt(mass)mi = IntegerparseInt(jTextField1getText())fx = 00fy = 00for(j=1 jlt=5 j++)

if (j=mi)rx = x[j]-x[mi]ry = y[j]-y[mi]rp = Mathpow((rxrx+ryry)15) (rx^2 + ry^2) raised to the power of 32co = Gm[mi]m[j]fx = fx + corxrpfy = fy + coryrp

f = Mathsqrt(fxfx + fyfy)

dirf = Mathatan(fyfx)angdirf = (dirf(22070))1800 Systemoutprintf(ldquoThe resultant force on particle [ldquo+ mi +rdquo] due to the other masses is

14e N 22f degnrdquofangdirf)dff = new javatextDecimalFormat(ldquoE00rdquo)dfangdirf = new DecimalFormat(ldquo000rdquo)

jTextField2setText(ldquordquo + mi) jTextField3setText(ldquordquo + dffformat(f)) jTextField4setText(ldquordquo + dfangdirfformat(angdirf))

Note

Brown = automatically generatedBlue = the original code from ldquoGravityAttract07ajavardquoGreen = coding from ldquoGravityAttract07ajavardquo which were being disabled due to

incompatibilities with NetbeansRed = a new inserted coding a few of them is to rephrasesubstitute the green one

Javascripts command line useBufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin))String mass=Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))try mass=dataInreadLine()catch(IOException e)Systemoutprintln(Not a valid input value)

int mi = IntegerparseInt(mass)

to read any input from the keyboard typed by the user while Netbeans uses egldquoIntegerparseInt(jTextField1getText())rdquo etc Javascripts command line uses ldquoSystemoutprintfrdquo to format the printed output whileNetbeans uses more complicated way to do the same job (see below)

Javascript command line

Systemoutprintf(The resultant force on particle [+ mi +] due to the other masses is 14e N22f degnfangdirf)

Netbeansdff = new javatextDecimalFormat(ldquo0E00rdquo)df di f D i lF t(ldquo0 00rdquo)

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3558

jTextField2setText(ldquordquo + mi) jTextField3setText(ldquordquo + dffformat(f)) jTextField4setText(ldquordquo + dfangdirfformat(angdirf))

ldquodffrdquo and ldquodfangdirfrdquo are new variables that should be declared and added to format thenumbers into decimal format ie ldquo0E00rdquo to get a scientific format eg ldquo20019E-10rdquo and ldquo000rdquoto get a decimal number eg ldquo-1570rdquo

To use DecimalFormat you have to put ldquoimport javatextDecimalFormatrdquo in the top of thecoding as shown in Figure 8

Figure 8

To reset the calculation you need to put coding into the ldquoresetrdquo button (this is ldquoJButton2rdquo)First click on the ldquoresetrdquo button right click choose Events and then choose Action thenactionPerformed (similar like in Figure 6) It will bring you to where you should insert your coding(Figure 9) Write the following coding (Figure 9)

Figure 9

To exit the program you need to put coding into the ldquoexitrdquo button (this is ldquoJButton3rdquo) Firstclick on the ldquoexitrdquo button right click choose Events and then choose Action then actionPerformed(similar like in Figure 6) It will bring you to where you should insert your coding (Figure 10) Writethe following coding (Figure 10)

Figure 10

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3658

Once you finished now you can run and see the result If everything is fine then the output wilbe look like this (Figure 11)

Figure 11

Remember when you compile and run ldquoGravityAttract07ajavardquo using Javascripts commandline once you compile and run the result was looked like this

On which particle you want to calculate the resulting force 2The resultant force on particle [2] due to the other masses is 30673e-10 N 8837 deg

SEE THE DIFFERENCES AT THE USER INTERFACE

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3758

8th Step

Written by Mohd Khalid Kasmin

Monday 27 August 2007

Programming using command line interface (CLI) has its advantages but todays computer users are so used to graphical user

interface (GUI) that we just can no longer be contented with writing application that do not use the GUI Hence starting from this 8thstep we are going to embark on learning how to make java application that utilises the the GUI

It is possible to write a GUI application with the text based tools that we have been using for these past 7 steps but the job is so

messy that most of you will definitely be turnoff by it So to make life slightly easier well use an integrated development

environment (IDE) known as NetBeans specifically NetBeans 551

0 First ssh to the our server Next once logged in just type nice -n15 netbeans amp on the terminal If you had sshed with the -Y

switch after a few tens of seconds NetBeans should emerge on your display screen

One important point to note Since our server can readily handle up to 100 users (maybe even

more) doing work in CLI mode this machine will certainly be taxed to its maximum capability once

we start using graphical based application NetBeans 551 uses a geat deal of computer memory

and with the 4Gbytes RAM server it can only accomodate up to 25 users concurrently using

NetBeans if the users use the application wisely What is meant by wisely here is that (a) when

starting the NetBeans application please use it together with the nice -n15 command (b) in using

the NetBeans please make sure you only have one project open at a time and (c) please close your

project everytime before quitting the NetBeans

Home Profile Teaching Links Contact Me My Work

Newsflash

Yesterday all servers in the US went out on strike in a bid to get more RAM and better CPUs A spokes person said that the

need for better RAM was due to some fool increasing the front-side bus speed In future busses will be told to slow down in

residential motherboards

search

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3858

1

2

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3958

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4058

[ Back ]

5

Last Updated ( Wednesday 31 October 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4158

8th Step (Cont)

Written by Mohd Khalid Kasmin

Monday 27 August 2007

Up to this point we have define the name of our project Next we construct a class and to follow what we already know we name the class the sam

project name latihan01

6

7

Home Profile Teaching Links Contact Me My Work

Newsflash

Joomla 10 - Experience the Freedom It has never been easier to create your own dynamic site Manage all your content from

the best CMS admin interface

search

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4258

8

9

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4358

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4458

8th Step (Cont still)

Written by Mohd Khalid Kasmin

Monday 27 August 2007

In item (9) we see that Netbeans does not provide us with the main method So to follow what we already know we now put in the

main method

10

Home Profile Teaching Links Contact Me My Work

Newsflash

Aoccdrnig to a rscheearch at an Elingsh uinervtisy it deosnt mttaer in waht oredr the ltteers in a wrod are the olny iprmoetnt

tihng is taht frist and lsat ltteer is at the rghit pclae The rset can be a toatl mses and you can sitll raed it wouthit porbelm Tihs is

bcuseae we do not raed ervey lteter by itslef but the wrod as a wlohe

search

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4558

11

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4658

12

13

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4758

14

15

Last Updated ( Monday 27 August 2007 )

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4858

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4958

9th Step

Thursday 06 September 2007

Step by Step into Java Programming

for

University Physics Student

By

Mohd Khalid Kasmin

A Building graphical user interface - preparing the JFrame Form

At this stage of our programming experience with Java and NetBeans please follow strictly the steps outline in this article First and

make a new GUI project make sure there is no other project that already been loaded by Netbeans Please close any project displayed

1 Create new project

- On the main menu click on File --gt New Project (note Close first any previous project)

- Specify project name amp project location - Untick Create Main Class item

- Click on the Finish button

Home Profile Teaching Links Contact Me My Work

Newsflash

Aoccdrnig to a rscheearch at an Elingsh uinervtisy it deosnt mttaer in waht oredr the ltteers in a wrod are the olny iprmoetnt

tihng is taht frist and lsat ltteer is at the rghit pclae The rset can be a toatl mses and you can sitll raed it wouthit porbelm Tihs is

bcuseae we do not raed ervey lteter by itslef but the wrod as a wlohe

search

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5058

Figure 91

2 Create UI class

- Right click on the project name in the Project pane

on the popup menu select New --gt JFrame Form

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5158

Figure 92

- In the New JFrame Form dialog specify the Class Name and the Package name

eg Class Name multiplierUI Package mymultiplier

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5258

Figure 93

After clicking on the Finish button we should get something like the following

Figure 94

Last Updated ( Tuesday 18 September 2007 )

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5358

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5458

9th Step (Cont)

Written by Mohd Khalid Kasmin

Tuesday 11 September 2007

Step by Step into Java Programming

for University Physics Student

By

Mohd Khalid Kasmin

B Building user interface (UI) - putting in the components

Now that we have a proper blank form we can start filling the form with the suitable graphical components For introduction to

GUI programming an appropriate application to build (for a physics student) is a simple solver So our first exercise is to build an

application that takes two numbers and multiply them and display the result

With GUI we usually give input values to an application via input boxes that are labelled When all inputs are typed in properlywe click on a button to ask the application to execute the job Hence in this exercise well do just that The target now is to

construct a user interface that looks like Figure 95 below

Figure 95

Home Profile Teaching Links Contact Me My Work

Newsflash

Joomla 10 - Experience the Freedom It has never been easier to create your own dynamic site Manage all your content from

the best CMS admin interface

search

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5558

[ Back ]

1 Problemapplication description box

- In the palette pane click on Swing component named JTextPane (Note click just once) Move the mouse pointer on top of

your JFrame (Note just move not drag) When you see a little white box with orange border position it somewhere near the

top part of your JFrame and click once The JTextPane is then attached on the JFrame Addjust its size with the help of the

sizing hook around the perimeter of the JTextPane

- Edit the text property of the JTextPane with the description of your application

2 Input boxes

- In the palette pane click on Swing component named JTextField Next move the mouse pointer on top of your JFrame

When you see a white box with the word jTextField1 position it to the left and under the JTextPane1

- Click on Swing component JTextField in the palette pane again Move the mouse pointer on top of the JFrame you should

get a second JTextField named jTextField2 Position it to the right of jTextField1

Last Updated ( Tuesday 11 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5658

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5758

multiplication if a b and c were first declared as double or float or int variables So the code of the calculation should look like the

following

double a b c

a =

b =

c = a b

Clearly variables a and b need to be assigned with values of typed double Now looking back at inputboxes A and B we made

those boxes using the JTextField component meaning that any data or value written in a JTextField is of typed Text or String even

if the value displayed in the box is ldquo125rdquo But f irst here there is this task of getting to that Text (or String) values ldquo125rdquo and ldquo72rdquo

In Java we can get to a text in JTextField component using the getText() method So to retrieve values in jTextField1 and

jTextField2 we can use

jTextField1getText()

and

jTextField2getText()

But those values are still in text mode and we need another method to convert the data mode f rom text to double For that we can

use the method DoubleparseDouble() Thus the whole process of retrieve text -gt convert text to double -gt assign to double

variable can be done using

a = DoubleparseDouble( jTextField1getText() )

and

b = DoubleparseDouble( jTextField2getText() )

Hence the code for performing the multiplication becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

Up to this point we have the variable c as the result of the multiplication of a and b So the next thing to do is to display this

result on the JLabel that we have already prepared before

A x B = _____

The text A x B = ______ was made using JLabel component and its variable name is jLabel3 Since the variable c is of type

double and text property of JLabel is of type String we need to convert it first to String data type The method to convert a double

to String is DoubletoString() So we can write the code as DoubletoString(c) After getting the text we can now use it to be the

text displayed on jLabel3 To do that we use the method setText() Hence the appropriate code for displaying the result is

jLabel3setText( A x B = + DoubletoString(c) )

And the full code for our calculation becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

jLabel3setText( A x B = + DoubletoString(c) )

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5858

[ Back ]

Last Updated ( Tuesday 18 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

Page 9: Computional Physics

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 958

Source code GravityAttract07ajava

Java class for calculating the resultant force on any object(ie either on m1m2 m3 m4 or m5)

import static javalangMathimport javaiopublic class GravityAttract07a

public static void main(String args[])

double[] m = new double[6] mass of objectdouble[] x = new double[6] x-coor of objectdouble[] y = new double[6] y-coor of objectdouble f resultant forcedouble fxfy x amp y component of forcedouble rx ry rp co G dirfangdirfint j

G = 667e-11m[1]=25 x[1]=12 y[1]=25m[2]=15 x[2]=15 y[2]=15m[3]=75 x[3]=02 y[3]=55m[4]=92 x[4]=52 y[4]=25m[5]=66 x[5]=42 y[5]=45BufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin))String mass=Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))

try mass=dataInreadLine()catch(IOException e)Systemoutprintln(Not a valid input value)int mi = IntegerparseInt(mass)fx = 00fy = 00

for(j=1 jlt=5 j++)

if (j=mi)rx = x[j]-x[mi]ry = y[j]-y[mi]rp = pow((rxrx+ryry)15) (rx^2 + ry^2) raised to the power of 32

co = Gm[mi]m[j]fx = fx + corxrpfy = fy + coryrp

f = sqrt(fxfx + fyfy)dirf = toDegrees(atan(fyfx))Systemoutprintf(The force on m[+ mi +] due to the other masses is eN fdegnfdirf)

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1058

983107983119983117983120983125983124983105983124983113983119983118983105983116

983120983112983129983123983113983107983123 983080983123983123983120983090983089983090983090983081

983109983148983141983139983156983154983151983155983156983137983156983145983139 983152983151983156983141983150983156983145983137983148983138983161

983105983138983140983086 983115983144983137983149983145983149 983113983155983149983137983145983148

983117983151983144983140 983115983144983137983148983145983140 983115983137983155983149983145983150

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1158

1 Calculation of electrostatic potential for a group of point charges

You will learn about

- mesh method

a) using a for() loop inside another for() loop

to generate ordered pair (xy) points in a mesh on x-y plane

b) calculate physical value in this case V(xy) forevery point (xy) generated

- saving calculated values into a file

a) making a sequential text file

b) saving in a format that gnuplot can understand

for making a surface plot

- use gnuplot to visualize potential surface

a) making a surface plot from data points in a text file

b) making contour plot to visualize equipotential lines

c) changing the z-axis range to zoom in or zoom out on the surface

d) labelling the plot

e) save as an image the potential surface amp the contour plotf) save as an eps file the potential surface amp the contour plot

- including an eps file into a document

2 Calculation of electrostatic field for a group of point charges

You will learn again

- using mesh method to generate ordered pair (xy) points

in a mesh on x-y plane

You will also learn how to

- calculate vector field values in this case E(xy) for every

generated (xy) mesh point

- save the calculated vector points into a text file

in a format understood by gnuplot

- make a vector field plot using gnuplot

Source code

import static javalangMath

public class estatpot01b

public static void main(String[] args)

double[] q = new double[100]

double[] xq = new double[100]

double[] yq = new double[100]

double V r rx ry k x y

int i n

q[1] = 20e-6 xq[1] = -10 yq[1] = 10

q[2] =-30e-6 xq[2] = 30 yq[2] =-20

q[3] = 30e-6 xq[3] = -20 yq[3] = 40

q[4] =-50e-6 xq[4] = 20 yq[4] = 50

k = 9e9n = 4

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1258

for (x=-5 xlt=5 x = x+02)

for (y=5 ygt=-5 y = y-02)

V = 00

for(i=1 ilt=n i++)

rx = x - xq[i]

ry = y - yq[i]r = sqrt(rxrx+ryry)

if (rlt1e-6)

if (q[i]lt0) V = -10 else V = 10

break

V = V + q[i]r

V = kV

Systemoutprintf(f f fn x y V)

Systemoutprintf(n)

How to do surface plot using Gnuplot

To do a surface plotgnuplotgt splot epotdat with lineTo zoom in and zoom out z axisgnuplotgt set zrange [-2000020000]To convert the graph into imagegnuplotgt set term pngTerminal type set to pngOptions are small color picsize 640 480 Then typegnuplotgt set output epotpngAgain do splot as followgnuplotgt splot epotdat with lineNow the graph is saved into a file in png format named as epotpngTo go back into terminal ouputgnuplotgt set term x11To set to countor plotgnuplotgt set pm3dTo remove countor meshgnuplotgt unset surf

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1358

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1458

983107983119983117983120983125983124983105983124983113983119983118983105983116 983120983112983129983123983113983107983123

983080983123983123983120983090983089983090983090983081

983109983157983148983137983154 983149983141983156983144983151983140

983138983161

983105983138983140983086 983115983144983137983149983145983149 983113983155983149983137983145983148

983117983151983144983140 983115983144983137983148983145983140 983115983137983155983149983145983150

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1558

Free fall body problem

Consider an object thrown upward under gravitational force ( f=mg) with an initial velocity v0The object goes up and then stop at maximum y position ymax before it start to fall downback to the original position at y0 and then to the ground level (See figure)

ymdt

yd m

dt

dvmmaF ampamp====

2

2

t

Y

v1 (t1y1)

v2 (t2y2)

v4 (t4y4)

v5 (t5y5)

vn (tnyn)

v3 (t3y3)

v0 (t0y0)

∆t∆t ∆t ∆t

y1

y0

∆t ∆t

∆yv0

a = g

y0

t0

t0 t1 t2 t3 t4 t5 tn

ymax

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1658

Exact theoretical solution

( ) 2

21

00 at t v yt y ++=

The y projection of the exact solution can be plotted using the following java source code

Java class for solving free fall object under gravity (g = 98 mss)Exact solution y = y0 + v0t + 12at^2

public class freefallsol

public static void main(String args[])double av0yy0deltt

a = -98y0 = 10v0 = 10delt = 00001for(t=0 tlt=05 t=t+delt)

y = y0 + v0t + 05attSystemoutprintf(84f 84fnty)

Complile freefallsoljava to get the plot of the exact solution and then run and save theoutput into a file name as follows

[khamimsetpar65 ~]$ java freefallsol gtffsoldat

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1758

Euler Method

There is another way to see the projection of y position of the object using a numericalmethod called ldquoEuler methodrdquo See the following derivation

t avt v y

t

va

t

yv

∆=∆∆=∆

∆=

∆=

t v y y y y

t avvvv

t v y y y y

t avvvv

t v y y y y

t avvvv

nnnn

nnn

∆+=∆+=

∆+=∆+=

∆+=∆+=

∆+=∆+=

∆+=∆+=

∆+=∆+=

minus

minusminus

1

11

2112

112

1001

001

M

if ∆∆∆∆t rarrrarrrarrrarr 0 then y will close to the exact solution ( ( ) 2

21

00 at t v yt y ++= )

The algorithm 1 Declare the classmethod to be used

2 Declare and assign all variables to be used av0v1yy0y1deltt and j 3 Initialize value to all respective variables a = -98 y0 = 10 v0 = 10

4 Select appropriate value for ∆t

∆t = 00055 Start the loop to calculate the velocity and position for t=0

6 Calculate velocity v1 = v0 + ∆v = v0 + a ∆t

7 Calculate position y1 = y0 + ∆y = y0 + v1 ∆t8 Print out values of t and y1 9 Swap v0 = v1 and y0 = y1

10 Repeat from step 5 and increase the value of t by ∆t (t =t + ∆t) until stoppingcondition is satisfied at t=05

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1858

The java code

Java class for solving free fall object under gravity (g = 98 mss)by numerical method called ldquoEulers Methodrdquo

public class freefalleuler

public static void main(String args[])double av0v1yy0y1delttint j

a = -98y0 = 10v0 = 10delt = 0005

for(t=0 tlt=05 t =t + delt)v1 = v0 + adelty1 = y0 + v1deltSystemoutprintf(84f 84fnty1)v0 = v1y0 = y1

Complile freefalleulerjava for different delt ie 00005 0005 005 and then run and savethe output into a file name as follows

[khamimsetpar65 ~]$ java freefalleuler gtffeuler00005dat[khamimsetpar65 ~]$ java freefalleuler gtffeuler0005dat[khamimsetpar65 ~]$ java freefalleuler gtffeuler005dat

Plotting the results using gnuplot

Using gnuplot plot all the graphs using the following command and compare the results

gnuplotgt plot ffeuler00005dat using 12 title euler delt=00005 with linegt ffeuler0005dat using 12 title euler delt=0005 with linegt ffeuler005dat using 12 title euler deltt=005 with linegt ffsoldat using 12 title exact solution with line

In MS windows

gnuplotgt plot ffeuler00005dat using 12 title euler delt=00005 with lineffeuler0005dat using 12 title euler delt=0005 with lineffeuler005dat using 12 titleeuler delt=005 with line ffsoldat using 12 title exact solution with line

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1958

Gnuplot

The smaller the delt the closer the curve to the exact solution

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2058

983107983119983117983120983125983124983105983124983113983119983118983105983116 983120983112983129983123983113983107983123

983080983123983123983120983090983089983090983090983081

983113983149983152983154983151983158983141983140 983109983157983148983137983154 983149983141983156983144983151983140

983138983161

983105983138983140983086 983115983144983137983149983145983149 983113983155983149983137983145983148

983117983151983144983140 983115983144983137983148983145983140 983115983137983155983149983145983150

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2158

Improved Euler method

Comparison between analytical solution of F=ma with the Euler method shows that although the

method is able to find approximate solution the error is quite significant if computational step is

reduced The error is also cumulative ie as the iteration of the calculation increases the error also gets

bigger hence value furthest away from initial condition has the largest error

The Euler method gives us a way to approximate positions of object with respect to time but requires

an order of magnitude of computational iterations for each order of magnitude of accuracy For large

number of objects this method will certainly consume a great deal of computation time Hence

students are introduced to Initial Half-Step Method an Euler method with a much improved

computational effeciency

Source code

Study of motion of an object with mass m

experiencing a constant force F using

Improved Euler Method (Initial half step method)

In X and Y dimension

import static javalangMath

public class fma04

public static void main(String args[])

double m fx fy x y vo vx vy ax ay t delt

double pi th theta ymax tmax xmax

pi = 40atan(10)

m = 01 in kg

fy = -02 in N (pulling down force)

fx = 00 in N (retarding force)

ay = fym

ax = fxm

vo = 500 in ms

theta = 300 in degrees

th = theta(pi1800) degrees change to radian

vx = vo cos(th) in ms vy = vo sin(th) in ms

y = 12 in m

x = 00 in m

ymax=y initialize var ymax with initial value of y

xmax=00

tmax=00

delt = 001 in s

for(t=00 tlt=300 t=t+delt)

System outprintf(f f fntxy)

if (t==00)vx = vx + 05axdelt

vy = vy + 05aydelt

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2258

else

vx = vx + axdelt

vy = vy + aydelt

x = x + vxdelt

y = y + vydelt

if (ygtymax)

ymax=y

xmax=xtmax=t

if (ylt=00) break

System outprintf( Value of Y maximum is f happened when t = f

nymaxtmax)

System outprintf( at x = fnxmax)

System outprintf( The object reach the ground at x=f and t=fn xt)

gnuplot result

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2358

983107983119983117983120983125983124983105983124983113983119983118983105983116

983120983112983129983123983113983107983123 983080983123983123983120983090983089983090983090983081

983122983141983155983156983151983154983145983150983143 983110983151983154983139983141

983138983161

983105983138983140983086 983115983144983137983149983145983149 983113983155983149983137983145983148

983117983151983144983140 983115983144983137983148983145983140 983115983137983155983149983145983150

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2458

Damped Harmonic Oscillation

Object m is on a flat surface attached to a spring as in figure above The object is pulled

to position A and released so that it oscillates about x =0 Assuming that the force F=ma= -bv-kx where x is position of the object v is the velocity k the spring constant andb is a constant associated with friction with the flat surface

The algorithm

1 Declare the classmethod to be used2 Declare and assign all variables to be used

x t delt k b A m F a v v0 and x03 Declare and assign constants to the respective variables

k = 02 b = 01 A = 01 m = 054 Select appropriate value for ∆t 5 Initialized initial values xo=A vo=00

6 set x=xo v=vo and t = 07 calculate the value of force F = -kx-bv 8 calculate the value of acceleration a = Fm

9 calculate velocity v = vo + ∆v = vo + a ∆t

10 calculate position x = xo + ∆x = xo + v ∆t

11 increment value of t by ∆t 12 print out values of t and x

13 swap the value of xo=x vo=v 14 repeat from step 4 until stopping condition is satisfied (tlt=600)

The java code

public class simplehar01

public static void main(String args[])double x t deltdouble k b A m F a v v0 x0

k = 02b = 01A = 0 1

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2558

m = 05delt = 0001v0 = 00x0 = Ax = x0 v = v0

t = 00while(tlt=600)

F = -kx-bva = Fmv = v0 + adeltx = x0 + vdeltt = t + deltSystemoutprintf(f fntx)x0 = xv0 = v

gnuplot result

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2658

983107983119983117983120983125983124983105983124983113983119983118983105983116 983120983112983129983123983113983107983123

(983123983123983120983090983089983090983090983081

983106983157983145983148983140983145983150983143 983137 983152983144983161983155983145983139983155 983155983151983148983158983141983154

983157983155983145983150983143 983118983141983156983138983141983137983150983155

983138983161

983105983138983140983086 983115983144983137983149983145983149 983113983155983149983137983145983148

983117983151983144983140 983115983144983137983148983145983140 983115983137983155983149983145983150

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2758

How to create a user interface (UI) using Netbeans

Based on the ldquoGravityAttract07ajavardquo source code (shown below) build a user interface usingNetbeans The design of the UI could be look like this (Figure1)

Figure 1

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2858

ldquoGravityAttract07ajavardquo source code

Java class for calculating the resultant force on any object(ie either on m1m2 m3 m4 or m5)

import static javalangMathimport javaiopublic class GravityAttract07a

public static void main(String args[])

double[] m = new double[6] mass of objectdouble[] x = new double[6] x-coor of objectdouble[] y = new double[6] y-coor of objectdouble f resultant forcedouble fxfy x amp y component of forcedouble rx ry rp co G dirfangdirf

int j

G = 667e-11m[1]=25 x[1]=12 y[1]=25m[2]=15 x[2]=15 y[2]=15m[3]=75 x[3]=02 y[3]=55m[4]=92 x[4]=52 y[4]=25m[5]=66 x[5]=42 y[5]=45BufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin))String mass=Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))try mass=dataInreadLine()

catch(IOException e)Systemoutprintln(Not a valid input value)int mi = IntegerparseInt(mass)fx = 00fy = 00

for(j=1 jlt=5 j++)

if (j=mi)rx = x[j]-x[mi]ry = y[j]-y[mi]rp = pow((rxrx+ryry)15) (rx^2 + ry^2) raised to the power of 32co = Gm[mi]m[j]

fx = fx + corxrpfy = fy + coryrp

f = sqrt(fxfx + fyfy)dirf = toDegrees(atan(fyfx))Systemoutprintf(The force on m[+ mi +] due to the other masses is eN fdegnfdirf)

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2958

This example will not show you step by step details procedure since it already explained inthe class and in the e-learning Please refer to the e-learning about how to create a project how tocreate JFrame Form and as well how to create JButton JTextField JLabel JTextPane and so on

First create a new project and give a name eg ldquogravityrdquo and then create JFrame Form andthe following JButton JTextField JLabel JTextPane etc by using ldquoswing paletterdquo as shown in Figure2

Figure 2

To insert a picture into JLabel2 click on the JLabel2 go to properties and select ldquoiconrdquo Onthe icon editor choose file and then select file to locate the directory of your graphicpicture file(Figure 3) Select the file and click OK when finished Make sure you have already created your ownpicture file and save it as jpg png bmp etc format eg ldquogravityjpgrdquo Use any graphic package todraw your diagrampicture or use drawing tools available in Open Office or Microsoft Word

JTextPane1JLabel1

JLabel2

JLabel3JLabel4

JLabel5

JLabel6 JLabel7

JLabel8

JTextField2 JTextField3 JTextField4

JTextF

JButton1JButton2

JButton3

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3058

Figure 3

To write documentation or question eg in JTextPane1 click on the JTextPane1 go toproperties and select ldquotextrdquo Type the documentation in the text editor as shown in Figure 4 Click OKwhen finished

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3158

To do formatting on the text such as superscript and subscript eg in jLabel3 click on the

JLabel3 go to properties and select ldquotextrdquo Type the documentationmessage on the text editor asshown in Figure 5 as follows

eg

lthtmlgtOn which particle (mltsubgt1ltsubgtmltsubgt5ltsubgt) you want to calculate theresultant force

Use sup instead if you want a superscriptThe result will look like this

Click OK when finished

Figure 5

To do the calculation you need to put the coding similar like ldquoGravityAttract07ajavardquo and put itinto the ldquocalculaterdquo button (this is ldquoJButton1rdquo) You can copy ldquoGravityAttract07a javardquo source code

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3258

paste it and then do a few alterations First click on the ldquocalculaterdquo button right click choose Eventsand then choose Action then actionPerformed (Figure 6) It will bring you to where you should insertyour coding (Figure 7) You can write your own coding or copypaste any coding from somewhereelse (in this case from ldquoGravityAttract07ajavardquo)

Figure 6

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3358

Figure 7

Copy ldquoGravityAttract07ajavardquo source code and paste to the place after ldquo TODO add youhandling code hererdquo see Figure 7 To see which part has been modified the coding in Figure 7 hasbeen color coded for better understanding as follows

private void jButton1ActionPerformed(javaawteventActionEvent evt) TODO add your handling code here

double[] m = new double[6] mass of objectdouble[] x = new double[6] x-coor of objectdouble[] y = new double[6] y-coor of objectdouble f resultant forcedouble fxfy x amp y component of forcedouble rx ry rp co G dirfangdirfDecimalFormat dff dfangdirfint j mi

G = 667e-11m[1]=25 x[1]=12 y[1]=25m[2]=15 x[2]=15 y[2]=15m[3]=75 x[3]=02 y[3]=55m[4]=92 x[4]=52 y[4]=25m[5]=66 x[5]=42 y[5]=45

BufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin)) String mass= Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3458

catch(IOException e)Systemoutprintln(Not a valid input value) int mi = IntegerparseInt(mass)mi = IntegerparseInt(jTextField1getText())fx = 00fy = 00for(j=1 jlt=5 j++)

if (j=mi)rx = x[j]-x[mi]ry = y[j]-y[mi]rp = Mathpow((rxrx+ryry)15) (rx^2 + ry^2) raised to the power of 32co = Gm[mi]m[j]fx = fx + corxrpfy = fy + coryrp

f = Mathsqrt(fxfx + fyfy)

dirf = Mathatan(fyfx)angdirf = (dirf(22070))1800 Systemoutprintf(ldquoThe resultant force on particle [ldquo+ mi +rdquo] due to the other masses is

14e N 22f degnrdquofangdirf)dff = new javatextDecimalFormat(ldquoE00rdquo)dfangdirf = new DecimalFormat(ldquo000rdquo)

jTextField2setText(ldquordquo + mi) jTextField3setText(ldquordquo + dffformat(f)) jTextField4setText(ldquordquo + dfangdirfformat(angdirf))

Note

Brown = automatically generatedBlue = the original code from ldquoGravityAttract07ajavardquoGreen = coding from ldquoGravityAttract07ajavardquo which were being disabled due to

incompatibilities with NetbeansRed = a new inserted coding a few of them is to rephrasesubstitute the green one

Javascripts command line useBufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin))String mass=Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))try mass=dataInreadLine()catch(IOException e)Systemoutprintln(Not a valid input value)

int mi = IntegerparseInt(mass)

to read any input from the keyboard typed by the user while Netbeans uses egldquoIntegerparseInt(jTextField1getText())rdquo etc Javascripts command line uses ldquoSystemoutprintfrdquo to format the printed output whileNetbeans uses more complicated way to do the same job (see below)

Javascript command line

Systemoutprintf(The resultant force on particle [+ mi +] due to the other masses is 14e N22f degnfangdirf)

Netbeansdff = new javatextDecimalFormat(ldquo0E00rdquo)df di f D i lF t(ldquo0 00rdquo)

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3558

jTextField2setText(ldquordquo + mi) jTextField3setText(ldquordquo + dffformat(f)) jTextField4setText(ldquordquo + dfangdirfformat(angdirf))

ldquodffrdquo and ldquodfangdirfrdquo are new variables that should be declared and added to format thenumbers into decimal format ie ldquo0E00rdquo to get a scientific format eg ldquo20019E-10rdquo and ldquo000rdquoto get a decimal number eg ldquo-1570rdquo

To use DecimalFormat you have to put ldquoimport javatextDecimalFormatrdquo in the top of thecoding as shown in Figure 8

Figure 8

To reset the calculation you need to put coding into the ldquoresetrdquo button (this is ldquoJButton2rdquo)First click on the ldquoresetrdquo button right click choose Events and then choose Action thenactionPerformed (similar like in Figure 6) It will bring you to where you should insert your coding(Figure 9) Write the following coding (Figure 9)

Figure 9

To exit the program you need to put coding into the ldquoexitrdquo button (this is ldquoJButton3rdquo) Firstclick on the ldquoexitrdquo button right click choose Events and then choose Action then actionPerformed(similar like in Figure 6) It will bring you to where you should insert your coding (Figure 10) Writethe following coding (Figure 10)

Figure 10

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3658

Once you finished now you can run and see the result If everything is fine then the output wilbe look like this (Figure 11)

Figure 11

Remember when you compile and run ldquoGravityAttract07ajavardquo using Javascripts commandline once you compile and run the result was looked like this

On which particle you want to calculate the resulting force 2The resultant force on particle [2] due to the other masses is 30673e-10 N 8837 deg

SEE THE DIFFERENCES AT THE USER INTERFACE

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3758

8th Step

Written by Mohd Khalid Kasmin

Monday 27 August 2007

Programming using command line interface (CLI) has its advantages but todays computer users are so used to graphical user

interface (GUI) that we just can no longer be contented with writing application that do not use the GUI Hence starting from this 8thstep we are going to embark on learning how to make java application that utilises the the GUI

It is possible to write a GUI application with the text based tools that we have been using for these past 7 steps but the job is so

messy that most of you will definitely be turnoff by it So to make life slightly easier well use an integrated development

environment (IDE) known as NetBeans specifically NetBeans 551

0 First ssh to the our server Next once logged in just type nice -n15 netbeans amp on the terminal If you had sshed with the -Y

switch after a few tens of seconds NetBeans should emerge on your display screen

One important point to note Since our server can readily handle up to 100 users (maybe even

more) doing work in CLI mode this machine will certainly be taxed to its maximum capability once

we start using graphical based application NetBeans 551 uses a geat deal of computer memory

and with the 4Gbytes RAM server it can only accomodate up to 25 users concurrently using

NetBeans if the users use the application wisely What is meant by wisely here is that (a) when

starting the NetBeans application please use it together with the nice -n15 command (b) in using

the NetBeans please make sure you only have one project open at a time and (c) please close your

project everytime before quitting the NetBeans

Home Profile Teaching Links Contact Me My Work

Newsflash

Yesterday all servers in the US went out on strike in a bid to get more RAM and better CPUs A spokes person said that the

need for better RAM was due to some fool increasing the front-side bus speed In future busses will be told to slow down in

residential motherboards

search

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3858

1

2

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3958

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4058

[ Back ]

5

Last Updated ( Wednesday 31 October 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4158

8th Step (Cont)

Written by Mohd Khalid Kasmin

Monday 27 August 2007

Up to this point we have define the name of our project Next we construct a class and to follow what we already know we name the class the sam

project name latihan01

6

7

Home Profile Teaching Links Contact Me My Work

Newsflash

Joomla 10 - Experience the Freedom It has never been easier to create your own dynamic site Manage all your content from

the best CMS admin interface

search

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4258

8

9

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4358

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4458

8th Step (Cont still)

Written by Mohd Khalid Kasmin

Monday 27 August 2007

In item (9) we see that Netbeans does not provide us with the main method So to follow what we already know we now put in the

main method

10

Home Profile Teaching Links Contact Me My Work

Newsflash

Aoccdrnig to a rscheearch at an Elingsh uinervtisy it deosnt mttaer in waht oredr the ltteers in a wrod are the olny iprmoetnt

tihng is taht frist and lsat ltteer is at the rghit pclae The rset can be a toatl mses and you can sitll raed it wouthit porbelm Tihs is

bcuseae we do not raed ervey lteter by itslef but the wrod as a wlohe

search

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4558

11

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4658

12

13

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4758

14

15

Last Updated ( Monday 27 August 2007 )

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4858

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4958

9th Step

Thursday 06 September 2007

Step by Step into Java Programming

for

University Physics Student

By

Mohd Khalid Kasmin

A Building graphical user interface - preparing the JFrame Form

At this stage of our programming experience with Java and NetBeans please follow strictly the steps outline in this article First and

make a new GUI project make sure there is no other project that already been loaded by Netbeans Please close any project displayed

1 Create new project

- On the main menu click on File --gt New Project (note Close first any previous project)

- Specify project name amp project location - Untick Create Main Class item

- Click on the Finish button

Home Profile Teaching Links Contact Me My Work

Newsflash

Aoccdrnig to a rscheearch at an Elingsh uinervtisy it deosnt mttaer in waht oredr the ltteers in a wrod are the olny iprmoetnt

tihng is taht frist and lsat ltteer is at the rghit pclae The rset can be a toatl mses and you can sitll raed it wouthit porbelm Tihs is

bcuseae we do not raed ervey lteter by itslef but the wrod as a wlohe

search

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5058

Figure 91

2 Create UI class

- Right click on the project name in the Project pane

on the popup menu select New --gt JFrame Form

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5158

Figure 92

- In the New JFrame Form dialog specify the Class Name and the Package name

eg Class Name multiplierUI Package mymultiplier

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5258

Figure 93

After clicking on the Finish button we should get something like the following

Figure 94

Last Updated ( Tuesday 18 September 2007 )

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5358

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5458

9th Step (Cont)

Written by Mohd Khalid Kasmin

Tuesday 11 September 2007

Step by Step into Java Programming

for University Physics Student

By

Mohd Khalid Kasmin

B Building user interface (UI) - putting in the components

Now that we have a proper blank form we can start filling the form with the suitable graphical components For introduction to

GUI programming an appropriate application to build (for a physics student) is a simple solver So our first exercise is to build an

application that takes two numbers and multiply them and display the result

With GUI we usually give input values to an application via input boxes that are labelled When all inputs are typed in properlywe click on a button to ask the application to execute the job Hence in this exercise well do just that The target now is to

construct a user interface that looks like Figure 95 below

Figure 95

Home Profile Teaching Links Contact Me My Work

Newsflash

Joomla 10 - Experience the Freedom It has never been easier to create your own dynamic site Manage all your content from

the best CMS admin interface

search

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5558

[ Back ]

1 Problemapplication description box

- In the palette pane click on Swing component named JTextPane (Note click just once) Move the mouse pointer on top of

your JFrame (Note just move not drag) When you see a little white box with orange border position it somewhere near the

top part of your JFrame and click once The JTextPane is then attached on the JFrame Addjust its size with the help of the

sizing hook around the perimeter of the JTextPane

- Edit the text property of the JTextPane with the description of your application

2 Input boxes

- In the palette pane click on Swing component named JTextField Next move the mouse pointer on top of your JFrame

When you see a white box with the word jTextField1 position it to the left and under the JTextPane1

- Click on Swing component JTextField in the palette pane again Move the mouse pointer on top of the JFrame you should

get a second JTextField named jTextField2 Position it to the right of jTextField1

Last Updated ( Tuesday 11 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5658

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5758

multiplication if a b and c were first declared as double or float or int variables So the code of the calculation should look like the

following

double a b c

a =

b =

c = a b

Clearly variables a and b need to be assigned with values of typed double Now looking back at inputboxes A and B we made

those boxes using the JTextField component meaning that any data or value written in a JTextField is of typed Text or String even

if the value displayed in the box is ldquo125rdquo But f irst here there is this task of getting to that Text (or String) values ldquo125rdquo and ldquo72rdquo

In Java we can get to a text in JTextField component using the getText() method So to retrieve values in jTextField1 and

jTextField2 we can use

jTextField1getText()

and

jTextField2getText()

But those values are still in text mode and we need another method to convert the data mode f rom text to double For that we can

use the method DoubleparseDouble() Thus the whole process of retrieve text -gt convert text to double -gt assign to double

variable can be done using

a = DoubleparseDouble( jTextField1getText() )

and

b = DoubleparseDouble( jTextField2getText() )

Hence the code for performing the multiplication becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

Up to this point we have the variable c as the result of the multiplication of a and b So the next thing to do is to display this

result on the JLabel that we have already prepared before

A x B = _____

The text A x B = ______ was made using JLabel component and its variable name is jLabel3 Since the variable c is of type

double and text property of JLabel is of type String we need to convert it first to String data type The method to convert a double

to String is DoubletoString() So we can write the code as DoubletoString(c) After getting the text we can now use it to be the

text displayed on jLabel3 To do that we use the method setText() Hence the appropriate code for displaying the result is

jLabel3setText( A x B = + DoubletoString(c) )

And the full code for our calculation becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

jLabel3setText( A x B = + DoubletoString(c) )

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5858

[ Back ]

Last Updated ( Tuesday 18 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

Page 10: Computional Physics

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1058

983107983119983117983120983125983124983105983124983113983119983118983105983116

983120983112983129983123983113983107983123 983080983123983123983120983090983089983090983090983081

983109983148983141983139983156983154983151983155983156983137983156983145983139 983152983151983156983141983150983156983145983137983148983138983161

983105983138983140983086 983115983144983137983149983145983149 983113983155983149983137983145983148

983117983151983144983140 983115983144983137983148983145983140 983115983137983155983149983145983150

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1158

1 Calculation of electrostatic potential for a group of point charges

You will learn about

- mesh method

a) using a for() loop inside another for() loop

to generate ordered pair (xy) points in a mesh on x-y plane

b) calculate physical value in this case V(xy) forevery point (xy) generated

- saving calculated values into a file

a) making a sequential text file

b) saving in a format that gnuplot can understand

for making a surface plot

- use gnuplot to visualize potential surface

a) making a surface plot from data points in a text file

b) making contour plot to visualize equipotential lines

c) changing the z-axis range to zoom in or zoom out on the surface

d) labelling the plot

e) save as an image the potential surface amp the contour plotf) save as an eps file the potential surface amp the contour plot

- including an eps file into a document

2 Calculation of electrostatic field for a group of point charges

You will learn again

- using mesh method to generate ordered pair (xy) points

in a mesh on x-y plane

You will also learn how to

- calculate vector field values in this case E(xy) for every

generated (xy) mesh point

- save the calculated vector points into a text file

in a format understood by gnuplot

- make a vector field plot using gnuplot

Source code

import static javalangMath

public class estatpot01b

public static void main(String[] args)

double[] q = new double[100]

double[] xq = new double[100]

double[] yq = new double[100]

double V r rx ry k x y

int i n

q[1] = 20e-6 xq[1] = -10 yq[1] = 10

q[2] =-30e-6 xq[2] = 30 yq[2] =-20

q[3] = 30e-6 xq[3] = -20 yq[3] = 40

q[4] =-50e-6 xq[4] = 20 yq[4] = 50

k = 9e9n = 4

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1258

for (x=-5 xlt=5 x = x+02)

for (y=5 ygt=-5 y = y-02)

V = 00

for(i=1 ilt=n i++)

rx = x - xq[i]

ry = y - yq[i]r = sqrt(rxrx+ryry)

if (rlt1e-6)

if (q[i]lt0) V = -10 else V = 10

break

V = V + q[i]r

V = kV

Systemoutprintf(f f fn x y V)

Systemoutprintf(n)

How to do surface plot using Gnuplot

To do a surface plotgnuplotgt splot epotdat with lineTo zoom in and zoom out z axisgnuplotgt set zrange [-2000020000]To convert the graph into imagegnuplotgt set term pngTerminal type set to pngOptions are small color picsize 640 480 Then typegnuplotgt set output epotpngAgain do splot as followgnuplotgt splot epotdat with lineNow the graph is saved into a file in png format named as epotpngTo go back into terminal ouputgnuplotgt set term x11To set to countor plotgnuplotgt set pm3dTo remove countor meshgnuplotgt unset surf

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1358

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1458

983107983119983117983120983125983124983105983124983113983119983118983105983116 983120983112983129983123983113983107983123

983080983123983123983120983090983089983090983090983081

983109983157983148983137983154 983149983141983156983144983151983140

983138983161

983105983138983140983086 983115983144983137983149983145983149 983113983155983149983137983145983148

983117983151983144983140 983115983144983137983148983145983140 983115983137983155983149983145983150

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1558

Free fall body problem

Consider an object thrown upward under gravitational force ( f=mg) with an initial velocity v0The object goes up and then stop at maximum y position ymax before it start to fall downback to the original position at y0 and then to the ground level (See figure)

ymdt

yd m

dt

dvmmaF ampamp====

2

2

t

Y

v1 (t1y1)

v2 (t2y2)

v4 (t4y4)

v5 (t5y5)

vn (tnyn)

v3 (t3y3)

v0 (t0y0)

∆t∆t ∆t ∆t

y1

y0

∆t ∆t

∆yv0

a = g

y0

t0

t0 t1 t2 t3 t4 t5 tn

ymax

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1658

Exact theoretical solution

( ) 2

21

00 at t v yt y ++=

The y projection of the exact solution can be plotted using the following java source code

Java class for solving free fall object under gravity (g = 98 mss)Exact solution y = y0 + v0t + 12at^2

public class freefallsol

public static void main(String args[])double av0yy0deltt

a = -98y0 = 10v0 = 10delt = 00001for(t=0 tlt=05 t=t+delt)

y = y0 + v0t + 05attSystemoutprintf(84f 84fnty)

Complile freefallsoljava to get the plot of the exact solution and then run and save theoutput into a file name as follows

[khamimsetpar65 ~]$ java freefallsol gtffsoldat

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1758

Euler Method

There is another way to see the projection of y position of the object using a numericalmethod called ldquoEuler methodrdquo See the following derivation

t avt v y

t

va

t

yv

∆=∆∆=∆

∆=

∆=

t v y y y y

t avvvv

t v y y y y

t avvvv

t v y y y y

t avvvv

nnnn

nnn

∆+=∆+=

∆+=∆+=

∆+=∆+=

∆+=∆+=

∆+=∆+=

∆+=∆+=

minus

minusminus

1

11

2112

112

1001

001

M

if ∆∆∆∆t rarrrarrrarrrarr 0 then y will close to the exact solution ( ( ) 2

21

00 at t v yt y ++= )

The algorithm 1 Declare the classmethod to be used

2 Declare and assign all variables to be used av0v1yy0y1deltt and j 3 Initialize value to all respective variables a = -98 y0 = 10 v0 = 10

4 Select appropriate value for ∆t

∆t = 00055 Start the loop to calculate the velocity and position for t=0

6 Calculate velocity v1 = v0 + ∆v = v0 + a ∆t

7 Calculate position y1 = y0 + ∆y = y0 + v1 ∆t8 Print out values of t and y1 9 Swap v0 = v1 and y0 = y1

10 Repeat from step 5 and increase the value of t by ∆t (t =t + ∆t) until stoppingcondition is satisfied at t=05

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1858

The java code

Java class for solving free fall object under gravity (g = 98 mss)by numerical method called ldquoEulers Methodrdquo

public class freefalleuler

public static void main(String args[])double av0v1yy0y1delttint j

a = -98y0 = 10v0 = 10delt = 0005

for(t=0 tlt=05 t =t + delt)v1 = v0 + adelty1 = y0 + v1deltSystemoutprintf(84f 84fnty1)v0 = v1y0 = y1

Complile freefalleulerjava for different delt ie 00005 0005 005 and then run and savethe output into a file name as follows

[khamimsetpar65 ~]$ java freefalleuler gtffeuler00005dat[khamimsetpar65 ~]$ java freefalleuler gtffeuler0005dat[khamimsetpar65 ~]$ java freefalleuler gtffeuler005dat

Plotting the results using gnuplot

Using gnuplot plot all the graphs using the following command and compare the results

gnuplotgt plot ffeuler00005dat using 12 title euler delt=00005 with linegt ffeuler0005dat using 12 title euler delt=0005 with linegt ffeuler005dat using 12 title euler deltt=005 with linegt ffsoldat using 12 title exact solution with line

In MS windows

gnuplotgt plot ffeuler00005dat using 12 title euler delt=00005 with lineffeuler0005dat using 12 title euler delt=0005 with lineffeuler005dat using 12 titleeuler delt=005 with line ffsoldat using 12 title exact solution with line

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1958

Gnuplot

The smaller the delt the closer the curve to the exact solution

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2058

983107983119983117983120983125983124983105983124983113983119983118983105983116 983120983112983129983123983113983107983123

983080983123983123983120983090983089983090983090983081

983113983149983152983154983151983158983141983140 983109983157983148983137983154 983149983141983156983144983151983140

983138983161

983105983138983140983086 983115983144983137983149983145983149 983113983155983149983137983145983148

983117983151983144983140 983115983144983137983148983145983140 983115983137983155983149983145983150

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2158

Improved Euler method

Comparison between analytical solution of F=ma with the Euler method shows that although the

method is able to find approximate solution the error is quite significant if computational step is

reduced The error is also cumulative ie as the iteration of the calculation increases the error also gets

bigger hence value furthest away from initial condition has the largest error

The Euler method gives us a way to approximate positions of object with respect to time but requires

an order of magnitude of computational iterations for each order of magnitude of accuracy For large

number of objects this method will certainly consume a great deal of computation time Hence

students are introduced to Initial Half-Step Method an Euler method with a much improved

computational effeciency

Source code

Study of motion of an object with mass m

experiencing a constant force F using

Improved Euler Method (Initial half step method)

In X and Y dimension

import static javalangMath

public class fma04

public static void main(String args[])

double m fx fy x y vo vx vy ax ay t delt

double pi th theta ymax tmax xmax

pi = 40atan(10)

m = 01 in kg

fy = -02 in N (pulling down force)

fx = 00 in N (retarding force)

ay = fym

ax = fxm

vo = 500 in ms

theta = 300 in degrees

th = theta(pi1800) degrees change to radian

vx = vo cos(th) in ms vy = vo sin(th) in ms

y = 12 in m

x = 00 in m

ymax=y initialize var ymax with initial value of y

xmax=00

tmax=00

delt = 001 in s

for(t=00 tlt=300 t=t+delt)

System outprintf(f f fntxy)

if (t==00)vx = vx + 05axdelt

vy = vy + 05aydelt

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2258

else

vx = vx + axdelt

vy = vy + aydelt

x = x + vxdelt

y = y + vydelt

if (ygtymax)

ymax=y

xmax=xtmax=t

if (ylt=00) break

System outprintf( Value of Y maximum is f happened when t = f

nymaxtmax)

System outprintf( at x = fnxmax)

System outprintf( The object reach the ground at x=f and t=fn xt)

gnuplot result

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2358

983107983119983117983120983125983124983105983124983113983119983118983105983116

983120983112983129983123983113983107983123 983080983123983123983120983090983089983090983090983081

983122983141983155983156983151983154983145983150983143 983110983151983154983139983141

983138983161

983105983138983140983086 983115983144983137983149983145983149 983113983155983149983137983145983148

983117983151983144983140 983115983144983137983148983145983140 983115983137983155983149983145983150

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2458

Damped Harmonic Oscillation

Object m is on a flat surface attached to a spring as in figure above The object is pulled

to position A and released so that it oscillates about x =0 Assuming that the force F=ma= -bv-kx where x is position of the object v is the velocity k the spring constant andb is a constant associated with friction with the flat surface

The algorithm

1 Declare the classmethod to be used2 Declare and assign all variables to be used

x t delt k b A m F a v v0 and x03 Declare and assign constants to the respective variables

k = 02 b = 01 A = 01 m = 054 Select appropriate value for ∆t 5 Initialized initial values xo=A vo=00

6 set x=xo v=vo and t = 07 calculate the value of force F = -kx-bv 8 calculate the value of acceleration a = Fm

9 calculate velocity v = vo + ∆v = vo + a ∆t

10 calculate position x = xo + ∆x = xo + v ∆t

11 increment value of t by ∆t 12 print out values of t and x

13 swap the value of xo=x vo=v 14 repeat from step 4 until stopping condition is satisfied (tlt=600)

The java code

public class simplehar01

public static void main(String args[])double x t deltdouble k b A m F a v v0 x0

k = 02b = 01A = 0 1

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2558

m = 05delt = 0001v0 = 00x0 = Ax = x0 v = v0

t = 00while(tlt=600)

F = -kx-bva = Fmv = v0 + adeltx = x0 + vdeltt = t + deltSystemoutprintf(f fntx)x0 = xv0 = v

gnuplot result

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2658

983107983119983117983120983125983124983105983124983113983119983118983105983116 983120983112983129983123983113983107983123

(983123983123983120983090983089983090983090983081

983106983157983145983148983140983145983150983143 983137 983152983144983161983155983145983139983155 983155983151983148983158983141983154

983157983155983145983150983143 983118983141983156983138983141983137983150983155

983138983161

983105983138983140983086 983115983144983137983149983145983149 983113983155983149983137983145983148

983117983151983144983140 983115983144983137983148983145983140 983115983137983155983149983145983150

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2758

How to create a user interface (UI) using Netbeans

Based on the ldquoGravityAttract07ajavardquo source code (shown below) build a user interface usingNetbeans The design of the UI could be look like this (Figure1)

Figure 1

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2858

ldquoGravityAttract07ajavardquo source code

Java class for calculating the resultant force on any object(ie either on m1m2 m3 m4 or m5)

import static javalangMathimport javaiopublic class GravityAttract07a

public static void main(String args[])

double[] m = new double[6] mass of objectdouble[] x = new double[6] x-coor of objectdouble[] y = new double[6] y-coor of objectdouble f resultant forcedouble fxfy x amp y component of forcedouble rx ry rp co G dirfangdirf

int j

G = 667e-11m[1]=25 x[1]=12 y[1]=25m[2]=15 x[2]=15 y[2]=15m[3]=75 x[3]=02 y[3]=55m[4]=92 x[4]=52 y[4]=25m[5]=66 x[5]=42 y[5]=45BufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin))String mass=Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))try mass=dataInreadLine()

catch(IOException e)Systemoutprintln(Not a valid input value)int mi = IntegerparseInt(mass)fx = 00fy = 00

for(j=1 jlt=5 j++)

if (j=mi)rx = x[j]-x[mi]ry = y[j]-y[mi]rp = pow((rxrx+ryry)15) (rx^2 + ry^2) raised to the power of 32co = Gm[mi]m[j]

fx = fx + corxrpfy = fy + coryrp

f = sqrt(fxfx + fyfy)dirf = toDegrees(atan(fyfx))Systemoutprintf(The force on m[+ mi +] due to the other masses is eN fdegnfdirf)

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2958

This example will not show you step by step details procedure since it already explained inthe class and in the e-learning Please refer to the e-learning about how to create a project how tocreate JFrame Form and as well how to create JButton JTextField JLabel JTextPane and so on

First create a new project and give a name eg ldquogravityrdquo and then create JFrame Form andthe following JButton JTextField JLabel JTextPane etc by using ldquoswing paletterdquo as shown in Figure2

Figure 2

To insert a picture into JLabel2 click on the JLabel2 go to properties and select ldquoiconrdquo Onthe icon editor choose file and then select file to locate the directory of your graphicpicture file(Figure 3) Select the file and click OK when finished Make sure you have already created your ownpicture file and save it as jpg png bmp etc format eg ldquogravityjpgrdquo Use any graphic package todraw your diagrampicture or use drawing tools available in Open Office or Microsoft Word

JTextPane1JLabel1

JLabel2

JLabel3JLabel4

JLabel5

JLabel6 JLabel7

JLabel8

JTextField2 JTextField3 JTextField4

JTextF

JButton1JButton2

JButton3

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3058

Figure 3

To write documentation or question eg in JTextPane1 click on the JTextPane1 go toproperties and select ldquotextrdquo Type the documentation in the text editor as shown in Figure 4 Click OKwhen finished

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3158

To do formatting on the text such as superscript and subscript eg in jLabel3 click on the

JLabel3 go to properties and select ldquotextrdquo Type the documentationmessage on the text editor asshown in Figure 5 as follows

eg

lthtmlgtOn which particle (mltsubgt1ltsubgtmltsubgt5ltsubgt) you want to calculate theresultant force

Use sup instead if you want a superscriptThe result will look like this

Click OK when finished

Figure 5

To do the calculation you need to put the coding similar like ldquoGravityAttract07ajavardquo and put itinto the ldquocalculaterdquo button (this is ldquoJButton1rdquo) You can copy ldquoGravityAttract07a javardquo source code

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3258

paste it and then do a few alterations First click on the ldquocalculaterdquo button right click choose Eventsand then choose Action then actionPerformed (Figure 6) It will bring you to where you should insertyour coding (Figure 7) You can write your own coding or copypaste any coding from somewhereelse (in this case from ldquoGravityAttract07ajavardquo)

Figure 6

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3358

Figure 7

Copy ldquoGravityAttract07ajavardquo source code and paste to the place after ldquo TODO add youhandling code hererdquo see Figure 7 To see which part has been modified the coding in Figure 7 hasbeen color coded for better understanding as follows

private void jButton1ActionPerformed(javaawteventActionEvent evt) TODO add your handling code here

double[] m = new double[6] mass of objectdouble[] x = new double[6] x-coor of objectdouble[] y = new double[6] y-coor of objectdouble f resultant forcedouble fxfy x amp y component of forcedouble rx ry rp co G dirfangdirfDecimalFormat dff dfangdirfint j mi

G = 667e-11m[1]=25 x[1]=12 y[1]=25m[2]=15 x[2]=15 y[2]=15m[3]=75 x[3]=02 y[3]=55m[4]=92 x[4]=52 y[4]=25m[5]=66 x[5]=42 y[5]=45

BufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin)) String mass= Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3458

catch(IOException e)Systemoutprintln(Not a valid input value) int mi = IntegerparseInt(mass)mi = IntegerparseInt(jTextField1getText())fx = 00fy = 00for(j=1 jlt=5 j++)

if (j=mi)rx = x[j]-x[mi]ry = y[j]-y[mi]rp = Mathpow((rxrx+ryry)15) (rx^2 + ry^2) raised to the power of 32co = Gm[mi]m[j]fx = fx + corxrpfy = fy + coryrp

f = Mathsqrt(fxfx + fyfy)

dirf = Mathatan(fyfx)angdirf = (dirf(22070))1800 Systemoutprintf(ldquoThe resultant force on particle [ldquo+ mi +rdquo] due to the other masses is

14e N 22f degnrdquofangdirf)dff = new javatextDecimalFormat(ldquoE00rdquo)dfangdirf = new DecimalFormat(ldquo000rdquo)

jTextField2setText(ldquordquo + mi) jTextField3setText(ldquordquo + dffformat(f)) jTextField4setText(ldquordquo + dfangdirfformat(angdirf))

Note

Brown = automatically generatedBlue = the original code from ldquoGravityAttract07ajavardquoGreen = coding from ldquoGravityAttract07ajavardquo which were being disabled due to

incompatibilities with NetbeansRed = a new inserted coding a few of them is to rephrasesubstitute the green one

Javascripts command line useBufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin))String mass=Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))try mass=dataInreadLine()catch(IOException e)Systemoutprintln(Not a valid input value)

int mi = IntegerparseInt(mass)

to read any input from the keyboard typed by the user while Netbeans uses egldquoIntegerparseInt(jTextField1getText())rdquo etc Javascripts command line uses ldquoSystemoutprintfrdquo to format the printed output whileNetbeans uses more complicated way to do the same job (see below)

Javascript command line

Systemoutprintf(The resultant force on particle [+ mi +] due to the other masses is 14e N22f degnfangdirf)

Netbeansdff = new javatextDecimalFormat(ldquo0E00rdquo)df di f D i lF t(ldquo0 00rdquo)

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3558

jTextField2setText(ldquordquo + mi) jTextField3setText(ldquordquo + dffformat(f)) jTextField4setText(ldquordquo + dfangdirfformat(angdirf))

ldquodffrdquo and ldquodfangdirfrdquo are new variables that should be declared and added to format thenumbers into decimal format ie ldquo0E00rdquo to get a scientific format eg ldquo20019E-10rdquo and ldquo000rdquoto get a decimal number eg ldquo-1570rdquo

To use DecimalFormat you have to put ldquoimport javatextDecimalFormatrdquo in the top of thecoding as shown in Figure 8

Figure 8

To reset the calculation you need to put coding into the ldquoresetrdquo button (this is ldquoJButton2rdquo)First click on the ldquoresetrdquo button right click choose Events and then choose Action thenactionPerformed (similar like in Figure 6) It will bring you to where you should insert your coding(Figure 9) Write the following coding (Figure 9)

Figure 9

To exit the program you need to put coding into the ldquoexitrdquo button (this is ldquoJButton3rdquo) Firstclick on the ldquoexitrdquo button right click choose Events and then choose Action then actionPerformed(similar like in Figure 6) It will bring you to where you should insert your coding (Figure 10) Writethe following coding (Figure 10)

Figure 10

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3658

Once you finished now you can run and see the result If everything is fine then the output wilbe look like this (Figure 11)

Figure 11

Remember when you compile and run ldquoGravityAttract07ajavardquo using Javascripts commandline once you compile and run the result was looked like this

On which particle you want to calculate the resulting force 2The resultant force on particle [2] due to the other masses is 30673e-10 N 8837 deg

SEE THE DIFFERENCES AT THE USER INTERFACE

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3758

8th Step

Written by Mohd Khalid Kasmin

Monday 27 August 2007

Programming using command line interface (CLI) has its advantages but todays computer users are so used to graphical user

interface (GUI) that we just can no longer be contented with writing application that do not use the GUI Hence starting from this 8thstep we are going to embark on learning how to make java application that utilises the the GUI

It is possible to write a GUI application with the text based tools that we have been using for these past 7 steps but the job is so

messy that most of you will definitely be turnoff by it So to make life slightly easier well use an integrated development

environment (IDE) known as NetBeans specifically NetBeans 551

0 First ssh to the our server Next once logged in just type nice -n15 netbeans amp on the terminal If you had sshed with the -Y

switch after a few tens of seconds NetBeans should emerge on your display screen

One important point to note Since our server can readily handle up to 100 users (maybe even

more) doing work in CLI mode this machine will certainly be taxed to its maximum capability once

we start using graphical based application NetBeans 551 uses a geat deal of computer memory

and with the 4Gbytes RAM server it can only accomodate up to 25 users concurrently using

NetBeans if the users use the application wisely What is meant by wisely here is that (a) when

starting the NetBeans application please use it together with the nice -n15 command (b) in using

the NetBeans please make sure you only have one project open at a time and (c) please close your

project everytime before quitting the NetBeans

Home Profile Teaching Links Contact Me My Work

Newsflash

Yesterday all servers in the US went out on strike in a bid to get more RAM and better CPUs A spokes person said that the

need for better RAM was due to some fool increasing the front-side bus speed In future busses will be told to slow down in

residential motherboards

search

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3858

1

2

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3958

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4058

[ Back ]

5

Last Updated ( Wednesday 31 October 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4158

8th Step (Cont)

Written by Mohd Khalid Kasmin

Monday 27 August 2007

Up to this point we have define the name of our project Next we construct a class and to follow what we already know we name the class the sam

project name latihan01

6

7

Home Profile Teaching Links Contact Me My Work

Newsflash

Joomla 10 - Experience the Freedom It has never been easier to create your own dynamic site Manage all your content from

the best CMS admin interface

search

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4258

8

9

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4358

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4458

8th Step (Cont still)

Written by Mohd Khalid Kasmin

Monday 27 August 2007

In item (9) we see that Netbeans does not provide us with the main method So to follow what we already know we now put in the

main method

10

Home Profile Teaching Links Contact Me My Work

Newsflash

Aoccdrnig to a rscheearch at an Elingsh uinervtisy it deosnt mttaer in waht oredr the ltteers in a wrod are the olny iprmoetnt

tihng is taht frist and lsat ltteer is at the rghit pclae The rset can be a toatl mses and you can sitll raed it wouthit porbelm Tihs is

bcuseae we do not raed ervey lteter by itslef but the wrod as a wlohe

search

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4558

11

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4658

12

13

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4758

14

15

Last Updated ( Monday 27 August 2007 )

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4858

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4958

9th Step

Thursday 06 September 2007

Step by Step into Java Programming

for

University Physics Student

By

Mohd Khalid Kasmin

A Building graphical user interface - preparing the JFrame Form

At this stage of our programming experience with Java and NetBeans please follow strictly the steps outline in this article First and

make a new GUI project make sure there is no other project that already been loaded by Netbeans Please close any project displayed

1 Create new project

- On the main menu click on File --gt New Project (note Close first any previous project)

- Specify project name amp project location - Untick Create Main Class item

- Click on the Finish button

Home Profile Teaching Links Contact Me My Work

Newsflash

Aoccdrnig to a rscheearch at an Elingsh uinervtisy it deosnt mttaer in waht oredr the ltteers in a wrod are the olny iprmoetnt

tihng is taht frist and lsat ltteer is at the rghit pclae The rset can be a toatl mses and you can sitll raed it wouthit porbelm Tihs is

bcuseae we do not raed ervey lteter by itslef but the wrod as a wlohe

search

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5058

Figure 91

2 Create UI class

- Right click on the project name in the Project pane

on the popup menu select New --gt JFrame Form

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5158

Figure 92

- In the New JFrame Form dialog specify the Class Name and the Package name

eg Class Name multiplierUI Package mymultiplier

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5258

Figure 93

After clicking on the Finish button we should get something like the following

Figure 94

Last Updated ( Tuesday 18 September 2007 )

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5358

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5458

9th Step (Cont)

Written by Mohd Khalid Kasmin

Tuesday 11 September 2007

Step by Step into Java Programming

for University Physics Student

By

Mohd Khalid Kasmin

B Building user interface (UI) - putting in the components

Now that we have a proper blank form we can start filling the form with the suitable graphical components For introduction to

GUI programming an appropriate application to build (for a physics student) is a simple solver So our first exercise is to build an

application that takes two numbers and multiply them and display the result

With GUI we usually give input values to an application via input boxes that are labelled When all inputs are typed in properlywe click on a button to ask the application to execute the job Hence in this exercise well do just that The target now is to

construct a user interface that looks like Figure 95 below

Figure 95

Home Profile Teaching Links Contact Me My Work

Newsflash

Joomla 10 - Experience the Freedom It has never been easier to create your own dynamic site Manage all your content from

the best CMS admin interface

search

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5558

[ Back ]

1 Problemapplication description box

- In the palette pane click on Swing component named JTextPane (Note click just once) Move the mouse pointer on top of

your JFrame (Note just move not drag) When you see a little white box with orange border position it somewhere near the

top part of your JFrame and click once The JTextPane is then attached on the JFrame Addjust its size with the help of the

sizing hook around the perimeter of the JTextPane

- Edit the text property of the JTextPane with the description of your application

2 Input boxes

- In the palette pane click on Swing component named JTextField Next move the mouse pointer on top of your JFrame

When you see a white box with the word jTextField1 position it to the left and under the JTextPane1

- Click on Swing component JTextField in the palette pane again Move the mouse pointer on top of the JFrame you should

get a second JTextField named jTextField2 Position it to the right of jTextField1

Last Updated ( Tuesday 11 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5658

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5758

multiplication if a b and c were first declared as double or float or int variables So the code of the calculation should look like the

following

double a b c

a =

b =

c = a b

Clearly variables a and b need to be assigned with values of typed double Now looking back at inputboxes A and B we made

those boxes using the JTextField component meaning that any data or value written in a JTextField is of typed Text or String even

if the value displayed in the box is ldquo125rdquo But f irst here there is this task of getting to that Text (or String) values ldquo125rdquo and ldquo72rdquo

In Java we can get to a text in JTextField component using the getText() method So to retrieve values in jTextField1 and

jTextField2 we can use

jTextField1getText()

and

jTextField2getText()

But those values are still in text mode and we need another method to convert the data mode f rom text to double For that we can

use the method DoubleparseDouble() Thus the whole process of retrieve text -gt convert text to double -gt assign to double

variable can be done using

a = DoubleparseDouble( jTextField1getText() )

and

b = DoubleparseDouble( jTextField2getText() )

Hence the code for performing the multiplication becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

Up to this point we have the variable c as the result of the multiplication of a and b So the next thing to do is to display this

result on the JLabel that we have already prepared before

A x B = _____

The text A x B = ______ was made using JLabel component and its variable name is jLabel3 Since the variable c is of type

double and text property of JLabel is of type String we need to convert it first to String data type The method to convert a double

to String is DoubletoString() So we can write the code as DoubletoString(c) After getting the text we can now use it to be the

text displayed on jLabel3 To do that we use the method setText() Hence the appropriate code for displaying the result is

jLabel3setText( A x B = + DoubletoString(c) )

And the full code for our calculation becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

jLabel3setText( A x B = + DoubletoString(c) )

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5858

[ Back ]

Last Updated ( Tuesday 18 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

Page 11: Computional Physics

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1158

1 Calculation of electrostatic potential for a group of point charges

You will learn about

- mesh method

a) using a for() loop inside another for() loop

to generate ordered pair (xy) points in a mesh on x-y plane

b) calculate physical value in this case V(xy) forevery point (xy) generated

- saving calculated values into a file

a) making a sequential text file

b) saving in a format that gnuplot can understand

for making a surface plot

- use gnuplot to visualize potential surface

a) making a surface plot from data points in a text file

b) making contour plot to visualize equipotential lines

c) changing the z-axis range to zoom in or zoom out on the surface

d) labelling the plot

e) save as an image the potential surface amp the contour plotf) save as an eps file the potential surface amp the contour plot

- including an eps file into a document

2 Calculation of electrostatic field for a group of point charges

You will learn again

- using mesh method to generate ordered pair (xy) points

in a mesh on x-y plane

You will also learn how to

- calculate vector field values in this case E(xy) for every

generated (xy) mesh point

- save the calculated vector points into a text file

in a format understood by gnuplot

- make a vector field plot using gnuplot

Source code

import static javalangMath

public class estatpot01b

public static void main(String[] args)

double[] q = new double[100]

double[] xq = new double[100]

double[] yq = new double[100]

double V r rx ry k x y

int i n

q[1] = 20e-6 xq[1] = -10 yq[1] = 10

q[2] =-30e-6 xq[2] = 30 yq[2] =-20

q[3] = 30e-6 xq[3] = -20 yq[3] = 40

q[4] =-50e-6 xq[4] = 20 yq[4] = 50

k = 9e9n = 4

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1258

for (x=-5 xlt=5 x = x+02)

for (y=5 ygt=-5 y = y-02)

V = 00

for(i=1 ilt=n i++)

rx = x - xq[i]

ry = y - yq[i]r = sqrt(rxrx+ryry)

if (rlt1e-6)

if (q[i]lt0) V = -10 else V = 10

break

V = V + q[i]r

V = kV

Systemoutprintf(f f fn x y V)

Systemoutprintf(n)

How to do surface plot using Gnuplot

To do a surface plotgnuplotgt splot epotdat with lineTo zoom in and zoom out z axisgnuplotgt set zrange [-2000020000]To convert the graph into imagegnuplotgt set term pngTerminal type set to pngOptions are small color picsize 640 480 Then typegnuplotgt set output epotpngAgain do splot as followgnuplotgt splot epotdat with lineNow the graph is saved into a file in png format named as epotpngTo go back into terminal ouputgnuplotgt set term x11To set to countor plotgnuplotgt set pm3dTo remove countor meshgnuplotgt unset surf

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1358

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1458

983107983119983117983120983125983124983105983124983113983119983118983105983116 983120983112983129983123983113983107983123

983080983123983123983120983090983089983090983090983081

983109983157983148983137983154 983149983141983156983144983151983140

983138983161

983105983138983140983086 983115983144983137983149983145983149 983113983155983149983137983145983148

983117983151983144983140 983115983144983137983148983145983140 983115983137983155983149983145983150

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1558

Free fall body problem

Consider an object thrown upward under gravitational force ( f=mg) with an initial velocity v0The object goes up and then stop at maximum y position ymax before it start to fall downback to the original position at y0 and then to the ground level (See figure)

ymdt

yd m

dt

dvmmaF ampamp====

2

2

t

Y

v1 (t1y1)

v2 (t2y2)

v4 (t4y4)

v5 (t5y5)

vn (tnyn)

v3 (t3y3)

v0 (t0y0)

∆t∆t ∆t ∆t

y1

y0

∆t ∆t

∆yv0

a = g

y0

t0

t0 t1 t2 t3 t4 t5 tn

ymax

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1658

Exact theoretical solution

( ) 2

21

00 at t v yt y ++=

The y projection of the exact solution can be plotted using the following java source code

Java class for solving free fall object under gravity (g = 98 mss)Exact solution y = y0 + v0t + 12at^2

public class freefallsol

public static void main(String args[])double av0yy0deltt

a = -98y0 = 10v0 = 10delt = 00001for(t=0 tlt=05 t=t+delt)

y = y0 + v0t + 05attSystemoutprintf(84f 84fnty)

Complile freefallsoljava to get the plot of the exact solution and then run and save theoutput into a file name as follows

[khamimsetpar65 ~]$ java freefallsol gtffsoldat

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1758

Euler Method

There is another way to see the projection of y position of the object using a numericalmethod called ldquoEuler methodrdquo See the following derivation

t avt v y

t

va

t

yv

∆=∆∆=∆

∆=

∆=

t v y y y y

t avvvv

t v y y y y

t avvvv

t v y y y y

t avvvv

nnnn

nnn

∆+=∆+=

∆+=∆+=

∆+=∆+=

∆+=∆+=

∆+=∆+=

∆+=∆+=

minus

minusminus

1

11

2112

112

1001

001

M

if ∆∆∆∆t rarrrarrrarrrarr 0 then y will close to the exact solution ( ( ) 2

21

00 at t v yt y ++= )

The algorithm 1 Declare the classmethod to be used

2 Declare and assign all variables to be used av0v1yy0y1deltt and j 3 Initialize value to all respective variables a = -98 y0 = 10 v0 = 10

4 Select appropriate value for ∆t

∆t = 00055 Start the loop to calculate the velocity and position for t=0

6 Calculate velocity v1 = v0 + ∆v = v0 + a ∆t

7 Calculate position y1 = y0 + ∆y = y0 + v1 ∆t8 Print out values of t and y1 9 Swap v0 = v1 and y0 = y1

10 Repeat from step 5 and increase the value of t by ∆t (t =t + ∆t) until stoppingcondition is satisfied at t=05

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1858

The java code

Java class for solving free fall object under gravity (g = 98 mss)by numerical method called ldquoEulers Methodrdquo

public class freefalleuler

public static void main(String args[])double av0v1yy0y1delttint j

a = -98y0 = 10v0 = 10delt = 0005

for(t=0 tlt=05 t =t + delt)v1 = v0 + adelty1 = y0 + v1deltSystemoutprintf(84f 84fnty1)v0 = v1y0 = y1

Complile freefalleulerjava for different delt ie 00005 0005 005 and then run and savethe output into a file name as follows

[khamimsetpar65 ~]$ java freefalleuler gtffeuler00005dat[khamimsetpar65 ~]$ java freefalleuler gtffeuler0005dat[khamimsetpar65 ~]$ java freefalleuler gtffeuler005dat

Plotting the results using gnuplot

Using gnuplot plot all the graphs using the following command and compare the results

gnuplotgt plot ffeuler00005dat using 12 title euler delt=00005 with linegt ffeuler0005dat using 12 title euler delt=0005 with linegt ffeuler005dat using 12 title euler deltt=005 with linegt ffsoldat using 12 title exact solution with line

In MS windows

gnuplotgt plot ffeuler00005dat using 12 title euler delt=00005 with lineffeuler0005dat using 12 title euler delt=0005 with lineffeuler005dat using 12 titleeuler delt=005 with line ffsoldat using 12 title exact solution with line

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1958

Gnuplot

The smaller the delt the closer the curve to the exact solution

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2058

983107983119983117983120983125983124983105983124983113983119983118983105983116 983120983112983129983123983113983107983123

983080983123983123983120983090983089983090983090983081

983113983149983152983154983151983158983141983140 983109983157983148983137983154 983149983141983156983144983151983140

983138983161

983105983138983140983086 983115983144983137983149983145983149 983113983155983149983137983145983148

983117983151983144983140 983115983144983137983148983145983140 983115983137983155983149983145983150

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2158

Improved Euler method

Comparison between analytical solution of F=ma with the Euler method shows that although the

method is able to find approximate solution the error is quite significant if computational step is

reduced The error is also cumulative ie as the iteration of the calculation increases the error also gets

bigger hence value furthest away from initial condition has the largest error

The Euler method gives us a way to approximate positions of object with respect to time but requires

an order of magnitude of computational iterations for each order of magnitude of accuracy For large

number of objects this method will certainly consume a great deal of computation time Hence

students are introduced to Initial Half-Step Method an Euler method with a much improved

computational effeciency

Source code

Study of motion of an object with mass m

experiencing a constant force F using

Improved Euler Method (Initial half step method)

In X and Y dimension

import static javalangMath

public class fma04

public static void main(String args[])

double m fx fy x y vo vx vy ax ay t delt

double pi th theta ymax tmax xmax

pi = 40atan(10)

m = 01 in kg

fy = -02 in N (pulling down force)

fx = 00 in N (retarding force)

ay = fym

ax = fxm

vo = 500 in ms

theta = 300 in degrees

th = theta(pi1800) degrees change to radian

vx = vo cos(th) in ms vy = vo sin(th) in ms

y = 12 in m

x = 00 in m

ymax=y initialize var ymax with initial value of y

xmax=00

tmax=00

delt = 001 in s

for(t=00 tlt=300 t=t+delt)

System outprintf(f f fntxy)

if (t==00)vx = vx + 05axdelt

vy = vy + 05aydelt

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2258

else

vx = vx + axdelt

vy = vy + aydelt

x = x + vxdelt

y = y + vydelt

if (ygtymax)

ymax=y

xmax=xtmax=t

if (ylt=00) break

System outprintf( Value of Y maximum is f happened when t = f

nymaxtmax)

System outprintf( at x = fnxmax)

System outprintf( The object reach the ground at x=f and t=fn xt)

gnuplot result

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2358

983107983119983117983120983125983124983105983124983113983119983118983105983116

983120983112983129983123983113983107983123 983080983123983123983120983090983089983090983090983081

983122983141983155983156983151983154983145983150983143 983110983151983154983139983141

983138983161

983105983138983140983086 983115983144983137983149983145983149 983113983155983149983137983145983148

983117983151983144983140 983115983144983137983148983145983140 983115983137983155983149983145983150

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2458

Damped Harmonic Oscillation

Object m is on a flat surface attached to a spring as in figure above The object is pulled

to position A and released so that it oscillates about x =0 Assuming that the force F=ma= -bv-kx where x is position of the object v is the velocity k the spring constant andb is a constant associated with friction with the flat surface

The algorithm

1 Declare the classmethod to be used2 Declare and assign all variables to be used

x t delt k b A m F a v v0 and x03 Declare and assign constants to the respective variables

k = 02 b = 01 A = 01 m = 054 Select appropriate value for ∆t 5 Initialized initial values xo=A vo=00

6 set x=xo v=vo and t = 07 calculate the value of force F = -kx-bv 8 calculate the value of acceleration a = Fm

9 calculate velocity v = vo + ∆v = vo + a ∆t

10 calculate position x = xo + ∆x = xo + v ∆t

11 increment value of t by ∆t 12 print out values of t and x

13 swap the value of xo=x vo=v 14 repeat from step 4 until stopping condition is satisfied (tlt=600)

The java code

public class simplehar01

public static void main(String args[])double x t deltdouble k b A m F a v v0 x0

k = 02b = 01A = 0 1

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2558

m = 05delt = 0001v0 = 00x0 = Ax = x0 v = v0

t = 00while(tlt=600)

F = -kx-bva = Fmv = v0 + adeltx = x0 + vdeltt = t + deltSystemoutprintf(f fntx)x0 = xv0 = v

gnuplot result

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2658

983107983119983117983120983125983124983105983124983113983119983118983105983116 983120983112983129983123983113983107983123

(983123983123983120983090983089983090983090983081

983106983157983145983148983140983145983150983143 983137 983152983144983161983155983145983139983155 983155983151983148983158983141983154

983157983155983145983150983143 983118983141983156983138983141983137983150983155

983138983161

983105983138983140983086 983115983144983137983149983145983149 983113983155983149983137983145983148

983117983151983144983140 983115983144983137983148983145983140 983115983137983155983149983145983150

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2758

How to create a user interface (UI) using Netbeans

Based on the ldquoGravityAttract07ajavardquo source code (shown below) build a user interface usingNetbeans The design of the UI could be look like this (Figure1)

Figure 1

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2858

ldquoGravityAttract07ajavardquo source code

Java class for calculating the resultant force on any object(ie either on m1m2 m3 m4 or m5)

import static javalangMathimport javaiopublic class GravityAttract07a

public static void main(String args[])

double[] m = new double[6] mass of objectdouble[] x = new double[6] x-coor of objectdouble[] y = new double[6] y-coor of objectdouble f resultant forcedouble fxfy x amp y component of forcedouble rx ry rp co G dirfangdirf

int j

G = 667e-11m[1]=25 x[1]=12 y[1]=25m[2]=15 x[2]=15 y[2]=15m[3]=75 x[3]=02 y[3]=55m[4]=92 x[4]=52 y[4]=25m[5]=66 x[5]=42 y[5]=45BufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin))String mass=Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))try mass=dataInreadLine()

catch(IOException e)Systemoutprintln(Not a valid input value)int mi = IntegerparseInt(mass)fx = 00fy = 00

for(j=1 jlt=5 j++)

if (j=mi)rx = x[j]-x[mi]ry = y[j]-y[mi]rp = pow((rxrx+ryry)15) (rx^2 + ry^2) raised to the power of 32co = Gm[mi]m[j]

fx = fx + corxrpfy = fy + coryrp

f = sqrt(fxfx + fyfy)dirf = toDegrees(atan(fyfx))Systemoutprintf(The force on m[+ mi +] due to the other masses is eN fdegnfdirf)

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2958

This example will not show you step by step details procedure since it already explained inthe class and in the e-learning Please refer to the e-learning about how to create a project how tocreate JFrame Form and as well how to create JButton JTextField JLabel JTextPane and so on

First create a new project and give a name eg ldquogravityrdquo and then create JFrame Form andthe following JButton JTextField JLabel JTextPane etc by using ldquoswing paletterdquo as shown in Figure2

Figure 2

To insert a picture into JLabel2 click on the JLabel2 go to properties and select ldquoiconrdquo Onthe icon editor choose file and then select file to locate the directory of your graphicpicture file(Figure 3) Select the file and click OK when finished Make sure you have already created your ownpicture file and save it as jpg png bmp etc format eg ldquogravityjpgrdquo Use any graphic package todraw your diagrampicture or use drawing tools available in Open Office or Microsoft Word

JTextPane1JLabel1

JLabel2

JLabel3JLabel4

JLabel5

JLabel6 JLabel7

JLabel8

JTextField2 JTextField3 JTextField4

JTextF

JButton1JButton2

JButton3

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3058

Figure 3

To write documentation or question eg in JTextPane1 click on the JTextPane1 go toproperties and select ldquotextrdquo Type the documentation in the text editor as shown in Figure 4 Click OKwhen finished

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3158

To do formatting on the text such as superscript and subscript eg in jLabel3 click on the

JLabel3 go to properties and select ldquotextrdquo Type the documentationmessage on the text editor asshown in Figure 5 as follows

eg

lthtmlgtOn which particle (mltsubgt1ltsubgtmltsubgt5ltsubgt) you want to calculate theresultant force

Use sup instead if you want a superscriptThe result will look like this

Click OK when finished

Figure 5

To do the calculation you need to put the coding similar like ldquoGravityAttract07ajavardquo and put itinto the ldquocalculaterdquo button (this is ldquoJButton1rdquo) You can copy ldquoGravityAttract07a javardquo source code

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3258

paste it and then do a few alterations First click on the ldquocalculaterdquo button right click choose Eventsand then choose Action then actionPerformed (Figure 6) It will bring you to where you should insertyour coding (Figure 7) You can write your own coding or copypaste any coding from somewhereelse (in this case from ldquoGravityAttract07ajavardquo)

Figure 6

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3358

Figure 7

Copy ldquoGravityAttract07ajavardquo source code and paste to the place after ldquo TODO add youhandling code hererdquo see Figure 7 To see which part has been modified the coding in Figure 7 hasbeen color coded for better understanding as follows

private void jButton1ActionPerformed(javaawteventActionEvent evt) TODO add your handling code here

double[] m = new double[6] mass of objectdouble[] x = new double[6] x-coor of objectdouble[] y = new double[6] y-coor of objectdouble f resultant forcedouble fxfy x amp y component of forcedouble rx ry rp co G dirfangdirfDecimalFormat dff dfangdirfint j mi

G = 667e-11m[1]=25 x[1]=12 y[1]=25m[2]=15 x[2]=15 y[2]=15m[3]=75 x[3]=02 y[3]=55m[4]=92 x[4]=52 y[4]=25m[5]=66 x[5]=42 y[5]=45

BufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin)) String mass= Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3458

catch(IOException e)Systemoutprintln(Not a valid input value) int mi = IntegerparseInt(mass)mi = IntegerparseInt(jTextField1getText())fx = 00fy = 00for(j=1 jlt=5 j++)

if (j=mi)rx = x[j]-x[mi]ry = y[j]-y[mi]rp = Mathpow((rxrx+ryry)15) (rx^2 + ry^2) raised to the power of 32co = Gm[mi]m[j]fx = fx + corxrpfy = fy + coryrp

f = Mathsqrt(fxfx + fyfy)

dirf = Mathatan(fyfx)angdirf = (dirf(22070))1800 Systemoutprintf(ldquoThe resultant force on particle [ldquo+ mi +rdquo] due to the other masses is

14e N 22f degnrdquofangdirf)dff = new javatextDecimalFormat(ldquoE00rdquo)dfangdirf = new DecimalFormat(ldquo000rdquo)

jTextField2setText(ldquordquo + mi) jTextField3setText(ldquordquo + dffformat(f)) jTextField4setText(ldquordquo + dfangdirfformat(angdirf))

Note

Brown = automatically generatedBlue = the original code from ldquoGravityAttract07ajavardquoGreen = coding from ldquoGravityAttract07ajavardquo which were being disabled due to

incompatibilities with NetbeansRed = a new inserted coding a few of them is to rephrasesubstitute the green one

Javascripts command line useBufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin))String mass=Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))try mass=dataInreadLine()catch(IOException e)Systemoutprintln(Not a valid input value)

int mi = IntegerparseInt(mass)

to read any input from the keyboard typed by the user while Netbeans uses egldquoIntegerparseInt(jTextField1getText())rdquo etc Javascripts command line uses ldquoSystemoutprintfrdquo to format the printed output whileNetbeans uses more complicated way to do the same job (see below)

Javascript command line

Systemoutprintf(The resultant force on particle [+ mi +] due to the other masses is 14e N22f degnfangdirf)

Netbeansdff = new javatextDecimalFormat(ldquo0E00rdquo)df di f D i lF t(ldquo0 00rdquo)

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3558

jTextField2setText(ldquordquo + mi) jTextField3setText(ldquordquo + dffformat(f)) jTextField4setText(ldquordquo + dfangdirfformat(angdirf))

ldquodffrdquo and ldquodfangdirfrdquo are new variables that should be declared and added to format thenumbers into decimal format ie ldquo0E00rdquo to get a scientific format eg ldquo20019E-10rdquo and ldquo000rdquoto get a decimal number eg ldquo-1570rdquo

To use DecimalFormat you have to put ldquoimport javatextDecimalFormatrdquo in the top of thecoding as shown in Figure 8

Figure 8

To reset the calculation you need to put coding into the ldquoresetrdquo button (this is ldquoJButton2rdquo)First click on the ldquoresetrdquo button right click choose Events and then choose Action thenactionPerformed (similar like in Figure 6) It will bring you to where you should insert your coding(Figure 9) Write the following coding (Figure 9)

Figure 9

To exit the program you need to put coding into the ldquoexitrdquo button (this is ldquoJButton3rdquo) Firstclick on the ldquoexitrdquo button right click choose Events and then choose Action then actionPerformed(similar like in Figure 6) It will bring you to where you should insert your coding (Figure 10) Writethe following coding (Figure 10)

Figure 10

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3658

Once you finished now you can run and see the result If everything is fine then the output wilbe look like this (Figure 11)

Figure 11

Remember when you compile and run ldquoGravityAttract07ajavardquo using Javascripts commandline once you compile and run the result was looked like this

On which particle you want to calculate the resulting force 2The resultant force on particle [2] due to the other masses is 30673e-10 N 8837 deg

SEE THE DIFFERENCES AT THE USER INTERFACE

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3758

8th Step

Written by Mohd Khalid Kasmin

Monday 27 August 2007

Programming using command line interface (CLI) has its advantages but todays computer users are so used to graphical user

interface (GUI) that we just can no longer be contented with writing application that do not use the GUI Hence starting from this 8thstep we are going to embark on learning how to make java application that utilises the the GUI

It is possible to write a GUI application with the text based tools that we have been using for these past 7 steps but the job is so

messy that most of you will definitely be turnoff by it So to make life slightly easier well use an integrated development

environment (IDE) known as NetBeans specifically NetBeans 551

0 First ssh to the our server Next once logged in just type nice -n15 netbeans amp on the terminal If you had sshed with the -Y

switch after a few tens of seconds NetBeans should emerge on your display screen

One important point to note Since our server can readily handle up to 100 users (maybe even

more) doing work in CLI mode this machine will certainly be taxed to its maximum capability once

we start using graphical based application NetBeans 551 uses a geat deal of computer memory

and with the 4Gbytes RAM server it can only accomodate up to 25 users concurrently using

NetBeans if the users use the application wisely What is meant by wisely here is that (a) when

starting the NetBeans application please use it together with the nice -n15 command (b) in using

the NetBeans please make sure you only have one project open at a time and (c) please close your

project everytime before quitting the NetBeans

Home Profile Teaching Links Contact Me My Work

Newsflash

Yesterday all servers in the US went out on strike in a bid to get more RAM and better CPUs A spokes person said that the

need for better RAM was due to some fool increasing the front-side bus speed In future busses will be told to slow down in

residential motherboards

search

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3858

1

2

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3958

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4058

[ Back ]

5

Last Updated ( Wednesday 31 October 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4158

8th Step (Cont)

Written by Mohd Khalid Kasmin

Monday 27 August 2007

Up to this point we have define the name of our project Next we construct a class and to follow what we already know we name the class the sam

project name latihan01

6

7

Home Profile Teaching Links Contact Me My Work

Newsflash

Joomla 10 - Experience the Freedom It has never been easier to create your own dynamic site Manage all your content from

the best CMS admin interface

search

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4258

8

9

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4358

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4458

8th Step (Cont still)

Written by Mohd Khalid Kasmin

Monday 27 August 2007

In item (9) we see that Netbeans does not provide us with the main method So to follow what we already know we now put in the

main method

10

Home Profile Teaching Links Contact Me My Work

Newsflash

Aoccdrnig to a rscheearch at an Elingsh uinervtisy it deosnt mttaer in waht oredr the ltteers in a wrod are the olny iprmoetnt

tihng is taht frist and lsat ltteer is at the rghit pclae The rset can be a toatl mses and you can sitll raed it wouthit porbelm Tihs is

bcuseae we do not raed ervey lteter by itslef but the wrod as a wlohe

search

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4558

11

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4658

12

13

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4758

14

15

Last Updated ( Monday 27 August 2007 )

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4858

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4958

9th Step

Thursday 06 September 2007

Step by Step into Java Programming

for

University Physics Student

By

Mohd Khalid Kasmin

A Building graphical user interface - preparing the JFrame Form

At this stage of our programming experience with Java and NetBeans please follow strictly the steps outline in this article First and

make a new GUI project make sure there is no other project that already been loaded by Netbeans Please close any project displayed

1 Create new project

- On the main menu click on File --gt New Project (note Close first any previous project)

- Specify project name amp project location - Untick Create Main Class item

- Click on the Finish button

Home Profile Teaching Links Contact Me My Work

Newsflash

Aoccdrnig to a rscheearch at an Elingsh uinervtisy it deosnt mttaer in waht oredr the ltteers in a wrod are the olny iprmoetnt

tihng is taht frist and lsat ltteer is at the rghit pclae The rset can be a toatl mses and you can sitll raed it wouthit porbelm Tihs is

bcuseae we do not raed ervey lteter by itslef but the wrod as a wlohe

search

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5058

Figure 91

2 Create UI class

- Right click on the project name in the Project pane

on the popup menu select New --gt JFrame Form

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5158

Figure 92

- In the New JFrame Form dialog specify the Class Name and the Package name

eg Class Name multiplierUI Package mymultiplier

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5258

Figure 93

After clicking on the Finish button we should get something like the following

Figure 94

Last Updated ( Tuesday 18 September 2007 )

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5358

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5458

9th Step (Cont)

Written by Mohd Khalid Kasmin

Tuesday 11 September 2007

Step by Step into Java Programming

for University Physics Student

By

Mohd Khalid Kasmin

B Building user interface (UI) - putting in the components

Now that we have a proper blank form we can start filling the form with the suitable graphical components For introduction to

GUI programming an appropriate application to build (for a physics student) is a simple solver So our first exercise is to build an

application that takes two numbers and multiply them and display the result

With GUI we usually give input values to an application via input boxes that are labelled When all inputs are typed in properlywe click on a button to ask the application to execute the job Hence in this exercise well do just that The target now is to

construct a user interface that looks like Figure 95 below

Figure 95

Home Profile Teaching Links Contact Me My Work

Newsflash

Joomla 10 - Experience the Freedom It has never been easier to create your own dynamic site Manage all your content from

the best CMS admin interface

search

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5558

[ Back ]

1 Problemapplication description box

- In the palette pane click on Swing component named JTextPane (Note click just once) Move the mouse pointer on top of

your JFrame (Note just move not drag) When you see a little white box with orange border position it somewhere near the

top part of your JFrame and click once The JTextPane is then attached on the JFrame Addjust its size with the help of the

sizing hook around the perimeter of the JTextPane

- Edit the text property of the JTextPane with the description of your application

2 Input boxes

- In the palette pane click on Swing component named JTextField Next move the mouse pointer on top of your JFrame

When you see a white box with the word jTextField1 position it to the left and under the JTextPane1

- Click on Swing component JTextField in the palette pane again Move the mouse pointer on top of the JFrame you should

get a second JTextField named jTextField2 Position it to the right of jTextField1

Last Updated ( Tuesday 11 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5658

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5758

multiplication if a b and c were first declared as double or float or int variables So the code of the calculation should look like the

following

double a b c

a =

b =

c = a b

Clearly variables a and b need to be assigned with values of typed double Now looking back at inputboxes A and B we made

those boxes using the JTextField component meaning that any data or value written in a JTextField is of typed Text or String even

if the value displayed in the box is ldquo125rdquo But f irst here there is this task of getting to that Text (or String) values ldquo125rdquo and ldquo72rdquo

In Java we can get to a text in JTextField component using the getText() method So to retrieve values in jTextField1 and

jTextField2 we can use

jTextField1getText()

and

jTextField2getText()

But those values are still in text mode and we need another method to convert the data mode f rom text to double For that we can

use the method DoubleparseDouble() Thus the whole process of retrieve text -gt convert text to double -gt assign to double

variable can be done using

a = DoubleparseDouble( jTextField1getText() )

and

b = DoubleparseDouble( jTextField2getText() )

Hence the code for performing the multiplication becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

Up to this point we have the variable c as the result of the multiplication of a and b So the next thing to do is to display this

result on the JLabel that we have already prepared before

A x B = _____

The text A x B = ______ was made using JLabel component and its variable name is jLabel3 Since the variable c is of type

double and text property of JLabel is of type String we need to convert it first to String data type The method to convert a double

to String is DoubletoString() So we can write the code as DoubletoString(c) After getting the text we can now use it to be the

text displayed on jLabel3 To do that we use the method setText() Hence the appropriate code for displaying the result is

jLabel3setText( A x B = + DoubletoString(c) )

And the full code for our calculation becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

jLabel3setText( A x B = + DoubletoString(c) )

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5858

[ Back ]

Last Updated ( Tuesday 18 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

Page 12: Computional Physics

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1258

for (x=-5 xlt=5 x = x+02)

for (y=5 ygt=-5 y = y-02)

V = 00

for(i=1 ilt=n i++)

rx = x - xq[i]

ry = y - yq[i]r = sqrt(rxrx+ryry)

if (rlt1e-6)

if (q[i]lt0) V = -10 else V = 10

break

V = V + q[i]r

V = kV

Systemoutprintf(f f fn x y V)

Systemoutprintf(n)

How to do surface plot using Gnuplot

To do a surface plotgnuplotgt splot epotdat with lineTo zoom in and zoom out z axisgnuplotgt set zrange [-2000020000]To convert the graph into imagegnuplotgt set term pngTerminal type set to pngOptions are small color picsize 640 480 Then typegnuplotgt set output epotpngAgain do splot as followgnuplotgt splot epotdat with lineNow the graph is saved into a file in png format named as epotpngTo go back into terminal ouputgnuplotgt set term x11To set to countor plotgnuplotgt set pm3dTo remove countor meshgnuplotgt unset surf

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1358

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1458

983107983119983117983120983125983124983105983124983113983119983118983105983116 983120983112983129983123983113983107983123

983080983123983123983120983090983089983090983090983081

983109983157983148983137983154 983149983141983156983144983151983140

983138983161

983105983138983140983086 983115983144983137983149983145983149 983113983155983149983137983145983148

983117983151983144983140 983115983144983137983148983145983140 983115983137983155983149983145983150

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1558

Free fall body problem

Consider an object thrown upward under gravitational force ( f=mg) with an initial velocity v0The object goes up and then stop at maximum y position ymax before it start to fall downback to the original position at y0 and then to the ground level (See figure)

ymdt

yd m

dt

dvmmaF ampamp====

2

2

t

Y

v1 (t1y1)

v2 (t2y2)

v4 (t4y4)

v5 (t5y5)

vn (tnyn)

v3 (t3y3)

v0 (t0y0)

∆t∆t ∆t ∆t

y1

y0

∆t ∆t

∆yv0

a = g

y0

t0

t0 t1 t2 t3 t4 t5 tn

ymax

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1658

Exact theoretical solution

( ) 2

21

00 at t v yt y ++=

The y projection of the exact solution can be plotted using the following java source code

Java class for solving free fall object under gravity (g = 98 mss)Exact solution y = y0 + v0t + 12at^2

public class freefallsol

public static void main(String args[])double av0yy0deltt

a = -98y0 = 10v0 = 10delt = 00001for(t=0 tlt=05 t=t+delt)

y = y0 + v0t + 05attSystemoutprintf(84f 84fnty)

Complile freefallsoljava to get the plot of the exact solution and then run and save theoutput into a file name as follows

[khamimsetpar65 ~]$ java freefallsol gtffsoldat

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1758

Euler Method

There is another way to see the projection of y position of the object using a numericalmethod called ldquoEuler methodrdquo See the following derivation

t avt v y

t

va

t

yv

∆=∆∆=∆

∆=

∆=

t v y y y y

t avvvv

t v y y y y

t avvvv

t v y y y y

t avvvv

nnnn

nnn

∆+=∆+=

∆+=∆+=

∆+=∆+=

∆+=∆+=

∆+=∆+=

∆+=∆+=

minus

minusminus

1

11

2112

112

1001

001

M

if ∆∆∆∆t rarrrarrrarrrarr 0 then y will close to the exact solution ( ( ) 2

21

00 at t v yt y ++= )

The algorithm 1 Declare the classmethod to be used

2 Declare and assign all variables to be used av0v1yy0y1deltt and j 3 Initialize value to all respective variables a = -98 y0 = 10 v0 = 10

4 Select appropriate value for ∆t

∆t = 00055 Start the loop to calculate the velocity and position for t=0

6 Calculate velocity v1 = v0 + ∆v = v0 + a ∆t

7 Calculate position y1 = y0 + ∆y = y0 + v1 ∆t8 Print out values of t and y1 9 Swap v0 = v1 and y0 = y1

10 Repeat from step 5 and increase the value of t by ∆t (t =t + ∆t) until stoppingcondition is satisfied at t=05

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1858

The java code

Java class for solving free fall object under gravity (g = 98 mss)by numerical method called ldquoEulers Methodrdquo

public class freefalleuler

public static void main(String args[])double av0v1yy0y1delttint j

a = -98y0 = 10v0 = 10delt = 0005

for(t=0 tlt=05 t =t + delt)v1 = v0 + adelty1 = y0 + v1deltSystemoutprintf(84f 84fnty1)v0 = v1y0 = y1

Complile freefalleulerjava for different delt ie 00005 0005 005 and then run and savethe output into a file name as follows

[khamimsetpar65 ~]$ java freefalleuler gtffeuler00005dat[khamimsetpar65 ~]$ java freefalleuler gtffeuler0005dat[khamimsetpar65 ~]$ java freefalleuler gtffeuler005dat

Plotting the results using gnuplot

Using gnuplot plot all the graphs using the following command and compare the results

gnuplotgt plot ffeuler00005dat using 12 title euler delt=00005 with linegt ffeuler0005dat using 12 title euler delt=0005 with linegt ffeuler005dat using 12 title euler deltt=005 with linegt ffsoldat using 12 title exact solution with line

In MS windows

gnuplotgt plot ffeuler00005dat using 12 title euler delt=00005 with lineffeuler0005dat using 12 title euler delt=0005 with lineffeuler005dat using 12 titleeuler delt=005 with line ffsoldat using 12 title exact solution with line

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1958

Gnuplot

The smaller the delt the closer the curve to the exact solution

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2058

983107983119983117983120983125983124983105983124983113983119983118983105983116 983120983112983129983123983113983107983123

983080983123983123983120983090983089983090983090983081

983113983149983152983154983151983158983141983140 983109983157983148983137983154 983149983141983156983144983151983140

983138983161

983105983138983140983086 983115983144983137983149983145983149 983113983155983149983137983145983148

983117983151983144983140 983115983144983137983148983145983140 983115983137983155983149983145983150

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2158

Improved Euler method

Comparison between analytical solution of F=ma with the Euler method shows that although the

method is able to find approximate solution the error is quite significant if computational step is

reduced The error is also cumulative ie as the iteration of the calculation increases the error also gets

bigger hence value furthest away from initial condition has the largest error

The Euler method gives us a way to approximate positions of object with respect to time but requires

an order of magnitude of computational iterations for each order of magnitude of accuracy For large

number of objects this method will certainly consume a great deal of computation time Hence

students are introduced to Initial Half-Step Method an Euler method with a much improved

computational effeciency

Source code

Study of motion of an object with mass m

experiencing a constant force F using

Improved Euler Method (Initial half step method)

In X and Y dimension

import static javalangMath

public class fma04

public static void main(String args[])

double m fx fy x y vo vx vy ax ay t delt

double pi th theta ymax tmax xmax

pi = 40atan(10)

m = 01 in kg

fy = -02 in N (pulling down force)

fx = 00 in N (retarding force)

ay = fym

ax = fxm

vo = 500 in ms

theta = 300 in degrees

th = theta(pi1800) degrees change to radian

vx = vo cos(th) in ms vy = vo sin(th) in ms

y = 12 in m

x = 00 in m

ymax=y initialize var ymax with initial value of y

xmax=00

tmax=00

delt = 001 in s

for(t=00 tlt=300 t=t+delt)

System outprintf(f f fntxy)

if (t==00)vx = vx + 05axdelt

vy = vy + 05aydelt

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2258

else

vx = vx + axdelt

vy = vy + aydelt

x = x + vxdelt

y = y + vydelt

if (ygtymax)

ymax=y

xmax=xtmax=t

if (ylt=00) break

System outprintf( Value of Y maximum is f happened when t = f

nymaxtmax)

System outprintf( at x = fnxmax)

System outprintf( The object reach the ground at x=f and t=fn xt)

gnuplot result

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2358

983107983119983117983120983125983124983105983124983113983119983118983105983116

983120983112983129983123983113983107983123 983080983123983123983120983090983089983090983090983081

983122983141983155983156983151983154983145983150983143 983110983151983154983139983141

983138983161

983105983138983140983086 983115983144983137983149983145983149 983113983155983149983137983145983148

983117983151983144983140 983115983144983137983148983145983140 983115983137983155983149983145983150

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2458

Damped Harmonic Oscillation

Object m is on a flat surface attached to a spring as in figure above The object is pulled

to position A and released so that it oscillates about x =0 Assuming that the force F=ma= -bv-kx where x is position of the object v is the velocity k the spring constant andb is a constant associated with friction with the flat surface

The algorithm

1 Declare the classmethod to be used2 Declare and assign all variables to be used

x t delt k b A m F a v v0 and x03 Declare and assign constants to the respective variables

k = 02 b = 01 A = 01 m = 054 Select appropriate value for ∆t 5 Initialized initial values xo=A vo=00

6 set x=xo v=vo and t = 07 calculate the value of force F = -kx-bv 8 calculate the value of acceleration a = Fm

9 calculate velocity v = vo + ∆v = vo + a ∆t

10 calculate position x = xo + ∆x = xo + v ∆t

11 increment value of t by ∆t 12 print out values of t and x

13 swap the value of xo=x vo=v 14 repeat from step 4 until stopping condition is satisfied (tlt=600)

The java code

public class simplehar01

public static void main(String args[])double x t deltdouble k b A m F a v v0 x0

k = 02b = 01A = 0 1

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2558

m = 05delt = 0001v0 = 00x0 = Ax = x0 v = v0

t = 00while(tlt=600)

F = -kx-bva = Fmv = v0 + adeltx = x0 + vdeltt = t + deltSystemoutprintf(f fntx)x0 = xv0 = v

gnuplot result

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2658

983107983119983117983120983125983124983105983124983113983119983118983105983116 983120983112983129983123983113983107983123

(983123983123983120983090983089983090983090983081

983106983157983145983148983140983145983150983143 983137 983152983144983161983155983145983139983155 983155983151983148983158983141983154

983157983155983145983150983143 983118983141983156983138983141983137983150983155

983138983161

983105983138983140983086 983115983144983137983149983145983149 983113983155983149983137983145983148

983117983151983144983140 983115983144983137983148983145983140 983115983137983155983149983145983150

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2758

How to create a user interface (UI) using Netbeans

Based on the ldquoGravityAttract07ajavardquo source code (shown below) build a user interface usingNetbeans The design of the UI could be look like this (Figure1)

Figure 1

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2858

ldquoGravityAttract07ajavardquo source code

Java class for calculating the resultant force on any object(ie either on m1m2 m3 m4 or m5)

import static javalangMathimport javaiopublic class GravityAttract07a

public static void main(String args[])

double[] m = new double[6] mass of objectdouble[] x = new double[6] x-coor of objectdouble[] y = new double[6] y-coor of objectdouble f resultant forcedouble fxfy x amp y component of forcedouble rx ry rp co G dirfangdirf

int j

G = 667e-11m[1]=25 x[1]=12 y[1]=25m[2]=15 x[2]=15 y[2]=15m[3]=75 x[3]=02 y[3]=55m[4]=92 x[4]=52 y[4]=25m[5]=66 x[5]=42 y[5]=45BufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin))String mass=Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))try mass=dataInreadLine()

catch(IOException e)Systemoutprintln(Not a valid input value)int mi = IntegerparseInt(mass)fx = 00fy = 00

for(j=1 jlt=5 j++)

if (j=mi)rx = x[j]-x[mi]ry = y[j]-y[mi]rp = pow((rxrx+ryry)15) (rx^2 + ry^2) raised to the power of 32co = Gm[mi]m[j]

fx = fx + corxrpfy = fy + coryrp

f = sqrt(fxfx + fyfy)dirf = toDegrees(atan(fyfx))Systemoutprintf(The force on m[+ mi +] due to the other masses is eN fdegnfdirf)

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2958

This example will not show you step by step details procedure since it already explained inthe class and in the e-learning Please refer to the e-learning about how to create a project how tocreate JFrame Form and as well how to create JButton JTextField JLabel JTextPane and so on

First create a new project and give a name eg ldquogravityrdquo and then create JFrame Form andthe following JButton JTextField JLabel JTextPane etc by using ldquoswing paletterdquo as shown in Figure2

Figure 2

To insert a picture into JLabel2 click on the JLabel2 go to properties and select ldquoiconrdquo Onthe icon editor choose file and then select file to locate the directory of your graphicpicture file(Figure 3) Select the file and click OK when finished Make sure you have already created your ownpicture file and save it as jpg png bmp etc format eg ldquogravityjpgrdquo Use any graphic package todraw your diagrampicture or use drawing tools available in Open Office or Microsoft Word

JTextPane1JLabel1

JLabel2

JLabel3JLabel4

JLabel5

JLabel6 JLabel7

JLabel8

JTextField2 JTextField3 JTextField4

JTextF

JButton1JButton2

JButton3

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3058

Figure 3

To write documentation or question eg in JTextPane1 click on the JTextPane1 go toproperties and select ldquotextrdquo Type the documentation in the text editor as shown in Figure 4 Click OKwhen finished

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3158

To do formatting on the text such as superscript and subscript eg in jLabel3 click on the

JLabel3 go to properties and select ldquotextrdquo Type the documentationmessage on the text editor asshown in Figure 5 as follows

eg

lthtmlgtOn which particle (mltsubgt1ltsubgtmltsubgt5ltsubgt) you want to calculate theresultant force

Use sup instead if you want a superscriptThe result will look like this

Click OK when finished

Figure 5

To do the calculation you need to put the coding similar like ldquoGravityAttract07ajavardquo and put itinto the ldquocalculaterdquo button (this is ldquoJButton1rdquo) You can copy ldquoGravityAttract07a javardquo source code

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3258

paste it and then do a few alterations First click on the ldquocalculaterdquo button right click choose Eventsand then choose Action then actionPerformed (Figure 6) It will bring you to where you should insertyour coding (Figure 7) You can write your own coding or copypaste any coding from somewhereelse (in this case from ldquoGravityAttract07ajavardquo)

Figure 6

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3358

Figure 7

Copy ldquoGravityAttract07ajavardquo source code and paste to the place after ldquo TODO add youhandling code hererdquo see Figure 7 To see which part has been modified the coding in Figure 7 hasbeen color coded for better understanding as follows

private void jButton1ActionPerformed(javaawteventActionEvent evt) TODO add your handling code here

double[] m = new double[6] mass of objectdouble[] x = new double[6] x-coor of objectdouble[] y = new double[6] y-coor of objectdouble f resultant forcedouble fxfy x amp y component of forcedouble rx ry rp co G dirfangdirfDecimalFormat dff dfangdirfint j mi

G = 667e-11m[1]=25 x[1]=12 y[1]=25m[2]=15 x[2]=15 y[2]=15m[3]=75 x[3]=02 y[3]=55m[4]=92 x[4]=52 y[4]=25m[5]=66 x[5]=42 y[5]=45

BufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin)) String mass= Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3458

catch(IOException e)Systemoutprintln(Not a valid input value) int mi = IntegerparseInt(mass)mi = IntegerparseInt(jTextField1getText())fx = 00fy = 00for(j=1 jlt=5 j++)

if (j=mi)rx = x[j]-x[mi]ry = y[j]-y[mi]rp = Mathpow((rxrx+ryry)15) (rx^2 + ry^2) raised to the power of 32co = Gm[mi]m[j]fx = fx + corxrpfy = fy + coryrp

f = Mathsqrt(fxfx + fyfy)

dirf = Mathatan(fyfx)angdirf = (dirf(22070))1800 Systemoutprintf(ldquoThe resultant force on particle [ldquo+ mi +rdquo] due to the other masses is

14e N 22f degnrdquofangdirf)dff = new javatextDecimalFormat(ldquoE00rdquo)dfangdirf = new DecimalFormat(ldquo000rdquo)

jTextField2setText(ldquordquo + mi) jTextField3setText(ldquordquo + dffformat(f)) jTextField4setText(ldquordquo + dfangdirfformat(angdirf))

Note

Brown = automatically generatedBlue = the original code from ldquoGravityAttract07ajavardquoGreen = coding from ldquoGravityAttract07ajavardquo which were being disabled due to

incompatibilities with NetbeansRed = a new inserted coding a few of them is to rephrasesubstitute the green one

Javascripts command line useBufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin))String mass=Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))try mass=dataInreadLine()catch(IOException e)Systemoutprintln(Not a valid input value)

int mi = IntegerparseInt(mass)

to read any input from the keyboard typed by the user while Netbeans uses egldquoIntegerparseInt(jTextField1getText())rdquo etc Javascripts command line uses ldquoSystemoutprintfrdquo to format the printed output whileNetbeans uses more complicated way to do the same job (see below)

Javascript command line

Systemoutprintf(The resultant force on particle [+ mi +] due to the other masses is 14e N22f degnfangdirf)

Netbeansdff = new javatextDecimalFormat(ldquo0E00rdquo)df di f D i lF t(ldquo0 00rdquo)

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3558

jTextField2setText(ldquordquo + mi) jTextField3setText(ldquordquo + dffformat(f)) jTextField4setText(ldquordquo + dfangdirfformat(angdirf))

ldquodffrdquo and ldquodfangdirfrdquo are new variables that should be declared and added to format thenumbers into decimal format ie ldquo0E00rdquo to get a scientific format eg ldquo20019E-10rdquo and ldquo000rdquoto get a decimal number eg ldquo-1570rdquo

To use DecimalFormat you have to put ldquoimport javatextDecimalFormatrdquo in the top of thecoding as shown in Figure 8

Figure 8

To reset the calculation you need to put coding into the ldquoresetrdquo button (this is ldquoJButton2rdquo)First click on the ldquoresetrdquo button right click choose Events and then choose Action thenactionPerformed (similar like in Figure 6) It will bring you to where you should insert your coding(Figure 9) Write the following coding (Figure 9)

Figure 9

To exit the program you need to put coding into the ldquoexitrdquo button (this is ldquoJButton3rdquo) Firstclick on the ldquoexitrdquo button right click choose Events and then choose Action then actionPerformed(similar like in Figure 6) It will bring you to where you should insert your coding (Figure 10) Writethe following coding (Figure 10)

Figure 10

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3658

Once you finished now you can run and see the result If everything is fine then the output wilbe look like this (Figure 11)

Figure 11

Remember when you compile and run ldquoGravityAttract07ajavardquo using Javascripts commandline once you compile and run the result was looked like this

On which particle you want to calculate the resulting force 2The resultant force on particle [2] due to the other masses is 30673e-10 N 8837 deg

SEE THE DIFFERENCES AT THE USER INTERFACE

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3758

8th Step

Written by Mohd Khalid Kasmin

Monday 27 August 2007

Programming using command line interface (CLI) has its advantages but todays computer users are so used to graphical user

interface (GUI) that we just can no longer be contented with writing application that do not use the GUI Hence starting from this 8thstep we are going to embark on learning how to make java application that utilises the the GUI

It is possible to write a GUI application with the text based tools that we have been using for these past 7 steps but the job is so

messy that most of you will definitely be turnoff by it So to make life slightly easier well use an integrated development

environment (IDE) known as NetBeans specifically NetBeans 551

0 First ssh to the our server Next once logged in just type nice -n15 netbeans amp on the terminal If you had sshed with the -Y

switch after a few tens of seconds NetBeans should emerge on your display screen

One important point to note Since our server can readily handle up to 100 users (maybe even

more) doing work in CLI mode this machine will certainly be taxed to its maximum capability once

we start using graphical based application NetBeans 551 uses a geat deal of computer memory

and with the 4Gbytes RAM server it can only accomodate up to 25 users concurrently using

NetBeans if the users use the application wisely What is meant by wisely here is that (a) when

starting the NetBeans application please use it together with the nice -n15 command (b) in using

the NetBeans please make sure you only have one project open at a time and (c) please close your

project everytime before quitting the NetBeans

Home Profile Teaching Links Contact Me My Work

Newsflash

Yesterday all servers in the US went out on strike in a bid to get more RAM and better CPUs A spokes person said that the

need for better RAM was due to some fool increasing the front-side bus speed In future busses will be told to slow down in

residential motherboards

search

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3858

1

2

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3958

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4058

[ Back ]

5

Last Updated ( Wednesday 31 October 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4158

8th Step (Cont)

Written by Mohd Khalid Kasmin

Monday 27 August 2007

Up to this point we have define the name of our project Next we construct a class and to follow what we already know we name the class the sam

project name latihan01

6

7

Home Profile Teaching Links Contact Me My Work

Newsflash

Joomla 10 - Experience the Freedom It has never been easier to create your own dynamic site Manage all your content from

the best CMS admin interface

search

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4258

8

9

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4358

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4458

8th Step (Cont still)

Written by Mohd Khalid Kasmin

Monday 27 August 2007

In item (9) we see that Netbeans does not provide us with the main method So to follow what we already know we now put in the

main method

10

Home Profile Teaching Links Contact Me My Work

Newsflash

Aoccdrnig to a rscheearch at an Elingsh uinervtisy it deosnt mttaer in waht oredr the ltteers in a wrod are the olny iprmoetnt

tihng is taht frist and lsat ltteer is at the rghit pclae The rset can be a toatl mses and you can sitll raed it wouthit porbelm Tihs is

bcuseae we do not raed ervey lteter by itslef but the wrod as a wlohe

search

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4558

11

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4658

12

13

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4758

14

15

Last Updated ( Monday 27 August 2007 )

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4858

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4958

9th Step

Thursday 06 September 2007

Step by Step into Java Programming

for

University Physics Student

By

Mohd Khalid Kasmin

A Building graphical user interface - preparing the JFrame Form

At this stage of our programming experience with Java and NetBeans please follow strictly the steps outline in this article First and

make a new GUI project make sure there is no other project that already been loaded by Netbeans Please close any project displayed

1 Create new project

- On the main menu click on File --gt New Project (note Close first any previous project)

- Specify project name amp project location - Untick Create Main Class item

- Click on the Finish button

Home Profile Teaching Links Contact Me My Work

Newsflash

Aoccdrnig to a rscheearch at an Elingsh uinervtisy it deosnt mttaer in waht oredr the ltteers in a wrod are the olny iprmoetnt

tihng is taht frist and lsat ltteer is at the rghit pclae The rset can be a toatl mses and you can sitll raed it wouthit porbelm Tihs is

bcuseae we do not raed ervey lteter by itslef but the wrod as a wlohe

search

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5058

Figure 91

2 Create UI class

- Right click on the project name in the Project pane

on the popup menu select New --gt JFrame Form

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5158

Figure 92

- In the New JFrame Form dialog specify the Class Name and the Package name

eg Class Name multiplierUI Package mymultiplier

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5258

Figure 93

After clicking on the Finish button we should get something like the following

Figure 94

Last Updated ( Tuesday 18 September 2007 )

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5358

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5458

9th Step (Cont)

Written by Mohd Khalid Kasmin

Tuesday 11 September 2007

Step by Step into Java Programming

for University Physics Student

By

Mohd Khalid Kasmin

B Building user interface (UI) - putting in the components

Now that we have a proper blank form we can start filling the form with the suitable graphical components For introduction to

GUI programming an appropriate application to build (for a physics student) is a simple solver So our first exercise is to build an

application that takes two numbers and multiply them and display the result

With GUI we usually give input values to an application via input boxes that are labelled When all inputs are typed in properlywe click on a button to ask the application to execute the job Hence in this exercise well do just that The target now is to

construct a user interface that looks like Figure 95 below

Figure 95

Home Profile Teaching Links Contact Me My Work

Newsflash

Joomla 10 - Experience the Freedom It has never been easier to create your own dynamic site Manage all your content from

the best CMS admin interface

search

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5558

[ Back ]

1 Problemapplication description box

- In the palette pane click on Swing component named JTextPane (Note click just once) Move the mouse pointer on top of

your JFrame (Note just move not drag) When you see a little white box with orange border position it somewhere near the

top part of your JFrame and click once The JTextPane is then attached on the JFrame Addjust its size with the help of the

sizing hook around the perimeter of the JTextPane

- Edit the text property of the JTextPane with the description of your application

2 Input boxes

- In the palette pane click on Swing component named JTextField Next move the mouse pointer on top of your JFrame

When you see a white box with the word jTextField1 position it to the left and under the JTextPane1

- Click on Swing component JTextField in the palette pane again Move the mouse pointer on top of the JFrame you should

get a second JTextField named jTextField2 Position it to the right of jTextField1

Last Updated ( Tuesday 11 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5658

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5758

multiplication if a b and c were first declared as double or float or int variables So the code of the calculation should look like the

following

double a b c

a =

b =

c = a b

Clearly variables a and b need to be assigned with values of typed double Now looking back at inputboxes A and B we made

those boxes using the JTextField component meaning that any data or value written in a JTextField is of typed Text or String even

if the value displayed in the box is ldquo125rdquo But f irst here there is this task of getting to that Text (or String) values ldquo125rdquo and ldquo72rdquo

In Java we can get to a text in JTextField component using the getText() method So to retrieve values in jTextField1 and

jTextField2 we can use

jTextField1getText()

and

jTextField2getText()

But those values are still in text mode and we need another method to convert the data mode f rom text to double For that we can

use the method DoubleparseDouble() Thus the whole process of retrieve text -gt convert text to double -gt assign to double

variable can be done using

a = DoubleparseDouble( jTextField1getText() )

and

b = DoubleparseDouble( jTextField2getText() )

Hence the code for performing the multiplication becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

Up to this point we have the variable c as the result of the multiplication of a and b So the next thing to do is to display this

result on the JLabel that we have already prepared before

A x B = _____

The text A x B = ______ was made using JLabel component and its variable name is jLabel3 Since the variable c is of type

double and text property of JLabel is of type String we need to convert it first to String data type The method to convert a double

to String is DoubletoString() So we can write the code as DoubletoString(c) After getting the text we can now use it to be the

text displayed on jLabel3 To do that we use the method setText() Hence the appropriate code for displaying the result is

jLabel3setText( A x B = + DoubletoString(c) )

And the full code for our calculation becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

jLabel3setText( A x B = + DoubletoString(c) )

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5858

[ Back ]

Last Updated ( Tuesday 18 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

Page 13: Computional Physics

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1358

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1458

983107983119983117983120983125983124983105983124983113983119983118983105983116 983120983112983129983123983113983107983123

983080983123983123983120983090983089983090983090983081

983109983157983148983137983154 983149983141983156983144983151983140

983138983161

983105983138983140983086 983115983144983137983149983145983149 983113983155983149983137983145983148

983117983151983144983140 983115983144983137983148983145983140 983115983137983155983149983145983150

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1558

Free fall body problem

Consider an object thrown upward under gravitational force ( f=mg) with an initial velocity v0The object goes up and then stop at maximum y position ymax before it start to fall downback to the original position at y0 and then to the ground level (See figure)

ymdt

yd m

dt

dvmmaF ampamp====

2

2

t

Y

v1 (t1y1)

v2 (t2y2)

v4 (t4y4)

v5 (t5y5)

vn (tnyn)

v3 (t3y3)

v0 (t0y0)

∆t∆t ∆t ∆t

y1

y0

∆t ∆t

∆yv0

a = g

y0

t0

t0 t1 t2 t3 t4 t5 tn

ymax

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1658

Exact theoretical solution

( ) 2

21

00 at t v yt y ++=

The y projection of the exact solution can be plotted using the following java source code

Java class for solving free fall object under gravity (g = 98 mss)Exact solution y = y0 + v0t + 12at^2

public class freefallsol

public static void main(String args[])double av0yy0deltt

a = -98y0 = 10v0 = 10delt = 00001for(t=0 tlt=05 t=t+delt)

y = y0 + v0t + 05attSystemoutprintf(84f 84fnty)

Complile freefallsoljava to get the plot of the exact solution and then run and save theoutput into a file name as follows

[khamimsetpar65 ~]$ java freefallsol gtffsoldat

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1758

Euler Method

There is another way to see the projection of y position of the object using a numericalmethod called ldquoEuler methodrdquo See the following derivation

t avt v y

t

va

t

yv

∆=∆∆=∆

∆=

∆=

t v y y y y

t avvvv

t v y y y y

t avvvv

t v y y y y

t avvvv

nnnn

nnn

∆+=∆+=

∆+=∆+=

∆+=∆+=

∆+=∆+=

∆+=∆+=

∆+=∆+=

minus

minusminus

1

11

2112

112

1001

001

M

if ∆∆∆∆t rarrrarrrarrrarr 0 then y will close to the exact solution ( ( ) 2

21

00 at t v yt y ++= )

The algorithm 1 Declare the classmethod to be used

2 Declare and assign all variables to be used av0v1yy0y1deltt and j 3 Initialize value to all respective variables a = -98 y0 = 10 v0 = 10

4 Select appropriate value for ∆t

∆t = 00055 Start the loop to calculate the velocity and position for t=0

6 Calculate velocity v1 = v0 + ∆v = v0 + a ∆t

7 Calculate position y1 = y0 + ∆y = y0 + v1 ∆t8 Print out values of t and y1 9 Swap v0 = v1 and y0 = y1

10 Repeat from step 5 and increase the value of t by ∆t (t =t + ∆t) until stoppingcondition is satisfied at t=05

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1858

The java code

Java class for solving free fall object under gravity (g = 98 mss)by numerical method called ldquoEulers Methodrdquo

public class freefalleuler

public static void main(String args[])double av0v1yy0y1delttint j

a = -98y0 = 10v0 = 10delt = 0005

for(t=0 tlt=05 t =t + delt)v1 = v0 + adelty1 = y0 + v1deltSystemoutprintf(84f 84fnty1)v0 = v1y0 = y1

Complile freefalleulerjava for different delt ie 00005 0005 005 and then run and savethe output into a file name as follows

[khamimsetpar65 ~]$ java freefalleuler gtffeuler00005dat[khamimsetpar65 ~]$ java freefalleuler gtffeuler0005dat[khamimsetpar65 ~]$ java freefalleuler gtffeuler005dat

Plotting the results using gnuplot

Using gnuplot plot all the graphs using the following command and compare the results

gnuplotgt plot ffeuler00005dat using 12 title euler delt=00005 with linegt ffeuler0005dat using 12 title euler delt=0005 with linegt ffeuler005dat using 12 title euler deltt=005 with linegt ffsoldat using 12 title exact solution with line

In MS windows

gnuplotgt plot ffeuler00005dat using 12 title euler delt=00005 with lineffeuler0005dat using 12 title euler delt=0005 with lineffeuler005dat using 12 titleeuler delt=005 with line ffsoldat using 12 title exact solution with line

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1958

Gnuplot

The smaller the delt the closer the curve to the exact solution

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2058

983107983119983117983120983125983124983105983124983113983119983118983105983116 983120983112983129983123983113983107983123

983080983123983123983120983090983089983090983090983081

983113983149983152983154983151983158983141983140 983109983157983148983137983154 983149983141983156983144983151983140

983138983161

983105983138983140983086 983115983144983137983149983145983149 983113983155983149983137983145983148

983117983151983144983140 983115983144983137983148983145983140 983115983137983155983149983145983150

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2158

Improved Euler method

Comparison between analytical solution of F=ma with the Euler method shows that although the

method is able to find approximate solution the error is quite significant if computational step is

reduced The error is also cumulative ie as the iteration of the calculation increases the error also gets

bigger hence value furthest away from initial condition has the largest error

The Euler method gives us a way to approximate positions of object with respect to time but requires

an order of magnitude of computational iterations for each order of magnitude of accuracy For large

number of objects this method will certainly consume a great deal of computation time Hence

students are introduced to Initial Half-Step Method an Euler method with a much improved

computational effeciency

Source code

Study of motion of an object with mass m

experiencing a constant force F using

Improved Euler Method (Initial half step method)

In X and Y dimension

import static javalangMath

public class fma04

public static void main(String args[])

double m fx fy x y vo vx vy ax ay t delt

double pi th theta ymax tmax xmax

pi = 40atan(10)

m = 01 in kg

fy = -02 in N (pulling down force)

fx = 00 in N (retarding force)

ay = fym

ax = fxm

vo = 500 in ms

theta = 300 in degrees

th = theta(pi1800) degrees change to radian

vx = vo cos(th) in ms vy = vo sin(th) in ms

y = 12 in m

x = 00 in m

ymax=y initialize var ymax with initial value of y

xmax=00

tmax=00

delt = 001 in s

for(t=00 tlt=300 t=t+delt)

System outprintf(f f fntxy)

if (t==00)vx = vx + 05axdelt

vy = vy + 05aydelt

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2258

else

vx = vx + axdelt

vy = vy + aydelt

x = x + vxdelt

y = y + vydelt

if (ygtymax)

ymax=y

xmax=xtmax=t

if (ylt=00) break

System outprintf( Value of Y maximum is f happened when t = f

nymaxtmax)

System outprintf( at x = fnxmax)

System outprintf( The object reach the ground at x=f and t=fn xt)

gnuplot result

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2358

983107983119983117983120983125983124983105983124983113983119983118983105983116

983120983112983129983123983113983107983123 983080983123983123983120983090983089983090983090983081

983122983141983155983156983151983154983145983150983143 983110983151983154983139983141

983138983161

983105983138983140983086 983115983144983137983149983145983149 983113983155983149983137983145983148

983117983151983144983140 983115983144983137983148983145983140 983115983137983155983149983145983150

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2458

Damped Harmonic Oscillation

Object m is on a flat surface attached to a spring as in figure above The object is pulled

to position A and released so that it oscillates about x =0 Assuming that the force F=ma= -bv-kx where x is position of the object v is the velocity k the spring constant andb is a constant associated with friction with the flat surface

The algorithm

1 Declare the classmethod to be used2 Declare and assign all variables to be used

x t delt k b A m F a v v0 and x03 Declare and assign constants to the respective variables

k = 02 b = 01 A = 01 m = 054 Select appropriate value for ∆t 5 Initialized initial values xo=A vo=00

6 set x=xo v=vo and t = 07 calculate the value of force F = -kx-bv 8 calculate the value of acceleration a = Fm

9 calculate velocity v = vo + ∆v = vo + a ∆t

10 calculate position x = xo + ∆x = xo + v ∆t

11 increment value of t by ∆t 12 print out values of t and x

13 swap the value of xo=x vo=v 14 repeat from step 4 until stopping condition is satisfied (tlt=600)

The java code

public class simplehar01

public static void main(String args[])double x t deltdouble k b A m F a v v0 x0

k = 02b = 01A = 0 1

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2558

m = 05delt = 0001v0 = 00x0 = Ax = x0 v = v0

t = 00while(tlt=600)

F = -kx-bva = Fmv = v0 + adeltx = x0 + vdeltt = t + deltSystemoutprintf(f fntx)x0 = xv0 = v

gnuplot result

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2658

983107983119983117983120983125983124983105983124983113983119983118983105983116 983120983112983129983123983113983107983123

(983123983123983120983090983089983090983090983081

983106983157983145983148983140983145983150983143 983137 983152983144983161983155983145983139983155 983155983151983148983158983141983154

983157983155983145983150983143 983118983141983156983138983141983137983150983155

983138983161

983105983138983140983086 983115983144983137983149983145983149 983113983155983149983137983145983148

983117983151983144983140 983115983144983137983148983145983140 983115983137983155983149983145983150

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2758

How to create a user interface (UI) using Netbeans

Based on the ldquoGravityAttract07ajavardquo source code (shown below) build a user interface usingNetbeans The design of the UI could be look like this (Figure1)

Figure 1

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2858

ldquoGravityAttract07ajavardquo source code

Java class for calculating the resultant force on any object(ie either on m1m2 m3 m4 or m5)

import static javalangMathimport javaiopublic class GravityAttract07a

public static void main(String args[])

double[] m = new double[6] mass of objectdouble[] x = new double[6] x-coor of objectdouble[] y = new double[6] y-coor of objectdouble f resultant forcedouble fxfy x amp y component of forcedouble rx ry rp co G dirfangdirf

int j

G = 667e-11m[1]=25 x[1]=12 y[1]=25m[2]=15 x[2]=15 y[2]=15m[3]=75 x[3]=02 y[3]=55m[4]=92 x[4]=52 y[4]=25m[5]=66 x[5]=42 y[5]=45BufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin))String mass=Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))try mass=dataInreadLine()

catch(IOException e)Systemoutprintln(Not a valid input value)int mi = IntegerparseInt(mass)fx = 00fy = 00

for(j=1 jlt=5 j++)

if (j=mi)rx = x[j]-x[mi]ry = y[j]-y[mi]rp = pow((rxrx+ryry)15) (rx^2 + ry^2) raised to the power of 32co = Gm[mi]m[j]

fx = fx + corxrpfy = fy + coryrp

f = sqrt(fxfx + fyfy)dirf = toDegrees(atan(fyfx))Systemoutprintf(The force on m[+ mi +] due to the other masses is eN fdegnfdirf)

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2958

This example will not show you step by step details procedure since it already explained inthe class and in the e-learning Please refer to the e-learning about how to create a project how tocreate JFrame Form and as well how to create JButton JTextField JLabel JTextPane and so on

First create a new project and give a name eg ldquogravityrdquo and then create JFrame Form andthe following JButton JTextField JLabel JTextPane etc by using ldquoswing paletterdquo as shown in Figure2

Figure 2

To insert a picture into JLabel2 click on the JLabel2 go to properties and select ldquoiconrdquo Onthe icon editor choose file and then select file to locate the directory of your graphicpicture file(Figure 3) Select the file and click OK when finished Make sure you have already created your ownpicture file and save it as jpg png bmp etc format eg ldquogravityjpgrdquo Use any graphic package todraw your diagrampicture or use drawing tools available in Open Office or Microsoft Word

JTextPane1JLabel1

JLabel2

JLabel3JLabel4

JLabel5

JLabel6 JLabel7

JLabel8

JTextField2 JTextField3 JTextField4

JTextF

JButton1JButton2

JButton3

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3058

Figure 3

To write documentation or question eg in JTextPane1 click on the JTextPane1 go toproperties and select ldquotextrdquo Type the documentation in the text editor as shown in Figure 4 Click OKwhen finished

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3158

To do formatting on the text such as superscript and subscript eg in jLabel3 click on the

JLabel3 go to properties and select ldquotextrdquo Type the documentationmessage on the text editor asshown in Figure 5 as follows

eg

lthtmlgtOn which particle (mltsubgt1ltsubgtmltsubgt5ltsubgt) you want to calculate theresultant force

Use sup instead if you want a superscriptThe result will look like this

Click OK when finished

Figure 5

To do the calculation you need to put the coding similar like ldquoGravityAttract07ajavardquo and put itinto the ldquocalculaterdquo button (this is ldquoJButton1rdquo) You can copy ldquoGravityAttract07a javardquo source code

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3258

paste it and then do a few alterations First click on the ldquocalculaterdquo button right click choose Eventsand then choose Action then actionPerformed (Figure 6) It will bring you to where you should insertyour coding (Figure 7) You can write your own coding or copypaste any coding from somewhereelse (in this case from ldquoGravityAttract07ajavardquo)

Figure 6

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3358

Figure 7

Copy ldquoGravityAttract07ajavardquo source code and paste to the place after ldquo TODO add youhandling code hererdquo see Figure 7 To see which part has been modified the coding in Figure 7 hasbeen color coded for better understanding as follows

private void jButton1ActionPerformed(javaawteventActionEvent evt) TODO add your handling code here

double[] m = new double[6] mass of objectdouble[] x = new double[6] x-coor of objectdouble[] y = new double[6] y-coor of objectdouble f resultant forcedouble fxfy x amp y component of forcedouble rx ry rp co G dirfangdirfDecimalFormat dff dfangdirfint j mi

G = 667e-11m[1]=25 x[1]=12 y[1]=25m[2]=15 x[2]=15 y[2]=15m[3]=75 x[3]=02 y[3]=55m[4]=92 x[4]=52 y[4]=25m[5]=66 x[5]=42 y[5]=45

BufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin)) String mass= Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3458

catch(IOException e)Systemoutprintln(Not a valid input value) int mi = IntegerparseInt(mass)mi = IntegerparseInt(jTextField1getText())fx = 00fy = 00for(j=1 jlt=5 j++)

if (j=mi)rx = x[j]-x[mi]ry = y[j]-y[mi]rp = Mathpow((rxrx+ryry)15) (rx^2 + ry^2) raised to the power of 32co = Gm[mi]m[j]fx = fx + corxrpfy = fy + coryrp

f = Mathsqrt(fxfx + fyfy)

dirf = Mathatan(fyfx)angdirf = (dirf(22070))1800 Systemoutprintf(ldquoThe resultant force on particle [ldquo+ mi +rdquo] due to the other masses is

14e N 22f degnrdquofangdirf)dff = new javatextDecimalFormat(ldquoE00rdquo)dfangdirf = new DecimalFormat(ldquo000rdquo)

jTextField2setText(ldquordquo + mi) jTextField3setText(ldquordquo + dffformat(f)) jTextField4setText(ldquordquo + dfangdirfformat(angdirf))

Note

Brown = automatically generatedBlue = the original code from ldquoGravityAttract07ajavardquoGreen = coding from ldquoGravityAttract07ajavardquo which were being disabled due to

incompatibilities with NetbeansRed = a new inserted coding a few of them is to rephrasesubstitute the green one

Javascripts command line useBufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin))String mass=Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))try mass=dataInreadLine()catch(IOException e)Systemoutprintln(Not a valid input value)

int mi = IntegerparseInt(mass)

to read any input from the keyboard typed by the user while Netbeans uses egldquoIntegerparseInt(jTextField1getText())rdquo etc Javascripts command line uses ldquoSystemoutprintfrdquo to format the printed output whileNetbeans uses more complicated way to do the same job (see below)

Javascript command line

Systemoutprintf(The resultant force on particle [+ mi +] due to the other masses is 14e N22f degnfangdirf)

Netbeansdff = new javatextDecimalFormat(ldquo0E00rdquo)df di f D i lF t(ldquo0 00rdquo)

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3558

jTextField2setText(ldquordquo + mi) jTextField3setText(ldquordquo + dffformat(f)) jTextField4setText(ldquordquo + dfangdirfformat(angdirf))

ldquodffrdquo and ldquodfangdirfrdquo are new variables that should be declared and added to format thenumbers into decimal format ie ldquo0E00rdquo to get a scientific format eg ldquo20019E-10rdquo and ldquo000rdquoto get a decimal number eg ldquo-1570rdquo

To use DecimalFormat you have to put ldquoimport javatextDecimalFormatrdquo in the top of thecoding as shown in Figure 8

Figure 8

To reset the calculation you need to put coding into the ldquoresetrdquo button (this is ldquoJButton2rdquo)First click on the ldquoresetrdquo button right click choose Events and then choose Action thenactionPerformed (similar like in Figure 6) It will bring you to where you should insert your coding(Figure 9) Write the following coding (Figure 9)

Figure 9

To exit the program you need to put coding into the ldquoexitrdquo button (this is ldquoJButton3rdquo) Firstclick on the ldquoexitrdquo button right click choose Events and then choose Action then actionPerformed(similar like in Figure 6) It will bring you to where you should insert your coding (Figure 10) Writethe following coding (Figure 10)

Figure 10

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3658

Once you finished now you can run and see the result If everything is fine then the output wilbe look like this (Figure 11)

Figure 11

Remember when you compile and run ldquoGravityAttract07ajavardquo using Javascripts commandline once you compile and run the result was looked like this

On which particle you want to calculate the resulting force 2The resultant force on particle [2] due to the other masses is 30673e-10 N 8837 deg

SEE THE DIFFERENCES AT THE USER INTERFACE

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3758

8th Step

Written by Mohd Khalid Kasmin

Monday 27 August 2007

Programming using command line interface (CLI) has its advantages but todays computer users are so used to graphical user

interface (GUI) that we just can no longer be contented with writing application that do not use the GUI Hence starting from this 8thstep we are going to embark on learning how to make java application that utilises the the GUI

It is possible to write a GUI application with the text based tools that we have been using for these past 7 steps but the job is so

messy that most of you will definitely be turnoff by it So to make life slightly easier well use an integrated development

environment (IDE) known as NetBeans specifically NetBeans 551

0 First ssh to the our server Next once logged in just type nice -n15 netbeans amp on the terminal If you had sshed with the -Y

switch after a few tens of seconds NetBeans should emerge on your display screen

One important point to note Since our server can readily handle up to 100 users (maybe even

more) doing work in CLI mode this machine will certainly be taxed to its maximum capability once

we start using graphical based application NetBeans 551 uses a geat deal of computer memory

and with the 4Gbytes RAM server it can only accomodate up to 25 users concurrently using

NetBeans if the users use the application wisely What is meant by wisely here is that (a) when

starting the NetBeans application please use it together with the nice -n15 command (b) in using

the NetBeans please make sure you only have one project open at a time and (c) please close your

project everytime before quitting the NetBeans

Home Profile Teaching Links Contact Me My Work

Newsflash

Yesterday all servers in the US went out on strike in a bid to get more RAM and better CPUs A spokes person said that the

need for better RAM was due to some fool increasing the front-side bus speed In future busses will be told to slow down in

residential motherboards

search

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3858

1

2

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3958

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4058

[ Back ]

5

Last Updated ( Wednesday 31 October 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4158

8th Step (Cont)

Written by Mohd Khalid Kasmin

Monday 27 August 2007

Up to this point we have define the name of our project Next we construct a class and to follow what we already know we name the class the sam

project name latihan01

6

7

Home Profile Teaching Links Contact Me My Work

Newsflash

Joomla 10 - Experience the Freedom It has never been easier to create your own dynamic site Manage all your content from

the best CMS admin interface

search

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4258

8

9

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4358

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4458

8th Step (Cont still)

Written by Mohd Khalid Kasmin

Monday 27 August 2007

In item (9) we see that Netbeans does not provide us with the main method So to follow what we already know we now put in the

main method

10

Home Profile Teaching Links Contact Me My Work

Newsflash

Aoccdrnig to a rscheearch at an Elingsh uinervtisy it deosnt mttaer in waht oredr the ltteers in a wrod are the olny iprmoetnt

tihng is taht frist and lsat ltteer is at the rghit pclae The rset can be a toatl mses and you can sitll raed it wouthit porbelm Tihs is

bcuseae we do not raed ervey lteter by itslef but the wrod as a wlohe

search

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4558

11

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4658

12

13

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4758

14

15

Last Updated ( Monday 27 August 2007 )

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4858

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4958

9th Step

Thursday 06 September 2007

Step by Step into Java Programming

for

University Physics Student

By

Mohd Khalid Kasmin

A Building graphical user interface - preparing the JFrame Form

At this stage of our programming experience with Java and NetBeans please follow strictly the steps outline in this article First and

make a new GUI project make sure there is no other project that already been loaded by Netbeans Please close any project displayed

1 Create new project

- On the main menu click on File --gt New Project (note Close first any previous project)

- Specify project name amp project location - Untick Create Main Class item

- Click on the Finish button

Home Profile Teaching Links Contact Me My Work

Newsflash

Aoccdrnig to a rscheearch at an Elingsh uinervtisy it deosnt mttaer in waht oredr the ltteers in a wrod are the olny iprmoetnt

tihng is taht frist and lsat ltteer is at the rghit pclae The rset can be a toatl mses and you can sitll raed it wouthit porbelm Tihs is

bcuseae we do not raed ervey lteter by itslef but the wrod as a wlohe

search

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5058

Figure 91

2 Create UI class

- Right click on the project name in the Project pane

on the popup menu select New --gt JFrame Form

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5158

Figure 92

- In the New JFrame Form dialog specify the Class Name and the Package name

eg Class Name multiplierUI Package mymultiplier

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5258

Figure 93

After clicking on the Finish button we should get something like the following

Figure 94

Last Updated ( Tuesday 18 September 2007 )

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5358

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5458

9th Step (Cont)

Written by Mohd Khalid Kasmin

Tuesday 11 September 2007

Step by Step into Java Programming

for University Physics Student

By

Mohd Khalid Kasmin

B Building user interface (UI) - putting in the components

Now that we have a proper blank form we can start filling the form with the suitable graphical components For introduction to

GUI programming an appropriate application to build (for a physics student) is a simple solver So our first exercise is to build an

application that takes two numbers and multiply them and display the result

With GUI we usually give input values to an application via input boxes that are labelled When all inputs are typed in properlywe click on a button to ask the application to execute the job Hence in this exercise well do just that The target now is to

construct a user interface that looks like Figure 95 below

Figure 95

Home Profile Teaching Links Contact Me My Work

Newsflash

Joomla 10 - Experience the Freedom It has never been easier to create your own dynamic site Manage all your content from

the best CMS admin interface

search

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5558

[ Back ]

1 Problemapplication description box

- In the palette pane click on Swing component named JTextPane (Note click just once) Move the mouse pointer on top of

your JFrame (Note just move not drag) When you see a little white box with orange border position it somewhere near the

top part of your JFrame and click once The JTextPane is then attached on the JFrame Addjust its size with the help of the

sizing hook around the perimeter of the JTextPane

- Edit the text property of the JTextPane with the description of your application

2 Input boxes

- In the palette pane click on Swing component named JTextField Next move the mouse pointer on top of your JFrame

When you see a white box with the word jTextField1 position it to the left and under the JTextPane1

- Click on Swing component JTextField in the palette pane again Move the mouse pointer on top of the JFrame you should

get a second JTextField named jTextField2 Position it to the right of jTextField1

Last Updated ( Tuesday 11 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5658

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5758

multiplication if a b and c were first declared as double or float or int variables So the code of the calculation should look like the

following

double a b c

a =

b =

c = a b

Clearly variables a and b need to be assigned with values of typed double Now looking back at inputboxes A and B we made

those boxes using the JTextField component meaning that any data or value written in a JTextField is of typed Text or String even

if the value displayed in the box is ldquo125rdquo But f irst here there is this task of getting to that Text (or String) values ldquo125rdquo and ldquo72rdquo

In Java we can get to a text in JTextField component using the getText() method So to retrieve values in jTextField1 and

jTextField2 we can use

jTextField1getText()

and

jTextField2getText()

But those values are still in text mode and we need another method to convert the data mode f rom text to double For that we can

use the method DoubleparseDouble() Thus the whole process of retrieve text -gt convert text to double -gt assign to double

variable can be done using

a = DoubleparseDouble( jTextField1getText() )

and

b = DoubleparseDouble( jTextField2getText() )

Hence the code for performing the multiplication becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

Up to this point we have the variable c as the result of the multiplication of a and b So the next thing to do is to display this

result on the JLabel that we have already prepared before

A x B = _____

The text A x B = ______ was made using JLabel component and its variable name is jLabel3 Since the variable c is of type

double and text property of JLabel is of type String we need to convert it first to String data type The method to convert a double

to String is DoubletoString() So we can write the code as DoubletoString(c) After getting the text we can now use it to be the

text displayed on jLabel3 To do that we use the method setText() Hence the appropriate code for displaying the result is

jLabel3setText( A x B = + DoubletoString(c) )

And the full code for our calculation becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

jLabel3setText( A x B = + DoubletoString(c) )

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5858

[ Back ]

Last Updated ( Tuesday 18 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

Page 14: Computional Physics

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1458

983107983119983117983120983125983124983105983124983113983119983118983105983116 983120983112983129983123983113983107983123

983080983123983123983120983090983089983090983090983081

983109983157983148983137983154 983149983141983156983144983151983140

983138983161

983105983138983140983086 983115983144983137983149983145983149 983113983155983149983137983145983148

983117983151983144983140 983115983144983137983148983145983140 983115983137983155983149983145983150

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1558

Free fall body problem

Consider an object thrown upward under gravitational force ( f=mg) with an initial velocity v0The object goes up and then stop at maximum y position ymax before it start to fall downback to the original position at y0 and then to the ground level (See figure)

ymdt

yd m

dt

dvmmaF ampamp====

2

2

t

Y

v1 (t1y1)

v2 (t2y2)

v4 (t4y4)

v5 (t5y5)

vn (tnyn)

v3 (t3y3)

v0 (t0y0)

∆t∆t ∆t ∆t

y1

y0

∆t ∆t

∆yv0

a = g

y0

t0

t0 t1 t2 t3 t4 t5 tn

ymax

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1658

Exact theoretical solution

( ) 2

21

00 at t v yt y ++=

The y projection of the exact solution can be plotted using the following java source code

Java class for solving free fall object under gravity (g = 98 mss)Exact solution y = y0 + v0t + 12at^2

public class freefallsol

public static void main(String args[])double av0yy0deltt

a = -98y0 = 10v0 = 10delt = 00001for(t=0 tlt=05 t=t+delt)

y = y0 + v0t + 05attSystemoutprintf(84f 84fnty)

Complile freefallsoljava to get the plot of the exact solution and then run and save theoutput into a file name as follows

[khamimsetpar65 ~]$ java freefallsol gtffsoldat

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1758

Euler Method

There is another way to see the projection of y position of the object using a numericalmethod called ldquoEuler methodrdquo See the following derivation

t avt v y

t

va

t

yv

∆=∆∆=∆

∆=

∆=

t v y y y y

t avvvv

t v y y y y

t avvvv

t v y y y y

t avvvv

nnnn

nnn

∆+=∆+=

∆+=∆+=

∆+=∆+=

∆+=∆+=

∆+=∆+=

∆+=∆+=

minus

minusminus

1

11

2112

112

1001

001

M

if ∆∆∆∆t rarrrarrrarrrarr 0 then y will close to the exact solution ( ( ) 2

21

00 at t v yt y ++= )

The algorithm 1 Declare the classmethod to be used

2 Declare and assign all variables to be used av0v1yy0y1deltt and j 3 Initialize value to all respective variables a = -98 y0 = 10 v0 = 10

4 Select appropriate value for ∆t

∆t = 00055 Start the loop to calculate the velocity and position for t=0

6 Calculate velocity v1 = v0 + ∆v = v0 + a ∆t

7 Calculate position y1 = y0 + ∆y = y0 + v1 ∆t8 Print out values of t and y1 9 Swap v0 = v1 and y0 = y1

10 Repeat from step 5 and increase the value of t by ∆t (t =t + ∆t) until stoppingcondition is satisfied at t=05

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1858

The java code

Java class for solving free fall object under gravity (g = 98 mss)by numerical method called ldquoEulers Methodrdquo

public class freefalleuler

public static void main(String args[])double av0v1yy0y1delttint j

a = -98y0 = 10v0 = 10delt = 0005

for(t=0 tlt=05 t =t + delt)v1 = v0 + adelty1 = y0 + v1deltSystemoutprintf(84f 84fnty1)v0 = v1y0 = y1

Complile freefalleulerjava for different delt ie 00005 0005 005 and then run and savethe output into a file name as follows

[khamimsetpar65 ~]$ java freefalleuler gtffeuler00005dat[khamimsetpar65 ~]$ java freefalleuler gtffeuler0005dat[khamimsetpar65 ~]$ java freefalleuler gtffeuler005dat

Plotting the results using gnuplot

Using gnuplot plot all the graphs using the following command and compare the results

gnuplotgt plot ffeuler00005dat using 12 title euler delt=00005 with linegt ffeuler0005dat using 12 title euler delt=0005 with linegt ffeuler005dat using 12 title euler deltt=005 with linegt ffsoldat using 12 title exact solution with line

In MS windows

gnuplotgt plot ffeuler00005dat using 12 title euler delt=00005 with lineffeuler0005dat using 12 title euler delt=0005 with lineffeuler005dat using 12 titleeuler delt=005 with line ffsoldat using 12 title exact solution with line

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1958

Gnuplot

The smaller the delt the closer the curve to the exact solution

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2058

983107983119983117983120983125983124983105983124983113983119983118983105983116 983120983112983129983123983113983107983123

983080983123983123983120983090983089983090983090983081

983113983149983152983154983151983158983141983140 983109983157983148983137983154 983149983141983156983144983151983140

983138983161

983105983138983140983086 983115983144983137983149983145983149 983113983155983149983137983145983148

983117983151983144983140 983115983144983137983148983145983140 983115983137983155983149983145983150

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2158

Improved Euler method

Comparison between analytical solution of F=ma with the Euler method shows that although the

method is able to find approximate solution the error is quite significant if computational step is

reduced The error is also cumulative ie as the iteration of the calculation increases the error also gets

bigger hence value furthest away from initial condition has the largest error

The Euler method gives us a way to approximate positions of object with respect to time but requires

an order of magnitude of computational iterations for each order of magnitude of accuracy For large

number of objects this method will certainly consume a great deal of computation time Hence

students are introduced to Initial Half-Step Method an Euler method with a much improved

computational effeciency

Source code

Study of motion of an object with mass m

experiencing a constant force F using

Improved Euler Method (Initial half step method)

In X and Y dimension

import static javalangMath

public class fma04

public static void main(String args[])

double m fx fy x y vo vx vy ax ay t delt

double pi th theta ymax tmax xmax

pi = 40atan(10)

m = 01 in kg

fy = -02 in N (pulling down force)

fx = 00 in N (retarding force)

ay = fym

ax = fxm

vo = 500 in ms

theta = 300 in degrees

th = theta(pi1800) degrees change to radian

vx = vo cos(th) in ms vy = vo sin(th) in ms

y = 12 in m

x = 00 in m

ymax=y initialize var ymax with initial value of y

xmax=00

tmax=00

delt = 001 in s

for(t=00 tlt=300 t=t+delt)

System outprintf(f f fntxy)

if (t==00)vx = vx + 05axdelt

vy = vy + 05aydelt

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2258

else

vx = vx + axdelt

vy = vy + aydelt

x = x + vxdelt

y = y + vydelt

if (ygtymax)

ymax=y

xmax=xtmax=t

if (ylt=00) break

System outprintf( Value of Y maximum is f happened when t = f

nymaxtmax)

System outprintf( at x = fnxmax)

System outprintf( The object reach the ground at x=f and t=fn xt)

gnuplot result

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2358

983107983119983117983120983125983124983105983124983113983119983118983105983116

983120983112983129983123983113983107983123 983080983123983123983120983090983089983090983090983081

983122983141983155983156983151983154983145983150983143 983110983151983154983139983141

983138983161

983105983138983140983086 983115983144983137983149983145983149 983113983155983149983137983145983148

983117983151983144983140 983115983144983137983148983145983140 983115983137983155983149983145983150

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2458

Damped Harmonic Oscillation

Object m is on a flat surface attached to a spring as in figure above The object is pulled

to position A and released so that it oscillates about x =0 Assuming that the force F=ma= -bv-kx where x is position of the object v is the velocity k the spring constant andb is a constant associated with friction with the flat surface

The algorithm

1 Declare the classmethod to be used2 Declare and assign all variables to be used

x t delt k b A m F a v v0 and x03 Declare and assign constants to the respective variables

k = 02 b = 01 A = 01 m = 054 Select appropriate value for ∆t 5 Initialized initial values xo=A vo=00

6 set x=xo v=vo and t = 07 calculate the value of force F = -kx-bv 8 calculate the value of acceleration a = Fm

9 calculate velocity v = vo + ∆v = vo + a ∆t

10 calculate position x = xo + ∆x = xo + v ∆t

11 increment value of t by ∆t 12 print out values of t and x

13 swap the value of xo=x vo=v 14 repeat from step 4 until stopping condition is satisfied (tlt=600)

The java code

public class simplehar01

public static void main(String args[])double x t deltdouble k b A m F a v v0 x0

k = 02b = 01A = 0 1

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2558

m = 05delt = 0001v0 = 00x0 = Ax = x0 v = v0

t = 00while(tlt=600)

F = -kx-bva = Fmv = v0 + adeltx = x0 + vdeltt = t + deltSystemoutprintf(f fntx)x0 = xv0 = v

gnuplot result

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2658

983107983119983117983120983125983124983105983124983113983119983118983105983116 983120983112983129983123983113983107983123

(983123983123983120983090983089983090983090983081

983106983157983145983148983140983145983150983143 983137 983152983144983161983155983145983139983155 983155983151983148983158983141983154

983157983155983145983150983143 983118983141983156983138983141983137983150983155

983138983161

983105983138983140983086 983115983144983137983149983145983149 983113983155983149983137983145983148

983117983151983144983140 983115983144983137983148983145983140 983115983137983155983149983145983150

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2758

How to create a user interface (UI) using Netbeans

Based on the ldquoGravityAttract07ajavardquo source code (shown below) build a user interface usingNetbeans The design of the UI could be look like this (Figure1)

Figure 1

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2858

ldquoGravityAttract07ajavardquo source code

Java class for calculating the resultant force on any object(ie either on m1m2 m3 m4 or m5)

import static javalangMathimport javaiopublic class GravityAttract07a

public static void main(String args[])

double[] m = new double[6] mass of objectdouble[] x = new double[6] x-coor of objectdouble[] y = new double[6] y-coor of objectdouble f resultant forcedouble fxfy x amp y component of forcedouble rx ry rp co G dirfangdirf

int j

G = 667e-11m[1]=25 x[1]=12 y[1]=25m[2]=15 x[2]=15 y[2]=15m[3]=75 x[3]=02 y[3]=55m[4]=92 x[4]=52 y[4]=25m[5]=66 x[5]=42 y[5]=45BufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin))String mass=Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))try mass=dataInreadLine()

catch(IOException e)Systemoutprintln(Not a valid input value)int mi = IntegerparseInt(mass)fx = 00fy = 00

for(j=1 jlt=5 j++)

if (j=mi)rx = x[j]-x[mi]ry = y[j]-y[mi]rp = pow((rxrx+ryry)15) (rx^2 + ry^2) raised to the power of 32co = Gm[mi]m[j]

fx = fx + corxrpfy = fy + coryrp

f = sqrt(fxfx + fyfy)dirf = toDegrees(atan(fyfx))Systemoutprintf(The force on m[+ mi +] due to the other masses is eN fdegnfdirf)

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2958

This example will not show you step by step details procedure since it already explained inthe class and in the e-learning Please refer to the e-learning about how to create a project how tocreate JFrame Form and as well how to create JButton JTextField JLabel JTextPane and so on

First create a new project and give a name eg ldquogravityrdquo and then create JFrame Form andthe following JButton JTextField JLabel JTextPane etc by using ldquoswing paletterdquo as shown in Figure2

Figure 2

To insert a picture into JLabel2 click on the JLabel2 go to properties and select ldquoiconrdquo Onthe icon editor choose file and then select file to locate the directory of your graphicpicture file(Figure 3) Select the file and click OK when finished Make sure you have already created your ownpicture file and save it as jpg png bmp etc format eg ldquogravityjpgrdquo Use any graphic package todraw your diagrampicture or use drawing tools available in Open Office or Microsoft Word

JTextPane1JLabel1

JLabel2

JLabel3JLabel4

JLabel5

JLabel6 JLabel7

JLabel8

JTextField2 JTextField3 JTextField4

JTextF

JButton1JButton2

JButton3

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3058

Figure 3

To write documentation or question eg in JTextPane1 click on the JTextPane1 go toproperties and select ldquotextrdquo Type the documentation in the text editor as shown in Figure 4 Click OKwhen finished

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3158

To do formatting on the text such as superscript and subscript eg in jLabel3 click on the

JLabel3 go to properties and select ldquotextrdquo Type the documentationmessage on the text editor asshown in Figure 5 as follows

eg

lthtmlgtOn which particle (mltsubgt1ltsubgtmltsubgt5ltsubgt) you want to calculate theresultant force

Use sup instead if you want a superscriptThe result will look like this

Click OK when finished

Figure 5

To do the calculation you need to put the coding similar like ldquoGravityAttract07ajavardquo and put itinto the ldquocalculaterdquo button (this is ldquoJButton1rdquo) You can copy ldquoGravityAttract07a javardquo source code

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3258

paste it and then do a few alterations First click on the ldquocalculaterdquo button right click choose Eventsand then choose Action then actionPerformed (Figure 6) It will bring you to where you should insertyour coding (Figure 7) You can write your own coding or copypaste any coding from somewhereelse (in this case from ldquoGravityAttract07ajavardquo)

Figure 6

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3358

Figure 7

Copy ldquoGravityAttract07ajavardquo source code and paste to the place after ldquo TODO add youhandling code hererdquo see Figure 7 To see which part has been modified the coding in Figure 7 hasbeen color coded for better understanding as follows

private void jButton1ActionPerformed(javaawteventActionEvent evt) TODO add your handling code here

double[] m = new double[6] mass of objectdouble[] x = new double[6] x-coor of objectdouble[] y = new double[6] y-coor of objectdouble f resultant forcedouble fxfy x amp y component of forcedouble rx ry rp co G dirfangdirfDecimalFormat dff dfangdirfint j mi

G = 667e-11m[1]=25 x[1]=12 y[1]=25m[2]=15 x[2]=15 y[2]=15m[3]=75 x[3]=02 y[3]=55m[4]=92 x[4]=52 y[4]=25m[5]=66 x[5]=42 y[5]=45

BufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin)) String mass= Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3458

catch(IOException e)Systemoutprintln(Not a valid input value) int mi = IntegerparseInt(mass)mi = IntegerparseInt(jTextField1getText())fx = 00fy = 00for(j=1 jlt=5 j++)

if (j=mi)rx = x[j]-x[mi]ry = y[j]-y[mi]rp = Mathpow((rxrx+ryry)15) (rx^2 + ry^2) raised to the power of 32co = Gm[mi]m[j]fx = fx + corxrpfy = fy + coryrp

f = Mathsqrt(fxfx + fyfy)

dirf = Mathatan(fyfx)angdirf = (dirf(22070))1800 Systemoutprintf(ldquoThe resultant force on particle [ldquo+ mi +rdquo] due to the other masses is

14e N 22f degnrdquofangdirf)dff = new javatextDecimalFormat(ldquoE00rdquo)dfangdirf = new DecimalFormat(ldquo000rdquo)

jTextField2setText(ldquordquo + mi) jTextField3setText(ldquordquo + dffformat(f)) jTextField4setText(ldquordquo + dfangdirfformat(angdirf))

Note

Brown = automatically generatedBlue = the original code from ldquoGravityAttract07ajavardquoGreen = coding from ldquoGravityAttract07ajavardquo which were being disabled due to

incompatibilities with NetbeansRed = a new inserted coding a few of them is to rephrasesubstitute the green one

Javascripts command line useBufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin))String mass=Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))try mass=dataInreadLine()catch(IOException e)Systemoutprintln(Not a valid input value)

int mi = IntegerparseInt(mass)

to read any input from the keyboard typed by the user while Netbeans uses egldquoIntegerparseInt(jTextField1getText())rdquo etc Javascripts command line uses ldquoSystemoutprintfrdquo to format the printed output whileNetbeans uses more complicated way to do the same job (see below)

Javascript command line

Systemoutprintf(The resultant force on particle [+ mi +] due to the other masses is 14e N22f degnfangdirf)

Netbeansdff = new javatextDecimalFormat(ldquo0E00rdquo)df di f D i lF t(ldquo0 00rdquo)

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3558

jTextField2setText(ldquordquo + mi) jTextField3setText(ldquordquo + dffformat(f)) jTextField4setText(ldquordquo + dfangdirfformat(angdirf))

ldquodffrdquo and ldquodfangdirfrdquo are new variables that should be declared and added to format thenumbers into decimal format ie ldquo0E00rdquo to get a scientific format eg ldquo20019E-10rdquo and ldquo000rdquoto get a decimal number eg ldquo-1570rdquo

To use DecimalFormat you have to put ldquoimport javatextDecimalFormatrdquo in the top of thecoding as shown in Figure 8

Figure 8

To reset the calculation you need to put coding into the ldquoresetrdquo button (this is ldquoJButton2rdquo)First click on the ldquoresetrdquo button right click choose Events and then choose Action thenactionPerformed (similar like in Figure 6) It will bring you to where you should insert your coding(Figure 9) Write the following coding (Figure 9)

Figure 9

To exit the program you need to put coding into the ldquoexitrdquo button (this is ldquoJButton3rdquo) Firstclick on the ldquoexitrdquo button right click choose Events and then choose Action then actionPerformed(similar like in Figure 6) It will bring you to where you should insert your coding (Figure 10) Writethe following coding (Figure 10)

Figure 10

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3658

Once you finished now you can run and see the result If everything is fine then the output wilbe look like this (Figure 11)

Figure 11

Remember when you compile and run ldquoGravityAttract07ajavardquo using Javascripts commandline once you compile and run the result was looked like this

On which particle you want to calculate the resulting force 2The resultant force on particle [2] due to the other masses is 30673e-10 N 8837 deg

SEE THE DIFFERENCES AT THE USER INTERFACE

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3758

8th Step

Written by Mohd Khalid Kasmin

Monday 27 August 2007

Programming using command line interface (CLI) has its advantages but todays computer users are so used to graphical user

interface (GUI) that we just can no longer be contented with writing application that do not use the GUI Hence starting from this 8thstep we are going to embark on learning how to make java application that utilises the the GUI

It is possible to write a GUI application with the text based tools that we have been using for these past 7 steps but the job is so

messy that most of you will definitely be turnoff by it So to make life slightly easier well use an integrated development

environment (IDE) known as NetBeans specifically NetBeans 551

0 First ssh to the our server Next once logged in just type nice -n15 netbeans amp on the terminal If you had sshed with the -Y

switch after a few tens of seconds NetBeans should emerge on your display screen

One important point to note Since our server can readily handle up to 100 users (maybe even

more) doing work in CLI mode this machine will certainly be taxed to its maximum capability once

we start using graphical based application NetBeans 551 uses a geat deal of computer memory

and with the 4Gbytes RAM server it can only accomodate up to 25 users concurrently using

NetBeans if the users use the application wisely What is meant by wisely here is that (a) when

starting the NetBeans application please use it together with the nice -n15 command (b) in using

the NetBeans please make sure you only have one project open at a time and (c) please close your

project everytime before quitting the NetBeans

Home Profile Teaching Links Contact Me My Work

Newsflash

Yesterday all servers in the US went out on strike in a bid to get more RAM and better CPUs A spokes person said that the

need for better RAM was due to some fool increasing the front-side bus speed In future busses will be told to slow down in

residential motherboards

search

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3858

1

2

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3958

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4058

[ Back ]

5

Last Updated ( Wednesday 31 October 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4158

8th Step (Cont)

Written by Mohd Khalid Kasmin

Monday 27 August 2007

Up to this point we have define the name of our project Next we construct a class and to follow what we already know we name the class the sam

project name latihan01

6

7

Home Profile Teaching Links Contact Me My Work

Newsflash

Joomla 10 - Experience the Freedom It has never been easier to create your own dynamic site Manage all your content from

the best CMS admin interface

search

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4258

8

9

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4358

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4458

8th Step (Cont still)

Written by Mohd Khalid Kasmin

Monday 27 August 2007

In item (9) we see that Netbeans does not provide us with the main method So to follow what we already know we now put in the

main method

10

Home Profile Teaching Links Contact Me My Work

Newsflash

Aoccdrnig to a rscheearch at an Elingsh uinervtisy it deosnt mttaer in waht oredr the ltteers in a wrod are the olny iprmoetnt

tihng is taht frist and lsat ltteer is at the rghit pclae The rset can be a toatl mses and you can sitll raed it wouthit porbelm Tihs is

bcuseae we do not raed ervey lteter by itslef but the wrod as a wlohe

search

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4558

11

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4658

12

13

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4758

14

15

Last Updated ( Monday 27 August 2007 )

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4858

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4958

9th Step

Thursday 06 September 2007

Step by Step into Java Programming

for

University Physics Student

By

Mohd Khalid Kasmin

A Building graphical user interface - preparing the JFrame Form

At this stage of our programming experience with Java and NetBeans please follow strictly the steps outline in this article First and

make a new GUI project make sure there is no other project that already been loaded by Netbeans Please close any project displayed

1 Create new project

- On the main menu click on File --gt New Project (note Close first any previous project)

- Specify project name amp project location - Untick Create Main Class item

- Click on the Finish button

Home Profile Teaching Links Contact Me My Work

Newsflash

Aoccdrnig to a rscheearch at an Elingsh uinervtisy it deosnt mttaer in waht oredr the ltteers in a wrod are the olny iprmoetnt

tihng is taht frist and lsat ltteer is at the rghit pclae The rset can be a toatl mses and you can sitll raed it wouthit porbelm Tihs is

bcuseae we do not raed ervey lteter by itslef but the wrod as a wlohe

search

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5058

Figure 91

2 Create UI class

- Right click on the project name in the Project pane

on the popup menu select New --gt JFrame Form

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5158

Figure 92

- In the New JFrame Form dialog specify the Class Name and the Package name

eg Class Name multiplierUI Package mymultiplier

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5258

Figure 93

After clicking on the Finish button we should get something like the following

Figure 94

Last Updated ( Tuesday 18 September 2007 )

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5358

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5458

9th Step (Cont)

Written by Mohd Khalid Kasmin

Tuesday 11 September 2007

Step by Step into Java Programming

for University Physics Student

By

Mohd Khalid Kasmin

B Building user interface (UI) - putting in the components

Now that we have a proper blank form we can start filling the form with the suitable graphical components For introduction to

GUI programming an appropriate application to build (for a physics student) is a simple solver So our first exercise is to build an

application that takes two numbers and multiply them and display the result

With GUI we usually give input values to an application via input boxes that are labelled When all inputs are typed in properlywe click on a button to ask the application to execute the job Hence in this exercise well do just that The target now is to

construct a user interface that looks like Figure 95 below

Figure 95

Home Profile Teaching Links Contact Me My Work

Newsflash

Joomla 10 - Experience the Freedom It has never been easier to create your own dynamic site Manage all your content from

the best CMS admin interface

search

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5558

[ Back ]

1 Problemapplication description box

- In the palette pane click on Swing component named JTextPane (Note click just once) Move the mouse pointer on top of

your JFrame (Note just move not drag) When you see a little white box with orange border position it somewhere near the

top part of your JFrame and click once The JTextPane is then attached on the JFrame Addjust its size with the help of the

sizing hook around the perimeter of the JTextPane

- Edit the text property of the JTextPane with the description of your application

2 Input boxes

- In the palette pane click on Swing component named JTextField Next move the mouse pointer on top of your JFrame

When you see a white box with the word jTextField1 position it to the left and under the JTextPane1

- Click on Swing component JTextField in the palette pane again Move the mouse pointer on top of the JFrame you should

get a second JTextField named jTextField2 Position it to the right of jTextField1

Last Updated ( Tuesday 11 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5658

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5758

multiplication if a b and c were first declared as double or float or int variables So the code of the calculation should look like the

following

double a b c

a =

b =

c = a b

Clearly variables a and b need to be assigned with values of typed double Now looking back at inputboxes A and B we made

those boxes using the JTextField component meaning that any data or value written in a JTextField is of typed Text or String even

if the value displayed in the box is ldquo125rdquo But f irst here there is this task of getting to that Text (or String) values ldquo125rdquo and ldquo72rdquo

In Java we can get to a text in JTextField component using the getText() method So to retrieve values in jTextField1 and

jTextField2 we can use

jTextField1getText()

and

jTextField2getText()

But those values are still in text mode and we need another method to convert the data mode f rom text to double For that we can

use the method DoubleparseDouble() Thus the whole process of retrieve text -gt convert text to double -gt assign to double

variable can be done using

a = DoubleparseDouble( jTextField1getText() )

and

b = DoubleparseDouble( jTextField2getText() )

Hence the code for performing the multiplication becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

Up to this point we have the variable c as the result of the multiplication of a and b So the next thing to do is to display this

result on the JLabel that we have already prepared before

A x B = _____

The text A x B = ______ was made using JLabel component and its variable name is jLabel3 Since the variable c is of type

double and text property of JLabel is of type String we need to convert it first to String data type The method to convert a double

to String is DoubletoString() So we can write the code as DoubletoString(c) After getting the text we can now use it to be the

text displayed on jLabel3 To do that we use the method setText() Hence the appropriate code for displaying the result is

jLabel3setText( A x B = + DoubletoString(c) )

And the full code for our calculation becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

jLabel3setText( A x B = + DoubletoString(c) )

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5858

[ Back ]

Last Updated ( Tuesday 18 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

Page 15: Computional Physics

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1558

Free fall body problem

Consider an object thrown upward under gravitational force ( f=mg) with an initial velocity v0The object goes up and then stop at maximum y position ymax before it start to fall downback to the original position at y0 and then to the ground level (See figure)

ymdt

yd m

dt

dvmmaF ampamp====

2

2

t

Y

v1 (t1y1)

v2 (t2y2)

v4 (t4y4)

v5 (t5y5)

vn (tnyn)

v3 (t3y3)

v0 (t0y0)

∆t∆t ∆t ∆t

y1

y0

∆t ∆t

∆yv0

a = g

y0

t0

t0 t1 t2 t3 t4 t5 tn

ymax

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1658

Exact theoretical solution

( ) 2

21

00 at t v yt y ++=

The y projection of the exact solution can be plotted using the following java source code

Java class for solving free fall object under gravity (g = 98 mss)Exact solution y = y0 + v0t + 12at^2

public class freefallsol

public static void main(String args[])double av0yy0deltt

a = -98y0 = 10v0 = 10delt = 00001for(t=0 tlt=05 t=t+delt)

y = y0 + v0t + 05attSystemoutprintf(84f 84fnty)

Complile freefallsoljava to get the plot of the exact solution and then run and save theoutput into a file name as follows

[khamimsetpar65 ~]$ java freefallsol gtffsoldat

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1758

Euler Method

There is another way to see the projection of y position of the object using a numericalmethod called ldquoEuler methodrdquo See the following derivation

t avt v y

t

va

t

yv

∆=∆∆=∆

∆=

∆=

t v y y y y

t avvvv

t v y y y y

t avvvv

t v y y y y

t avvvv

nnnn

nnn

∆+=∆+=

∆+=∆+=

∆+=∆+=

∆+=∆+=

∆+=∆+=

∆+=∆+=

minus

minusminus

1

11

2112

112

1001

001

M

if ∆∆∆∆t rarrrarrrarrrarr 0 then y will close to the exact solution ( ( ) 2

21

00 at t v yt y ++= )

The algorithm 1 Declare the classmethod to be used

2 Declare and assign all variables to be used av0v1yy0y1deltt and j 3 Initialize value to all respective variables a = -98 y0 = 10 v0 = 10

4 Select appropriate value for ∆t

∆t = 00055 Start the loop to calculate the velocity and position for t=0

6 Calculate velocity v1 = v0 + ∆v = v0 + a ∆t

7 Calculate position y1 = y0 + ∆y = y0 + v1 ∆t8 Print out values of t and y1 9 Swap v0 = v1 and y0 = y1

10 Repeat from step 5 and increase the value of t by ∆t (t =t + ∆t) until stoppingcondition is satisfied at t=05

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1858

The java code

Java class for solving free fall object under gravity (g = 98 mss)by numerical method called ldquoEulers Methodrdquo

public class freefalleuler

public static void main(String args[])double av0v1yy0y1delttint j

a = -98y0 = 10v0 = 10delt = 0005

for(t=0 tlt=05 t =t + delt)v1 = v0 + adelty1 = y0 + v1deltSystemoutprintf(84f 84fnty1)v0 = v1y0 = y1

Complile freefalleulerjava for different delt ie 00005 0005 005 and then run and savethe output into a file name as follows

[khamimsetpar65 ~]$ java freefalleuler gtffeuler00005dat[khamimsetpar65 ~]$ java freefalleuler gtffeuler0005dat[khamimsetpar65 ~]$ java freefalleuler gtffeuler005dat

Plotting the results using gnuplot

Using gnuplot plot all the graphs using the following command and compare the results

gnuplotgt plot ffeuler00005dat using 12 title euler delt=00005 with linegt ffeuler0005dat using 12 title euler delt=0005 with linegt ffeuler005dat using 12 title euler deltt=005 with linegt ffsoldat using 12 title exact solution with line

In MS windows

gnuplotgt plot ffeuler00005dat using 12 title euler delt=00005 with lineffeuler0005dat using 12 title euler delt=0005 with lineffeuler005dat using 12 titleeuler delt=005 with line ffsoldat using 12 title exact solution with line

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1958

Gnuplot

The smaller the delt the closer the curve to the exact solution

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2058

983107983119983117983120983125983124983105983124983113983119983118983105983116 983120983112983129983123983113983107983123

983080983123983123983120983090983089983090983090983081

983113983149983152983154983151983158983141983140 983109983157983148983137983154 983149983141983156983144983151983140

983138983161

983105983138983140983086 983115983144983137983149983145983149 983113983155983149983137983145983148

983117983151983144983140 983115983144983137983148983145983140 983115983137983155983149983145983150

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2158

Improved Euler method

Comparison between analytical solution of F=ma with the Euler method shows that although the

method is able to find approximate solution the error is quite significant if computational step is

reduced The error is also cumulative ie as the iteration of the calculation increases the error also gets

bigger hence value furthest away from initial condition has the largest error

The Euler method gives us a way to approximate positions of object with respect to time but requires

an order of magnitude of computational iterations for each order of magnitude of accuracy For large

number of objects this method will certainly consume a great deal of computation time Hence

students are introduced to Initial Half-Step Method an Euler method with a much improved

computational effeciency

Source code

Study of motion of an object with mass m

experiencing a constant force F using

Improved Euler Method (Initial half step method)

In X and Y dimension

import static javalangMath

public class fma04

public static void main(String args[])

double m fx fy x y vo vx vy ax ay t delt

double pi th theta ymax tmax xmax

pi = 40atan(10)

m = 01 in kg

fy = -02 in N (pulling down force)

fx = 00 in N (retarding force)

ay = fym

ax = fxm

vo = 500 in ms

theta = 300 in degrees

th = theta(pi1800) degrees change to radian

vx = vo cos(th) in ms vy = vo sin(th) in ms

y = 12 in m

x = 00 in m

ymax=y initialize var ymax with initial value of y

xmax=00

tmax=00

delt = 001 in s

for(t=00 tlt=300 t=t+delt)

System outprintf(f f fntxy)

if (t==00)vx = vx + 05axdelt

vy = vy + 05aydelt

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2258

else

vx = vx + axdelt

vy = vy + aydelt

x = x + vxdelt

y = y + vydelt

if (ygtymax)

ymax=y

xmax=xtmax=t

if (ylt=00) break

System outprintf( Value of Y maximum is f happened when t = f

nymaxtmax)

System outprintf( at x = fnxmax)

System outprintf( The object reach the ground at x=f and t=fn xt)

gnuplot result

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2358

983107983119983117983120983125983124983105983124983113983119983118983105983116

983120983112983129983123983113983107983123 983080983123983123983120983090983089983090983090983081

983122983141983155983156983151983154983145983150983143 983110983151983154983139983141

983138983161

983105983138983140983086 983115983144983137983149983145983149 983113983155983149983137983145983148

983117983151983144983140 983115983144983137983148983145983140 983115983137983155983149983145983150

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2458

Damped Harmonic Oscillation

Object m is on a flat surface attached to a spring as in figure above The object is pulled

to position A and released so that it oscillates about x =0 Assuming that the force F=ma= -bv-kx where x is position of the object v is the velocity k the spring constant andb is a constant associated with friction with the flat surface

The algorithm

1 Declare the classmethod to be used2 Declare and assign all variables to be used

x t delt k b A m F a v v0 and x03 Declare and assign constants to the respective variables

k = 02 b = 01 A = 01 m = 054 Select appropriate value for ∆t 5 Initialized initial values xo=A vo=00

6 set x=xo v=vo and t = 07 calculate the value of force F = -kx-bv 8 calculate the value of acceleration a = Fm

9 calculate velocity v = vo + ∆v = vo + a ∆t

10 calculate position x = xo + ∆x = xo + v ∆t

11 increment value of t by ∆t 12 print out values of t and x

13 swap the value of xo=x vo=v 14 repeat from step 4 until stopping condition is satisfied (tlt=600)

The java code

public class simplehar01

public static void main(String args[])double x t deltdouble k b A m F a v v0 x0

k = 02b = 01A = 0 1

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2558

m = 05delt = 0001v0 = 00x0 = Ax = x0 v = v0

t = 00while(tlt=600)

F = -kx-bva = Fmv = v0 + adeltx = x0 + vdeltt = t + deltSystemoutprintf(f fntx)x0 = xv0 = v

gnuplot result

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2658

983107983119983117983120983125983124983105983124983113983119983118983105983116 983120983112983129983123983113983107983123

(983123983123983120983090983089983090983090983081

983106983157983145983148983140983145983150983143 983137 983152983144983161983155983145983139983155 983155983151983148983158983141983154

983157983155983145983150983143 983118983141983156983138983141983137983150983155

983138983161

983105983138983140983086 983115983144983137983149983145983149 983113983155983149983137983145983148

983117983151983144983140 983115983144983137983148983145983140 983115983137983155983149983145983150

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2758

How to create a user interface (UI) using Netbeans

Based on the ldquoGravityAttract07ajavardquo source code (shown below) build a user interface usingNetbeans The design of the UI could be look like this (Figure1)

Figure 1

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2858

ldquoGravityAttract07ajavardquo source code

Java class for calculating the resultant force on any object(ie either on m1m2 m3 m4 or m5)

import static javalangMathimport javaiopublic class GravityAttract07a

public static void main(String args[])

double[] m = new double[6] mass of objectdouble[] x = new double[6] x-coor of objectdouble[] y = new double[6] y-coor of objectdouble f resultant forcedouble fxfy x amp y component of forcedouble rx ry rp co G dirfangdirf

int j

G = 667e-11m[1]=25 x[1]=12 y[1]=25m[2]=15 x[2]=15 y[2]=15m[3]=75 x[3]=02 y[3]=55m[4]=92 x[4]=52 y[4]=25m[5]=66 x[5]=42 y[5]=45BufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin))String mass=Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))try mass=dataInreadLine()

catch(IOException e)Systemoutprintln(Not a valid input value)int mi = IntegerparseInt(mass)fx = 00fy = 00

for(j=1 jlt=5 j++)

if (j=mi)rx = x[j]-x[mi]ry = y[j]-y[mi]rp = pow((rxrx+ryry)15) (rx^2 + ry^2) raised to the power of 32co = Gm[mi]m[j]

fx = fx + corxrpfy = fy + coryrp

f = sqrt(fxfx + fyfy)dirf = toDegrees(atan(fyfx))Systemoutprintf(The force on m[+ mi +] due to the other masses is eN fdegnfdirf)

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2958

This example will not show you step by step details procedure since it already explained inthe class and in the e-learning Please refer to the e-learning about how to create a project how tocreate JFrame Form and as well how to create JButton JTextField JLabel JTextPane and so on

First create a new project and give a name eg ldquogravityrdquo and then create JFrame Form andthe following JButton JTextField JLabel JTextPane etc by using ldquoswing paletterdquo as shown in Figure2

Figure 2

To insert a picture into JLabel2 click on the JLabel2 go to properties and select ldquoiconrdquo Onthe icon editor choose file and then select file to locate the directory of your graphicpicture file(Figure 3) Select the file and click OK when finished Make sure you have already created your ownpicture file and save it as jpg png bmp etc format eg ldquogravityjpgrdquo Use any graphic package todraw your diagrampicture or use drawing tools available in Open Office or Microsoft Word

JTextPane1JLabel1

JLabel2

JLabel3JLabel4

JLabel5

JLabel6 JLabel7

JLabel8

JTextField2 JTextField3 JTextField4

JTextF

JButton1JButton2

JButton3

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3058

Figure 3

To write documentation or question eg in JTextPane1 click on the JTextPane1 go toproperties and select ldquotextrdquo Type the documentation in the text editor as shown in Figure 4 Click OKwhen finished

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3158

To do formatting on the text such as superscript and subscript eg in jLabel3 click on the

JLabel3 go to properties and select ldquotextrdquo Type the documentationmessage on the text editor asshown in Figure 5 as follows

eg

lthtmlgtOn which particle (mltsubgt1ltsubgtmltsubgt5ltsubgt) you want to calculate theresultant force

Use sup instead if you want a superscriptThe result will look like this

Click OK when finished

Figure 5

To do the calculation you need to put the coding similar like ldquoGravityAttract07ajavardquo and put itinto the ldquocalculaterdquo button (this is ldquoJButton1rdquo) You can copy ldquoGravityAttract07a javardquo source code

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3258

paste it and then do a few alterations First click on the ldquocalculaterdquo button right click choose Eventsand then choose Action then actionPerformed (Figure 6) It will bring you to where you should insertyour coding (Figure 7) You can write your own coding or copypaste any coding from somewhereelse (in this case from ldquoGravityAttract07ajavardquo)

Figure 6

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3358

Figure 7

Copy ldquoGravityAttract07ajavardquo source code and paste to the place after ldquo TODO add youhandling code hererdquo see Figure 7 To see which part has been modified the coding in Figure 7 hasbeen color coded for better understanding as follows

private void jButton1ActionPerformed(javaawteventActionEvent evt) TODO add your handling code here

double[] m = new double[6] mass of objectdouble[] x = new double[6] x-coor of objectdouble[] y = new double[6] y-coor of objectdouble f resultant forcedouble fxfy x amp y component of forcedouble rx ry rp co G dirfangdirfDecimalFormat dff dfangdirfint j mi

G = 667e-11m[1]=25 x[1]=12 y[1]=25m[2]=15 x[2]=15 y[2]=15m[3]=75 x[3]=02 y[3]=55m[4]=92 x[4]=52 y[4]=25m[5]=66 x[5]=42 y[5]=45

BufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin)) String mass= Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3458

catch(IOException e)Systemoutprintln(Not a valid input value) int mi = IntegerparseInt(mass)mi = IntegerparseInt(jTextField1getText())fx = 00fy = 00for(j=1 jlt=5 j++)

if (j=mi)rx = x[j]-x[mi]ry = y[j]-y[mi]rp = Mathpow((rxrx+ryry)15) (rx^2 + ry^2) raised to the power of 32co = Gm[mi]m[j]fx = fx + corxrpfy = fy + coryrp

f = Mathsqrt(fxfx + fyfy)

dirf = Mathatan(fyfx)angdirf = (dirf(22070))1800 Systemoutprintf(ldquoThe resultant force on particle [ldquo+ mi +rdquo] due to the other masses is

14e N 22f degnrdquofangdirf)dff = new javatextDecimalFormat(ldquoE00rdquo)dfangdirf = new DecimalFormat(ldquo000rdquo)

jTextField2setText(ldquordquo + mi) jTextField3setText(ldquordquo + dffformat(f)) jTextField4setText(ldquordquo + dfangdirfformat(angdirf))

Note

Brown = automatically generatedBlue = the original code from ldquoGravityAttract07ajavardquoGreen = coding from ldquoGravityAttract07ajavardquo which were being disabled due to

incompatibilities with NetbeansRed = a new inserted coding a few of them is to rephrasesubstitute the green one

Javascripts command line useBufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin))String mass=Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))try mass=dataInreadLine()catch(IOException e)Systemoutprintln(Not a valid input value)

int mi = IntegerparseInt(mass)

to read any input from the keyboard typed by the user while Netbeans uses egldquoIntegerparseInt(jTextField1getText())rdquo etc Javascripts command line uses ldquoSystemoutprintfrdquo to format the printed output whileNetbeans uses more complicated way to do the same job (see below)

Javascript command line

Systemoutprintf(The resultant force on particle [+ mi +] due to the other masses is 14e N22f degnfangdirf)

Netbeansdff = new javatextDecimalFormat(ldquo0E00rdquo)df di f D i lF t(ldquo0 00rdquo)

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3558

jTextField2setText(ldquordquo + mi) jTextField3setText(ldquordquo + dffformat(f)) jTextField4setText(ldquordquo + dfangdirfformat(angdirf))

ldquodffrdquo and ldquodfangdirfrdquo are new variables that should be declared and added to format thenumbers into decimal format ie ldquo0E00rdquo to get a scientific format eg ldquo20019E-10rdquo and ldquo000rdquoto get a decimal number eg ldquo-1570rdquo

To use DecimalFormat you have to put ldquoimport javatextDecimalFormatrdquo in the top of thecoding as shown in Figure 8

Figure 8

To reset the calculation you need to put coding into the ldquoresetrdquo button (this is ldquoJButton2rdquo)First click on the ldquoresetrdquo button right click choose Events and then choose Action thenactionPerformed (similar like in Figure 6) It will bring you to where you should insert your coding(Figure 9) Write the following coding (Figure 9)

Figure 9

To exit the program you need to put coding into the ldquoexitrdquo button (this is ldquoJButton3rdquo) Firstclick on the ldquoexitrdquo button right click choose Events and then choose Action then actionPerformed(similar like in Figure 6) It will bring you to where you should insert your coding (Figure 10) Writethe following coding (Figure 10)

Figure 10

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3658

Once you finished now you can run and see the result If everything is fine then the output wilbe look like this (Figure 11)

Figure 11

Remember when you compile and run ldquoGravityAttract07ajavardquo using Javascripts commandline once you compile and run the result was looked like this

On which particle you want to calculate the resulting force 2The resultant force on particle [2] due to the other masses is 30673e-10 N 8837 deg

SEE THE DIFFERENCES AT THE USER INTERFACE

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3758

8th Step

Written by Mohd Khalid Kasmin

Monday 27 August 2007

Programming using command line interface (CLI) has its advantages but todays computer users are so used to graphical user

interface (GUI) that we just can no longer be contented with writing application that do not use the GUI Hence starting from this 8thstep we are going to embark on learning how to make java application that utilises the the GUI

It is possible to write a GUI application with the text based tools that we have been using for these past 7 steps but the job is so

messy that most of you will definitely be turnoff by it So to make life slightly easier well use an integrated development

environment (IDE) known as NetBeans specifically NetBeans 551

0 First ssh to the our server Next once logged in just type nice -n15 netbeans amp on the terminal If you had sshed with the -Y

switch after a few tens of seconds NetBeans should emerge on your display screen

One important point to note Since our server can readily handle up to 100 users (maybe even

more) doing work in CLI mode this machine will certainly be taxed to its maximum capability once

we start using graphical based application NetBeans 551 uses a geat deal of computer memory

and with the 4Gbytes RAM server it can only accomodate up to 25 users concurrently using

NetBeans if the users use the application wisely What is meant by wisely here is that (a) when

starting the NetBeans application please use it together with the nice -n15 command (b) in using

the NetBeans please make sure you only have one project open at a time and (c) please close your

project everytime before quitting the NetBeans

Home Profile Teaching Links Contact Me My Work

Newsflash

Yesterday all servers in the US went out on strike in a bid to get more RAM and better CPUs A spokes person said that the

need for better RAM was due to some fool increasing the front-side bus speed In future busses will be told to slow down in

residential motherboards

search

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3858

1

2

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3958

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4058

[ Back ]

5

Last Updated ( Wednesday 31 October 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4158

8th Step (Cont)

Written by Mohd Khalid Kasmin

Monday 27 August 2007

Up to this point we have define the name of our project Next we construct a class and to follow what we already know we name the class the sam

project name latihan01

6

7

Home Profile Teaching Links Contact Me My Work

Newsflash

Joomla 10 - Experience the Freedom It has never been easier to create your own dynamic site Manage all your content from

the best CMS admin interface

search

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4258

8

9

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4358

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4458

8th Step (Cont still)

Written by Mohd Khalid Kasmin

Monday 27 August 2007

In item (9) we see that Netbeans does not provide us with the main method So to follow what we already know we now put in the

main method

10

Home Profile Teaching Links Contact Me My Work

Newsflash

Aoccdrnig to a rscheearch at an Elingsh uinervtisy it deosnt mttaer in waht oredr the ltteers in a wrod are the olny iprmoetnt

tihng is taht frist and lsat ltteer is at the rghit pclae The rset can be a toatl mses and you can sitll raed it wouthit porbelm Tihs is

bcuseae we do not raed ervey lteter by itslef but the wrod as a wlohe

search

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4558

11

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4658

12

13

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4758

14

15

Last Updated ( Monday 27 August 2007 )

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4858

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4958

9th Step

Thursday 06 September 2007

Step by Step into Java Programming

for

University Physics Student

By

Mohd Khalid Kasmin

A Building graphical user interface - preparing the JFrame Form

At this stage of our programming experience with Java and NetBeans please follow strictly the steps outline in this article First and

make a new GUI project make sure there is no other project that already been loaded by Netbeans Please close any project displayed

1 Create new project

- On the main menu click on File --gt New Project (note Close first any previous project)

- Specify project name amp project location - Untick Create Main Class item

- Click on the Finish button

Home Profile Teaching Links Contact Me My Work

Newsflash

Aoccdrnig to a rscheearch at an Elingsh uinervtisy it deosnt mttaer in waht oredr the ltteers in a wrod are the olny iprmoetnt

tihng is taht frist and lsat ltteer is at the rghit pclae The rset can be a toatl mses and you can sitll raed it wouthit porbelm Tihs is

bcuseae we do not raed ervey lteter by itslef but the wrod as a wlohe

search

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5058

Figure 91

2 Create UI class

- Right click on the project name in the Project pane

on the popup menu select New --gt JFrame Form

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5158

Figure 92

- In the New JFrame Form dialog specify the Class Name and the Package name

eg Class Name multiplierUI Package mymultiplier

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5258

Figure 93

After clicking on the Finish button we should get something like the following

Figure 94

Last Updated ( Tuesday 18 September 2007 )

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5358

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5458

9th Step (Cont)

Written by Mohd Khalid Kasmin

Tuesday 11 September 2007

Step by Step into Java Programming

for University Physics Student

By

Mohd Khalid Kasmin

B Building user interface (UI) - putting in the components

Now that we have a proper blank form we can start filling the form with the suitable graphical components For introduction to

GUI programming an appropriate application to build (for a physics student) is a simple solver So our first exercise is to build an

application that takes two numbers and multiply them and display the result

With GUI we usually give input values to an application via input boxes that are labelled When all inputs are typed in properlywe click on a button to ask the application to execute the job Hence in this exercise well do just that The target now is to

construct a user interface that looks like Figure 95 below

Figure 95

Home Profile Teaching Links Contact Me My Work

Newsflash

Joomla 10 - Experience the Freedom It has never been easier to create your own dynamic site Manage all your content from

the best CMS admin interface

search

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5558

[ Back ]

1 Problemapplication description box

- In the palette pane click on Swing component named JTextPane (Note click just once) Move the mouse pointer on top of

your JFrame (Note just move not drag) When you see a little white box with orange border position it somewhere near the

top part of your JFrame and click once The JTextPane is then attached on the JFrame Addjust its size with the help of the

sizing hook around the perimeter of the JTextPane

- Edit the text property of the JTextPane with the description of your application

2 Input boxes

- In the palette pane click on Swing component named JTextField Next move the mouse pointer on top of your JFrame

When you see a white box with the word jTextField1 position it to the left and under the JTextPane1

- Click on Swing component JTextField in the palette pane again Move the mouse pointer on top of the JFrame you should

get a second JTextField named jTextField2 Position it to the right of jTextField1

Last Updated ( Tuesday 11 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5658

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5758

multiplication if a b and c were first declared as double or float or int variables So the code of the calculation should look like the

following

double a b c

a =

b =

c = a b

Clearly variables a and b need to be assigned with values of typed double Now looking back at inputboxes A and B we made

those boxes using the JTextField component meaning that any data or value written in a JTextField is of typed Text or String even

if the value displayed in the box is ldquo125rdquo But f irst here there is this task of getting to that Text (or String) values ldquo125rdquo and ldquo72rdquo

In Java we can get to a text in JTextField component using the getText() method So to retrieve values in jTextField1 and

jTextField2 we can use

jTextField1getText()

and

jTextField2getText()

But those values are still in text mode and we need another method to convert the data mode f rom text to double For that we can

use the method DoubleparseDouble() Thus the whole process of retrieve text -gt convert text to double -gt assign to double

variable can be done using

a = DoubleparseDouble( jTextField1getText() )

and

b = DoubleparseDouble( jTextField2getText() )

Hence the code for performing the multiplication becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

Up to this point we have the variable c as the result of the multiplication of a and b So the next thing to do is to display this

result on the JLabel that we have already prepared before

A x B = _____

The text A x B = ______ was made using JLabel component and its variable name is jLabel3 Since the variable c is of type

double and text property of JLabel is of type String we need to convert it first to String data type The method to convert a double

to String is DoubletoString() So we can write the code as DoubletoString(c) After getting the text we can now use it to be the

text displayed on jLabel3 To do that we use the method setText() Hence the appropriate code for displaying the result is

jLabel3setText( A x B = + DoubletoString(c) )

And the full code for our calculation becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

jLabel3setText( A x B = + DoubletoString(c) )

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5858

[ Back ]

Last Updated ( Tuesday 18 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

Page 16: Computional Physics

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1658

Exact theoretical solution

( ) 2

21

00 at t v yt y ++=

The y projection of the exact solution can be plotted using the following java source code

Java class for solving free fall object under gravity (g = 98 mss)Exact solution y = y0 + v0t + 12at^2

public class freefallsol

public static void main(String args[])double av0yy0deltt

a = -98y0 = 10v0 = 10delt = 00001for(t=0 tlt=05 t=t+delt)

y = y0 + v0t + 05attSystemoutprintf(84f 84fnty)

Complile freefallsoljava to get the plot of the exact solution and then run and save theoutput into a file name as follows

[khamimsetpar65 ~]$ java freefallsol gtffsoldat

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1758

Euler Method

There is another way to see the projection of y position of the object using a numericalmethod called ldquoEuler methodrdquo See the following derivation

t avt v y

t

va

t

yv

∆=∆∆=∆

∆=

∆=

t v y y y y

t avvvv

t v y y y y

t avvvv

t v y y y y

t avvvv

nnnn

nnn

∆+=∆+=

∆+=∆+=

∆+=∆+=

∆+=∆+=

∆+=∆+=

∆+=∆+=

minus

minusminus

1

11

2112

112

1001

001

M

if ∆∆∆∆t rarrrarrrarrrarr 0 then y will close to the exact solution ( ( ) 2

21

00 at t v yt y ++= )

The algorithm 1 Declare the classmethod to be used

2 Declare and assign all variables to be used av0v1yy0y1deltt and j 3 Initialize value to all respective variables a = -98 y0 = 10 v0 = 10

4 Select appropriate value for ∆t

∆t = 00055 Start the loop to calculate the velocity and position for t=0

6 Calculate velocity v1 = v0 + ∆v = v0 + a ∆t

7 Calculate position y1 = y0 + ∆y = y0 + v1 ∆t8 Print out values of t and y1 9 Swap v0 = v1 and y0 = y1

10 Repeat from step 5 and increase the value of t by ∆t (t =t + ∆t) until stoppingcondition is satisfied at t=05

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1858

The java code

Java class for solving free fall object under gravity (g = 98 mss)by numerical method called ldquoEulers Methodrdquo

public class freefalleuler

public static void main(String args[])double av0v1yy0y1delttint j

a = -98y0 = 10v0 = 10delt = 0005

for(t=0 tlt=05 t =t + delt)v1 = v0 + adelty1 = y0 + v1deltSystemoutprintf(84f 84fnty1)v0 = v1y0 = y1

Complile freefalleulerjava for different delt ie 00005 0005 005 and then run and savethe output into a file name as follows

[khamimsetpar65 ~]$ java freefalleuler gtffeuler00005dat[khamimsetpar65 ~]$ java freefalleuler gtffeuler0005dat[khamimsetpar65 ~]$ java freefalleuler gtffeuler005dat

Plotting the results using gnuplot

Using gnuplot plot all the graphs using the following command and compare the results

gnuplotgt plot ffeuler00005dat using 12 title euler delt=00005 with linegt ffeuler0005dat using 12 title euler delt=0005 with linegt ffeuler005dat using 12 title euler deltt=005 with linegt ffsoldat using 12 title exact solution with line

In MS windows

gnuplotgt plot ffeuler00005dat using 12 title euler delt=00005 with lineffeuler0005dat using 12 title euler delt=0005 with lineffeuler005dat using 12 titleeuler delt=005 with line ffsoldat using 12 title exact solution with line

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1958

Gnuplot

The smaller the delt the closer the curve to the exact solution

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2058

983107983119983117983120983125983124983105983124983113983119983118983105983116 983120983112983129983123983113983107983123

983080983123983123983120983090983089983090983090983081

983113983149983152983154983151983158983141983140 983109983157983148983137983154 983149983141983156983144983151983140

983138983161

983105983138983140983086 983115983144983137983149983145983149 983113983155983149983137983145983148

983117983151983144983140 983115983144983137983148983145983140 983115983137983155983149983145983150

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2158

Improved Euler method

Comparison between analytical solution of F=ma with the Euler method shows that although the

method is able to find approximate solution the error is quite significant if computational step is

reduced The error is also cumulative ie as the iteration of the calculation increases the error also gets

bigger hence value furthest away from initial condition has the largest error

The Euler method gives us a way to approximate positions of object with respect to time but requires

an order of magnitude of computational iterations for each order of magnitude of accuracy For large

number of objects this method will certainly consume a great deal of computation time Hence

students are introduced to Initial Half-Step Method an Euler method with a much improved

computational effeciency

Source code

Study of motion of an object with mass m

experiencing a constant force F using

Improved Euler Method (Initial half step method)

In X and Y dimension

import static javalangMath

public class fma04

public static void main(String args[])

double m fx fy x y vo vx vy ax ay t delt

double pi th theta ymax tmax xmax

pi = 40atan(10)

m = 01 in kg

fy = -02 in N (pulling down force)

fx = 00 in N (retarding force)

ay = fym

ax = fxm

vo = 500 in ms

theta = 300 in degrees

th = theta(pi1800) degrees change to radian

vx = vo cos(th) in ms vy = vo sin(th) in ms

y = 12 in m

x = 00 in m

ymax=y initialize var ymax with initial value of y

xmax=00

tmax=00

delt = 001 in s

for(t=00 tlt=300 t=t+delt)

System outprintf(f f fntxy)

if (t==00)vx = vx + 05axdelt

vy = vy + 05aydelt

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2258

else

vx = vx + axdelt

vy = vy + aydelt

x = x + vxdelt

y = y + vydelt

if (ygtymax)

ymax=y

xmax=xtmax=t

if (ylt=00) break

System outprintf( Value of Y maximum is f happened when t = f

nymaxtmax)

System outprintf( at x = fnxmax)

System outprintf( The object reach the ground at x=f and t=fn xt)

gnuplot result

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2358

983107983119983117983120983125983124983105983124983113983119983118983105983116

983120983112983129983123983113983107983123 983080983123983123983120983090983089983090983090983081

983122983141983155983156983151983154983145983150983143 983110983151983154983139983141

983138983161

983105983138983140983086 983115983144983137983149983145983149 983113983155983149983137983145983148

983117983151983144983140 983115983144983137983148983145983140 983115983137983155983149983145983150

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2458

Damped Harmonic Oscillation

Object m is on a flat surface attached to a spring as in figure above The object is pulled

to position A and released so that it oscillates about x =0 Assuming that the force F=ma= -bv-kx where x is position of the object v is the velocity k the spring constant andb is a constant associated with friction with the flat surface

The algorithm

1 Declare the classmethod to be used2 Declare and assign all variables to be used

x t delt k b A m F a v v0 and x03 Declare and assign constants to the respective variables

k = 02 b = 01 A = 01 m = 054 Select appropriate value for ∆t 5 Initialized initial values xo=A vo=00

6 set x=xo v=vo and t = 07 calculate the value of force F = -kx-bv 8 calculate the value of acceleration a = Fm

9 calculate velocity v = vo + ∆v = vo + a ∆t

10 calculate position x = xo + ∆x = xo + v ∆t

11 increment value of t by ∆t 12 print out values of t and x

13 swap the value of xo=x vo=v 14 repeat from step 4 until stopping condition is satisfied (tlt=600)

The java code

public class simplehar01

public static void main(String args[])double x t deltdouble k b A m F a v v0 x0

k = 02b = 01A = 0 1

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2558

m = 05delt = 0001v0 = 00x0 = Ax = x0 v = v0

t = 00while(tlt=600)

F = -kx-bva = Fmv = v0 + adeltx = x0 + vdeltt = t + deltSystemoutprintf(f fntx)x0 = xv0 = v

gnuplot result

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2658

983107983119983117983120983125983124983105983124983113983119983118983105983116 983120983112983129983123983113983107983123

(983123983123983120983090983089983090983090983081

983106983157983145983148983140983145983150983143 983137 983152983144983161983155983145983139983155 983155983151983148983158983141983154

983157983155983145983150983143 983118983141983156983138983141983137983150983155

983138983161

983105983138983140983086 983115983144983137983149983145983149 983113983155983149983137983145983148

983117983151983144983140 983115983144983137983148983145983140 983115983137983155983149983145983150

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2758

How to create a user interface (UI) using Netbeans

Based on the ldquoGravityAttract07ajavardquo source code (shown below) build a user interface usingNetbeans The design of the UI could be look like this (Figure1)

Figure 1

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2858

ldquoGravityAttract07ajavardquo source code

Java class for calculating the resultant force on any object(ie either on m1m2 m3 m4 or m5)

import static javalangMathimport javaiopublic class GravityAttract07a

public static void main(String args[])

double[] m = new double[6] mass of objectdouble[] x = new double[6] x-coor of objectdouble[] y = new double[6] y-coor of objectdouble f resultant forcedouble fxfy x amp y component of forcedouble rx ry rp co G dirfangdirf

int j

G = 667e-11m[1]=25 x[1]=12 y[1]=25m[2]=15 x[2]=15 y[2]=15m[3]=75 x[3]=02 y[3]=55m[4]=92 x[4]=52 y[4]=25m[5]=66 x[5]=42 y[5]=45BufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin))String mass=Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))try mass=dataInreadLine()

catch(IOException e)Systemoutprintln(Not a valid input value)int mi = IntegerparseInt(mass)fx = 00fy = 00

for(j=1 jlt=5 j++)

if (j=mi)rx = x[j]-x[mi]ry = y[j]-y[mi]rp = pow((rxrx+ryry)15) (rx^2 + ry^2) raised to the power of 32co = Gm[mi]m[j]

fx = fx + corxrpfy = fy + coryrp

f = sqrt(fxfx + fyfy)dirf = toDegrees(atan(fyfx))Systemoutprintf(The force on m[+ mi +] due to the other masses is eN fdegnfdirf)

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2958

This example will not show you step by step details procedure since it already explained inthe class and in the e-learning Please refer to the e-learning about how to create a project how tocreate JFrame Form and as well how to create JButton JTextField JLabel JTextPane and so on

First create a new project and give a name eg ldquogravityrdquo and then create JFrame Form andthe following JButton JTextField JLabel JTextPane etc by using ldquoswing paletterdquo as shown in Figure2

Figure 2

To insert a picture into JLabel2 click on the JLabel2 go to properties and select ldquoiconrdquo Onthe icon editor choose file and then select file to locate the directory of your graphicpicture file(Figure 3) Select the file and click OK when finished Make sure you have already created your ownpicture file and save it as jpg png bmp etc format eg ldquogravityjpgrdquo Use any graphic package todraw your diagrampicture or use drawing tools available in Open Office or Microsoft Word

JTextPane1JLabel1

JLabel2

JLabel3JLabel4

JLabel5

JLabel6 JLabel7

JLabel8

JTextField2 JTextField3 JTextField4

JTextF

JButton1JButton2

JButton3

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3058

Figure 3

To write documentation or question eg in JTextPane1 click on the JTextPane1 go toproperties and select ldquotextrdquo Type the documentation in the text editor as shown in Figure 4 Click OKwhen finished

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3158

To do formatting on the text such as superscript and subscript eg in jLabel3 click on the

JLabel3 go to properties and select ldquotextrdquo Type the documentationmessage on the text editor asshown in Figure 5 as follows

eg

lthtmlgtOn which particle (mltsubgt1ltsubgtmltsubgt5ltsubgt) you want to calculate theresultant force

Use sup instead if you want a superscriptThe result will look like this

Click OK when finished

Figure 5

To do the calculation you need to put the coding similar like ldquoGravityAttract07ajavardquo and put itinto the ldquocalculaterdquo button (this is ldquoJButton1rdquo) You can copy ldquoGravityAttract07a javardquo source code

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3258

paste it and then do a few alterations First click on the ldquocalculaterdquo button right click choose Eventsand then choose Action then actionPerformed (Figure 6) It will bring you to where you should insertyour coding (Figure 7) You can write your own coding or copypaste any coding from somewhereelse (in this case from ldquoGravityAttract07ajavardquo)

Figure 6

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3358

Figure 7

Copy ldquoGravityAttract07ajavardquo source code and paste to the place after ldquo TODO add youhandling code hererdquo see Figure 7 To see which part has been modified the coding in Figure 7 hasbeen color coded for better understanding as follows

private void jButton1ActionPerformed(javaawteventActionEvent evt) TODO add your handling code here

double[] m = new double[6] mass of objectdouble[] x = new double[6] x-coor of objectdouble[] y = new double[6] y-coor of objectdouble f resultant forcedouble fxfy x amp y component of forcedouble rx ry rp co G dirfangdirfDecimalFormat dff dfangdirfint j mi

G = 667e-11m[1]=25 x[1]=12 y[1]=25m[2]=15 x[2]=15 y[2]=15m[3]=75 x[3]=02 y[3]=55m[4]=92 x[4]=52 y[4]=25m[5]=66 x[5]=42 y[5]=45

BufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin)) String mass= Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3458

catch(IOException e)Systemoutprintln(Not a valid input value) int mi = IntegerparseInt(mass)mi = IntegerparseInt(jTextField1getText())fx = 00fy = 00for(j=1 jlt=5 j++)

if (j=mi)rx = x[j]-x[mi]ry = y[j]-y[mi]rp = Mathpow((rxrx+ryry)15) (rx^2 + ry^2) raised to the power of 32co = Gm[mi]m[j]fx = fx + corxrpfy = fy + coryrp

f = Mathsqrt(fxfx + fyfy)

dirf = Mathatan(fyfx)angdirf = (dirf(22070))1800 Systemoutprintf(ldquoThe resultant force on particle [ldquo+ mi +rdquo] due to the other masses is

14e N 22f degnrdquofangdirf)dff = new javatextDecimalFormat(ldquoE00rdquo)dfangdirf = new DecimalFormat(ldquo000rdquo)

jTextField2setText(ldquordquo + mi) jTextField3setText(ldquordquo + dffformat(f)) jTextField4setText(ldquordquo + dfangdirfformat(angdirf))

Note

Brown = automatically generatedBlue = the original code from ldquoGravityAttract07ajavardquoGreen = coding from ldquoGravityAttract07ajavardquo which were being disabled due to

incompatibilities with NetbeansRed = a new inserted coding a few of them is to rephrasesubstitute the green one

Javascripts command line useBufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin))String mass=Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))try mass=dataInreadLine()catch(IOException e)Systemoutprintln(Not a valid input value)

int mi = IntegerparseInt(mass)

to read any input from the keyboard typed by the user while Netbeans uses egldquoIntegerparseInt(jTextField1getText())rdquo etc Javascripts command line uses ldquoSystemoutprintfrdquo to format the printed output whileNetbeans uses more complicated way to do the same job (see below)

Javascript command line

Systemoutprintf(The resultant force on particle [+ mi +] due to the other masses is 14e N22f degnfangdirf)

Netbeansdff = new javatextDecimalFormat(ldquo0E00rdquo)df di f D i lF t(ldquo0 00rdquo)

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3558

jTextField2setText(ldquordquo + mi) jTextField3setText(ldquordquo + dffformat(f)) jTextField4setText(ldquordquo + dfangdirfformat(angdirf))

ldquodffrdquo and ldquodfangdirfrdquo are new variables that should be declared and added to format thenumbers into decimal format ie ldquo0E00rdquo to get a scientific format eg ldquo20019E-10rdquo and ldquo000rdquoto get a decimal number eg ldquo-1570rdquo

To use DecimalFormat you have to put ldquoimport javatextDecimalFormatrdquo in the top of thecoding as shown in Figure 8

Figure 8

To reset the calculation you need to put coding into the ldquoresetrdquo button (this is ldquoJButton2rdquo)First click on the ldquoresetrdquo button right click choose Events and then choose Action thenactionPerformed (similar like in Figure 6) It will bring you to where you should insert your coding(Figure 9) Write the following coding (Figure 9)

Figure 9

To exit the program you need to put coding into the ldquoexitrdquo button (this is ldquoJButton3rdquo) Firstclick on the ldquoexitrdquo button right click choose Events and then choose Action then actionPerformed(similar like in Figure 6) It will bring you to where you should insert your coding (Figure 10) Writethe following coding (Figure 10)

Figure 10

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3658

Once you finished now you can run and see the result If everything is fine then the output wilbe look like this (Figure 11)

Figure 11

Remember when you compile and run ldquoGravityAttract07ajavardquo using Javascripts commandline once you compile and run the result was looked like this

On which particle you want to calculate the resulting force 2The resultant force on particle [2] due to the other masses is 30673e-10 N 8837 deg

SEE THE DIFFERENCES AT THE USER INTERFACE

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3758

8th Step

Written by Mohd Khalid Kasmin

Monday 27 August 2007

Programming using command line interface (CLI) has its advantages but todays computer users are so used to graphical user

interface (GUI) that we just can no longer be contented with writing application that do not use the GUI Hence starting from this 8thstep we are going to embark on learning how to make java application that utilises the the GUI

It is possible to write a GUI application with the text based tools that we have been using for these past 7 steps but the job is so

messy that most of you will definitely be turnoff by it So to make life slightly easier well use an integrated development

environment (IDE) known as NetBeans specifically NetBeans 551

0 First ssh to the our server Next once logged in just type nice -n15 netbeans amp on the terminal If you had sshed with the -Y

switch after a few tens of seconds NetBeans should emerge on your display screen

One important point to note Since our server can readily handle up to 100 users (maybe even

more) doing work in CLI mode this machine will certainly be taxed to its maximum capability once

we start using graphical based application NetBeans 551 uses a geat deal of computer memory

and with the 4Gbytes RAM server it can only accomodate up to 25 users concurrently using

NetBeans if the users use the application wisely What is meant by wisely here is that (a) when

starting the NetBeans application please use it together with the nice -n15 command (b) in using

the NetBeans please make sure you only have one project open at a time and (c) please close your

project everytime before quitting the NetBeans

Home Profile Teaching Links Contact Me My Work

Newsflash

Yesterday all servers in the US went out on strike in a bid to get more RAM and better CPUs A spokes person said that the

need for better RAM was due to some fool increasing the front-side bus speed In future busses will be told to slow down in

residential motherboards

search

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3858

1

2

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3958

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4058

[ Back ]

5

Last Updated ( Wednesday 31 October 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4158

8th Step (Cont)

Written by Mohd Khalid Kasmin

Monday 27 August 2007

Up to this point we have define the name of our project Next we construct a class and to follow what we already know we name the class the sam

project name latihan01

6

7

Home Profile Teaching Links Contact Me My Work

Newsflash

Joomla 10 - Experience the Freedom It has never been easier to create your own dynamic site Manage all your content from

the best CMS admin interface

search

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4258

8

9

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4358

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4458

8th Step (Cont still)

Written by Mohd Khalid Kasmin

Monday 27 August 2007

In item (9) we see that Netbeans does not provide us with the main method So to follow what we already know we now put in the

main method

10

Home Profile Teaching Links Contact Me My Work

Newsflash

Aoccdrnig to a rscheearch at an Elingsh uinervtisy it deosnt mttaer in waht oredr the ltteers in a wrod are the olny iprmoetnt

tihng is taht frist and lsat ltteer is at the rghit pclae The rset can be a toatl mses and you can sitll raed it wouthit porbelm Tihs is

bcuseae we do not raed ervey lteter by itslef but the wrod as a wlohe

search

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4558

11

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4658

12

13

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4758

14

15

Last Updated ( Monday 27 August 2007 )

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4858

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4958

9th Step

Thursday 06 September 2007

Step by Step into Java Programming

for

University Physics Student

By

Mohd Khalid Kasmin

A Building graphical user interface - preparing the JFrame Form

At this stage of our programming experience with Java and NetBeans please follow strictly the steps outline in this article First and

make a new GUI project make sure there is no other project that already been loaded by Netbeans Please close any project displayed

1 Create new project

- On the main menu click on File --gt New Project (note Close first any previous project)

- Specify project name amp project location - Untick Create Main Class item

- Click on the Finish button

Home Profile Teaching Links Contact Me My Work

Newsflash

Aoccdrnig to a rscheearch at an Elingsh uinervtisy it deosnt mttaer in waht oredr the ltteers in a wrod are the olny iprmoetnt

tihng is taht frist and lsat ltteer is at the rghit pclae The rset can be a toatl mses and you can sitll raed it wouthit porbelm Tihs is

bcuseae we do not raed ervey lteter by itslef but the wrod as a wlohe

search

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5058

Figure 91

2 Create UI class

- Right click on the project name in the Project pane

on the popup menu select New --gt JFrame Form

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5158

Figure 92

- In the New JFrame Form dialog specify the Class Name and the Package name

eg Class Name multiplierUI Package mymultiplier

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5258

Figure 93

After clicking on the Finish button we should get something like the following

Figure 94

Last Updated ( Tuesday 18 September 2007 )

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5358

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5458

9th Step (Cont)

Written by Mohd Khalid Kasmin

Tuesday 11 September 2007

Step by Step into Java Programming

for University Physics Student

By

Mohd Khalid Kasmin

B Building user interface (UI) - putting in the components

Now that we have a proper blank form we can start filling the form with the suitable graphical components For introduction to

GUI programming an appropriate application to build (for a physics student) is a simple solver So our first exercise is to build an

application that takes two numbers and multiply them and display the result

With GUI we usually give input values to an application via input boxes that are labelled When all inputs are typed in properlywe click on a button to ask the application to execute the job Hence in this exercise well do just that The target now is to

construct a user interface that looks like Figure 95 below

Figure 95

Home Profile Teaching Links Contact Me My Work

Newsflash

Joomla 10 - Experience the Freedom It has never been easier to create your own dynamic site Manage all your content from

the best CMS admin interface

search

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5558

[ Back ]

1 Problemapplication description box

- In the palette pane click on Swing component named JTextPane (Note click just once) Move the mouse pointer on top of

your JFrame (Note just move not drag) When you see a little white box with orange border position it somewhere near the

top part of your JFrame and click once The JTextPane is then attached on the JFrame Addjust its size with the help of the

sizing hook around the perimeter of the JTextPane

- Edit the text property of the JTextPane with the description of your application

2 Input boxes

- In the palette pane click on Swing component named JTextField Next move the mouse pointer on top of your JFrame

When you see a white box with the word jTextField1 position it to the left and under the JTextPane1

- Click on Swing component JTextField in the palette pane again Move the mouse pointer on top of the JFrame you should

get a second JTextField named jTextField2 Position it to the right of jTextField1

Last Updated ( Tuesday 11 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5658

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5758

multiplication if a b and c were first declared as double or float or int variables So the code of the calculation should look like the

following

double a b c

a =

b =

c = a b

Clearly variables a and b need to be assigned with values of typed double Now looking back at inputboxes A and B we made

those boxes using the JTextField component meaning that any data or value written in a JTextField is of typed Text or String even

if the value displayed in the box is ldquo125rdquo But f irst here there is this task of getting to that Text (or String) values ldquo125rdquo and ldquo72rdquo

In Java we can get to a text in JTextField component using the getText() method So to retrieve values in jTextField1 and

jTextField2 we can use

jTextField1getText()

and

jTextField2getText()

But those values are still in text mode and we need another method to convert the data mode f rom text to double For that we can

use the method DoubleparseDouble() Thus the whole process of retrieve text -gt convert text to double -gt assign to double

variable can be done using

a = DoubleparseDouble( jTextField1getText() )

and

b = DoubleparseDouble( jTextField2getText() )

Hence the code for performing the multiplication becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

Up to this point we have the variable c as the result of the multiplication of a and b So the next thing to do is to display this

result on the JLabel that we have already prepared before

A x B = _____

The text A x B = ______ was made using JLabel component and its variable name is jLabel3 Since the variable c is of type

double and text property of JLabel is of type String we need to convert it first to String data type The method to convert a double

to String is DoubletoString() So we can write the code as DoubletoString(c) After getting the text we can now use it to be the

text displayed on jLabel3 To do that we use the method setText() Hence the appropriate code for displaying the result is

jLabel3setText( A x B = + DoubletoString(c) )

And the full code for our calculation becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

jLabel3setText( A x B = + DoubletoString(c) )

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5858

[ Back ]

Last Updated ( Tuesday 18 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

Page 17: Computional Physics

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1758

Euler Method

There is another way to see the projection of y position of the object using a numericalmethod called ldquoEuler methodrdquo See the following derivation

t avt v y

t

va

t

yv

∆=∆∆=∆

∆=

∆=

t v y y y y

t avvvv

t v y y y y

t avvvv

t v y y y y

t avvvv

nnnn

nnn

∆+=∆+=

∆+=∆+=

∆+=∆+=

∆+=∆+=

∆+=∆+=

∆+=∆+=

minus

minusminus

1

11

2112

112

1001

001

M

if ∆∆∆∆t rarrrarrrarrrarr 0 then y will close to the exact solution ( ( ) 2

21

00 at t v yt y ++= )

The algorithm 1 Declare the classmethod to be used

2 Declare and assign all variables to be used av0v1yy0y1deltt and j 3 Initialize value to all respective variables a = -98 y0 = 10 v0 = 10

4 Select appropriate value for ∆t

∆t = 00055 Start the loop to calculate the velocity and position for t=0

6 Calculate velocity v1 = v0 + ∆v = v0 + a ∆t

7 Calculate position y1 = y0 + ∆y = y0 + v1 ∆t8 Print out values of t and y1 9 Swap v0 = v1 and y0 = y1

10 Repeat from step 5 and increase the value of t by ∆t (t =t + ∆t) until stoppingcondition is satisfied at t=05

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1858

The java code

Java class for solving free fall object under gravity (g = 98 mss)by numerical method called ldquoEulers Methodrdquo

public class freefalleuler

public static void main(String args[])double av0v1yy0y1delttint j

a = -98y0 = 10v0 = 10delt = 0005

for(t=0 tlt=05 t =t + delt)v1 = v0 + adelty1 = y0 + v1deltSystemoutprintf(84f 84fnty1)v0 = v1y0 = y1

Complile freefalleulerjava for different delt ie 00005 0005 005 and then run and savethe output into a file name as follows

[khamimsetpar65 ~]$ java freefalleuler gtffeuler00005dat[khamimsetpar65 ~]$ java freefalleuler gtffeuler0005dat[khamimsetpar65 ~]$ java freefalleuler gtffeuler005dat

Plotting the results using gnuplot

Using gnuplot plot all the graphs using the following command and compare the results

gnuplotgt plot ffeuler00005dat using 12 title euler delt=00005 with linegt ffeuler0005dat using 12 title euler delt=0005 with linegt ffeuler005dat using 12 title euler deltt=005 with linegt ffsoldat using 12 title exact solution with line

In MS windows

gnuplotgt plot ffeuler00005dat using 12 title euler delt=00005 with lineffeuler0005dat using 12 title euler delt=0005 with lineffeuler005dat using 12 titleeuler delt=005 with line ffsoldat using 12 title exact solution with line

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1958

Gnuplot

The smaller the delt the closer the curve to the exact solution

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2058

983107983119983117983120983125983124983105983124983113983119983118983105983116 983120983112983129983123983113983107983123

983080983123983123983120983090983089983090983090983081

983113983149983152983154983151983158983141983140 983109983157983148983137983154 983149983141983156983144983151983140

983138983161

983105983138983140983086 983115983144983137983149983145983149 983113983155983149983137983145983148

983117983151983144983140 983115983144983137983148983145983140 983115983137983155983149983145983150

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2158

Improved Euler method

Comparison between analytical solution of F=ma with the Euler method shows that although the

method is able to find approximate solution the error is quite significant if computational step is

reduced The error is also cumulative ie as the iteration of the calculation increases the error also gets

bigger hence value furthest away from initial condition has the largest error

The Euler method gives us a way to approximate positions of object with respect to time but requires

an order of magnitude of computational iterations for each order of magnitude of accuracy For large

number of objects this method will certainly consume a great deal of computation time Hence

students are introduced to Initial Half-Step Method an Euler method with a much improved

computational effeciency

Source code

Study of motion of an object with mass m

experiencing a constant force F using

Improved Euler Method (Initial half step method)

In X and Y dimension

import static javalangMath

public class fma04

public static void main(String args[])

double m fx fy x y vo vx vy ax ay t delt

double pi th theta ymax tmax xmax

pi = 40atan(10)

m = 01 in kg

fy = -02 in N (pulling down force)

fx = 00 in N (retarding force)

ay = fym

ax = fxm

vo = 500 in ms

theta = 300 in degrees

th = theta(pi1800) degrees change to radian

vx = vo cos(th) in ms vy = vo sin(th) in ms

y = 12 in m

x = 00 in m

ymax=y initialize var ymax with initial value of y

xmax=00

tmax=00

delt = 001 in s

for(t=00 tlt=300 t=t+delt)

System outprintf(f f fntxy)

if (t==00)vx = vx + 05axdelt

vy = vy + 05aydelt

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2258

else

vx = vx + axdelt

vy = vy + aydelt

x = x + vxdelt

y = y + vydelt

if (ygtymax)

ymax=y

xmax=xtmax=t

if (ylt=00) break

System outprintf( Value of Y maximum is f happened when t = f

nymaxtmax)

System outprintf( at x = fnxmax)

System outprintf( The object reach the ground at x=f and t=fn xt)

gnuplot result

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2358

983107983119983117983120983125983124983105983124983113983119983118983105983116

983120983112983129983123983113983107983123 983080983123983123983120983090983089983090983090983081

983122983141983155983156983151983154983145983150983143 983110983151983154983139983141

983138983161

983105983138983140983086 983115983144983137983149983145983149 983113983155983149983137983145983148

983117983151983144983140 983115983144983137983148983145983140 983115983137983155983149983145983150

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2458

Damped Harmonic Oscillation

Object m is on a flat surface attached to a spring as in figure above The object is pulled

to position A and released so that it oscillates about x =0 Assuming that the force F=ma= -bv-kx where x is position of the object v is the velocity k the spring constant andb is a constant associated with friction with the flat surface

The algorithm

1 Declare the classmethod to be used2 Declare and assign all variables to be used

x t delt k b A m F a v v0 and x03 Declare and assign constants to the respective variables

k = 02 b = 01 A = 01 m = 054 Select appropriate value for ∆t 5 Initialized initial values xo=A vo=00

6 set x=xo v=vo and t = 07 calculate the value of force F = -kx-bv 8 calculate the value of acceleration a = Fm

9 calculate velocity v = vo + ∆v = vo + a ∆t

10 calculate position x = xo + ∆x = xo + v ∆t

11 increment value of t by ∆t 12 print out values of t and x

13 swap the value of xo=x vo=v 14 repeat from step 4 until stopping condition is satisfied (tlt=600)

The java code

public class simplehar01

public static void main(String args[])double x t deltdouble k b A m F a v v0 x0

k = 02b = 01A = 0 1

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2558

m = 05delt = 0001v0 = 00x0 = Ax = x0 v = v0

t = 00while(tlt=600)

F = -kx-bva = Fmv = v0 + adeltx = x0 + vdeltt = t + deltSystemoutprintf(f fntx)x0 = xv0 = v

gnuplot result

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2658

983107983119983117983120983125983124983105983124983113983119983118983105983116 983120983112983129983123983113983107983123

(983123983123983120983090983089983090983090983081

983106983157983145983148983140983145983150983143 983137 983152983144983161983155983145983139983155 983155983151983148983158983141983154

983157983155983145983150983143 983118983141983156983138983141983137983150983155

983138983161

983105983138983140983086 983115983144983137983149983145983149 983113983155983149983137983145983148

983117983151983144983140 983115983144983137983148983145983140 983115983137983155983149983145983150

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2758

How to create a user interface (UI) using Netbeans

Based on the ldquoGravityAttract07ajavardquo source code (shown below) build a user interface usingNetbeans The design of the UI could be look like this (Figure1)

Figure 1

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2858

ldquoGravityAttract07ajavardquo source code

Java class for calculating the resultant force on any object(ie either on m1m2 m3 m4 or m5)

import static javalangMathimport javaiopublic class GravityAttract07a

public static void main(String args[])

double[] m = new double[6] mass of objectdouble[] x = new double[6] x-coor of objectdouble[] y = new double[6] y-coor of objectdouble f resultant forcedouble fxfy x amp y component of forcedouble rx ry rp co G dirfangdirf

int j

G = 667e-11m[1]=25 x[1]=12 y[1]=25m[2]=15 x[2]=15 y[2]=15m[3]=75 x[3]=02 y[3]=55m[4]=92 x[4]=52 y[4]=25m[5]=66 x[5]=42 y[5]=45BufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin))String mass=Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))try mass=dataInreadLine()

catch(IOException e)Systemoutprintln(Not a valid input value)int mi = IntegerparseInt(mass)fx = 00fy = 00

for(j=1 jlt=5 j++)

if (j=mi)rx = x[j]-x[mi]ry = y[j]-y[mi]rp = pow((rxrx+ryry)15) (rx^2 + ry^2) raised to the power of 32co = Gm[mi]m[j]

fx = fx + corxrpfy = fy + coryrp

f = sqrt(fxfx + fyfy)dirf = toDegrees(atan(fyfx))Systemoutprintf(The force on m[+ mi +] due to the other masses is eN fdegnfdirf)

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2958

This example will not show you step by step details procedure since it already explained inthe class and in the e-learning Please refer to the e-learning about how to create a project how tocreate JFrame Form and as well how to create JButton JTextField JLabel JTextPane and so on

First create a new project and give a name eg ldquogravityrdquo and then create JFrame Form andthe following JButton JTextField JLabel JTextPane etc by using ldquoswing paletterdquo as shown in Figure2

Figure 2

To insert a picture into JLabel2 click on the JLabel2 go to properties and select ldquoiconrdquo Onthe icon editor choose file and then select file to locate the directory of your graphicpicture file(Figure 3) Select the file and click OK when finished Make sure you have already created your ownpicture file and save it as jpg png bmp etc format eg ldquogravityjpgrdquo Use any graphic package todraw your diagrampicture or use drawing tools available in Open Office or Microsoft Word

JTextPane1JLabel1

JLabel2

JLabel3JLabel4

JLabel5

JLabel6 JLabel7

JLabel8

JTextField2 JTextField3 JTextField4

JTextF

JButton1JButton2

JButton3

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3058

Figure 3

To write documentation or question eg in JTextPane1 click on the JTextPane1 go toproperties and select ldquotextrdquo Type the documentation in the text editor as shown in Figure 4 Click OKwhen finished

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3158

To do formatting on the text such as superscript and subscript eg in jLabel3 click on the

JLabel3 go to properties and select ldquotextrdquo Type the documentationmessage on the text editor asshown in Figure 5 as follows

eg

lthtmlgtOn which particle (mltsubgt1ltsubgtmltsubgt5ltsubgt) you want to calculate theresultant force

Use sup instead if you want a superscriptThe result will look like this

Click OK when finished

Figure 5

To do the calculation you need to put the coding similar like ldquoGravityAttract07ajavardquo and put itinto the ldquocalculaterdquo button (this is ldquoJButton1rdquo) You can copy ldquoGravityAttract07a javardquo source code

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3258

paste it and then do a few alterations First click on the ldquocalculaterdquo button right click choose Eventsand then choose Action then actionPerformed (Figure 6) It will bring you to where you should insertyour coding (Figure 7) You can write your own coding or copypaste any coding from somewhereelse (in this case from ldquoGravityAttract07ajavardquo)

Figure 6

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3358

Figure 7

Copy ldquoGravityAttract07ajavardquo source code and paste to the place after ldquo TODO add youhandling code hererdquo see Figure 7 To see which part has been modified the coding in Figure 7 hasbeen color coded for better understanding as follows

private void jButton1ActionPerformed(javaawteventActionEvent evt) TODO add your handling code here

double[] m = new double[6] mass of objectdouble[] x = new double[6] x-coor of objectdouble[] y = new double[6] y-coor of objectdouble f resultant forcedouble fxfy x amp y component of forcedouble rx ry rp co G dirfangdirfDecimalFormat dff dfangdirfint j mi

G = 667e-11m[1]=25 x[1]=12 y[1]=25m[2]=15 x[2]=15 y[2]=15m[3]=75 x[3]=02 y[3]=55m[4]=92 x[4]=52 y[4]=25m[5]=66 x[5]=42 y[5]=45

BufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin)) String mass= Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3458

catch(IOException e)Systemoutprintln(Not a valid input value) int mi = IntegerparseInt(mass)mi = IntegerparseInt(jTextField1getText())fx = 00fy = 00for(j=1 jlt=5 j++)

if (j=mi)rx = x[j]-x[mi]ry = y[j]-y[mi]rp = Mathpow((rxrx+ryry)15) (rx^2 + ry^2) raised to the power of 32co = Gm[mi]m[j]fx = fx + corxrpfy = fy + coryrp

f = Mathsqrt(fxfx + fyfy)

dirf = Mathatan(fyfx)angdirf = (dirf(22070))1800 Systemoutprintf(ldquoThe resultant force on particle [ldquo+ mi +rdquo] due to the other masses is

14e N 22f degnrdquofangdirf)dff = new javatextDecimalFormat(ldquoE00rdquo)dfangdirf = new DecimalFormat(ldquo000rdquo)

jTextField2setText(ldquordquo + mi) jTextField3setText(ldquordquo + dffformat(f)) jTextField4setText(ldquordquo + dfangdirfformat(angdirf))

Note

Brown = automatically generatedBlue = the original code from ldquoGravityAttract07ajavardquoGreen = coding from ldquoGravityAttract07ajavardquo which were being disabled due to

incompatibilities with NetbeansRed = a new inserted coding a few of them is to rephrasesubstitute the green one

Javascripts command line useBufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin))String mass=Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))try mass=dataInreadLine()catch(IOException e)Systemoutprintln(Not a valid input value)

int mi = IntegerparseInt(mass)

to read any input from the keyboard typed by the user while Netbeans uses egldquoIntegerparseInt(jTextField1getText())rdquo etc Javascripts command line uses ldquoSystemoutprintfrdquo to format the printed output whileNetbeans uses more complicated way to do the same job (see below)

Javascript command line

Systemoutprintf(The resultant force on particle [+ mi +] due to the other masses is 14e N22f degnfangdirf)

Netbeansdff = new javatextDecimalFormat(ldquo0E00rdquo)df di f D i lF t(ldquo0 00rdquo)

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3558

jTextField2setText(ldquordquo + mi) jTextField3setText(ldquordquo + dffformat(f)) jTextField4setText(ldquordquo + dfangdirfformat(angdirf))

ldquodffrdquo and ldquodfangdirfrdquo are new variables that should be declared and added to format thenumbers into decimal format ie ldquo0E00rdquo to get a scientific format eg ldquo20019E-10rdquo and ldquo000rdquoto get a decimal number eg ldquo-1570rdquo

To use DecimalFormat you have to put ldquoimport javatextDecimalFormatrdquo in the top of thecoding as shown in Figure 8

Figure 8

To reset the calculation you need to put coding into the ldquoresetrdquo button (this is ldquoJButton2rdquo)First click on the ldquoresetrdquo button right click choose Events and then choose Action thenactionPerformed (similar like in Figure 6) It will bring you to where you should insert your coding(Figure 9) Write the following coding (Figure 9)

Figure 9

To exit the program you need to put coding into the ldquoexitrdquo button (this is ldquoJButton3rdquo) Firstclick on the ldquoexitrdquo button right click choose Events and then choose Action then actionPerformed(similar like in Figure 6) It will bring you to where you should insert your coding (Figure 10) Writethe following coding (Figure 10)

Figure 10

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3658

Once you finished now you can run and see the result If everything is fine then the output wilbe look like this (Figure 11)

Figure 11

Remember when you compile and run ldquoGravityAttract07ajavardquo using Javascripts commandline once you compile and run the result was looked like this

On which particle you want to calculate the resulting force 2The resultant force on particle [2] due to the other masses is 30673e-10 N 8837 deg

SEE THE DIFFERENCES AT THE USER INTERFACE

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3758

8th Step

Written by Mohd Khalid Kasmin

Monday 27 August 2007

Programming using command line interface (CLI) has its advantages but todays computer users are so used to graphical user

interface (GUI) that we just can no longer be contented with writing application that do not use the GUI Hence starting from this 8thstep we are going to embark on learning how to make java application that utilises the the GUI

It is possible to write a GUI application with the text based tools that we have been using for these past 7 steps but the job is so

messy that most of you will definitely be turnoff by it So to make life slightly easier well use an integrated development

environment (IDE) known as NetBeans specifically NetBeans 551

0 First ssh to the our server Next once logged in just type nice -n15 netbeans amp on the terminal If you had sshed with the -Y

switch after a few tens of seconds NetBeans should emerge on your display screen

One important point to note Since our server can readily handle up to 100 users (maybe even

more) doing work in CLI mode this machine will certainly be taxed to its maximum capability once

we start using graphical based application NetBeans 551 uses a geat deal of computer memory

and with the 4Gbytes RAM server it can only accomodate up to 25 users concurrently using

NetBeans if the users use the application wisely What is meant by wisely here is that (a) when

starting the NetBeans application please use it together with the nice -n15 command (b) in using

the NetBeans please make sure you only have one project open at a time and (c) please close your

project everytime before quitting the NetBeans

Home Profile Teaching Links Contact Me My Work

Newsflash

Yesterday all servers in the US went out on strike in a bid to get more RAM and better CPUs A spokes person said that the

need for better RAM was due to some fool increasing the front-side bus speed In future busses will be told to slow down in

residential motherboards

search

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3858

1

2

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3958

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4058

[ Back ]

5

Last Updated ( Wednesday 31 October 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4158

8th Step (Cont)

Written by Mohd Khalid Kasmin

Monday 27 August 2007

Up to this point we have define the name of our project Next we construct a class and to follow what we already know we name the class the sam

project name latihan01

6

7

Home Profile Teaching Links Contact Me My Work

Newsflash

Joomla 10 - Experience the Freedom It has never been easier to create your own dynamic site Manage all your content from

the best CMS admin interface

search

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4258

8

9

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4358

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4458

8th Step (Cont still)

Written by Mohd Khalid Kasmin

Monday 27 August 2007

In item (9) we see that Netbeans does not provide us with the main method So to follow what we already know we now put in the

main method

10

Home Profile Teaching Links Contact Me My Work

Newsflash

Aoccdrnig to a rscheearch at an Elingsh uinervtisy it deosnt mttaer in waht oredr the ltteers in a wrod are the olny iprmoetnt

tihng is taht frist and lsat ltteer is at the rghit pclae The rset can be a toatl mses and you can sitll raed it wouthit porbelm Tihs is

bcuseae we do not raed ervey lteter by itslef but the wrod as a wlohe

search

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4558

11

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4658

12

13

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4758

14

15

Last Updated ( Monday 27 August 2007 )

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4858

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4958

9th Step

Thursday 06 September 2007

Step by Step into Java Programming

for

University Physics Student

By

Mohd Khalid Kasmin

A Building graphical user interface - preparing the JFrame Form

At this stage of our programming experience with Java and NetBeans please follow strictly the steps outline in this article First and

make a new GUI project make sure there is no other project that already been loaded by Netbeans Please close any project displayed

1 Create new project

- On the main menu click on File --gt New Project (note Close first any previous project)

- Specify project name amp project location - Untick Create Main Class item

- Click on the Finish button

Home Profile Teaching Links Contact Me My Work

Newsflash

Aoccdrnig to a rscheearch at an Elingsh uinervtisy it deosnt mttaer in waht oredr the ltteers in a wrod are the olny iprmoetnt

tihng is taht frist and lsat ltteer is at the rghit pclae The rset can be a toatl mses and you can sitll raed it wouthit porbelm Tihs is

bcuseae we do not raed ervey lteter by itslef but the wrod as a wlohe

search

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5058

Figure 91

2 Create UI class

- Right click on the project name in the Project pane

on the popup menu select New --gt JFrame Form

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5158

Figure 92

- In the New JFrame Form dialog specify the Class Name and the Package name

eg Class Name multiplierUI Package mymultiplier

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5258

Figure 93

After clicking on the Finish button we should get something like the following

Figure 94

Last Updated ( Tuesday 18 September 2007 )

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5358

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5458

9th Step (Cont)

Written by Mohd Khalid Kasmin

Tuesday 11 September 2007

Step by Step into Java Programming

for University Physics Student

By

Mohd Khalid Kasmin

B Building user interface (UI) - putting in the components

Now that we have a proper blank form we can start filling the form with the suitable graphical components For introduction to

GUI programming an appropriate application to build (for a physics student) is a simple solver So our first exercise is to build an

application that takes two numbers and multiply them and display the result

With GUI we usually give input values to an application via input boxes that are labelled When all inputs are typed in properlywe click on a button to ask the application to execute the job Hence in this exercise well do just that The target now is to

construct a user interface that looks like Figure 95 below

Figure 95

Home Profile Teaching Links Contact Me My Work

Newsflash

Joomla 10 - Experience the Freedom It has never been easier to create your own dynamic site Manage all your content from

the best CMS admin interface

search

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5558

[ Back ]

1 Problemapplication description box

- In the palette pane click on Swing component named JTextPane (Note click just once) Move the mouse pointer on top of

your JFrame (Note just move not drag) When you see a little white box with orange border position it somewhere near the

top part of your JFrame and click once The JTextPane is then attached on the JFrame Addjust its size with the help of the

sizing hook around the perimeter of the JTextPane

- Edit the text property of the JTextPane with the description of your application

2 Input boxes

- In the palette pane click on Swing component named JTextField Next move the mouse pointer on top of your JFrame

When you see a white box with the word jTextField1 position it to the left and under the JTextPane1

- Click on Swing component JTextField in the palette pane again Move the mouse pointer on top of the JFrame you should

get a second JTextField named jTextField2 Position it to the right of jTextField1

Last Updated ( Tuesday 11 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5658

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5758

multiplication if a b and c were first declared as double or float or int variables So the code of the calculation should look like the

following

double a b c

a =

b =

c = a b

Clearly variables a and b need to be assigned with values of typed double Now looking back at inputboxes A and B we made

those boxes using the JTextField component meaning that any data or value written in a JTextField is of typed Text or String even

if the value displayed in the box is ldquo125rdquo But f irst here there is this task of getting to that Text (or String) values ldquo125rdquo and ldquo72rdquo

In Java we can get to a text in JTextField component using the getText() method So to retrieve values in jTextField1 and

jTextField2 we can use

jTextField1getText()

and

jTextField2getText()

But those values are still in text mode and we need another method to convert the data mode f rom text to double For that we can

use the method DoubleparseDouble() Thus the whole process of retrieve text -gt convert text to double -gt assign to double

variable can be done using

a = DoubleparseDouble( jTextField1getText() )

and

b = DoubleparseDouble( jTextField2getText() )

Hence the code for performing the multiplication becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

Up to this point we have the variable c as the result of the multiplication of a and b So the next thing to do is to display this

result on the JLabel that we have already prepared before

A x B = _____

The text A x B = ______ was made using JLabel component and its variable name is jLabel3 Since the variable c is of type

double and text property of JLabel is of type String we need to convert it first to String data type The method to convert a double

to String is DoubletoString() So we can write the code as DoubletoString(c) After getting the text we can now use it to be the

text displayed on jLabel3 To do that we use the method setText() Hence the appropriate code for displaying the result is

jLabel3setText( A x B = + DoubletoString(c) )

And the full code for our calculation becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

jLabel3setText( A x B = + DoubletoString(c) )

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5858

[ Back ]

Last Updated ( Tuesday 18 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

Page 18: Computional Physics

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1858

The java code

Java class for solving free fall object under gravity (g = 98 mss)by numerical method called ldquoEulers Methodrdquo

public class freefalleuler

public static void main(String args[])double av0v1yy0y1delttint j

a = -98y0 = 10v0 = 10delt = 0005

for(t=0 tlt=05 t =t + delt)v1 = v0 + adelty1 = y0 + v1deltSystemoutprintf(84f 84fnty1)v0 = v1y0 = y1

Complile freefalleulerjava for different delt ie 00005 0005 005 and then run and savethe output into a file name as follows

[khamimsetpar65 ~]$ java freefalleuler gtffeuler00005dat[khamimsetpar65 ~]$ java freefalleuler gtffeuler0005dat[khamimsetpar65 ~]$ java freefalleuler gtffeuler005dat

Plotting the results using gnuplot

Using gnuplot plot all the graphs using the following command and compare the results

gnuplotgt plot ffeuler00005dat using 12 title euler delt=00005 with linegt ffeuler0005dat using 12 title euler delt=0005 with linegt ffeuler005dat using 12 title euler deltt=005 with linegt ffsoldat using 12 title exact solution with line

In MS windows

gnuplotgt plot ffeuler00005dat using 12 title euler delt=00005 with lineffeuler0005dat using 12 title euler delt=0005 with lineffeuler005dat using 12 titleeuler delt=005 with line ffsoldat using 12 title exact solution with line

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1958

Gnuplot

The smaller the delt the closer the curve to the exact solution

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2058

983107983119983117983120983125983124983105983124983113983119983118983105983116 983120983112983129983123983113983107983123

983080983123983123983120983090983089983090983090983081

983113983149983152983154983151983158983141983140 983109983157983148983137983154 983149983141983156983144983151983140

983138983161

983105983138983140983086 983115983144983137983149983145983149 983113983155983149983137983145983148

983117983151983144983140 983115983144983137983148983145983140 983115983137983155983149983145983150

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2158

Improved Euler method

Comparison between analytical solution of F=ma with the Euler method shows that although the

method is able to find approximate solution the error is quite significant if computational step is

reduced The error is also cumulative ie as the iteration of the calculation increases the error also gets

bigger hence value furthest away from initial condition has the largest error

The Euler method gives us a way to approximate positions of object with respect to time but requires

an order of magnitude of computational iterations for each order of magnitude of accuracy For large

number of objects this method will certainly consume a great deal of computation time Hence

students are introduced to Initial Half-Step Method an Euler method with a much improved

computational effeciency

Source code

Study of motion of an object with mass m

experiencing a constant force F using

Improved Euler Method (Initial half step method)

In X and Y dimension

import static javalangMath

public class fma04

public static void main(String args[])

double m fx fy x y vo vx vy ax ay t delt

double pi th theta ymax tmax xmax

pi = 40atan(10)

m = 01 in kg

fy = -02 in N (pulling down force)

fx = 00 in N (retarding force)

ay = fym

ax = fxm

vo = 500 in ms

theta = 300 in degrees

th = theta(pi1800) degrees change to radian

vx = vo cos(th) in ms vy = vo sin(th) in ms

y = 12 in m

x = 00 in m

ymax=y initialize var ymax with initial value of y

xmax=00

tmax=00

delt = 001 in s

for(t=00 tlt=300 t=t+delt)

System outprintf(f f fntxy)

if (t==00)vx = vx + 05axdelt

vy = vy + 05aydelt

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2258

else

vx = vx + axdelt

vy = vy + aydelt

x = x + vxdelt

y = y + vydelt

if (ygtymax)

ymax=y

xmax=xtmax=t

if (ylt=00) break

System outprintf( Value of Y maximum is f happened when t = f

nymaxtmax)

System outprintf( at x = fnxmax)

System outprintf( The object reach the ground at x=f and t=fn xt)

gnuplot result

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2358

983107983119983117983120983125983124983105983124983113983119983118983105983116

983120983112983129983123983113983107983123 983080983123983123983120983090983089983090983090983081

983122983141983155983156983151983154983145983150983143 983110983151983154983139983141

983138983161

983105983138983140983086 983115983144983137983149983145983149 983113983155983149983137983145983148

983117983151983144983140 983115983144983137983148983145983140 983115983137983155983149983145983150

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2458

Damped Harmonic Oscillation

Object m is on a flat surface attached to a spring as in figure above The object is pulled

to position A and released so that it oscillates about x =0 Assuming that the force F=ma= -bv-kx where x is position of the object v is the velocity k the spring constant andb is a constant associated with friction with the flat surface

The algorithm

1 Declare the classmethod to be used2 Declare and assign all variables to be used

x t delt k b A m F a v v0 and x03 Declare and assign constants to the respective variables

k = 02 b = 01 A = 01 m = 054 Select appropriate value for ∆t 5 Initialized initial values xo=A vo=00

6 set x=xo v=vo and t = 07 calculate the value of force F = -kx-bv 8 calculate the value of acceleration a = Fm

9 calculate velocity v = vo + ∆v = vo + a ∆t

10 calculate position x = xo + ∆x = xo + v ∆t

11 increment value of t by ∆t 12 print out values of t and x

13 swap the value of xo=x vo=v 14 repeat from step 4 until stopping condition is satisfied (tlt=600)

The java code

public class simplehar01

public static void main(String args[])double x t deltdouble k b A m F a v v0 x0

k = 02b = 01A = 0 1

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2558

m = 05delt = 0001v0 = 00x0 = Ax = x0 v = v0

t = 00while(tlt=600)

F = -kx-bva = Fmv = v0 + adeltx = x0 + vdeltt = t + deltSystemoutprintf(f fntx)x0 = xv0 = v

gnuplot result

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2658

983107983119983117983120983125983124983105983124983113983119983118983105983116 983120983112983129983123983113983107983123

(983123983123983120983090983089983090983090983081

983106983157983145983148983140983145983150983143 983137 983152983144983161983155983145983139983155 983155983151983148983158983141983154

983157983155983145983150983143 983118983141983156983138983141983137983150983155

983138983161

983105983138983140983086 983115983144983137983149983145983149 983113983155983149983137983145983148

983117983151983144983140 983115983144983137983148983145983140 983115983137983155983149983145983150

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2758

How to create a user interface (UI) using Netbeans

Based on the ldquoGravityAttract07ajavardquo source code (shown below) build a user interface usingNetbeans The design of the UI could be look like this (Figure1)

Figure 1

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2858

ldquoGravityAttract07ajavardquo source code

Java class for calculating the resultant force on any object(ie either on m1m2 m3 m4 or m5)

import static javalangMathimport javaiopublic class GravityAttract07a

public static void main(String args[])

double[] m = new double[6] mass of objectdouble[] x = new double[6] x-coor of objectdouble[] y = new double[6] y-coor of objectdouble f resultant forcedouble fxfy x amp y component of forcedouble rx ry rp co G dirfangdirf

int j

G = 667e-11m[1]=25 x[1]=12 y[1]=25m[2]=15 x[2]=15 y[2]=15m[3]=75 x[3]=02 y[3]=55m[4]=92 x[4]=52 y[4]=25m[5]=66 x[5]=42 y[5]=45BufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin))String mass=Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))try mass=dataInreadLine()

catch(IOException e)Systemoutprintln(Not a valid input value)int mi = IntegerparseInt(mass)fx = 00fy = 00

for(j=1 jlt=5 j++)

if (j=mi)rx = x[j]-x[mi]ry = y[j]-y[mi]rp = pow((rxrx+ryry)15) (rx^2 + ry^2) raised to the power of 32co = Gm[mi]m[j]

fx = fx + corxrpfy = fy + coryrp

f = sqrt(fxfx + fyfy)dirf = toDegrees(atan(fyfx))Systemoutprintf(The force on m[+ mi +] due to the other masses is eN fdegnfdirf)

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2958

This example will not show you step by step details procedure since it already explained inthe class and in the e-learning Please refer to the e-learning about how to create a project how tocreate JFrame Form and as well how to create JButton JTextField JLabel JTextPane and so on

First create a new project and give a name eg ldquogravityrdquo and then create JFrame Form andthe following JButton JTextField JLabel JTextPane etc by using ldquoswing paletterdquo as shown in Figure2

Figure 2

To insert a picture into JLabel2 click on the JLabel2 go to properties and select ldquoiconrdquo Onthe icon editor choose file and then select file to locate the directory of your graphicpicture file(Figure 3) Select the file and click OK when finished Make sure you have already created your ownpicture file and save it as jpg png bmp etc format eg ldquogravityjpgrdquo Use any graphic package todraw your diagrampicture or use drawing tools available in Open Office or Microsoft Word

JTextPane1JLabel1

JLabel2

JLabel3JLabel4

JLabel5

JLabel6 JLabel7

JLabel8

JTextField2 JTextField3 JTextField4

JTextF

JButton1JButton2

JButton3

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3058

Figure 3

To write documentation or question eg in JTextPane1 click on the JTextPane1 go toproperties and select ldquotextrdquo Type the documentation in the text editor as shown in Figure 4 Click OKwhen finished

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3158

To do formatting on the text such as superscript and subscript eg in jLabel3 click on the

JLabel3 go to properties and select ldquotextrdquo Type the documentationmessage on the text editor asshown in Figure 5 as follows

eg

lthtmlgtOn which particle (mltsubgt1ltsubgtmltsubgt5ltsubgt) you want to calculate theresultant force

Use sup instead if you want a superscriptThe result will look like this

Click OK when finished

Figure 5

To do the calculation you need to put the coding similar like ldquoGravityAttract07ajavardquo and put itinto the ldquocalculaterdquo button (this is ldquoJButton1rdquo) You can copy ldquoGravityAttract07a javardquo source code

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3258

paste it and then do a few alterations First click on the ldquocalculaterdquo button right click choose Eventsand then choose Action then actionPerformed (Figure 6) It will bring you to where you should insertyour coding (Figure 7) You can write your own coding or copypaste any coding from somewhereelse (in this case from ldquoGravityAttract07ajavardquo)

Figure 6

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3358

Figure 7

Copy ldquoGravityAttract07ajavardquo source code and paste to the place after ldquo TODO add youhandling code hererdquo see Figure 7 To see which part has been modified the coding in Figure 7 hasbeen color coded for better understanding as follows

private void jButton1ActionPerformed(javaawteventActionEvent evt) TODO add your handling code here

double[] m = new double[6] mass of objectdouble[] x = new double[6] x-coor of objectdouble[] y = new double[6] y-coor of objectdouble f resultant forcedouble fxfy x amp y component of forcedouble rx ry rp co G dirfangdirfDecimalFormat dff dfangdirfint j mi

G = 667e-11m[1]=25 x[1]=12 y[1]=25m[2]=15 x[2]=15 y[2]=15m[3]=75 x[3]=02 y[3]=55m[4]=92 x[4]=52 y[4]=25m[5]=66 x[5]=42 y[5]=45

BufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin)) String mass= Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3458

catch(IOException e)Systemoutprintln(Not a valid input value) int mi = IntegerparseInt(mass)mi = IntegerparseInt(jTextField1getText())fx = 00fy = 00for(j=1 jlt=5 j++)

if (j=mi)rx = x[j]-x[mi]ry = y[j]-y[mi]rp = Mathpow((rxrx+ryry)15) (rx^2 + ry^2) raised to the power of 32co = Gm[mi]m[j]fx = fx + corxrpfy = fy + coryrp

f = Mathsqrt(fxfx + fyfy)

dirf = Mathatan(fyfx)angdirf = (dirf(22070))1800 Systemoutprintf(ldquoThe resultant force on particle [ldquo+ mi +rdquo] due to the other masses is

14e N 22f degnrdquofangdirf)dff = new javatextDecimalFormat(ldquoE00rdquo)dfangdirf = new DecimalFormat(ldquo000rdquo)

jTextField2setText(ldquordquo + mi) jTextField3setText(ldquordquo + dffformat(f)) jTextField4setText(ldquordquo + dfangdirfformat(angdirf))

Note

Brown = automatically generatedBlue = the original code from ldquoGravityAttract07ajavardquoGreen = coding from ldquoGravityAttract07ajavardquo which were being disabled due to

incompatibilities with NetbeansRed = a new inserted coding a few of them is to rephrasesubstitute the green one

Javascripts command line useBufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin))String mass=Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))try mass=dataInreadLine()catch(IOException e)Systemoutprintln(Not a valid input value)

int mi = IntegerparseInt(mass)

to read any input from the keyboard typed by the user while Netbeans uses egldquoIntegerparseInt(jTextField1getText())rdquo etc Javascripts command line uses ldquoSystemoutprintfrdquo to format the printed output whileNetbeans uses more complicated way to do the same job (see below)

Javascript command line

Systemoutprintf(The resultant force on particle [+ mi +] due to the other masses is 14e N22f degnfangdirf)

Netbeansdff = new javatextDecimalFormat(ldquo0E00rdquo)df di f D i lF t(ldquo0 00rdquo)

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3558

jTextField2setText(ldquordquo + mi) jTextField3setText(ldquordquo + dffformat(f)) jTextField4setText(ldquordquo + dfangdirfformat(angdirf))

ldquodffrdquo and ldquodfangdirfrdquo are new variables that should be declared and added to format thenumbers into decimal format ie ldquo0E00rdquo to get a scientific format eg ldquo20019E-10rdquo and ldquo000rdquoto get a decimal number eg ldquo-1570rdquo

To use DecimalFormat you have to put ldquoimport javatextDecimalFormatrdquo in the top of thecoding as shown in Figure 8

Figure 8

To reset the calculation you need to put coding into the ldquoresetrdquo button (this is ldquoJButton2rdquo)First click on the ldquoresetrdquo button right click choose Events and then choose Action thenactionPerformed (similar like in Figure 6) It will bring you to where you should insert your coding(Figure 9) Write the following coding (Figure 9)

Figure 9

To exit the program you need to put coding into the ldquoexitrdquo button (this is ldquoJButton3rdquo) Firstclick on the ldquoexitrdquo button right click choose Events and then choose Action then actionPerformed(similar like in Figure 6) It will bring you to where you should insert your coding (Figure 10) Writethe following coding (Figure 10)

Figure 10

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3658

Once you finished now you can run and see the result If everything is fine then the output wilbe look like this (Figure 11)

Figure 11

Remember when you compile and run ldquoGravityAttract07ajavardquo using Javascripts commandline once you compile and run the result was looked like this

On which particle you want to calculate the resulting force 2The resultant force on particle [2] due to the other masses is 30673e-10 N 8837 deg

SEE THE DIFFERENCES AT THE USER INTERFACE

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3758

8th Step

Written by Mohd Khalid Kasmin

Monday 27 August 2007

Programming using command line interface (CLI) has its advantages but todays computer users are so used to graphical user

interface (GUI) that we just can no longer be contented with writing application that do not use the GUI Hence starting from this 8thstep we are going to embark on learning how to make java application that utilises the the GUI

It is possible to write a GUI application with the text based tools that we have been using for these past 7 steps but the job is so

messy that most of you will definitely be turnoff by it So to make life slightly easier well use an integrated development

environment (IDE) known as NetBeans specifically NetBeans 551

0 First ssh to the our server Next once logged in just type nice -n15 netbeans amp on the terminal If you had sshed with the -Y

switch after a few tens of seconds NetBeans should emerge on your display screen

One important point to note Since our server can readily handle up to 100 users (maybe even

more) doing work in CLI mode this machine will certainly be taxed to its maximum capability once

we start using graphical based application NetBeans 551 uses a geat deal of computer memory

and with the 4Gbytes RAM server it can only accomodate up to 25 users concurrently using

NetBeans if the users use the application wisely What is meant by wisely here is that (a) when

starting the NetBeans application please use it together with the nice -n15 command (b) in using

the NetBeans please make sure you only have one project open at a time and (c) please close your

project everytime before quitting the NetBeans

Home Profile Teaching Links Contact Me My Work

Newsflash

Yesterday all servers in the US went out on strike in a bid to get more RAM and better CPUs A spokes person said that the

need for better RAM was due to some fool increasing the front-side bus speed In future busses will be told to slow down in

residential motherboards

search

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3858

1

2

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3958

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4058

[ Back ]

5

Last Updated ( Wednesday 31 October 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4158

8th Step (Cont)

Written by Mohd Khalid Kasmin

Monday 27 August 2007

Up to this point we have define the name of our project Next we construct a class and to follow what we already know we name the class the sam

project name latihan01

6

7

Home Profile Teaching Links Contact Me My Work

Newsflash

Joomla 10 - Experience the Freedom It has never been easier to create your own dynamic site Manage all your content from

the best CMS admin interface

search

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4258

8

9

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4358

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4458

8th Step (Cont still)

Written by Mohd Khalid Kasmin

Monday 27 August 2007

In item (9) we see that Netbeans does not provide us with the main method So to follow what we already know we now put in the

main method

10

Home Profile Teaching Links Contact Me My Work

Newsflash

Aoccdrnig to a rscheearch at an Elingsh uinervtisy it deosnt mttaer in waht oredr the ltteers in a wrod are the olny iprmoetnt

tihng is taht frist and lsat ltteer is at the rghit pclae The rset can be a toatl mses and you can sitll raed it wouthit porbelm Tihs is

bcuseae we do not raed ervey lteter by itslef but the wrod as a wlohe

search

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4558

11

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4658

12

13

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4758

14

15

Last Updated ( Monday 27 August 2007 )

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4858

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4958

9th Step

Thursday 06 September 2007

Step by Step into Java Programming

for

University Physics Student

By

Mohd Khalid Kasmin

A Building graphical user interface - preparing the JFrame Form

At this stage of our programming experience with Java and NetBeans please follow strictly the steps outline in this article First and

make a new GUI project make sure there is no other project that already been loaded by Netbeans Please close any project displayed

1 Create new project

- On the main menu click on File --gt New Project (note Close first any previous project)

- Specify project name amp project location - Untick Create Main Class item

- Click on the Finish button

Home Profile Teaching Links Contact Me My Work

Newsflash

Aoccdrnig to a rscheearch at an Elingsh uinervtisy it deosnt mttaer in waht oredr the ltteers in a wrod are the olny iprmoetnt

tihng is taht frist and lsat ltteer is at the rghit pclae The rset can be a toatl mses and you can sitll raed it wouthit porbelm Tihs is

bcuseae we do not raed ervey lteter by itslef but the wrod as a wlohe

search

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5058

Figure 91

2 Create UI class

- Right click on the project name in the Project pane

on the popup menu select New --gt JFrame Form

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5158

Figure 92

- In the New JFrame Form dialog specify the Class Name and the Package name

eg Class Name multiplierUI Package mymultiplier

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5258

Figure 93

After clicking on the Finish button we should get something like the following

Figure 94

Last Updated ( Tuesday 18 September 2007 )

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5358

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5458

9th Step (Cont)

Written by Mohd Khalid Kasmin

Tuesday 11 September 2007

Step by Step into Java Programming

for University Physics Student

By

Mohd Khalid Kasmin

B Building user interface (UI) - putting in the components

Now that we have a proper blank form we can start filling the form with the suitable graphical components For introduction to

GUI programming an appropriate application to build (for a physics student) is a simple solver So our first exercise is to build an

application that takes two numbers and multiply them and display the result

With GUI we usually give input values to an application via input boxes that are labelled When all inputs are typed in properlywe click on a button to ask the application to execute the job Hence in this exercise well do just that The target now is to

construct a user interface that looks like Figure 95 below

Figure 95

Home Profile Teaching Links Contact Me My Work

Newsflash

Joomla 10 - Experience the Freedom It has never been easier to create your own dynamic site Manage all your content from

the best CMS admin interface

search

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5558

[ Back ]

1 Problemapplication description box

- In the palette pane click on Swing component named JTextPane (Note click just once) Move the mouse pointer on top of

your JFrame (Note just move not drag) When you see a little white box with orange border position it somewhere near the

top part of your JFrame and click once The JTextPane is then attached on the JFrame Addjust its size with the help of the

sizing hook around the perimeter of the JTextPane

- Edit the text property of the JTextPane with the description of your application

2 Input boxes

- In the palette pane click on Swing component named JTextField Next move the mouse pointer on top of your JFrame

When you see a white box with the word jTextField1 position it to the left and under the JTextPane1

- Click on Swing component JTextField in the palette pane again Move the mouse pointer on top of the JFrame you should

get a second JTextField named jTextField2 Position it to the right of jTextField1

Last Updated ( Tuesday 11 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5658

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5758

multiplication if a b and c were first declared as double or float or int variables So the code of the calculation should look like the

following

double a b c

a =

b =

c = a b

Clearly variables a and b need to be assigned with values of typed double Now looking back at inputboxes A and B we made

those boxes using the JTextField component meaning that any data or value written in a JTextField is of typed Text or String even

if the value displayed in the box is ldquo125rdquo But f irst here there is this task of getting to that Text (or String) values ldquo125rdquo and ldquo72rdquo

In Java we can get to a text in JTextField component using the getText() method So to retrieve values in jTextField1 and

jTextField2 we can use

jTextField1getText()

and

jTextField2getText()

But those values are still in text mode and we need another method to convert the data mode f rom text to double For that we can

use the method DoubleparseDouble() Thus the whole process of retrieve text -gt convert text to double -gt assign to double

variable can be done using

a = DoubleparseDouble( jTextField1getText() )

and

b = DoubleparseDouble( jTextField2getText() )

Hence the code for performing the multiplication becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

Up to this point we have the variable c as the result of the multiplication of a and b So the next thing to do is to display this

result on the JLabel that we have already prepared before

A x B = _____

The text A x B = ______ was made using JLabel component and its variable name is jLabel3 Since the variable c is of type

double and text property of JLabel is of type String we need to convert it first to String data type The method to convert a double

to String is DoubletoString() So we can write the code as DoubletoString(c) After getting the text we can now use it to be the

text displayed on jLabel3 To do that we use the method setText() Hence the appropriate code for displaying the result is

jLabel3setText( A x B = + DoubletoString(c) )

And the full code for our calculation becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

jLabel3setText( A x B = + DoubletoString(c) )

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5858

[ Back ]

Last Updated ( Tuesday 18 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

Page 19: Computional Physics

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 1958

Gnuplot

The smaller the delt the closer the curve to the exact solution

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2058

983107983119983117983120983125983124983105983124983113983119983118983105983116 983120983112983129983123983113983107983123

983080983123983123983120983090983089983090983090983081

983113983149983152983154983151983158983141983140 983109983157983148983137983154 983149983141983156983144983151983140

983138983161

983105983138983140983086 983115983144983137983149983145983149 983113983155983149983137983145983148

983117983151983144983140 983115983144983137983148983145983140 983115983137983155983149983145983150

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2158

Improved Euler method

Comparison between analytical solution of F=ma with the Euler method shows that although the

method is able to find approximate solution the error is quite significant if computational step is

reduced The error is also cumulative ie as the iteration of the calculation increases the error also gets

bigger hence value furthest away from initial condition has the largest error

The Euler method gives us a way to approximate positions of object with respect to time but requires

an order of magnitude of computational iterations for each order of magnitude of accuracy For large

number of objects this method will certainly consume a great deal of computation time Hence

students are introduced to Initial Half-Step Method an Euler method with a much improved

computational effeciency

Source code

Study of motion of an object with mass m

experiencing a constant force F using

Improved Euler Method (Initial half step method)

In X and Y dimension

import static javalangMath

public class fma04

public static void main(String args[])

double m fx fy x y vo vx vy ax ay t delt

double pi th theta ymax tmax xmax

pi = 40atan(10)

m = 01 in kg

fy = -02 in N (pulling down force)

fx = 00 in N (retarding force)

ay = fym

ax = fxm

vo = 500 in ms

theta = 300 in degrees

th = theta(pi1800) degrees change to radian

vx = vo cos(th) in ms vy = vo sin(th) in ms

y = 12 in m

x = 00 in m

ymax=y initialize var ymax with initial value of y

xmax=00

tmax=00

delt = 001 in s

for(t=00 tlt=300 t=t+delt)

System outprintf(f f fntxy)

if (t==00)vx = vx + 05axdelt

vy = vy + 05aydelt

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2258

else

vx = vx + axdelt

vy = vy + aydelt

x = x + vxdelt

y = y + vydelt

if (ygtymax)

ymax=y

xmax=xtmax=t

if (ylt=00) break

System outprintf( Value of Y maximum is f happened when t = f

nymaxtmax)

System outprintf( at x = fnxmax)

System outprintf( The object reach the ground at x=f and t=fn xt)

gnuplot result

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2358

983107983119983117983120983125983124983105983124983113983119983118983105983116

983120983112983129983123983113983107983123 983080983123983123983120983090983089983090983090983081

983122983141983155983156983151983154983145983150983143 983110983151983154983139983141

983138983161

983105983138983140983086 983115983144983137983149983145983149 983113983155983149983137983145983148

983117983151983144983140 983115983144983137983148983145983140 983115983137983155983149983145983150

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2458

Damped Harmonic Oscillation

Object m is on a flat surface attached to a spring as in figure above The object is pulled

to position A and released so that it oscillates about x =0 Assuming that the force F=ma= -bv-kx where x is position of the object v is the velocity k the spring constant andb is a constant associated with friction with the flat surface

The algorithm

1 Declare the classmethod to be used2 Declare and assign all variables to be used

x t delt k b A m F a v v0 and x03 Declare and assign constants to the respective variables

k = 02 b = 01 A = 01 m = 054 Select appropriate value for ∆t 5 Initialized initial values xo=A vo=00

6 set x=xo v=vo and t = 07 calculate the value of force F = -kx-bv 8 calculate the value of acceleration a = Fm

9 calculate velocity v = vo + ∆v = vo + a ∆t

10 calculate position x = xo + ∆x = xo + v ∆t

11 increment value of t by ∆t 12 print out values of t and x

13 swap the value of xo=x vo=v 14 repeat from step 4 until stopping condition is satisfied (tlt=600)

The java code

public class simplehar01

public static void main(String args[])double x t deltdouble k b A m F a v v0 x0

k = 02b = 01A = 0 1

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2558

m = 05delt = 0001v0 = 00x0 = Ax = x0 v = v0

t = 00while(tlt=600)

F = -kx-bva = Fmv = v0 + adeltx = x0 + vdeltt = t + deltSystemoutprintf(f fntx)x0 = xv0 = v

gnuplot result

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2658

983107983119983117983120983125983124983105983124983113983119983118983105983116 983120983112983129983123983113983107983123

(983123983123983120983090983089983090983090983081

983106983157983145983148983140983145983150983143 983137 983152983144983161983155983145983139983155 983155983151983148983158983141983154

983157983155983145983150983143 983118983141983156983138983141983137983150983155

983138983161

983105983138983140983086 983115983144983137983149983145983149 983113983155983149983137983145983148

983117983151983144983140 983115983144983137983148983145983140 983115983137983155983149983145983150

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2758

How to create a user interface (UI) using Netbeans

Based on the ldquoGravityAttract07ajavardquo source code (shown below) build a user interface usingNetbeans The design of the UI could be look like this (Figure1)

Figure 1

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2858

ldquoGravityAttract07ajavardquo source code

Java class for calculating the resultant force on any object(ie either on m1m2 m3 m4 or m5)

import static javalangMathimport javaiopublic class GravityAttract07a

public static void main(String args[])

double[] m = new double[6] mass of objectdouble[] x = new double[6] x-coor of objectdouble[] y = new double[6] y-coor of objectdouble f resultant forcedouble fxfy x amp y component of forcedouble rx ry rp co G dirfangdirf

int j

G = 667e-11m[1]=25 x[1]=12 y[1]=25m[2]=15 x[2]=15 y[2]=15m[3]=75 x[3]=02 y[3]=55m[4]=92 x[4]=52 y[4]=25m[5]=66 x[5]=42 y[5]=45BufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin))String mass=Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))try mass=dataInreadLine()

catch(IOException e)Systemoutprintln(Not a valid input value)int mi = IntegerparseInt(mass)fx = 00fy = 00

for(j=1 jlt=5 j++)

if (j=mi)rx = x[j]-x[mi]ry = y[j]-y[mi]rp = pow((rxrx+ryry)15) (rx^2 + ry^2) raised to the power of 32co = Gm[mi]m[j]

fx = fx + corxrpfy = fy + coryrp

f = sqrt(fxfx + fyfy)dirf = toDegrees(atan(fyfx))Systemoutprintf(The force on m[+ mi +] due to the other masses is eN fdegnfdirf)

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2958

This example will not show you step by step details procedure since it already explained inthe class and in the e-learning Please refer to the e-learning about how to create a project how tocreate JFrame Form and as well how to create JButton JTextField JLabel JTextPane and so on

First create a new project and give a name eg ldquogravityrdquo and then create JFrame Form andthe following JButton JTextField JLabel JTextPane etc by using ldquoswing paletterdquo as shown in Figure2

Figure 2

To insert a picture into JLabel2 click on the JLabel2 go to properties and select ldquoiconrdquo Onthe icon editor choose file and then select file to locate the directory of your graphicpicture file(Figure 3) Select the file and click OK when finished Make sure you have already created your ownpicture file and save it as jpg png bmp etc format eg ldquogravityjpgrdquo Use any graphic package todraw your diagrampicture or use drawing tools available in Open Office or Microsoft Word

JTextPane1JLabel1

JLabel2

JLabel3JLabel4

JLabel5

JLabel6 JLabel7

JLabel8

JTextField2 JTextField3 JTextField4

JTextF

JButton1JButton2

JButton3

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3058

Figure 3

To write documentation or question eg in JTextPane1 click on the JTextPane1 go toproperties and select ldquotextrdquo Type the documentation in the text editor as shown in Figure 4 Click OKwhen finished

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3158

To do formatting on the text such as superscript and subscript eg in jLabel3 click on the

JLabel3 go to properties and select ldquotextrdquo Type the documentationmessage on the text editor asshown in Figure 5 as follows

eg

lthtmlgtOn which particle (mltsubgt1ltsubgtmltsubgt5ltsubgt) you want to calculate theresultant force

Use sup instead if you want a superscriptThe result will look like this

Click OK when finished

Figure 5

To do the calculation you need to put the coding similar like ldquoGravityAttract07ajavardquo and put itinto the ldquocalculaterdquo button (this is ldquoJButton1rdquo) You can copy ldquoGravityAttract07a javardquo source code

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3258

paste it and then do a few alterations First click on the ldquocalculaterdquo button right click choose Eventsand then choose Action then actionPerformed (Figure 6) It will bring you to where you should insertyour coding (Figure 7) You can write your own coding or copypaste any coding from somewhereelse (in this case from ldquoGravityAttract07ajavardquo)

Figure 6

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3358

Figure 7

Copy ldquoGravityAttract07ajavardquo source code and paste to the place after ldquo TODO add youhandling code hererdquo see Figure 7 To see which part has been modified the coding in Figure 7 hasbeen color coded for better understanding as follows

private void jButton1ActionPerformed(javaawteventActionEvent evt) TODO add your handling code here

double[] m = new double[6] mass of objectdouble[] x = new double[6] x-coor of objectdouble[] y = new double[6] y-coor of objectdouble f resultant forcedouble fxfy x amp y component of forcedouble rx ry rp co G dirfangdirfDecimalFormat dff dfangdirfint j mi

G = 667e-11m[1]=25 x[1]=12 y[1]=25m[2]=15 x[2]=15 y[2]=15m[3]=75 x[3]=02 y[3]=55m[4]=92 x[4]=52 y[4]=25m[5]=66 x[5]=42 y[5]=45

BufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin)) String mass= Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3458

catch(IOException e)Systemoutprintln(Not a valid input value) int mi = IntegerparseInt(mass)mi = IntegerparseInt(jTextField1getText())fx = 00fy = 00for(j=1 jlt=5 j++)

if (j=mi)rx = x[j]-x[mi]ry = y[j]-y[mi]rp = Mathpow((rxrx+ryry)15) (rx^2 + ry^2) raised to the power of 32co = Gm[mi]m[j]fx = fx + corxrpfy = fy + coryrp

f = Mathsqrt(fxfx + fyfy)

dirf = Mathatan(fyfx)angdirf = (dirf(22070))1800 Systemoutprintf(ldquoThe resultant force on particle [ldquo+ mi +rdquo] due to the other masses is

14e N 22f degnrdquofangdirf)dff = new javatextDecimalFormat(ldquoE00rdquo)dfangdirf = new DecimalFormat(ldquo000rdquo)

jTextField2setText(ldquordquo + mi) jTextField3setText(ldquordquo + dffformat(f)) jTextField4setText(ldquordquo + dfangdirfformat(angdirf))

Note

Brown = automatically generatedBlue = the original code from ldquoGravityAttract07ajavardquoGreen = coding from ldquoGravityAttract07ajavardquo which were being disabled due to

incompatibilities with NetbeansRed = a new inserted coding a few of them is to rephrasesubstitute the green one

Javascripts command line useBufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin))String mass=Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))try mass=dataInreadLine()catch(IOException e)Systemoutprintln(Not a valid input value)

int mi = IntegerparseInt(mass)

to read any input from the keyboard typed by the user while Netbeans uses egldquoIntegerparseInt(jTextField1getText())rdquo etc Javascripts command line uses ldquoSystemoutprintfrdquo to format the printed output whileNetbeans uses more complicated way to do the same job (see below)

Javascript command line

Systemoutprintf(The resultant force on particle [+ mi +] due to the other masses is 14e N22f degnfangdirf)

Netbeansdff = new javatextDecimalFormat(ldquo0E00rdquo)df di f D i lF t(ldquo0 00rdquo)

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3558

jTextField2setText(ldquordquo + mi) jTextField3setText(ldquordquo + dffformat(f)) jTextField4setText(ldquordquo + dfangdirfformat(angdirf))

ldquodffrdquo and ldquodfangdirfrdquo are new variables that should be declared and added to format thenumbers into decimal format ie ldquo0E00rdquo to get a scientific format eg ldquo20019E-10rdquo and ldquo000rdquoto get a decimal number eg ldquo-1570rdquo

To use DecimalFormat you have to put ldquoimport javatextDecimalFormatrdquo in the top of thecoding as shown in Figure 8

Figure 8

To reset the calculation you need to put coding into the ldquoresetrdquo button (this is ldquoJButton2rdquo)First click on the ldquoresetrdquo button right click choose Events and then choose Action thenactionPerformed (similar like in Figure 6) It will bring you to where you should insert your coding(Figure 9) Write the following coding (Figure 9)

Figure 9

To exit the program you need to put coding into the ldquoexitrdquo button (this is ldquoJButton3rdquo) Firstclick on the ldquoexitrdquo button right click choose Events and then choose Action then actionPerformed(similar like in Figure 6) It will bring you to where you should insert your coding (Figure 10) Writethe following coding (Figure 10)

Figure 10

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3658

Once you finished now you can run and see the result If everything is fine then the output wilbe look like this (Figure 11)

Figure 11

Remember when you compile and run ldquoGravityAttract07ajavardquo using Javascripts commandline once you compile and run the result was looked like this

On which particle you want to calculate the resulting force 2The resultant force on particle [2] due to the other masses is 30673e-10 N 8837 deg

SEE THE DIFFERENCES AT THE USER INTERFACE

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3758

8th Step

Written by Mohd Khalid Kasmin

Monday 27 August 2007

Programming using command line interface (CLI) has its advantages but todays computer users are so used to graphical user

interface (GUI) that we just can no longer be contented with writing application that do not use the GUI Hence starting from this 8thstep we are going to embark on learning how to make java application that utilises the the GUI

It is possible to write a GUI application with the text based tools that we have been using for these past 7 steps but the job is so

messy that most of you will definitely be turnoff by it So to make life slightly easier well use an integrated development

environment (IDE) known as NetBeans specifically NetBeans 551

0 First ssh to the our server Next once logged in just type nice -n15 netbeans amp on the terminal If you had sshed with the -Y

switch after a few tens of seconds NetBeans should emerge on your display screen

One important point to note Since our server can readily handle up to 100 users (maybe even

more) doing work in CLI mode this machine will certainly be taxed to its maximum capability once

we start using graphical based application NetBeans 551 uses a geat deal of computer memory

and with the 4Gbytes RAM server it can only accomodate up to 25 users concurrently using

NetBeans if the users use the application wisely What is meant by wisely here is that (a) when

starting the NetBeans application please use it together with the nice -n15 command (b) in using

the NetBeans please make sure you only have one project open at a time and (c) please close your

project everytime before quitting the NetBeans

Home Profile Teaching Links Contact Me My Work

Newsflash

Yesterday all servers in the US went out on strike in a bid to get more RAM and better CPUs A spokes person said that the

need for better RAM was due to some fool increasing the front-side bus speed In future busses will be told to slow down in

residential motherboards

search

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3858

1

2

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3958

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4058

[ Back ]

5

Last Updated ( Wednesday 31 October 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4158

8th Step (Cont)

Written by Mohd Khalid Kasmin

Monday 27 August 2007

Up to this point we have define the name of our project Next we construct a class and to follow what we already know we name the class the sam

project name latihan01

6

7

Home Profile Teaching Links Contact Me My Work

Newsflash

Joomla 10 - Experience the Freedom It has never been easier to create your own dynamic site Manage all your content from

the best CMS admin interface

search

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4258

8

9

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4358

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4458

8th Step (Cont still)

Written by Mohd Khalid Kasmin

Monday 27 August 2007

In item (9) we see that Netbeans does not provide us with the main method So to follow what we already know we now put in the

main method

10

Home Profile Teaching Links Contact Me My Work

Newsflash

Aoccdrnig to a rscheearch at an Elingsh uinervtisy it deosnt mttaer in waht oredr the ltteers in a wrod are the olny iprmoetnt

tihng is taht frist and lsat ltteer is at the rghit pclae The rset can be a toatl mses and you can sitll raed it wouthit porbelm Tihs is

bcuseae we do not raed ervey lteter by itslef but the wrod as a wlohe

search

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4558

11

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4658

12

13

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4758

14

15

Last Updated ( Monday 27 August 2007 )

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4858

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4958

9th Step

Thursday 06 September 2007

Step by Step into Java Programming

for

University Physics Student

By

Mohd Khalid Kasmin

A Building graphical user interface - preparing the JFrame Form

At this stage of our programming experience with Java and NetBeans please follow strictly the steps outline in this article First and

make a new GUI project make sure there is no other project that already been loaded by Netbeans Please close any project displayed

1 Create new project

- On the main menu click on File --gt New Project (note Close first any previous project)

- Specify project name amp project location - Untick Create Main Class item

- Click on the Finish button

Home Profile Teaching Links Contact Me My Work

Newsflash

Aoccdrnig to a rscheearch at an Elingsh uinervtisy it deosnt mttaer in waht oredr the ltteers in a wrod are the olny iprmoetnt

tihng is taht frist and lsat ltteer is at the rghit pclae The rset can be a toatl mses and you can sitll raed it wouthit porbelm Tihs is

bcuseae we do not raed ervey lteter by itslef but the wrod as a wlohe

search

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5058

Figure 91

2 Create UI class

- Right click on the project name in the Project pane

on the popup menu select New --gt JFrame Form

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5158

Figure 92

- In the New JFrame Form dialog specify the Class Name and the Package name

eg Class Name multiplierUI Package mymultiplier

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5258

Figure 93

After clicking on the Finish button we should get something like the following

Figure 94

Last Updated ( Tuesday 18 September 2007 )

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5358

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5458

9th Step (Cont)

Written by Mohd Khalid Kasmin

Tuesday 11 September 2007

Step by Step into Java Programming

for University Physics Student

By

Mohd Khalid Kasmin

B Building user interface (UI) - putting in the components

Now that we have a proper blank form we can start filling the form with the suitable graphical components For introduction to

GUI programming an appropriate application to build (for a physics student) is a simple solver So our first exercise is to build an

application that takes two numbers and multiply them and display the result

With GUI we usually give input values to an application via input boxes that are labelled When all inputs are typed in properlywe click on a button to ask the application to execute the job Hence in this exercise well do just that The target now is to

construct a user interface that looks like Figure 95 below

Figure 95

Home Profile Teaching Links Contact Me My Work

Newsflash

Joomla 10 - Experience the Freedom It has never been easier to create your own dynamic site Manage all your content from

the best CMS admin interface

search

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5558

[ Back ]

1 Problemapplication description box

- In the palette pane click on Swing component named JTextPane (Note click just once) Move the mouse pointer on top of

your JFrame (Note just move not drag) When you see a little white box with orange border position it somewhere near the

top part of your JFrame and click once The JTextPane is then attached on the JFrame Addjust its size with the help of the

sizing hook around the perimeter of the JTextPane

- Edit the text property of the JTextPane with the description of your application

2 Input boxes

- In the palette pane click on Swing component named JTextField Next move the mouse pointer on top of your JFrame

When you see a white box with the word jTextField1 position it to the left and under the JTextPane1

- Click on Swing component JTextField in the palette pane again Move the mouse pointer on top of the JFrame you should

get a second JTextField named jTextField2 Position it to the right of jTextField1

Last Updated ( Tuesday 11 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5658

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5758

multiplication if a b and c were first declared as double or float or int variables So the code of the calculation should look like the

following

double a b c

a =

b =

c = a b

Clearly variables a and b need to be assigned with values of typed double Now looking back at inputboxes A and B we made

those boxes using the JTextField component meaning that any data or value written in a JTextField is of typed Text or String even

if the value displayed in the box is ldquo125rdquo But f irst here there is this task of getting to that Text (or String) values ldquo125rdquo and ldquo72rdquo

In Java we can get to a text in JTextField component using the getText() method So to retrieve values in jTextField1 and

jTextField2 we can use

jTextField1getText()

and

jTextField2getText()

But those values are still in text mode and we need another method to convert the data mode f rom text to double For that we can

use the method DoubleparseDouble() Thus the whole process of retrieve text -gt convert text to double -gt assign to double

variable can be done using

a = DoubleparseDouble( jTextField1getText() )

and

b = DoubleparseDouble( jTextField2getText() )

Hence the code for performing the multiplication becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

Up to this point we have the variable c as the result of the multiplication of a and b So the next thing to do is to display this

result on the JLabel that we have already prepared before

A x B = _____

The text A x B = ______ was made using JLabel component and its variable name is jLabel3 Since the variable c is of type

double and text property of JLabel is of type String we need to convert it first to String data type The method to convert a double

to String is DoubletoString() So we can write the code as DoubletoString(c) After getting the text we can now use it to be the

text displayed on jLabel3 To do that we use the method setText() Hence the appropriate code for displaying the result is

jLabel3setText( A x B = + DoubletoString(c) )

And the full code for our calculation becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

jLabel3setText( A x B = + DoubletoString(c) )

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5858

[ Back ]

Last Updated ( Tuesday 18 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

Page 20: Computional Physics

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2058

983107983119983117983120983125983124983105983124983113983119983118983105983116 983120983112983129983123983113983107983123

983080983123983123983120983090983089983090983090983081

983113983149983152983154983151983158983141983140 983109983157983148983137983154 983149983141983156983144983151983140

983138983161

983105983138983140983086 983115983144983137983149983145983149 983113983155983149983137983145983148

983117983151983144983140 983115983144983137983148983145983140 983115983137983155983149983145983150

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2158

Improved Euler method

Comparison between analytical solution of F=ma with the Euler method shows that although the

method is able to find approximate solution the error is quite significant if computational step is

reduced The error is also cumulative ie as the iteration of the calculation increases the error also gets

bigger hence value furthest away from initial condition has the largest error

The Euler method gives us a way to approximate positions of object with respect to time but requires

an order of magnitude of computational iterations for each order of magnitude of accuracy For large

number of objects this method will certainly consume a great deal of computation time Hence

students are introduced to Initial Half-Step Method an Euler method with a much improved

computational effeciency

Source code

Study of motion of an object with mass m

experiencing a constant force F using

Improved Euler Method (Initial half step method)

In X and Y dimension

import static javalangMath

public class fma04

public static void main(String args[])

double m fx fy x y vo vx vy ax ay t delt

double pi th theta ymax tmax xmax

pi = 40atan(10)

m = 01 in kg

fy = -02 in N (pulling down force)

fx = 00 in N (retarding force)

ay = fym

ax = fxm

vo = 500 in ms

theta = 300 in degrees

th = theta(pi1800) degrees change to radian

vx = vo cos(th) in ms vy = vo sin(th) in ms

y = 12 in m

x = 00 in m

ymax=y initialize var ymax with initial value of y

xmax=00

tmax=00

delt = 001 in s

for(t=00 tlt=300 t=t+delt)

System outprintf(f f fntxy)

if (t==00)vx = vx + 05axdelt

vy = vy + 05aydelt

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2258

else

vx = vx + axdelt

vy = vy + aydelt

x = x + vxdelt

y = y + vydelt

if (ygtymax)

ymax=y

xmax=xtmax=t

if (ylt=00) break

System outprintf( Value of Y maximum is f happened when t = f

nymaxtmax)

System outprintf( at x = fnxmax)

System outprintf( The object reach the ground at x=f and t=fn xt)

gnuplot result

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2358

983107983119983117983120983125983124983105983124983113983119983118983105983116

983120983112983129983123983113983107983123 983080983123983123983120983090983089983090983090983081

983122983141983155983156983151983154983145983150983143 983110983151983154983139983141

983138983161

983105983138983140983086 983115983144983137983149983145983149 983113983155983149983137983145983148

983117983151983144983140 983115983144983137983148983145983140 983115983137983155983149983145983150

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2458

Damped Harmonic Oscillation

Object m is on a flat surface attached to a spring as in figure above The object is pulled

to position A and released so that it oscillates about x =0 Assuming that the force F=ma= -bv-kx where x is position of the object v is the velocity k the spring constant andb is a constant associated with friction with the flat surface

The algorithm

1 Declare the classmethod to be used2 Declare and assign all variables to be used

x t delt k b A m F a v v0 and x03 Declare and assign constants to the respective variables

k = 02 b = 01 A = 01 m = 054 Select appropriate value for ∆t 5 Initialized initial values xo=A vo=00

6 set x=xo v=vo and t = 07 calculate the value of force F = -kx-bv 8 calculate the value of acceleration a = Fm

9 calculate velocity v = vo + ∆v = vo + a ∆t

10 calculate position x = xo + ∆x = xo + v ∆t

11 increment value of t by ∆t 12 print out values of t and x

13 swap the value of xo=x vo=v 14 repeat from step 4 until stopping condition is satisfied (tlt=600)

The java code

public class simplehar01

public static void main(String args[])double x t deltdouble k b A m F a v v0 x0

k = 02b = 01A = 0 1

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2558

m = 05delt = 0001v0 = 00x0 = Ax = x0 v = v0

t = 00while(tlt=600)

F = -kx-bva = Fmv = v0 + adeltx = x0 + vdeltt = t + deltSystemoutprintf(f fntx)x0 = xv0 = v

gnuplot result

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2658

983107983119983117983120983125983124983105983124983113983119983118983105983116 983120983112983129983123983113983107983123

(983123983123983120983090983089983090983090983081

983106983157983145983148983140983145983150983143 983137 983152983144983161983155983145983139983155 983155983151983148983158983141983154

983157983155983145983150983143 983118983141983156983138983141983137983150983155

983138983161

983105983138983140983086 983115983144983137983149983145983149 983113983155983149983137983145983148

983117983151983144983140 983115983144983137983148983145983140 983115983137983155983149983145983150

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2758

How to create a user interface (UI) using Netbeans

Based on the ldquoGravityAttract07ajavardquo source code (shown below) build a user interface usingNetbeans The design of the UI could be look like this (Figure1)

Figure 1

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2858

ldquoGravityAttract07ajavardquo source code

Java class for calculating the resultant force on any object(ie either on m1m2 m3 m4 or m5)

import static javalangMathimport javaiopublic class GravityAttract07a

public static void main(String args[])

double[] m = new double[6] mass of objectdouble[] x = new double[6] x-coor of objectdouble[] y = new double[6] y-coor of objectdouble f resultant forcedouble fxfy x amp y component of forcedouble rx ry rp co G dirfangdirf

int j

G = 667e-11m[1]=25 x[1]=12 y[1]=25m[2]=15 x[2]=15 y[2]=15m[3]=75 x[3]=02 y[3]=55m[4]=92 x[4]=52 y[4]=25m[5]=66 x[5]=42 y[5]=45BufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin))String mass=Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))try mass=dataInreadLine()

catch(IOException e)Systemoutprintln(Not a valid input value)int mi = IntegerparseInt(mass)fx = 00fy = 00

for(j=1 jlt=5 j++)

if (j=mi)rx = x[j]-x[mi]ry = y[j]-y[mi]rp = pow((rxrx+ryry)15) (rx^2 + ry^2) raised to the power of 32co = Gm[mi]m[j]

fx = fx + corxrpfy = fy + coryrp

f = sqrt(fxfx + fyfy)dirf = toDegrees(atan(fyfx))Systemoutprintf(The force on m[+ mi +] due to the other masses is eN fdegnfdirf)

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2958

This example will not show you step by step details procedure since it already explained inthe class and in the e-learning Please refer to the e-learning about how to create a project how tocreate JFrame Form and as well how to create JButton JTextField JLabel JTextPane and so on

First create a new project and give a name eg ldquogravityrdquo and then create JFrame Form andthe following JButton JTextField JLabel JTextPane etc by using ldquoswing paletterdquo as shown in Figure2

Figure 2

To insert a picture into JLabel2 click on the JLabel2 go to properties and select ldquoiconrdquo Onthe icon editor choose file and then select file to locate the directory of your graphicpicture file(Figure 3) Select the file and click OK when finished Make sure you have already created your ownpicture file and save it as jpg png bmp etc format eg ldquogravityjpgrdquo Use any graphic package todraw your diagrampicture or use drawing tools available in Open Office or Microsoft Word

JTextPane1JLabel1

JLabel2

JLabel3JLabel4

JLabel5

JLabel6 JLabel7

JLabel8

JTextField2 JTextField3 JTextField4

JTextF

JButton1JButton2

JButton3

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3058

Figure 3

To write documentation or question eg in JTextPane1 click on the JTextPane1 go toproperties and select ldquotextrdquo Type the documentation in the text editor as shown in Figure 4 Click OKwhen finished

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3158

To do formatting on the text such as superscript and subscript eg in jLabel3 click on the

JLabel3 go to properties and select ldquotextrdquo Type the documentationmessage on the text editor asshown in Figure 5 as follows

eg

lthtmlgtOn which particle (mltsubgt1ltsubgtmltsubgt5ltsubgt) you want to calculate theresultant force

Use sup instead if you want a superscriptThe result will look like this

Click OK when finished

Figure 5

To do the calculation you need to put the coding similar like ldquoGravityAttract07ajavardquo and put itinto the ldquocalculaterdquo button (this is ldquoJButton1rdquo) You can copy ldquoGravityAttract07a javardquo source code

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3258

paste it and then do a few alterations First click on the ldquocalculaterdquo button right click choose Eventsand then choose Action then actionPerformed (Figure 6) It will bring you to where you should insertyour coding (Figure 7) You can write your own coding or copypaste any coding from somewhereelse (in this case from ldquoGravityAttract07ajavardquo)

Figure 6

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3358

Figure 7

Copy ldquoGravityAttract07ajavardquo source code and paste to the place after ldquo TODO add youhandling code hererdquo see Figure 7 To see which part has been modified the coding in Figure 7 hasbeen color coded for better understanding as follows

private void jButton1ActionPerformed(javaawteventActionEvent evt) TODO add your handling code here

double[] m = new double[6] mass of objectdouble[] x = new double[6] x-coor of objectdouble[] y = new double[6] y-coor of objectdouble f resultant forcedouble fxfy x amp y component of forcedouble rx ry rp co G dirfangdirfDecimalFormat dff dfangdirfint j mi

G = 667e-11m[1]=25 x[1]=12 y[1]=25m[2]=15 x[2]=15 y[2]=15m[3]=75 x[3]=02 y[3]=55m[4]=92 x[4]=52 y[4]=25m[5]=66 x[5]=42 y[5]=45

BufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin)) String mass= Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3458

catch(IOException e)Systemoutprintln(Not a valid input value) int mi = IntegerparseInt(mass)mi = IntegerparseInt(jTextField1getText())fx = 00fy = 00for(j=1 jlt=5 j++)

if (j=mi)rx = x[j]-x[mi]ry = y[j]-y[mi]rp = Mathpow((rxrx+ryry)15) (rx^2 + ry^2) raised to the power of 32co = Gm[mi]m[j]fx = fx + corxrpfy = fy + coryrp

f = Mathsqrt(fxfx + fyfy)

dirf = Mathatan(fyfx)angdirf = (dirf(22070))1800 Systemoutprintf(ldquoThe resultant force on particle [ldquo+ mi +rdquo] due to the other masses is

14e N 22f degnrdquofangdirf)dff = new javatextDecimalFormat(ldquoE00rdquo)dfangdirf = new DecimalFormat(ldquo000rdquo)

jTextField2setText(ldquordquo + mi) jTextField3setText(ldquordquo + dffformat(f)) jTextField4setText(ldquordquo + dfangdirfformat(angdirf))

Note

Brown = automatically generatedBlue = the original code from ldquoGravityAttract07ajavardquoGreen = coding from ldquoGravityAttract07ajavardquo which were being disabled due to

incompatibilities with NetbeansRed = a new inserted coding a few of them is to rephrasesubstitute the green one

Javascripts command line useBufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin))String mass=Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))try mass=dataInreadLine()catch(IOException e)Systemoutprintln(Not a valid input value)

int mi = IntegerparseInt(mass)

to read any input from the keyboard typed by the user while Netbeans uses egldquoIntegerparseInt(jTextField1getText())rdquo etc Javascripts command line uses ldquoSystemoutprintfrdquo to format the printed output whileNetbeans uses more complicated way to do the same job (see below)

Javascript command line

Systemoutprintf(The resultant force on particle [+ mi +] due to the other masses is 14e N22f degnfangdirf)

Netbeansdff = new javatextDecimalFormat(ldquo0E00rdquo)df di f D i lF t(ldquo0 00rdquo)

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3558

jTextField2setText(ldquordquo + mi) jTextField3setText(ldquordquo + dffformat(f)) jTextField4setText(ldquordquo + dfangdirfformat(angdirf))

ldquodffrdquo and ldquodfangdirfrdquo are new variables that should be declared and added to format thenumbers into decimal format ie ldquo0E00rdquo to get a scientific format eg ldquo20019E-10rdquo and ldquo000rdquoto get a decimal number eg ldquo-1570rdquo

To use DecimalFormat you have to put ldquoimport javatextDecimalFormatrdquo in the top of thecoding as shown in Figure 8

Figure 8

To reset the calculation you need to put coding into the ldquoresetrdquo button (this is ldquoJButton2rdquo)First click on the ldquoresetrdquo button right click choose Events and then choose Action thenactionPerformed (similar like in Figure 6) It will bring you to where you should insert your coding(Figure 9) Write the following coding (Figure 9)

Figure 9

To exit the program you need to put coding into the ldquoexitrdquo button (this is ldquoJButton3rdquo) Firstclick on the ldquoexitrdquo button right click choose Events and then choose Action then actionPerformed(similar like in Figure 6) It will bring you to where you should insert your coding (Figure 10) Writethe following coding (Figure 10)

Figure 10

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3658

Once you finished now you can run and see the result If everything is fine then the output wilbe look like this (Figure 11)

Figure 11

Remember when you compile and run ldquoGravityAttract07ajavardquo using Javascripts commandline once you compile and run the result was looked like this

On which particle you want to calculate the resulting force 2The resultant force on particle [2] due to the other masses is 30673e-10 N 8837 deg

SEE THE DIFFERENCES AT THE USER INTERFACE

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3758

8th Step

Written by Mohd Khalid Kasmin

Monday 27 August 2007

Programming using command line interface (CLI) has its advantages but todays computer users are so used to graphical user

interface (GUI) that we just can no longer be contented with writing application that do not use the GUI Hence starting from this 8thstep we are going to embark on learning how to make java application that utilises the the GUI

It is possible to write a GUI application with the text based tools that we have been using for these past 7 steps but the job is so

messy that most of you will definitely be turnoff by it So to make life slightly easier well use an integrated development

environment (IDE) known as NetBeans specifically NetBeans 551

0 First ssh to the our server Next once logged in just type nice -n15 netbeans amp on the terminal If you had sshed with the -Y

switch after a few tens of seconds NetBeans should emerge on your display screen

One important point to note Since our server can readily handle up to 100 users (maybe even

more) doing work in CLI mode this machine will certainly be taxed to its maximum capability once

we start using graphical based application NetBeans 551 uses a geat deal of computer memory

and with the 4Gbytes RAM server it can only accomodate up to 25 users concurrently using

NetBeans if the users use the application wisely What is meant by wisely here is that (a) when

starting the NetBeans application please use it together with the nice -n15 command (b) in using

the NetBeans please make sure you only have one project open at a time and (c) please close your

project everytime before quitting the NetBeans

Home Profile Teaching Links Contact Me My Work

Newsflash

Yesterday all servers in the US went out on strike in a bid to get more RAM and better CPUs A spokes person said that the

need for better RAM was due to some fool increasing the front-side bus speed In future busses will be told to slow down in

residential motherboards

search

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3858

1

2

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3958

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4058

[ Back ]

5

Last Updated ( Wednesday 31 October 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4158

8th Step (Cont)

Written by Mohd Khalid Kasmin

Monday 27 August 2007

Up to this point we have define the name of our project Next we construct a class and to follow what we already know we name the class the sam

project name latihan01

6

7

Home Profile Teaching Links Contact Me My Work

Newsflash

Joomla 10 - Experience the Freedom It has never been easier to create your own dynamic site Manage all your content from

the best CMS admin interface

search

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4258

8

9

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4358

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4458

8th Step (Cont still)

Written by Mohd Khalid Kasmin

Monday 27 August 2007

In item (9) we see that Netbeans does not provide us with the main method So to follow what we already know we now put in the

main method

10

Home Profile Teaching Links Contact Me My Work

Newsflash

Aoccdrnig to a rscheearch at an Elingsh uinervtisy it deosnt mttaer in waht oredr the ltteers in a wrod are the olny iprmoetnt

tihng is taht frist and lsat ltteer is at the rghit pclae The rset can be a toatl mses and you can sitll raed it wouthit porbelm Tihs is

bcuseae we do not raed ervey lteter by itslef but the wrod as a wlohe

search

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4558

11

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4658

12

13

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4758

14

15

Last Updated ( Monday 27 August 2007 )

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4858

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4958

9th Step

Thursday 06 September 2007

Step by Step into Java Programming

for

University Physics Student

By

Mohd Khalid Kasmin

A Building graphical user interface - preparing the JFrame Form

At this stage of our programming experience with Java and NetBeans please follow strictly the steps outline in this article First and

make a new GUI project make sure there is no other project that already been loaded by Netbeans Please close any project displayed

1 Create new project

- On the main menu click on File --gt New Project (note Close first any previous project)

- Specify project name amp project location - Untick Create Main Class item

- Click on the Finish button

Home Profile Teaching Links Contact Me My Work

Newsflash

Aoccdrnig to a rscheearch at an Elingsh uinervtisy it deosnt mttaer in waht oredr the ltteers in a wrod are the olny iprmoetnt

tihng is taht frist and lsat ltteer is at the rghit pclae The rset can be a toatl mses and you can sitll raed it wouthit porbelm Tihs is

bcuseae we do not raed ervey lteter by itslef but the wrod as a wlohe

search

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5058

Figure 91

2 Create UI class

- Right click on the project name in the Project pane

on the popup menu select New --gt JFrame Form

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5158

Figure 92

- In the New JFrame Form dialog specify the Class Name and the Package name

eg Class Name multiplierUI Package mymultiplier

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5258

Figure 93

After clicking on the Finish button we should get something like the following

Figure 94

Last Updated ( Tuesday 18 September 2007 )

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5358

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5458

9th Step (Cont)

Written by Mohd Khalid Kasmin

Tuesday 11 September 2007

Step by Step into Java Programming

for University Physics Student

By

Mohd Khalid Kasmin

B Building user interface (UI) - putting in the components

Now that we have a proper blank form we can start filling the form with the suitable graphical components For introduction to

GUI programming an appropriate application to build (for a physics student) is a simple solver So our first exercise is to build an

application that takes two numbers and multiply them and display the result

With GUI we usually give input values to an application via input boxes that are labelled When all inputs are typed in properlywe click on a button to ask the application to execute the job Hence in this exercise well do just that The target now is to

construct a user interface that looks like Figure 95 below

Figure 95

Home Profile Teaching Links Contact Me My Work

Newsflash

Joomla 10 - Experience the Freedom It has never been easier to create your own dynamic site Manage all your content from

the best CMS admin interface

search

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5558

[ Back ]

1 Problemapplication description box

- In the palette pane click on Swing component named JTextPane (Note click just once) Move the mouse pointer on top of

your JFrame (Note just move not drag) When you see a little white box with orange border position it somewhere near the

top part of your JFrame and click once The JTextPane is then attached on the JFrame Addjust its size with the help of the

sizing hook around the perimeter of the JTextPane

- Edit the text property of the JTextPane with the description of your application

2 Input boxes

- In the palette pane click on Swing component named JTextField Next move the mouse pointer on top of your JFrame

When you see a white box with the word jTextField1 position it to the left and under the JTextPane1

- Click on Swing component JTextField in the palette pane again Move the mouse pointer on top of the JFrame you should

get a second JTextField named jTextField2 Position it to the right of jTextField1

Last Updated ( Tuesday 11 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5658

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5758

multiplication if a b and c were first declared as double or float or int variables So the code of the calculation should look like the

following

double a b c

a =

b =

c = a b

Clearly variables a and b need to be assigned with values of typed double Now looking back at inputboxes A and B we made

those boxes using the JTextField component meaning that any data or value written in a JTextField is of typed Text or String even

if the value displayed in the box is ldquo125rdquo But f irst here there is this task of getting to that Text (or String) values ldquo125rdquo and ldquo72rdquo

In Java we can get to a text in JTextField component using the getText() method So to retrieve values in jTextField1 and

jTextField2 we can use

jTextField1getText()

and

jTextField2getText()

But those values are still in text mode and we need another method to convert the data mode f rom text to double For that we can

use the method DoubleparseDouble() Thus the whole process of retrieve text -gt convert text to double -gt assign to double

variable can be done using

a = DoubleparseDouble( jTextField1getText() )

and

b = DoubleparseDouble( jTextField2getText() )

Hence the code for performing the multiplication becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

Up to this point we have the variable c as the result of the multiplication of a and b So the next thing to do is to display this

result on the JLabel that we have already prepared before

A x B = _____

The text A x B = ______ was made using JLabel component and its variable name is jLabel3 Since the variable c is of type

double and text property of JLabel is of type String we need to convert it first to String data type The method to convert a double

to String is DoubletoString() So we can write the code as DoubletoString(c) After getting the text we can now use it to be the

text displayed on jLabel3 To do that we use the method setText() Hence the appropriate code for displaying the result is

jLabel3setText( A x B = + DoubletoString(c) )

And the full code for our calculation becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

jLabel3setText( A x B = + DoubletoString(c) )

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5858

[ Back ]

Last Updated ( Tuesday 18 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

Page 21: Computional Physics

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2158

Improved Euler method

Comparison between analytical solution of F=ma with the Euler method shows that although the

method is able to find approximate solution the error is quite significant if computational step is

reduced The error is also cumulative ie as the iteration of the calculation increases the error also gets

bigger hence value furthest away from initial condition has the largest error

The Euler method gives us a way to approximate positions of object with respect to time but requires

an order of magnitude of computational iterations for each order of magnitude of accuracy For large

number of objects this method will certainly consume a great deal of computation time Hence

students are introduced to Initial Half-Step Method an Euler method with a much improved

computational effeciency

Source code

Study of motion of an object with mass m

experiencing a constant force F using

Improved Euler Method (Initial half step method)

In X and Y dimension

import static javalangMath

public class fma04

public static void main(String args[])

double m fx fy x y vo vx vy ax ay t delt

double pi th theta ymax tmax xmax

pi = 40atan(10)

m = 01 in kg

fy = -02 in N (pulling down force)

fx = 00 in N (retarding force)

ay = fym

ax = fxm

vo = 500 in ms

theta = 300 in degrees

th = theta(pi1800) degrees change to radian

vx = vo cos(th) in ms vy = vo sin(th) in ms

y = 12 in m

x = 00 in m

ymax=y initialize var ymax with initial value of y

xmax=00

tmax=00

delt = 001 in s

for(t=00 tlt=300 t=t+delt)

System outprintf(f f fntxy)

if (t==00)vx = vx + 05axdelt

vy = vy + 05aydelt

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2258

else

vx = vx + axdelt

vy = vy + aydelt

x = x + vxdelt

y = y + vydelt

if (ygtymax)

ymax=y

xmax=xtmax=t

if (ylt=00) break

System outprintf( Value of Y maximum is f happened when t = f

nymaxtmax)

System outprintf( at x = fnxmax)

System outprintf( The object reach the ground at x=f and t=fn xt)

gnuplot result

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2358

983107983119983117983120983125983124983105983124983113983119983118983105983116

983120983112983129983123983113983107983123 983080983123983123983120983090983089983090983090983081

983122983141983155983156983151983154983145983150983143 983110983151983154983139983141

983138983161

983105983138983140983086 983115983144983137983149983145983149 983113983155983149983137983145983148

983117983151983144983140 983115983144983137983148983145983140 983115983137983155983149983145983150

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2458

Damped Harmonic Oscillation

Object m is on a flat surface attached to a spring as in figure above The object is pulled

to position A and released so that it oscillates about x =0 Assuming that the force F=ma= -bv-kx where x is position of the object v is the velocity k the spring constant andb is a constant associated with friction with the flat surface

The algorithm

1 Declare the classmethod to be used2 Declare and assign all variables to be used

x t delt k b A m F a v v0 and x03 Declare and assign constants to the respective variables

k = 02 b = 01 A = 01 m = 054 Select appropriate value for ∆t 5 Initialized initial values xo=A vo=00

6 set x=xo v=vo and t = 07 calculate the value of force F = -kx-bv 8 calculate the value of acceleration a = Fm

9 calculate velocity v = vo + ∆v = vo + a ∆t

10 calculate position x = xo + ∆x = xo + v ∆t

11 increment value of t by ∆t 12 print out values of t and x

13 swap the value of xo=x vo=v 14 repeat from step 4 until stopping condition is satisfied (tlt=600)

The java code

public class simplehar01

public static void main(String args[])double x t deltdouble k b A m F a v v0 x0

k = 02b = 01A = 0 1

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2558

m = 05delt = 0001v0 = 00x0 = Ax = x0 v = v0

t = 00while(tlt=600)

F = -kx-bva = Fmv = v0 + adeltx = x0 + vdeltt = t + deltSystemoutprintf(f fntx)x0 = xv0 = v

gnuplot result

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2658

983107983119983117983120983125983124983105983124983113983119983118983105983116 983120983112983129983123983113983107983123

(983123983123983120983090983089983090983090983081

983106983157983145983148983140983145983150983143 983137 983152983144983161983155983145983139983155 983155983151983148983158983141983154

983157983155983145983150983143 983118983141983156983138983141983137983150983155

983138983161

983105983138983140983086 983115983144983137983149983145983149 983113983155983149983137983145983148

983117983151983144983140 983115983144983137983148983145983140 983115983137983155983149983145983150

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2758

How to create a user interface (UI) using Netbeans

Based on the ldquoGravityAttract07ajavardquo source code (shown below) build a user interface usingNetbeans The design of the UI could be look like this (Figure1)

Figure 1

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2858

ldquoGravityAttract07ajavardquo source code

Java class for calculating the resultant force on any object(ie either on m1m2 m3 m4 or m5)

import static javalangMathimport javaiopublic class GravityAttract07a

public static void main(String args[])

double[] m = new double[6] mass of objectdouble[] x = new double[6] x-coor of objectdouble[] y = new double[6] y-coor of objectdouble f resultant forcedouble fxfy x amp y component of forcedouble rx ry rp co G dirfangdirf

int j

G = 667e-11m[1]=25 x[1]=12 y[1]=25m[2]=15 x[2]=15 y[2]=15m[3]=75 x[3]=02 y[3]=55m[4]=92 x[4]=52 y[4]=25m[5]=66 x[5]=42 y[5]=45BufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin))String mass=Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))try mass=dataInreadLine()

catch(IOException e)Systemoutprintln(Not a valid input value)int mi = IntegerparseInt(mass)fx = 00fy = 00

for(j=1 jlt=5 j++)

if (j=mi)rx = x[j]-x[mi]ry = y[j]-y[mi]rp = pow((rxrx+ryry)15) (rx^2 + ry^2) raised to the power of 32co = Gm[mi]m[j]

fx = fx + corxrpfy = fy + coryrp

f = sqrt(fxfx + fyfy)dirf = toDegrees(atan(fyfx))Systemoutprintf(The force on m[+ mi +] due to the other masses is eN fdegnfdirf)

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2958

This example will not show you step by step details procedure since it already explained inthe class and in the e-learning Please refer to the e-learning about how to create a project how tocreate JFrame Form and as well how to create JButton JTextField JLabel JTextPane and so on

First create a new project and give a name eg ldquogravityrdquo and then create JFrame Form andthe following JButton JTextField JLabel JTextPane etc by using ldquoswing paletterdquo as shown in Figure2

Figure 2

To insert a picture into JLabel2 click on the JLabel2 go to properties and select ldquoiconrdquo Onthe icon editor choose file and then select file to locate the directory of your graphicpicture file(Figure 3) Select the file and click OK when finished Make sure you have already created your ownpicture file and save it as jpg png bmp etc format eg ldquogravityjpgrdquo Use any graphic package todraw your diagrampicture or use drawing tools available in Open Office or Microsoft Word

JTextPane1JLabel1

JLabel2

JLabel3JLabel4

JLabel5

JLabel6 JLabel7

JLabel8

JTextField2 JTextField3 JTextField4

JTextF

JButton1JButton2

JButton3

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3058

Figure 3

To write documentation or question eg in JTextPane1 click on the JTextPane1 go toproperties and select ldquotextrdquo Type the documentation in the text editor as shown in Figure 4 Click OKwhen finished

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3158

To do formatting on the text such as superscript and subscript eg in jLabel3 click on the

JLabel3 go to properties and select ldquotextrdquo Type the documentationmessage on the text editor asshown in Figure 5 as follows

eg

lthtmlgtOn which particle (mltsubgt1ltsubgtmltsubgt5ltsubgt) you want to calculate theresultant force

Use sup instead if you want a superscriptThe result will look like this

Click OK when finished

Figure 5

To do the calculation you need to put the coding similar like ldquoGravityAttract07ajavardquo and put itinto the ldquocalculaterdquo button (this is ldquoJButton1rdquo) You can copy ldquoGravityAttract07a javardquo source code

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3258

paste it and then do a few alterations First click on the ldquocalculaterdquo button right click choose Eventsand then choose Action then actionPerformed (Figure 6) It will bring you to where you should insertyour coding (Figure 7) You can write your own coding or copypaste any coding from somewhereelse (in this case from ldquoGravityAttract07ajavardquo)

Figure 6

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3358

Figure 7

Copy ldquoGravityAttract07ajavardquo source code and paste to the place after ldquo TODO add youhandling code hererdquo see Figure 7 To see which part has been modified the coding in Figure 7 hasbeen color coded for better understanding as follows

private void jButton1ActionPerformed(javaawteventActionEvent evt) TODO add your handling code here

double[] m = new double[6] mass of objectdouble[] x = new double[6] x-coor of objectdouble[] y = new double[6] y-coor of objectdouble f resultant forcedouble fxfy x amp y component of forcedouble rx ry rp co G dirfangdirfDecimalFormat dff dfangdirfint j mi

G = 667e-11m[1]=25 x[1]=12 y[1]=25m[2]=15 x[2]=15 y[2]=15m[3]=75 x[3]=02 y[3]=55m[4]=92 x[4]=52 y[4]=25m[5]=66 x[5]=42 y[5]=45

BufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin)) String mass= Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3458

catch(IOException e)Systemoutprintln(Not a valid input value) int mi = IntegerparseInt(mass)mi = IntegerparseInt(jTextField1getText())fx = 00fy = 00for(j=1 jlt=5 j++)

if (j=mi)rx = x[j]-x[mi]ry = y[j]-y[mi]rp = Mathpow((rxrx+ryry)15) (rx^2 + ry^2) raised to the power of 32co = Gm[mi]m[j]fx = fx + corxrpfy = fy + coryrp

f = Mathsqrt(fxfx + fyfy)

dirf = Mathatan(fyfx)angdirf = (dirf(22070))1800 Systemoutprintf(ldquoThe resultant force on particle [ldquo+ mi +rdquo] due to the other masses is

14e N 22f degnrdquofangdirf)dff = new javatextDecimalFormat(ldquoE00rdquo)dfangdirf = new DecimalFormat(ldquo000rdquo)

jTextField2setText(ldquordquo + mi) jTextField3setText(ldquordquo + dffformat(f)) jTextField4setText(ldquordquo + dfangdirfformat(angdirf))

Note

Brown = automatically generatedBlue = the original code from ldquoGravityAttract07ajavardquoGreen = coding from ldquoGravityAttract07ajavardquo which were being disabled due to

incompatibilities with NetbeansRed = a new inserted coding a few of them is to rephrasesubstitute the green one

Javascripts command line useBufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin))String mass=Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))try mass=dataInreadLine()catch(IOException e)Systemoutprintln(Not a valid input value)

int mi = IntegerparseInt(mass)

to read any input from the keyboard typed by the user while Netbeans uses egldquoIntegerparseInt(jTextField1getText())rdquo etc Javascripts command line uses ldquoSystemoutprintfrdquo to format the printed output whileNetbeans uses more complicated way to do the same job (see below)

Javascript command line

Systemoutprintf(The resultant force on particle [+ mi +] due to the other masses is 14e N22f degnfangdirf)

Netbeansdff = new javatextDecimalFormat(ldquo0E00rdquo)df di f D i lF t(ldquo0 00rdquo)

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3558

jTextField2setText(ldquordquo + mi) jTextField3setText(ldquordquo + dffformat(f)) jTextField4setText(ldquordquo + dfangdirfformat(angdirf))

ldquodffrdquo and ldquodfangdirfrdquo are new variables that should be declared and added to format thenumbers into decimal format ie ldquo0E00rdquo to get a scientific format eg ldquo20019E-10rdquo and ldquo000rdquoto get a decimal number eg ldquo-1570rdquo

To use DecimalFormat you have to put ldquoimport javatextDecimalFormatrdquo in the top of thecoding as shown in Figure 8

Figure 8

To reset the calculation you need to put coding into the ldquoresetrdquo button (this is ldquoJButton2rdquo)First click on the ldquoresetrdquo button right click choose Events and then choose Action thenactionPerformed (similar like in Figure 6) It will bring you to where you should insert your coding(Figure 9) Write the following coding (Figure 9)

Figure 9

To exit the program you need to put coding into the ldquoexitrdquo button (this is ldquoJButton3rdquo) Firstclick on the ldquoexitrdquo button right click choose Events and then choose Action then actionPerformed(similar like in Figure 6) It will bring you to where you should insert your coding (Figure 10) Writethe following coding (Figure 10)

Figure 10

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3658

Once you finished now you can run and see the result If everything is fine then the output wilbe look like this (Figure 11)

Figure 11

Remember when you compile and run ldquoGravityAttract07ajavardquo using Javascripts commandline once you compile and run the result was looked like this

On which particle you want to calculate the resulting force 2The resultant force on particle [2] due to the other masses is 30673e-10 N 8837 deg

SEE THE DIFFERENCES AT THE USER INTERFACE

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3758

8th Step

Written by Mohd Khalid Kasmin

Monday 27 August 2007

Programming using command line interface (CLI) has its advantages but todays computer users are so used to graphical user

interface (GUI) that we just can no longer be contented with writing application that do not use the GUI Hence starting from this 8thstep we are going to embark on learning how to make java application that utilises the the GUI

It is possible to write a GUI application with the text based tools that we have been using for these past 7 steps but the job is so

messy that most of you will definitely be turnoff by it So to make life slightly easier well use an integrated development

environment (IDE) known as NetBeans specifically NetBeans 551

0 First ssh to the our server Next once logged in just type nice -n15 netbeans amp on the terminal If you had sshed with the -Y

switch after a few tens of seconds NetBeans should emerge on your display screen

One important point to note Since our server can readily handle up to 100 users (maybe even

more) doing work in CLI mode this machine will certainly be taxed to its maximum capability once

we start using graphical based application NetBeans 551 uses a geat deal of computer memory

and with the 4Gbytes RAM server it can only accomodate up to 25 users concurrently using

NetBeans if the users use the application wisely What is meant by wisely here is that (a) when

starting the NetBeans application please use it together with the nice -n15 command (b) in using

the NetBeans please make sure you only have one project open at a time and (c) please close your

project everytime before quitting the NetBeans

Home Profile Teaching Links Contact Me My Work

Newsflash

Yesterday all servers in the US went out on strike in a bid to get more RAM and better CPUs A spokes person said that the

need for better RAM was due to some fool increasing the front-side bus speed In future busses will be told to slow down in

residential motherboards

search

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3858

1

2

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3958

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4058

[ Back ]

5

Last Updated ( Wednesday 31 October 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4158

8th Step (Cont)

Written by Mohd Khalid Kasmin

Monday 27 August 2007

Up to this point we have define the name of our project Next we construct a class and to follow what we already know we name the class the sam

project name latihan01

6

7

Home Profile Teaching Links Contact Me My Work

Newsflash

Joomla 10 - Experience the Freedom It has never been easier to create your own dynamic site Manage all your content from

the best CMS admin interface

search

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4258

8

9

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4358

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4458

8th Step (Cont still)

Written by Mohd Khalid Kasmin

Monday 27 August 2007

In item (9) we see that Netbeans does not provide us with the main method So to follow what we already know we now put in the

main method

10

Home Profile Teaching Links Contact Me My Work

Newsflash

Aoccdrnig to a rscheearch at an Elingsh uinervtisy it deosnt mttaer in waht oredr the ltteers in a wrod are the olny iprmoetnt

tihng is taht frist and lsat ltteer is at the rghit pclae The rset can be a toatl mses and you can sitll raed it wouthit porbelm Tihs is

bcuseae we do not raed ervey lteter by itslef but the wrod as a wlohe

search

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4558

11

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4658

12

13

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4758

14

15

Last Updated ( Monday 27 August 2007 )

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4858

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4958

9th Step

Thursday 06 September 2007

Step by Step into Java Programming

for

University Physics Student

By

Mohd Khalid Kasmin

A Building graphical user interface - preparing the JFrame Form

At this stage of our programming experience with Java and NetBeans please follow strictly the steps outline in this article First and

make a new GUI project make sure there is no other project that already been loaded by Netbeans Please close any project displayed

1 Create new project

- On the main menu click on File --gt New Project (note Close first any previous project)

- Specify project name amp project location - Untick Create Main Class item

- Click on the Finish button

Home Profile Teaching Links Contact Me My Work

Newsflash

Aoccdrnig to a rscheearch at an Elingsh uinervtisy it deosnt mttaer in waht oredr the ltteers in a wrod are the olny iprmoetnt

tihng is taht frist and lsat ltteer is at the rghit pclae The rset can be a toatl mses and you can sitll raed it wouthit porbelm Tihs is

bcuseae we do not raed ervey lteter by itslef but the wrod as a wlohe

search

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5058

Figure 91

2 Create UI class

- Right click on the project name in the Project pane

on the popup menu select New --gt JFrame Form

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5158

Figure 92

- In the New JFrame Form dialog specify the Class Name and the Package name

eg Class Name multiplierUI Package mymultiplier

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5258

Figure 93

After clicking on the Finish button we should get something like the following

Figure 94

Last Updated ( Tuesday 18 September 2007 )

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5358

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5458

9th Step (Cont)

Written by Mohd Khalid Kasmin

Tuesday 11 September 2007

Step by Step into Java Programming

for University Physics Student

By

Mohd Khalid Kasmin

B Building user interface (UI) - putting in the components

Now that we have a proper blank form we can start filling the form with the suitable graphical components For introduction to

GUI programming an appropriate application to build (for a physics student) is a simple solver So our first exercise is to build an

application that takes two numbers and multiply them and display the result

With GUI we usually give input values to an application via input boxes that are labelled When all inputs are typed in properlywe click on a button to ask the application to execute the job Hence in this exercise well do just that The target now is to

construct a user interface that looks like Figure 95 below

Figure 95

Home Profile Teaching Links Contact Me My Work

Newsflash

Joomla 10 - Experience the Freedom It has never been easier to create your own dynamic site Manage all your content from

the best CMS admin interface

search

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5558

[ Back ]

1 Problemapplication description box

- In the palette pane click on Swing component named JTextPane (Note click just once) Move the mouse pointer on top of

your JFrame (Note just move not drag) When you see a little white box with orange border position it somewhere near the

top part of your JFrame and click once The JTextPane is then attached on the JFrame Addjust its size with the help of the

sizing hook around the perimeter of the JTextPane

- Edit the text property of the JTextPane with the description of your application

2 Input boxes

- In the palette pane click on Swing component named JTextField Next move the mouse pointer on top of your JFrame

When you see a white box with the word jTextField1 position it to the left and under the JTextPane1

- Click on Swing component JTextField in the palette pane again Move the mouse pointer on top of the JFrame you should

get a second JTextField named jTextField2 Position it to the right of jTextField1

Last Updated ( Tuesday 11 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5658

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5758

multiplication if a b and c were first declared as double or float or int variables So the code of the calculation should look like the

following

double a b c

a =

b =

c = a b

Clearly variables a and b need to be assigned with values of typed double Now looking back at inputboxes A and B we made

those boxes using the JTextField component meaning that any data or value written in a JTextField is of typed Text or String even

if the value displayed in the box is ldquo125rdquo But f irst here there is this task of getting to that Text (or String) values ldquo125rdquo and ldquo72rdquo

In Java we can get to a text in JTextField component using the getText() method So to retrieve values in jTextField1 and

jTextField2 we can use

jTextField1getText()

and

jTextField2getText()

But those values are still in text mode and we need another method to convert the data mode f rom text to double For that we can

use the method DoubleparseDouble() Thus the whole process of retrieve text -gt convert text to double -gt assign to double

variable can be done using

a = DoubleparseDouble( jTextField1getText() )

and

b = DoubleparseDouble( jTextField2getText() )

Hence the code for performing the multiplication becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

Up to this point we have the variable c as the result of the multiplication of a and b So the next thing to do is to display this

result on the JLabel that we have already prepared before

A x B = _____

The text A x B = ______ was made using JLabel component and its variable name is jLabel3 Since the variable c is of type

double and text property of JLabel is of type String we need to convert it first to String data type The method to convert a double

to String is DoubletoString() So we can write the code as DoubletoString(c) After getting the text we can now use it to be the

text displayed on jLabel3 To do that we use the method setText() Hence the appropriate code for displaying the result is

jLabel3setText( A x B = + DoubletoString(c) )

And the full code for our calculation becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

jLabel3setText( A x B = + DoubletoString(c) )

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5858

[ Back ]

Last Updated ( Tuesday 18 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

Page 22: Computional Physics

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2258

else

vx = vx + axdelt

vy = vy + aydelt

x = x + vxdelt

y = y + vydelt

if (ygtymax)

ymax=y

xmax=xtmax=t

if (ylt=00) break

System outprintf( Value of Y maximum is f happened when t = f

nymaxtmax)

System outprintf( at x = fnxmax)

System outprintf( The object reach the ground at x=f and t=fn xt)

gnuplot result

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2358

983107983119983117983120983125983124983105983124983113983119983118983105983116

983120983112983129983123983113983107983123 983080983123983123983120983090983089983090983090983081

983122983141983155983156983151983154983145983150983143 983110983151983154983139983141

983138983161

983105983138983140983086 983115983144983137983149983145983149 983113983155983149983137983145983148

983117983151983144983140 983115983144983137983148983145983140 983115983137983155983149983145983150

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2458

Damped Harmonic Oscillation

Object m is on a flat surface attached to a spring as in figure above The object is pulled

to position A and released so that it oscillates about x =0 Assuming that the force F=ma= -bv-kx where x is position of the object v is the velocity k the spring constant andb is a constant associated with friction with the flat surface

The algorithm

1 Declare the classmethod to be used2 Declare and assign all variables to be used

x t delt k b A m F a v v0 and x03 Declare and assign constants to the respective variables

k = 02 b = 01 A = 01 m = 054 Select appropriate value for ∆t 5 Initialized initial values xo=A vo=00

6 set x=xo v=vo and t = 07 calculate the value of force F = -kx-bv 8 calculate the value of acceleration a = Fm

9 calculate velocity v = vo + ∆v = vo + a ∆t

10 calculate position x = xo + ∆x = xo + v ∆t

11 increment value of t by ∆t 12 print out values of t and x

13 swap the value of xo=x vo=v 14 repeat from step 4 until stopping condition is satisfied (tlt=600)

The java code

public class simplehar01

public static void main(String args[])double x t deltdouble k b A m F a v v0 x0

k = 02b = 01A = 0 1

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2558

m = 05delt = 0001v0 = 00x0 = Ax = x0 v = v0

t = 00while(tlt=600)

F = -kx-bva = Fmv = v0 + adeltx = x0 + vdeltt = t + deltSystemoutprintf(f fntx)x0 = xv0 = v

gnuplot result

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2658

983107983119983117983120983125983124983105983124983113983119983118983105983116 983120983112983129983123983113983107983123

(983123983123983120983090983089983090983090983081

983106983157983145983148983140983145983150983143 983137 983152983144983161983155983145983139983155 983155983151983148983158983141983154

983157983155983145983150983143 983118983141983156983138983141983137983150983155

983138983161

983105983138983140983086 983115983144983137983149983145983149 983113983155983149983137983145983148

983117983151983144983140 983115983144983137983148983145983140 983115983137983155983149983145983150

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2758

How to create a user interface (UI) using Netbeans

Based on the ldquoGravityAttract07ajavardquo source code (shown below) build a user interface usingNetbeans The design of the UI could be look like this (Figure1)

Figure 1

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2858

ldquoGravityAttract07ajavardquo source code

Java class for calculating the resultant force on any object(ie either on m1m2 m3 m4 or m5)

import static javalangMathimport javaiopublic class GravityAttract07a

public static void main(String args[])

double[] m = new double[6] mass of objectdouble[] x = new double[6] x-coor of objectdouble[] y = new double[6] y-coor of objectdouble f resultant forcedouble fxfy x amp y component of forcedouble rx ry rp co G dirfangdirf

int j

G = 667e-11m[1]=25 x[1]=12 y[1]=25m[2]=15 x[2]=15 y[2]=15m[3]=75 x[3]=02 y[3]=55m[4]=92 x[4]=52 y[4]=25m[5]=66 x[5]=42 y[5]=45BufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin))String mass=Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))try mass=dataInreadLine()

catch(IOException e)Systemoutprintln(Not a valid input value)int mi = IntegerparseInt(mass)fx = 00fy = 00

for(j=1 jlt=5 j++)

if (j=mi)rx = x[j]-x[mi]ry = y[j]-y[mi]rp = pow((rxrx+ryry)15) (rx^2 + ry^2) raised to the power of 32co = Gm[mi]m[j]

fx = fx + corxrpfy = fy + coryrp

f = sqrt(fxfx + fyfy)dirf = toDegrees(atan(fyfx))Systemoutprintf(The force on m[+ mi +] due to the other masses is eN fdegnfdirf)

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2958

This example will not show you step by step details procedure since it already explained inthe class and in the e-learning Please refer to the e-learning about how to create a project how tocreate JFrame Form and as well how to create JButton JTextField JLabel JTextPane and so on

First create a new project and give a name eg ldquogravityrdquo and then create JFrame Form andthe following JButton JTextField JLabel JTextPane etc by using ldquoswing paletterdquo as shown in Figure2

Figure 2

To insert a picture into JLabel2 click on the JLabel2 go to properties and select ldquoiconrdquo Onthe icon editor choose file and then select file to locate the directory of your graphicpicture file(Figure 3) Select the file and click OK when finished Make sure you have already created your ownpicture file and save it as jpg png bmp etc format eg ldquogravityjpgrdquo Use any graphic package todraw your diagrampicture or use drawing tools available in Open Office or Microsoft Word

JTextPane1JLabel1

JLabel2

JLabel3JLabel4

JLabel5

JLabel6 JLabel7

JLabel8

JTextField2 JTextField3 JTextField4

JTextF

JButton1JButton2

JButton3

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3058

Figure 3

To write documentation or question eg in JTextPane1 click on the JTextPane1 go toproperties and select ldquotextrdquo Type the documentation in the text editor as shown in Figure 4 Click OKwhen finished

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3158

To do formatting on the text such as superscript and subscript eg in jLabel3 click on the

JLabel3 go to properties and select ldquotextrdquo Type the documentationmessage on the text editor asshown in Figure 5 as follows

eg

lthtmlgtOn which particle (mltsubgt1ltsubgtmltsubgt5ltsubgt) you want to calculate theresultant force

Use sup instead if you want a superscriptThe result will look like this

Click OK when finished

Figure 5

To do the calculation you need to put the coding similar like ldquoGravityAttract07ajavardquo and put itinto the ldquocalculaterdquo button (this is ldquoJButton1rdquo) You can copy ldquoGravityAttract07a javardquo source code

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3258

paste it and then do a few alterations First click on the ldquocalculaterdquo button right click choose Eventsand then choose Action then actionPerformed (Figure 6) It will bring you to where you should insertyour coding (Figure 7) You can write your own coding or copypaste any coding from somewhereelse (in this case from ldquoGravityAttract07ajavardquo)

Figure 6

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3358

Figure 7

Copy ldquoGravityAttract07ajavardquo source code and paste to the place after ldquo TODO add youhandling code hererdquo see Figure 7 To see which part has been modified the coding in Figure 7 hasbeen color coded for better understanding as follows

private void jButton1ActionPerformed(javaawteventActionEvent evt) TODO add your handling code here

double[] m = new double[6] mass of objectdouble[] x = new double[6] x-coor of objectdouble[] y = new double[6] y-coor of objectdouble f resultant forcedouble fxfy x amp y component of forcedouble rx ry rp co G dirfangdirfDecimalFormat dff dfangdirfint j mi

G = 667e-11m[1]=25 x[1]=12 y[1]=25m[2]=15 x[2]=15 y[2]=15m[3]=75 x[3]=02 y[3]=55m[4]=92 x[4]=52 y[4]=25m[5]=66 x[5]=42 y[5]=45

BufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin)) String mass= Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3458

catch(IOException e)Systemoutprintln(Not a valid input value) int mi = IntegerparseInt(mass)mi = IntegerparseInt(jTextField1getText())fx = 00fy = 00for(j=1 jlt=5 j++)

if (j=mi)rx = x[j]-x[mi]ry = y[j]-y[mi]rp = Mathpow((rxrx+ryry)15) (rx^2 + ry^2) raised to the power of 32co = Gm[mi]m[j]fx = fx + corxrpfy = fy + coryrp

f = Mathsqrt(fxfx + fyfy)

dirf = Mathatan(fyfx)angdirf = (dirf(22070))1800 Systemoutprintf(ldquoThe resultant force on particle [ldquo+ mi +rdquo] due to the other masses is

14e N 22f degnrdquofangdirf)dff = new javatextDecimalFormat(ldquoE00rdquo)dfangdirf = new DecimalFormat(ldquo000rdquo)

jTextField2setText(ldquordquo + mi) jTextField3setText(ldquordquo + dffformat(f)) jTextField4setText(ldquordquo + dfangdirfformat(angdirf))

Note

Brown = automatically generatedBlue = the original code from ldquoGravityAttract07ajavardquoGreen = coding from ldquoGravityAttract07ajavardquo which were being disabled due to

incompatibilities with NetbeansRed = a new inserted coding a few of them is to rephrasesubstitute the green one

Javascripts command line useBufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin))String mass=Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))try mass=dataInreadLine()catch(IOException e)Systemoutprintln(Not a valid input value)

int mi = IntegerparseInt(mass)

to read any input from the keyboard typed by the user while Netbeans uses egldquoIntegerparseInt(jTextField1getText())rdquo etc Javascripts command line uses ldquoSystemoutprintfrdquo to format the printed output whileNetbeans uses more complicated way to do the same job (see below)

Javascript command line

Systemoutprintf(The resultant force on particle [+ mi +] due to the other masses is 14e N22f degnfangdirf)

Netbeansdff = new javatextDecimalFormat(ldquo0E00rdquo)df di f D i lF t(ldquo0 00rdquo)

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3558

jTextField2setText(ldquordquo + mi) jTextField3setText(ldquordquo + dffformat(f)) jTextField4setText(ldquordquo + dfangdirfformat(angdirf))

ldquodffrdquo and ldquodfangdirfrdquo are new variables that should be declared and added to format thenumbers into decimal format ie ldquo0E00rdquo to get a scientific format eg ldquo20019E-10rdquo and ldquo000rdquoto get a decimal number eg ldquo-1570rdquo

To use DecimalFormat you have to put ldquoimport javatextDecimalFormatrdquo in the top of thecoding as shown in Figure 8

Figure 8

To reset the calculation you need to put coding into the ldquoresetrdquo button (this is ldquoJButton2rdquo)First click on the ldquoresetrdquo button right click choose Events and then choose Action thenactionPerformed (similar like in Figure 6) It will bring you to where you should insert your coding(Figure 9) Write the following coding (Figure 9)

Figure 9

To exit the program you need to put coding into the ldquoexitrdquo button (this is ldquoJButton3rdquo) Firstclick on the ldquoexitrdquo button right click choose Events and then choose Action then actionPerformed(similar like in Figure 6) It will bring you to where you should insert your coding (Figure 10) Writethe following coding (Figure 10)

Figure 10

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3658

Once you finished now you can run and see the result If everything is fine then the output wilbe look like this (Figure 11)

Figure 11

Remember when you compile and run ldquoGravityAttract07ajavardquo using Javascripts commandline once you compile and run the result was looked like this

On which particle you want to calculate the resulting force 2The resultant force on particle [2] due to the other masses is 30673e-10 N 8837 deg

SEE THE DIFFERENCES AT THE USER INTERFACE

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3758

8th Step

Written by Mohd Khalid Kasmin

Monday 27 August 2007

Programming using command line interface (CLI) has its advantages but todays computer users are so used to graphical user

interface (GUI) that we just can no longer be contented with writing application that do not use the GUI Hence starting from this 8thstep we are going to embark on learning how to make java application that utilises the the GUI

It is possible to write a GUI application with the text based tools that we have been using for these past 7 steps but the job is so

messy that most of you will definitely be turnoff by it So to make life slightly easier well use an integrated development

environment (IDE) known as NetBeans specifically NetBeans 551

0 First ssh to the our server Next once logged in just type nice -n15 netbeans amp on the terminal If you had sshed with the -Y

switch after a few tens of seconds NetBeans should emerge on your display screen

One important point to note Since our server can readily handle up to 100 users (maybe even

more) doing work in CLI mode this machine will certainly be taxed to its maximum capability once

we start using graphical based application NetBeans 551 uses a geat deal of computer memory

and with the 4Gbytes RAM server it can only accomodate up to 25 users concurrently using

NetBeans if the users use the application wisely What is meant by wisely here is that (a) when

starting the NetBeans application please use it together with the nice -n15 command (b) in using

the NetBeans please make sure you only have one project open at a time and (c) please close your

project everytime before quitting the NetBeans

Home Profile Teaching Links Contact Me My Work

Newsflash

Yesterday all servers in the US went out on strike in a bid to get more RAM and better CPUs A spokes person said that the

need for better RAM was due to some fool increasing the front-side bus speed In future busses will be told to slow down in

residential motherboards

search

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3858

1

2

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3958

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4058

[ Back ]

5

Last Updated ( Wednesday 31 October 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4158

8th Step (Cont)

Written by Mohd Khalid Kasmin

Monday 27 August 2007

Up to this point we have define the name of our project Next we construct a class and to follow what we already know we name the class the sam

project name latihan01

6

7

Home Profile Teaching Links Contact Me My Work

Newsflash

Joomla 10 - Experience the Freedom It has never been easier to create your own dynamic site Manage all your content from

the best CMS admin interface

search

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4258

8

9

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4358

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4458

8th Step (Cont still)

Written by Mohd Khalid Kasmin

Monday 27 August 2007

In item (9) we see that Netbeans does not provide us with the main method So to follow what we already know we now put in the

main method

10

Home Profile Teaching Links Contact Me My Work

Newsflash

Aoccdrnig to a rscheearch at an Elingsh uinervtisy it deosnt mttaer in waht oredr the ltteers in a wrod are the olny iprmoetnt

tihng is taht frist and lsat ltteer is at the rghit pclae The rset can be a toatl mses and you can sitll raed it wouthit porbelm Tihs is

bcuseae we do not raed ervey lteter by itslef but the wrod as a wlohe

search

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4558

11

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4658

12

13

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4758

14

15

Last Updated ( Monday 27 August 2007 )

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4858

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4958

9th Step

Thursday 06 September 2007

Step by Step into Java Programming

for

University Physics Student

By

Mohd Khalid Kasmin

A Building graphical user interface - preparing the JFrame Form

At this stage of our programming experience with Java and NetBeans please follow strictly the steps outline in this article First and

make a new GUI project make sure there is no other project that already been loaded by Netbeans Please close any project displayed

1 Create new project

- On the main menu click on File --gt New Project (note Close first any previous project)

- Specify project name amp project location - Untick Create Main Class item

- Click on the Finish button

Home Profile Teaching Links Contact Me My Work

Newsflash

Aoccdrnig to a rscheearch at an Elingsh uinervtisy it deosnt mttaer in waht oredr the ltteers in a wrod are the olny iprmoetnt

tihng is taht frist and lsat ltteer is at the rghit pclae The rset can be a toatl mses and you can sitll raed it wouthit porbelm Tihs is

bcuseae we do not raed ervey lteter by itslef but the wrod as a wlohe

search

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5058

Figure 91

2 Create UI class

- Right click on the project name in the Project pane

on the popup menu select New --gt JFrame Form

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5158

Figure 92

- In the New JFrame Form dialog specify the Class Name and the Package name

eg Class Name multiplierUI Package mymultiplier

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5258

Figure 93

After clicking on the Finish button we should get something like the following

Figure 94

Last Updated ( Tuesday 18 September 2007 )

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5358

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5458

9th Step (Cont)

Written by Mohd Khalid Kasmin

Tuesday 11 September 2007

Step by Step into Java Programming

for University Physics Student

By

Mohd Khalid Kasmin

B Building user interface (UI) - putting in the components

Now that we have a proper blank form we can start filling the form with the suitable graphical components For introduction to

GUI programming an appropriate application to build (for a physics student) is a simple solver So our first exercise is to build an

application that takes two numbers and multiply them and display the result

With GUI we usually give input values to an application via input boxes that are labelled When all inputs are typed in properlywe click on a button to ask the application to execute the job Hence in this exercise well do just that The target now is to

construct a user interface that looks like Figure 95 below

Figure 95

Home Profile Teaching Links Contact Me My Work

Newsflash

Joomla 10 - Experience the Freedom It has never been easier to create your own dynamic site Manage all your content from

the best CMS admin interface

search

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5558

[ Back ]

1 Problemapplication description box

- In the palette pane click on Swing component named JTextPane (Note click just once) Move the mouse pointer on top of

your JFrame (Note just move not drag) When you see a little white box with orange border position it somewhere near the

top part of your JFrame and click once The JTextPane is then attached on the JFrame Addjust its size with the help of the

sizing hook around the perimeter of the JTextPane

- Edit the text property of the JTextPane with the description of your application

2 Input boxes

- In the palette pane click on Swing component named JTextField Next move the mouse pointer on top of your JFrame

When you see a white box with the word jTextField1 position it to the left and under the JTextPane1

- Click on Swing component JTextField in the palette pane again Move the mouse pointer on top of the JFrame you should

get a second JTextField named jTextField2 Position it to the right of jTextField1

Last Updated ( Tuesday 11 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5658

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5758

multiplication if a b and c were first declared as double or float or int variables So the code of the calculation should look like the

following

double a b c

a =

b =

c = a b

Clearly variables a and b need to be assigned with values of typed double Now looking back at inputboxes A and B we made

those boxes using the JTextField component meaning that any data or value written in a JTextField is of typed Text or String even

if the value displayed in the box is ldquo125rdquo But f irst here there is this task of getting to that Text (or String) values ldquo125rdquo and ldquo72rdquo

In Java we can get to a text in JTextField component using the getText() method So to retrieve values in jTextField1 and

jTextField2 we can use

jTextField1getText()

and

jTextField2getText()

But those values are still in text mode and we need another method to convert the data mode f rom text to double For that we can

use the method DoubleparseDouble() Thus the whole process of retrieve text -gt convert text to double -gt assign to double

variable can be done using

a = DoubleparseDouble( jTextField1getText() )

and

b = DoubleparseDouble( jTextField2getText() )

Hence the code for performing the multiplication becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

Up to this point we have the variable c as the result of the multiplication of a and b So the next thing to do is to display this

result on the JLabel that we have already prepared before

A x B = _____

The text A x B = ______ was made using JLabel component and its variable name is jLabel3 Since the variable c is of type

double and text property of JLabel is of type String we need to convert it first to String data type The method to convert a double

to String is DoubletoString() So we can write the code as DoubletoString(c) After getting the text we can now use it to be the

text displayed on jLabel3 To do that we use the method setText() Hence the appropriate code for displaying the result is

jLabel3setText( A x B = + DoubletoString(c) )

And the full code for our calculation becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

jLabel3setText( A x B = + DoubletoString(c) )

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5858

[ Back ]

Last Updated ( Tuesday 18 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

Page 23: Computional Physics

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2358

983107983119983117983120983125983124983105983124983113983119983118983105983116

983120983112983129983123983113983107983123 983080983123983123983120983090983089983090983090983081

983122983141983155983156983151983154983145983150983143 983110983151983154983139983141

983138983161

983105983138983140983086 983115983144983137983149983145983149 983113983155983149983137983145983148

983117983151983144983140 983115983144983137983148983145983140 983115983137983155983149983145983150

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2458

Damped Harmonic Oscillation

Object m is on a flat surface attached to a spring as in figure above The object is pulled

to position A and released so that it oscillates about x =0 Assuming that the force F=ma= -bv-kx where x is position of the object v is the velocity k the spring constant andb is a constant associated with friction with the flat surface

The algorithm

1 Declare the classmethod to be used2 Declare and assign all variables to be used

x t delt k b A m F a v v0 and x03 Declare and assign constants to the respective variables

k = 02 b = 01 A = 01 m = 054 Select appropriate value for ∆t 5 Initialized initial values xo=A vo=00

6 set x=xo v=vo and t = 07 calculate the value of force F = -kx-bv 8 calculate the value of acceleration a = Fm

9 calculate velocity v = vo + ∆v = vo + a ∆t

10 calculate position x = xo + ∆x = xo + v ∆t

11 increment value of t by ∆t 12 print out values of t and x

13 swap the value of xo=x vo=v 14 repeat from step 4 until stopping condition is satisfied (tlt=600)

The java code

public class simplehar01

public static void main(String args[])double x t deltdouble k b A m F a v v0 x0

k = 02b = 01A = 0 1

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2558

m = 05delt = 0001v0 = 00x0 = Ax = x0 v = v0

t = 00while(tlt=600)

F = -kx-bva = Fmv = v0 + adeltx = x0 + vdeltt = t + deltSystemoutprintf(f fntx)x0 = xv0 = v

gnuplot result

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2658

983107983119983117983120983125983124983105983124983113983119983118983105983116 983120983112983129983123983113983107983123

(983123983123983120983090983089983090983090983081

983106983157983145983148983140983145983150983143 983137 983152983144983161983155983145983139983155 983155983151983148983158983141983154

983157983155983145983150983143 983118983141983156983138983141983137983150983155

983138983161

983105983138983140983086 983115983144983137983149983145983149 983113983155983149983137983145983148

983117983151983144983140 983115983144983137983148983145983140 983115983137983155983149983145983150

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2758

How to create a user interface (UI) using Netbeans

Based on the ldquoGravityAttract07ajavardquo source code (shown below) build a user interface usingNetbeans The design of the UI could be look like this (Figure1)

Figure 1

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2858

ldquoGravityAttract07ajavardquo source code

Java class for calculating the resultant force on any object(ie either on m1m2 m3 m4 or m5)

import static javalangMathimport javaiopublic class GravityAttract07a

public static void main(String args[])

double[] m = new double[6] mass of objectdouble[] x = new double[6] x-coor of objectdouble[] y = new double[6] y-coor of objectdouble f resultant forcedouble fxfy x amp y component of forcedouble rx ry rp co G dirfangdirf

int j

G = 667e-11m[1]=25 x[1]=12 y[1]=25m[2]=15 x[2]=15 y[2]=15m[3]=75 x[3]=02 y[3]=55m[4]=92 x[4]=52 y[4]=25m[5]=66 x[5]=42 y[5]=45BufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin))String mass=Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))try mass=dataInreadLine()

catch(IOException e)Systemoutprintln(Not a valid input value)int mi = IntegerparseInt(mass)fx = 00fy = 00

for(j=1 jlt=5 j++)

if (j=mi)rx = x[j]-x[mi]ry = y[j]-y[mi]rp = pow((rxrx+ryry)15) (rx^2 + ry^2) raised to the power of 32co = Gm[mi]m[j]

fx = fx + corxrpfy = fy + coryrp

f = sqrt(fxfx + fyfy)dirf = toDegrees(atan(fyfx))Systemoutprintf(The force on m[+ mi +] due to the other masses is eN fdegnfdirf)

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2958

This example will not show you step by step details procedure since it already explained inthe class and in the e-learning Please refer to the e-learning about how to create a project how tocreate JFrame Form and as well how to create JButton JTextField JLabel JTextPane and so on

First create a new project and give a name eg ldquogravityrdquo and then create JFrame Form andthe following JButton JTextField JLabel JTextPane etc by using ldquoswing paletterdquo as shown in Figure2

Figure 2

To insert a picture into JLabel2 click on the JLabel2 go to properties and select ldquoiconrdquo Onthe icon editor choose file and then select file to locate the directory of your graphicpicture file(Figure 3) Select the file and click OK when finished Make sure you have already created your ownpicture file and save it as jpg png bmp etc format eg ldquogravityjpgrdquo Use any graphic package todraw your diagrampicture or use drawing tools available in Open Office or Microsoft Word

JTextPane1JLabel1

JLabel2

JLabel3JLabel4

JLabel5

JLabel6 JLabel7

JLabel8

JTextField2 JTextField3 JTextField4

JTextF

JButton1JButton2

JButton3

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3058

Figure 3

To write documentation or question eg in JTextPane1 click on the JTextPane1 go toproperties and select ldquotextrdquo Type the documentation in the text editor as shown in Figure 4 Click OKwhen finished

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3158

To do formatting on the text such as superscript and subscript eg in jLabel3 click on the

JLabel3 go to properties and select ldquotextrdquo Type the documentationmessage on the text editor asshown in Figure 5 as follows

eg

lthtmlgtOn which particle (mltsubgt1ltsubgtmltsubgt5ltsubgt) you want to calculate theresultant force

Use sup instead if you want a superscriptThe result will look like this

Click OK when finished

Figure 5

To do the calculation you need to put the coding similar like ldquoGravityAttract07ajavardquo and put itinto the ldquocalculaterdquo button (this is ldquoJButton1rdquo) You can copy ldquoGravityAttract07a javardquo source code

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3258

paste it and then do a few alterations First click on the ldquocalculaterdquo button right click choose Eventsand then choose Action then actionPerformed (Figure 6) It will bring you to where you should insertyour coding (Figure 7) You can write your own coding or copypaste any coding from somewhereelse (in this case from ldquoGravityAttract07ajavardquo)

Figure 6

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3358

Figure 7

Copy ldquoGravityAttract07ajavardquo source code and paste to the place after ldquo TODO add youhandling code hererdquo see Figure 7 To see which part has been modified the coding in Figure 7 hasbeen color coded for better understanding as follows

private void jButton1ActionPerformed(javaawteventActionEvent evt) TODO add your handling code here

double[] m = new double[6] mass of objectdouble[] x = new double[6] x-coor of objectdouble[] y = new double[6] y-coor of objectdouble f resultant forcedouble fxfy x amp y component of forcedouble rx ry rp co G dirfangdirfDecimalFormat dff dfangdirfint j mi

G = 667e-11m[1]=25 x[1]=12 y[1]=25m[2]=15 x[2]=15 y[2]=15m[3]=75 x[3]=02 y[3]=55m[4]=92 x[4]=52 y[4]=25m[5]=66 x[5]=42 y[5]=45

BufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin)) String mass= Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3458

catch(IOException e)Systemoutprintln(Not a valid input value) int mi = IntegerparseInt(mass)mi = IntegerparseInt(jTextField1getText())fx = 00fy = 00for(j=1 jlt=5 j++)

if (j=mi)rx = x[j]-x[mi]ry = y[j]-y[mi]rp = Mathpow((rxrx+ryry)15) (rx^2 + ry^2) raised to the power of 32co = Gm[mi]m[j]fx = fx + corxrpfy = fy + coryrp

f = Mathsqrt(fxfx + fyfy)

dirf = Mathatan(fyfx)angdirf = (dirf(22070))1800 Systemoutprintf(ldquoThe resultant force on particle [ldquo+ mi +rdquo] due to the other masses is

14e N 22f degnrdquofangdirf)dff = new javatextDecimalFormat(ldquoE00rdquo)dfangdirf = new DecimalFormat(ldquo000rdquo)

jTextField2setText(ldquordquo + mi) jTextField3setText(ldquordquo + dffformat(f)) jTextField4setText(ldquordquo + dfangdirfformat(angdirf))

Note

Brown = automatically generatedBlue = the original code from ldquoGravityAttract07ajavardquoGreen = coding from ldquoGravityAttract07ajavardquo which were being disabled due to

incompatibilities with NetbeansRed = a new inserted coding a few of them is to rephrasesubstitute the green one

Javascripts command line useBufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin))String mass=Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))try mass=dataInreadLine()catch(IOException e)Systemoutprintln(Not a valid input value)

int mi = IntegerparseInt(mass)

to read any input from the keyboard typed by the user while Netbeans uses egldquoIntegerparseInt(jTextField1getText())rdquo etc Javascripts command line uses ldquoSystemoutprintfrdquo to format the printed output whileNetbeans uses more complicated way to do the same job (see below)

Javascript command line

Systemoutprintf(The resultant force on particle [+ mi +] due to the other masses is 14e N22f degnfangdirf)

Netbeansdff = new javatextDecimalFormat(ldquo0E00rdquo)df di f D i lF t(ldquo0 00rdquo)

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3558

jTextField2setText(ldquordquo + mi) jTextField3setText(ldquordquo + dffformat(f)) jTextField4setText(ldquordquo + dfangdirfformat(angdirf))

ldquodffrdquo and ldquodfangdirfrdquo are new variables that should be declared and added to format thenumbers into decimal format ie ldquo0E00rdquo to get a scientific format eg ldquo20019E-10rdquo and ldquo000rdquoto get a decimal number eg ldquo-1570rdquo

To use DecimalFormat you have to put ldquoimport javatextDecimalFormatrdquo in the top of thecoding as shown in Figure 8

Figure 8

To reset the calculation you need to put coding into the ldquoresetrdquo button (this is ldquoJButton2rdquo)First click on the ldquoresetrdquo button right click choose Events and then choose Action thenactionPerformed (similar like in Figure 6) It will bring you to where you should insert your coding(Figure 9) Write the following coding (Figure 9)

Figure 9

To exit the program you need to put coding into the ldquoexitrdquo button (this is ldquoJButton3rdquo) Firstclick on the ldquoexitrdquo button right click choose Events and then choose Action then actionPerformed(similar like in Figure 6) It will bring you to where you should insert your coding (Figure 10) Writethe following coding (Figure 10)

Figure 10

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3658

Once you finished now you can run and see the result If everything is fine then the output wilbe look like this (Figure 11)

Figure 11

Remember when you compile and run ldquoGravityAttract07ajavardquo using Javascripts commandline once you compile and run the result was looked like this

On which particle you want to calculate the resulting force 2The resultant force on particle [2] due to the other masses is 30673e-10 N 8837 deg

SEE THE DIFFERENCES AT THE USER INTERFACE

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3758

8th Step

Written by Mohd Khalid Kasmin

Monday 27 August 2007

Programming using command line interface (CLI) has its advantages but todays computer users are so used to graphical user

interface (GUI) that we just can no longer be contented with writing application that do not use the GUI Hence starting from this 8thstep we are going to embark on learning how to make java application that utilises the the GUI

It is possible to write a GUI application with the text based tools that we have been using for these past 7 steps but the job is so

messy that most of you will definitely be turnoff by it So to make life slightly easier well use an integrated development

environment (IDE) known as NetBeans specifically NetBeans 551

0 First ssh to the our server Next once logged in just type nice -n15 netbeans amp on the terminal If you had sshed with the -Y

switch after a few tens of seconds NetBeans should emerge on your display screen

One important point to note Since our server can readily handle up to 100 users (maybe even

more) doing work in CLI mode this machine will certainly be taxed to its maximum capability once

we start using graphical based application NetBeans 551 uses a geat deal of computer memory

and with the 4Gbytes RAM server it can only accomodate up to 25 users concurrently using

NetBeans if the users use the application wisely What is meant by wisely here is that (a) when

starting the NetBeans application please use it together with the nice -n15 command (b) in using

the NetBeans please make sure you only have one project open at a time and (c) please close your

project everytime before quitting the NetBeans

Home Profile Teaching Links Contact Me My Work

Newsflash

Yesterday all servers in the US went out on strike in a bid to get more RAM and better CPUs A spokes person said that the

need for better RAM was due to some fool increasing the front-side bus speed In future busses will be told to slow down in

residential motherboards

search

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3858

1

2

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3958

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4058

[ Back ]

5

Last Updated ( Wednesday 31 October 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4158

8th Step (Cont)

Written by Mohd Khalid Kasmin

Monday 27 August 2007

Up to this point we have define the name of our project Next we construct a class and to follow what we already know we name the class the sam

project name latihan01

6

7

Home Profile Teaching Links Contact Me My Work

Newsflash

Joomla 10 - Experience the Freedom It has never been easier to create your own dynamic site Manage all your content from

the best CMS admin interface

search

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4258

8

9

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4358

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4458

8th Step (Cont still)

Written by Mohd Khalid Kasmin

Monday 27 August 2007

In item (9) we see that Netbeans does not provide us with the main method So to follow what we already know we now put in the

main method

10

Home Profile Teaching Links Contact Me My Work

Newsflash

Aoccdrnig to a rscheearch at an Elingsh uinervtisy it deosnt mttaer in waht oredr the ltteers in a wrod are the olny iprmoetnt

tihng is taht frist and lsat ltteer is at the rghit pclae The rset can be a toatl mses and you can sitll raed it wouthit porbelm Tihs is

bcuseae we do not raed ervey lteter by itslef but the wrod as a wlohe

search

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4558

11

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4658

12

13

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4758

14

15

Last Updated ( Monday 27 August 2007 )

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4858

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4958

9th Step

Thursday 06 September 2007

Step by Step into Java Programming

for

University Physics Student

By

Mohd Khalid Kasmin

A Building graphical user interface - preparing the JFrame Form

At this stage of our programming experience with Java and NetBeans please follow strictly the steps outline in this article First and

make a new GUI project make sure there is no other project that already been loaded by Netbeans Please close any project displayed

1 Create new project

- On the main menu click on File --gt New Project (note Close first any previous project)

- Specify project name amp project location - Untick Create Main Class item

- Click on the Finish button

Home Profile Teaching Links Contact Me My Work

Newsflash

Aoccdrnig to a rscheearch at an Elingsh uinervtisy it deosnt mttaer in waht oredr the ltteers in a wrod are the olny iprmoetnt

tihng is taht frist and lsat ltteer is at the rghit pclae The rset can be a toatl mses and you can sitll raed it wouthit porbelm Tihs is

bcuseae we do not raed ervey lteter by itslef but the wrod as a wlohe

search

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5058

Figure 91

2 Create UI class

- Right click on the project name in the Project pane

on the popup menu select New --gt JFrame Form

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5158

Figure 92

- In the New JFrame Form dialog specify the Class Name and the Package name

eg Class Name multiplierUI Package mymultiplier

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5258

Figure 93

After clicking on the Finish button we should get something like the following

Figure 94

Last Updated ( Tuesday 18 September 2007 )

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5358

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5458

9th Step (Cont)

Written by Mohd Khalid Kasmin

Tuesday 11 September 2007

Step by Step into Java Programming

for University Physics Student

By

Mohd Khalid Kasmin

B Building user interface (UI) - putting in the components

Now that we have a proper blank form we can start filling the form with the suitable graphical components For introduction to

GUI programming an appropriate application to build (for a physics student) is a simple solver So our first exercise is to build an

application that takes two numbers and multiply them and display the result

With GUI we usually give input values to an application via input boxes that are labelled When all inputs are typed in properlywe click on a button to ask the application to execute the job Hence in this exercise well do just that The target now is to

construct a user interface that looks like Figure 95 below

Figure 95

Home Profile Teaching Links Contact Me My Work

Newsflash

Joomla 10 - Experience the Freedom It has never been easier to create your own dynamic site Manage all your content from

the best CMS admin interface

search

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5558

[ Back ]

1 Problemapplication description box

- In the palette pane click on Swing component named JTextPane (Note click just once) Move the mouse pointer on top of

your JFrame (Note just move not drag) When you see a little white box with orange border position it somewhere near the

top part of your JFrame and click once The JTextPane is then attached on the JFrame Addjust its size with the help of the

sizing hook around the perimeter of the JTextPane

- Edit the text property of the JTextPane with the description of your application

2 Input boxes

- In the palette pane click on Swing component named JTextField Next move the mouse pointer on top of your JFrame

When you see a white box with the word jTextField1 position it to the left and under the JTextPane1

- Click on Swing component JTextField in the palette pane again Move the mouse pointer on top of the JFrame you should

get a second JTextField named jTextField2 Position it to the right of jTextField1

Last Updated ( Tuesday 11 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5658

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5758

multiplication if a b and c were first declared as double or float or int variables So the code of the calculation should look like the

following

double a b c

a =

b =

c = a b

Clearly variables a and b need to be assigned with values of typed double Now looking back at inputboxes A and B we made

those boxes using the JTextField component meaning that any data or value written in a JTextField is of typed Text or String even

if the value displayed in the box is ldquo125rdquo But f irst here there is this task of getting to that Text (or String) values ldquo125rdquo and ldquo72rdquo

In Java we can get to a text in JTextField component using the getText() method So to retrieve values in jTextField1 and

jTextField2 we can use

jTextField1getText()

and

jTextField2getText()

But those values are still in text mode and we need another method to convert the data mode f rom text to double For that we can

use the method DoubleparseDouble() Thus the whole process of retrieve text -gt convert text to double -gt assign to double

variable can be done using

a = DoubleparseDouble( jTextField1getText() )

and

b = DoubleparseDouble( jTextField2getText() )

Hence the code for performing the multiplication becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

Up to this point we have the variable c as the result of the multiplication of a and b So the next thing to do is to display this

result on the JLabel that we have already prepared before

A x B = _____

The text A x B = ______ was made using JLabel component and its variable name is jLabel3 Since the variable c is of type

double and text property of JLabel is of type String we need to convert it first to String data type The method to convert a double

to String is DoubletoString() So we can write the code as DoubletoString(c) After getting the text we can now use it to be the

text displayed on jLabel3 To do that we use the method setText() Hence the appropriate code for displaying the result is

jLabel3setText( A x B = + DoubletoString(c) )

And the full code for our calculation becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

jLabel3setText( A x B = + DoubletoString(c) )

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5858

[ Back ]

Last Updated ( Tuesday 18 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

Page 24: Computional Physics

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2458

Damped Harmonic Oscillation

Object m is on a flat surface attached to a spring as in figure above The object is pulled

to position A and released so that it oscillates about x =0 Assuming that the force F=ma= -bv-kx where x is position of the object v is the velocity k the spring constant andb is a constant associated with friction with the flat surface

The algorithm

1 Declare the classmethod to be used2 Declare and assign all variables to be used

x t delt k b A m F a v v0 and x03 Declare and assign constants to the respective variables

k = 02 b = 01 A = 01 m = 054 Select appropriate value for ∆t 5 Initialized initial values xo=A vo=00

6 set x=xo v=vo and t = 07 calculate the value of force F = -kx-bv 8 calculate the value of acceleration a = Fm

9 calculate velocity v = vo + ∆v = vo + a ∆t

10 calculate position x = xo + ∆x = xo + v ∆t

11 increment value of t by ∆t 12 print out values of t and x

13 swap the value of xo=x vo=v 14 repeat from step 4 until stopping condition is satisfied (tlt=600)

The java code

public class simplehar01

public static void main(String args[])double x t deltdouble k b A m F a v v0 x0

k = 02b = 01A = 0 1

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2558

m = 05delt = 0001v0 = 00x0 = Ax = x0 v = v0

t = 00while(tlt=600)

F = -kx-bva = Fmv = v0 + adeltx = x0 + vdeltt = t + deltSystemoutprintf(f fntx)x0 = xv0 = v

gnuplot result

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2658

983107983119983117983120983125983124983105983124983113983119983118983105983116 983120983112983129983123983113983107983123

(983123983123983120983090983089983090983090983081

983106983157983145983148983140983145983150983143 983137 983152983144983161983155983145983139983155 983155983151983148983158983141983154

983157983155983145983150983143 983118983141983156983138983141983137983150983155

983138983161

983105983138983140983086 983115983144983137983149983145983149 983113983155983149983137983145983148

983117983151983144983140 983115983144983137983148983145983140 983115983137983155983149983145983150

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2758

How to create a user interface (UI) using Netbeans

Based on the ldquoGravityAttract07ajavardquo source code (shown below) build a user interface usingNetbeans The design of the UI could be look like this (Figure1)

Figure 1

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2858

ldquoGravityAttract07ajavardquo source code

Java class for calculating the resultant force on any object(ie either on m1m2 m3 m4 or m5)

import static javalangMathimport javaiopublic class GravityAttract07a

public static void main(String args[])

double[] m = new double[6] mass of objectdouble[] x = new double[6] x-coor of objectdouble[] y = new double[6] y-coor of objectdouble f resultant forcedouble fxfy x amp y component of forcedouble rx ry rp co G dirfangdirf

int j

G = 667e-11m[1]=25 x[1]=12 y[1]=25m[2]=15 x[2]=15 y[2]=15m[3]=75 x[3]=02 y[3]=55m[4]=92 x[4]=52 y[4]=25m[5]=66 x[5]=42 y[5]=45BufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin))String mass=Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))try mass=dataInreadLine()

catch(IOException e)Systemoutprintln(Not a valid input value)int mi = IntegerparseInt(mass)fx = 00fy = 00

for(j=1 jlt=5 j++)

if (j=mi)rx = x[j]-x[mi]ry = y[j]-y[mi]rp = pow((rxrx+ryry)15) (rx^2 + ry^2) raised to the power of 32co = Gm[mi]m[j]

fx = fx + corxrpfy = fy + coryrp

f = sqrt(fxfx + fyfy)dirf = toDegrees(atan(fyfx))Systemoutprintf(The force on m[+ mi +] due to the other masses is eN fdegnfdirf)

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2958

This example will not show you step by step details procedure since it already explained inthe class and in the e-learning Please refer to the e-learning about how to create a project how tocreate JFrame Form and as well how to create JButton JTextField JLabel JTextPane and so on

First create a new project and give a name eg ldquogravityrdquo and then create JFrame Form andthe following JButton JTextField JLabel JTextPane etc by using ldquoswing paletterdquo as shown in Figure2

Figure 2

To insert a picture into JLabel2 click on the JLabel2 go to properties and select ldquoiconrdquo Onthe icon editor choose file and then select file to locate the directory of your graphicpicture file(Figure 3) Select the file and click OK when finished Make sure you have already created your ownpicture file and save it as jpg png bmp etc format eg ldquogravityjpgrdquo Use any graphic package todraw your diagrampicture or use drawing tools available in Open Office or Microsoft Word

JTextPane1JLabel1

JLabel2

JLabel3JLabel4

JLabel5

JLabel6 JLabel7

JLabel8

JTextField2 JTextField3 JTextField4

JTextF

JButton1JButton2

JButton3

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3058

Figure 3

To write documentation or question eg in JTextPane1 click on the JTextPane1 go toproperties and select ldquotextrdquo Type the documentation in the text editor as shown in Figure 4 Click OKwhen finished

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3158

To do formatting on the text such as superscript and subscript eg in jLabel3 click on the

JLabel3 go to properties and select ldquotextrdquo Type the documentationmessage on the text editor asshown in Figure 5 as follows

eg

lthtmlgtOn which particle (mltsubgt1ltsubgtmltsubgt5ltsubgt) you want to calculate theresultant force

Use sup instead if you want a superscriptThe result will look like this

Click OK when finished

Figure 5

To do the calculation you need to put the coding similar like ldquoGravityAttract07ajavardquo and put itinto the ldquocalculaterdquo button (this is ldquoJButton1rdquo) You can copy ldquoGravityAttract07a javardquo source code

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3258

paste it and then do a few alterations First click on the ldquocalculaterdquo button right click choose Eventsand then choose Action then actionPerformed (Figure 6) It will bring you to where you should insertyour coding (Figure 7) You can write your own coding or copypaste any coding from somewhereelse (in this case from ldquoGravityAttract07ajavardquo)

Figure 6

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3358

Figure 7

Copy ldquoGravityAttract07ajavardquo source code and paste to the place after ldquo TODO add youhandling code hererdquo see Figure 7 To see which part has been modified the coding in Figure 7 hasbeen color coded for better understanding as follows

private void jButton1ActionPerformed(javaawteventActionEvent evt) TODO add your handling code here

double[] m = new double[6] mass of objectdouble[] x = new double[6] x-coor of objectdouble[] y = new double[6] y-coor of objectdouble f resultant forcedouble fxfy x amp y component of forcedouble rx ry rp co G dirfangdirfDecimalFormat dff dfangdirfint j mi

G = 667e-11m[1]=25 x[1]=12 y[1]=25m[2]=15 x[2]=15 y[2]=15m[3]=75 x[3]=02 y[3]=55m[4]=92 x[4]=52 y[4]=25m[5]=66 x[5]=42 y[5]=45

BufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin)) String mass= Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3458

catch(IOException e)Systemoutprintln(Not a valid input value) int mi = IntegerparseInt(mass)mi = IntegerparseInt(jTextField1getText())fx = 00fy = 00for(j=1 jlt=5 j++)

if (j=mi)rx = x[j]-x[mi]ry = y[j]-y[mi]rp = Mathpow((rxrx+ryry)15) (rx^2 + ry^2) raised to the power of 32co = Gm[mi]m[j]fx = fx + corxrpfy = fy + coryrp

f = Mathsqrt(fxfx + fyfy)

dirf = Mathatan(fyfx)angdirf = (dirf(22070))1800 Systemoutprintf(ldquoThe resultant force on particle [ldquo+ mi +rdquo] due to the other masses is

14e N 22f degnrdquofangdirf)dff = new javatextDecimalFormat(ldquoE00rdquo)dfangdirf = new DecimalFormat(ldquo000rdquo)

jTextField2setText(ldquordquo + mi) jTextField3setText(ldquordquo + dffformat(f)) jTextField4setText(ldquordquo + dfangdirfformat(angdirf))

Note

Brown = automatically generatedBlue = the original code from ldquoGravityAttract07ajavardquoGreen = coding from ldquoGravityAttract07ajavardquo which were being disabled due to

incompatibilities with NetbeansRed = a new inserted coding a few of them is to rephrasesubstitute the green one

Javascripts command line useBufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin))String mass=Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))try mass=dataInreadLine()catch(IOException e)Systemoutprintln(Not a valid input value)

int mi = IntegerparseInt(mass)

to read any input from the keyboard typed by the user while Netbeans uses egldquoIntegerparseInt(jTextField1getText())rdquo etc Javascripts command line uses ldquoSystemoutprintfrdquo to format the printed output whileNetbeans uses more complicated way to do the same job (see below)

Javascript command line

Systemoutprintf(The resultant force on particle [+ mi +] due to the other masses is 14e N22f degnfangdirf)

Netbeansdff = new javatextDecimalFormat(ldquo0E00rdquo)df di f D i lF t(ldquo0 00rdquo)

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3558

jTextField2setText(ldquordquo + mi) jTextField3setText(ldquordquo + dffformat(f)) jTextField4setText(ldquordquo + dfangdirfformat(angdirf))

ldquodffrdquo and ldquodfangdirfrdquo are new variables that should be declared and added to format thenumbers into decimal format ie ldquo0E00rdquo to get a scientific format eg ldquo20019E-10rdquo and ldquo000rdquoto get a decimal number eg ldquo-1570rdquo

To use DecimalFormat you have to put ldquoimport javatextDecimalFormatrdquo in the top of thecoding as shown in Figure 8

Figure 8

To reset the calculation you need to put coding into the ldquoresetrdquo button (this is ldquoJButton2rdquo)First click on the ldquoresetrdquo button right click choose Events and then choose Action thenactionPerformed (similar like in Figure 6) It will bring you to where you should insert your coding(Figure 9) Write the following coding (Figure 9)

Figure 9

To exit the program you need to put coding into the ldquoexitrdquo button (this is ldquoJButton3rdquo) Firstclick on the ldquoexitrdquo button right click choose Events and then choose Action then actionPerformed(similar like in Figure 6) It will bring you to where you should insert your coding (Figure 10) Writethe following coding (Figure 10)

Figure 10

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3658

Once you finished now you can run and see the result If everything is fine then the output wilbe look like this (Figure 11)

Figure 11

Remember when you compile and run ldquoGravityAttract07ajavardquo using Javascripts commandline once you compile and run the result was looked like this

On which particle you want to calculate the resulting force 2The resultant force on particle [2] due to the other masses is 30673e-10 N 8837 deg

SEE THE DIFFERENCES AT THE USER INTERFACE

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3758

8th Step

Written by Mohd Khalid Kasmin

Monday 27 August 2007

Programming using command line interface (CLI) has its advantages but todays computer users are so used to graphical user

interface (GUI) that we just can no longer be contented with writing application that do not use the GUI Hence starting from this 8thstep we are going to embark on learning how to make java application that utilises the the GUI

It is possible to write a GUI application with the text based tools that we have been using for these past 7 steps but the job is so

messy that most of you will definitely be turnoff by it So to make life slightly easier well use an integrated development

environment (IDE) known as NetBeans specifically NetBeans 551

0 First ssh to the our server Next once logged in just type nice -n15 netbeans amp on the terminal If you had sshed with the -Y

switch after a few tens of seconds NetBeans should emerge on your display screen

One important point to note Since our server can readily handle up to 100 users (maybe even

more) doing work in CLI mode this machine will certainly be taxed to its maximum capability once

we start using graphical based application NetBeans 551 uses a geat deal of computer memory

and with the 4Gbytes RAM server it can only accomodate up to 25 users concurrently using

NetBeans if the users use the application wisely What is meant by wisely here is that (a) when

starting the NetBeans application please use it together with the nice -n15 command (b) in using

the NetBeans please make sure you only have one project open at a time and (c) please close your

project everytime before quitting the NetBeans

Home Profile Teaching Links Contact Me My Work

Newsflash

Yesterday all servers in the US went out on strike in a bid to get more RAM and better CPUs A spokes person said that the

need for better RAM was due to some fool increasing the front-side bus speed In future busses will be told to slow down in

residential motherboards

search

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3858

1

2

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3958

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4058

[ Back ]

5

Last Updated ( Wednesday 31 October 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4158

8th Step (Cont)

Written by Mohd Khalid Kasmin

Monday 27 August 2007

Up to this point we have define the name of our project Next we construct a class and to follow what we already know we name the class the sam

project name latihan01

6

7

Home Profile Teaching Links Contact Me My Work

Newsflash

Joomla 10 - Experience the Freedom It has never been easier to create your own dynamic site Manage all your content from

the best CMS admin interface

search

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4258

8

9

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4358

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4458

8th Step (Cont still)

Written by Mohd Khalid Kasmin

Monday 27 August 2007

In item (9) we see that Netbeans does not provide us with the main method So to follow what we already know we now put in the

main method

10

Home Profile Teaching Links Contact Me My Work

Newsflash

Aoccdrnig to a rscheearch at an Elingsh uinervtisy it deosnt mttaer in waht oredr the ltteers in a wrod are the olny iprmoetnt

tihng is taht frist and lsat ltteer is at the rghit pclae The rset can be a toatl mses and you can sitll raed it wouthit porbelm Tihs is

bcuseae we do not raed ervey lteter by itslef but the wrod as a wlohe

search

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4558

11

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4658

12

13

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4758

14

15

Last Updated ( Monday 27 August 2007 )

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4858

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4958

9th Step

Thursday 06 September 2007

Step by Step into Java Programming

for

University Physics Student

By

Mohd Khalid Kasmin

A Building graphical user interface - preparing the JFrame Form

At this stage of our programming experience with Java and NetBeans please follow strictly the steps outline in this article First and

make a new GUI project make sure there is no other project that already been loaded by Netbeans Please close any project displayed

1 Create new project

- On the main menu click on File --gt New Project (note Close first any previous project)

- Specify project name amp project location - Untick Create Main Class item

- Click on the Finish button

Home Profile Teaching Links Contact Me My Work

Newsflash

Aoccdrnig to a rscheearch at an Elingsh uinervtisy it deosnt mttaer in waht oredr the ltteers in a wrod are the olny iprmoetnt

tihng is taht frist and lsat ltteer is at the rghit pclae The rset can be a toatl mses and you can sitll raed it wouthit porbelm Tihs is

bcuseae we do not raed ervey lteter by itslef but the wrod as a wlohe

search

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5058

Figure 91

2 Create UI class

- Right click on the project name in the Project pane

on the popup menu select New --gt JFrame Form

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5158

Figure 92

- In the New JFrame Form dialog specify the Class Name and the Package name

eg Class Name multiplierUI Package mymultiplier

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5258

Figure 93

After clicking on the Finish button we should get something like the following

Figure 94

Last Updated ( Tuesday 18 September 2007 )

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5358

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5458

9th Step (Cont)

Written by Mohd Khalid Kasmin

Tuesday 11 September 2007

Step by Step into Java Programming

for University Physics Student

By

Mohd Khalid Kasmin

B Building user interface (UI) - putting in the components

Now that we have a proper blank form we can start filling the form with the suitable graphical components For introduction to

GUI programming an appropriate application to build (for a physics student) is a simple solver So our first exercise is to build an

application that takes two numbers and multiply them and display the result

With GUI we usually give input values to an application via input boxes that are labelled When all inputs are typed in properlywe click on a button to ask the application to execute the job Hence in this exercise well do just that The target now is to

construct a user interface that looks like Figure 95 below

Figure 95

Home Profile Teaching Links Contact Me My Work

Newsflash

Joomla 10 - Experience the Freedom It has never been easier to create your own dynamic site Manage all your content from

the best CMS admin interface

search

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5558

[ Back ]

1 Problemapplication description box

- In the palette pane click on Swing component named JTextPane (Note click just once) Move the mouse pointer on top of

your JFrame (Note just move not drag) When you see a little white box with orange border position it somewhere near the

top part of your JFrame and click once The JTextPane is then attached on the JFrame Addjust its size with the help of the

sizing hook around the perimeter of the JTextPane

- Edit the text property of the JTextPane with the description of your application

2 Input boxes

- In the palette pane click on Swing component named JTextField Next move the mouse pointer on top of your JFrame

When you see a white box with the word jTextField1 position it to the left and under the JTextPane1

- Click on Swing component JTextField in the palette pane again Move the mouse pointer on top of the JFrame you should

get a second JTextField named jTextField2 Position it to the right of jTextField1

Last Updated ( Tuesday 11 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5658

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5758

multiplication if a b and c were first declared as double or float or int variables So the code of the calculation should look like the

following

double a b c

a =

b =

c = a b

Clearly variables a and b need to be assigned with values of typed double Now looking back at inputboxes A and B we made

those boxes using the JTextField component meaning that any data or value written in a JTextField is of typed Text or String even

if the value displayed in the box is ldquo125rdquo But f irst here there is this task of getting to that Text (or String) values ldquo125rdquo and ldquo72rdquo

In Java we can get to a text in JTextField component using the getText() method So to retrieve values in jTextField1 and

jTextField2 we can use

jTextField1getText()

and

jTextField2getText()

But those values are still in text mode and we need another method to convert the data mode f rom text to double For that we can

use the method DoubleparseDouble() Thus the whole process of retrieve text -gt convert text to double -gt assign to double

variable can be done using

a = DoubleparseDouble( jTextField1getText() )

and

b = DoubleparseDouble( jTextField2getText() )

Hence the code for performing the multiplication becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

Up to this point we have the variable c as the result of the multiplication of a and b So the next thing to do is to display this

result on the JLabel that we have already prepared before

A x B = _____

The text A x B = ______ was made using JLabel component and its variable name is jLabel3 Since the variable c is of type

double and text property of JLabel is of type String we need to convert it first to String data type The method to convert a double

to String is DoubletoString() So we can write the code as DoubletoString(c) After getting the text we can now use it to be the

text displayed on jLabel3 To do that we use the method setText() Hence the appropriate code for displaying the result is

jLabel3setText( A x B = + DoubletoString(c) )

And the full code for our calculation becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

jLabel3setText( A x B = + DoubletoString(c) )

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5858

[ Back ]

Last Updated ( Tuesday 18 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

Page 25: Computional Physics

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2558

m = 05delt = 0001v0 = 00x0 = Ax = x0 v = v0

t = 00while(tlt=600)

F = -kx-bva = Fmv = v0 + adeltx = x0 + vdeltt = t + deltSystemoutprintf(f fntx)x0 = xv0 = v

gnuplot result

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2658

983107983119983117983120983125983124983105983124983113983119983118983105983116 983120983112983129983123983113983107983123

(983123983123983120983090983089983090983090983081

983106983157983145983148983140983145983150983143 983137 983152983144983161983155983145983139983155 983155983151983148983158983141983154

983157983155983145983150983143 983118983141983156983138983141983137983150983155

983138983161

983105983138983140983086 983115983144983137983149983145983149 983113983155983149983137983145983148

983117983151983144983140 983115983144983137983148983145983140 983115983137983155983149983145983150

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2758

How to create a user interface (UI) using Netbeans

Based on the ldquoGravityAttract07ajavardquo source code (shown below) build a user interface usingNetbeans The design of the UI could be look like this (Figure1)

Figure 1

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2858

ldquoGravityAttract07ajavardquo source code

Java class for calculating the resultant force on any object(ie either on m1m2 m3 m4 or m5)

import static javalangMathimport javaiopublic class GravityAttract07a

public static void main(String args[])

double[] m = new double[6] mass of objectdouble[] x = new double[6] x-coor of objectdouble[] y = new double[6] y-coor of objectdouble f resultant forcedouble fxfy x amp y component of forcedouble rx ry rp co G dirfangdirf

int j

G = 667e-11m[1]=25 x[1]=12 y[1]=25m[2]=15 x[2]=15 y[2]=15m[3]=75 x[3]=02 y[3]=55m[4]=92 x[4]=52 y[4]=25m[5]=66 x[5]=42 y[5]=45BufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin))String mass=Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))try mass=dataInreadLine()

catch(IOException e)Systemoutprintln(Not a valid input value)int mi = IntegerparseInt(mass)fx = 00fy = 00

for(j=1 jlt=5 j++)

if (j=mi)rx = x[j]-x[mi]ry = y[j]-y[mi]rp = pow((rxrx+ryry)15) (rx^2 + ry^2) raised to the power of 32co = Gm[mi]m[j]

fx = fx + corxrpfy = fy + coryrp

f = sqrt(fxfx + fyfy)dirf = toDegrees(atan(fyfx))Systemoutprintf(The force on m[+ mi +] due to the other masses is eN fdegnfdirf)

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2958

This example will not show you step by step details procedure since it already explained inthe class and in the e-learning Please refer to the e-learning about how to create a project how tocreate JFrame Form and as well how to create JButton JTextField JLabel JTextPane and so on

First create a new project and give a name eg ldquogravityrdquo and then create JFrame Form andthe following JButton JTextField JLabel JTextPane etc by using ldquoswing paletterdquo as shown in Figure2

Figure 2

To insert a picture into JLabel2 click on the JLabel2 go to properties and select ldquoiconrdquo Onthe icon editor choose file and then select file to locate the directory of your graphicpicture file(Figure 3) Select the file and click OK when finished Make sure you have already created your ownpicture file and save it as jpg png bmp etc format eg ldquogravityjpgrdquo Use any graphic package todraw your diagrampicture or use drawing tools available in Open Office or Microsoft Word

JTextPane1JLabel1

JLabel2

JLabel3JLabel4

JLabel5

JLabel6 JLabel7

JLabel8

JTextField2 JTextField3 JTextField4

JTextF

JButton1JButton2

JButton3

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3058

Figure 3

To write documentation or question eg in JTextPane1 click on the JTextPane1 go toproperties and select ldquotextrdquo Type the documentation in the text editor as shown in Figure 4 Click OKwhen finished

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3158

To do formatting on the text such as superscript and subscript eg in jLabel3 click on the

JLabel3 go to properties and select ldquotextrdquo Type the documentationmessage on the text editor asshown in Figure 5 as follows

eg

lthtmlgtOn which particle (mltsubgt1ltsubgtmltsubgt5ltsubgt) you want to calculate theresultant force

Use sup instead if you want a superscriptThe result will look like this

Click OK when finished

Figure 5

To do the calculation you need to put the coding similar like ldquoGravityAttract07ajavardquo and put itinto the ldquocalculaterdquo button (this is ldquoJButton1rdquo) You can copy ldquoGravityAttract07a javardquo source code

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3258

paste it and then do a few alterations First click on the ldquocalculaterdquo button right click choose Eventsand then choose Action then actionPerformed (Figure 6) It will bring you to where you should insertyour coding (Figure 7) You can write your own coding or copypaste any coding from somewhereelse (in this case from ldquoGravityAttract07ajavardquo)

Figure 6

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3358

Figure 7

Copy ldquoGravityAttract07ajavardquo source code and paste to the place after ldquo TODO add youhandling code hererdquo see Figure 7 To see which part has been modified the coding in Figure 7 hasbeen color coded for better understanding as follows

private void jButton1ActionPerformed(javaawteventActionEvent evt) TODO add your handling code here

double[] m = new double[6] mass of objectdouble[] x = new double[6] x-coor of objectdouble[] y = new double[6] y-coor of objectdouble f resultant forcedouble fxfy x amp y component of forcedouble rx ry rp co G dirfangdirfDecimalFormat dff dfangdirfint j mi

G = 667e-11m[1]=25 x[1]=12 y[1]=25m[2]=15 x[2]=15 y[2]=15m[3]=75 x[3]=02 y[3]=55m[4]=92 x[4]=52 y[4]=25m[5]=66 x[5]=42 y[5]=45

BufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin)) String mass= Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3458

catch(IOException e)Systemoutprintln(Not a valid input value) int mi = IntegerparseInt(mass)mi = IntegerparseInt(jTextField1getText())fx = 00fy = 00for(j=1 jlt=5 j++)

if (j=mi)rx = x[j]-x[mi]ry = y[j]-y[mi]rp = Mathpow((rxrx+ryry)15) (rx^2 + ry^2) raised to the power of 32co = Gm[mi]m[j]fx = fx + corxrpfy = fy + coryrp

f = Mathsqrt(fxfx + fyfy)

dirf = Mathatan(fyfx)angdirf = (dirf(22070))1800 Systemoutprintf(ldquoThe resultant force on particle [ldquo+ mi +rdquo] due to the other masses is

14e N 22f degnrdquofangdirf)dff = new javatextDecimalFormat(ldquoE00rdquo)dfangdirf = new DecimalFormat(ldquo000rdquo)

jTextField2setText(ldquordquo + mi) jTextField3setText(ldquordquo + dffformat(f)) jTextField4setText(ldquordquo + dfangdirfformat(angdirf))

Note

Brown = automatically generatedBlue = the original code from ldquoGravityAttract07ajavardquoGreen = coding from ldquoGravityAttract07ajavardquo which were being disabled due to

incompatibilities with NetbeansRed = a new inserted coding a few of them is to rephrasesubstitute the green one

Javascripts command line useBufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin))String mass=Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))try mass=dataInreadLine()catch(IOException e)Systemoutprintln(Not a valid input value)

int mi = IntegerparseInt(mass)

to read any input from the keyboard typed by the user while Netbeans uses egldquoIntegerparseInt(jTextField1getText())rdquo etc Javascripts command line uses ldquoSystemoutprintfrdquo to format the printed output whileNetbeans uses more complicated way to do the same job (see below)

Javascript command line

Systemoutprintf(The resultant force on particle [+ mi +] due to the other masses is 14e N22f degnfangdirf)

Netbeansdff = new javatextDecimalFormat(ldquo0E00rdquo)df di f D i lF t(ldquo0 00rdquo)

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3558

jTextField2setText(ldquordquo + mi) jTextField3setText(ldquordquo + dffformat(f)) jTextField4setText(ldquordquo + dfangdirfformat(angdirf))

ldquodffrdquo and ldquodfangdirfrdquo are new variables that should be declared and added to format thenumbers into decimal format ie ldquo0E00rdquo to get a scientific format eg ldquo20019E-10rdquo and ldquo000rdquoto get a decimal number eg ldquo-1570rdquo

To use DecimalFormat you have to put ldquoimport javatextDecimalFormatrdquo in the top of thecoding as shown in Figure 8

Figure 8

To reset the calculation you need to put coding into the ldquoresetrdquo button (this is ldquoJButton2rdquo)First click on the ldquoresetrdquo button right click choose Events and then choose Action thenactionPerformed (similar like in Figure 6) It will bring you to where you should insert your coding(Figure 9) Write the following coding (Figure 9)

Figure 9

To exit the program you need to put coding into the ldquoexitrdquo button (this is ldquoJButton3rdquo) Firstclick on the ldquoexitrdquo button right click choose Events and then choose Action then actionPerformed(similar like in Figure 6) It will bring you to where you should insert your coding (Figure 10) Writethe following coding (Figure 10)

Figure 10

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3658

Once you finished now you can run and see the result If everything is fine then the output wilbe look like this (Figure 11)

Figure 11

Remember when you compile and run ldquoGravityAttract07ajavardquo using Javascripts commandline once you compile and run the result was looked like this

On which particle you want to calculate the resulting force 2The resultant force on particle [2] due to the other masses is 30673e-10 N 8837 deg

SEE THE DIFFERENCES AT THE USER INTERFACE

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3758

8th Step

Written by Mohd Khalid Kasmin

Monday 27 August 2007

Programming using command line interface (CLI) has its advantages but todays computer users are so used to graphical user

interface (GUI) that we just can no longer be contented with writing application that do not use the GUI Hence starting from this 8thstep we are going to embark on learning how to make java application that utilises the the GUI

It is possible to write a GUI application with the text based tools that we have been using for these past 7 steps but the job is so

messy that most of you will definitely be turnoff by it So to make life slightly easier well use an integrated development

environment (IDE) known as NetBeans specifically NetBeans 551

0 First ssh to the our server Next once logged in just type nice -n15 netbeans amp on the terminal If you had sshed with the -Y

switch after a few tens of seconds NetBeans should emerge on your display screen

One important point to note Since our server can readily handle up to 100 users (maybe even

more) doing work in CLI mode this machine will certainly be taxed to its maximum capability once

we start using graphical based application NetBeans 551 uses a geat deal of computer memory

and with the 4Gbytes RAM server it can only accomodate up to 25 users concurrently using

NetBeans if the users use the application wisely What is meant by wisely here is that (a) when

starting the NetBeans application please use it together with the nice -n15 command (b) in using

the NetBeans please make sure you only have one project open at a time and (c) please close your

project everytime before quitting the NetBeans

Home Profile Teaching Links Contact Me My Work

Newsflash

Yesterday all servers in the US went out on strike in a bid to get more RAM and better CPUs A spokes person said that the

need for better RAM was due to some fool increasing the front-side bus speed In future busses will be told to slow down in

residential motherboards

search

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3858

1

2

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3958

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4058

[ Back ]

5

Last Updated ( Wednesday 31 October 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4158

8th Step (Cont)

Written by Mohd Khalid Kasmin

Monday 27 August 2007

Up to this point we have define the name of our project Next we construct a class and to follow what we already know we name the class the sam

project name latihan01

6

7

Home Profile Teaching Links Contact Me My Work

Newsflash

Joomla 10 - Experience the Freedom It has never been easier to create your own dynamic site Manage all your content from

the best CMS admin interface

search

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4258

8

9

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4358

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4458

8th Step (Cont still)

Written by Mohd Khalid Kasmin

Monday 27 August 2007

In item (9) we see that Netbeans does not provide us with the main method So to follow what we already know we now put in the

main method

10

Home Profile Teaching Links Contact Me My Work

Newsflash

Aoccdrnig to a rscheearch at an Elingsh uinervtisy it deosnt mttaer in waht oredr the ltteers in a wrod are the olny iprmoetnt

tihng is taht frist and lsat ltteer is at the rghit pclae The rset can be a toatl mses and you can sitll raed it wouthit porbelm Tihs is

bcuseae we do not raed ervey lteter by itslef but the wrod as a wlohe

search

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4558

11

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4658

12

13

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4758

14

15

Last Updated ( Monday 27 August 2007 )

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4858

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4958

9th Step

Thursday 06 September 2007

Step by Step into Java Programming

for

University Physics Student

By

Mohd Khalid Kasmin

A Building graphical user interface - preparing the JFrame Form

At this stage of our programming experience with Java and NetBeans please follow strictly the steps outline in this article First and

make a new GUI project make sure there is no other project that already been loaded by Netbeans Please close any project displayed

1 Create new project

- On the main menu click on File --gt New Project (note Close first any previous project)

- Specify project name amp project location - Untick Create Main Class item

- Click on the Finish button

Home Profile Teaching Links Contact Me My Work

Newsflash

Aoccdrnig to a rscheearch at an Elingsh uinervtisy it deosnt mttaer in waht oredr the ltteers in a wrod are the olny iprmoetnt

tihng is taht frist and lsat ltteer is at the rghit pclae The rset can be a toatl mses and you can sitll raed it wouthit porbelm Tihs is

bcuseae we do not raed ervey lteter by itslef but the wrod as a wlohe

search

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5058

Figure 91

2 Create UI class

- Right click on the project name in the Project pane

on the popup menu select New --gt JFrame Form

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5158

Figure 92

- In the New JFrame Form dialog specify the Class Name and the Package name

eg Class Name multiplierUI Package mymultiplier

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5258

Figure 93

After clicking on the Finish button we should get something like the following

Figure 94

Last Updated ( Tuesday 18 September 2007 )

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5358

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5458

9th Step (Cont)

Written by Mohd Khalid Kasmin

Tuesday 11 September 2007

Step by Step into Java Programming

for University Physics Student

By

Mohd Khalid Kasmin

B Building user interface (UI) - putting in the components

Now that we have a proper blank form we can start filling the form with the suitable graphical components For introduction to

GUI programming an appropriate application to build (for a physics student) is a simple solver So our first exercise is to build an

application that takes two numbers and multiply them and display the result

With GUI we usually give input values to an application via input boxes that are labelled When all inputs are typed in properlywe click on a button to ask the application to execute the job Hence in this exercise well do just that The target now is to

construct a user interface that looks like Figure 95 below

Figure 95

Home Profile Teaching Links Contact Me My Work

Newsflash

Joomla 10 - Experience the Freedom It has never been easier to create your own dynamic site Manage all your content from

the best CMS admin interface

search

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5558

[ Back ]

1 Problemapplication description box

- In the palette pane click on Swing component named JTextPane (Note click just once) Move the mouse pointer on top of

your JFrame (Note just move not drag) When you see a little white box with orange border position it somewhere near the

top part of your JFrame and click once The JTextPane is then attached on the JFrame Addjust its size with the help of the

sizing hook around the perimeter of the JTextPane

- Edit the text property of the JTextPane with the description of your application

2 Input boxes

- In the palette pane click on Swing component named JTextField Next move the mouse pointer on top of your JFrame

When you see a white box with the word jTextField1 position it to the left and under the JTextPane1

- Click on Swing component JTextField in the palette pane again Move the mouse pointer on top of the JFrame you should

get a second JTextField named jTextField2 Position it to the right of jTextField1

Last Updated ( Tuesday 11 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5658

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5758

multiplication if a b and c were first declared as double or float or int variables So the code of the calculation should look like the

following

double a b c

a =

b =

c = a b

Clearly variables a and b need to be assigned with values of typed double Now looking back at inputboxes A and B we made

those boxes using the JTextField component meaning that any data or value written in a JTextField is of typed Text or String even

if the value displayed in the box is ldquo125rdquo But f irst here there is this task of getting to that Text (or String) values ldquo125rdquo and ldquo72rdquo

In Java we can get to a text in JTextField component using the getText() method So to retrieve values in jTextField1 and

jTextField2 we can use

jTextField1getText()

and

jTextField2getText()

But those values are still in text mode and we need another method to convert the data mode f rom text to double For that we can

use the method DoubleparseDouble() Thus the whole process of retrieve text -gt convert text to double -gt assign to double

variable can be done using

a = DoubleparseDouble( jTextField1getText() )

and

b = DoubleparseDouble( jTextField2getText() )

Hence the code for performing the multiplication becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

Up to this point we have the variable c as the result of the multiplication of a and b So the next thing to do is to display this

result on the JLabel that we have already prepared before

A x B = _____

The text A x B = ______ was made using JLabel component and its variable name is jLabel3 Since the variable c is of type

double and text property of JLabel is of type String we need to convert it first to String data type The method to convert a double

to String is DoubletoString() So we can write the code as DoubletoString(c) After getting the text we can now use it to be the

text displayed on jLabel3 To do that we use the method setText() Hence the appropriate code for displaying the result is

jLabel3setText( A x B = + DoubletoString(c) )

And the full code for our calculation becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

jLabel3setText( A x B = + DoubletoString(c) )

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5858

[ Back ]

Last Updated ( Tuesday 18 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

Page 26: Computional Physics

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2658

983107983119983117983120983125983124983105983124983113983119983118983105983116 983120983112983129983123983113983107983123

(983123983123983120983090983089983090983090983081

983106983157983145983148983140983145983150983143 983137 983152983144983161983155983145983139983155 983155983151983148983158983141983154

983157983155983145983150983143 983118983141983156983138983141983137983150983155

983138983161

983105983138983140983086 983115983144983137983149983145983149 983113983155983149983137983145983148

983117983151983144983140 983115983144983137983148983145983140 983115983137983155983149983145983150

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2758

How to create a user interface (UI) using Netbeans

Based on the ldquoGravityAttract07ajavardquo source code (shown below) build a user interface usingNetbeans The design of the UI could be look like this (Figure1)

Figure 1

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2858

ldquoGravityAttract07ajavardquo source code

Java class for calculating the resultant force on any object(ie either on m1m2 m3 m4 or m5)

import static javalangMathimport javaiopublic class GravityAttract07a

public static void main(String args[])

double[] m = new double[6] mass of objectdouble[] x = new double[6] x-coor of objectdouble[] y = new double[6] y-coor of objectdouble f resultant forcedouble fxfy x amp y component of forcedouble rx ry rp co G dirfangdirf

int j

G = 667e-11m[1]=25 x[1]=12 y[1]=25m[2]=15 x[2]=15 y[2]=15m[3]=75 x[3]=02 y[3]=55m[4]=92 x[4]=52 y[4]=25m[5]=66 x[5]=42 y[5]=45BufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin))String mass=Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))try mass=dataInreadLine()

catch(IOException e)Systemoutprintln(Not a valid input value)int mi = IntegerparseInt(mass)fx = 00fy = 00

for(j=1 jlt=5 j++)

if (j=mi)rx = x[j]-x[mi]ry = y[j]-y[mi]rp = pow((rxrx+ryry)15) (rx^2 + ry^2) raised to the power of 32co = Gm[mi]m[j]

fx = fx + corxrpfy = fy + coryrp

f = sqrt(fxfx + fyfy)dirf = toDegrees(atan(fyfx))Systemoutprintf(The force on m[+ mi +] due to the other masses is eN fdegnfdirf)

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2958

This example will not show you step by step details procedure since it already explained inthe class and in the e-learning Please refer to the e-learning about how to create a project how tocreate JFrame Form and as well how to create JButton JTextField JLabel JTextPane and so on

First create a new project and give a name eg ldquogravityrdquo and then create JFrame Form andthe following JButton JTextField JLabel JTextPane etc by using ldquoswing paletterdquo as shown in Figure2

Figure 2

To insert a picture into JLabel2 click on the JLabel2 go to properties and select ldquoiconrdquo Onthe icon editor choose file and then select file to locate the directory of your graphicpicture file(Figure 3) Select the file and click OK when finished Make sure you have already created your ownpicture file and save it as jpg png bmp etc format eg ldquogravityjpgrdquo Use any graphic package todraw your diagrampicture or use drawing tools available in Open Office or Microsoft Word

JTextPane1JLabel1

JLabel2

JLabel3JLabel4

JLabel5

JLabel6 JLabel7

JLabel8

JTextField2 JTextField3 JTextField4

JTextF

JButton1JButton2

JButton3

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3058

Figure 3

To write documentation or question eg in JTextPane1 click on the JTextPane1 go toproperties and select ldquotextrdquo Type the documentation in the text editor as shown in Figure 4 Click OKwhen finished

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3158

To do formatting on the text such as superscript and subscript eg in jLabel3 click on the

JLabel3 go to properties and select ldquotextrdquo Type the documentationmessage on the text editor asshown in Figure 5 as follows

eg

lthtmlgtOn which particle (mltsubgt1ltsubgtmltsubgt5ltsubgt) you want to calculate theresultant force

Use sup instead if you want a superscriptThe result will look like this

Click OK when finished

Figure 5

To do the calculation you need to put the coding similar like ldquoGravityAttract07ajavardquo and put itinto the ldquocalculaterdquo button (this is ldquoJButton1rdquo) You can copy ldquoGravityAttract07a javardquo source code

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3258

paste it and then do a few alterations First click on the ldquocalculaterdquo button right click choose Eventsand then choose Action then actionPerformed (Figure 6) It will bring you to where you should insertyour coding (Figure 7) You can write your own coding or copypaste any coding from somewhereelse (in this case from ldquoGravityAttract07ajavardquo)

Figure 6

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3358

Figure 7

Copy ldquoGravityAttract07ajavardquo source code and paste to the place after ldquo TODO add youhandling code hererdquo see Figure 7 To see which part has been modified the coding in Figure 7 hasbeen color coded for better understanding as follows

private void jButton1ActionPerformed(javaawteventActionEvent evt) TODO add your handling code here

double[] m = new double[6] mass of objectdouble[] x = new double[6] x-coor of objectdouble[] y = new double[6] y-coor of objectdouble f resultant forcedouble fxfy x amp y component of forcedouble rx ry rp co G dirfangdirfDecimalFormat dff dfangdirfint j mi

G = 667e-11m[1]=25 x[1]=12 y[1]=25m[2]=15 x[2]=15 y[2]=15m[3]=75 x[3]=02 y[3]=55m[4]=92 x[4]=52 y[4]=25m[5]=66 x[5]=42 y[5]=45

BufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin)) String mass= Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3458

catch(IOException e)Systemoutprintln(Not a valid input value) int mi = IntegerparseInt(mass)mi = IntegerparseInt(jTextField1getText())fx = 00fy = 00for(j=1 jlt=5 j++)

if (j=mi)rx = x[j]-x[mi]ry = y[j]-y[mi]rp = Mathpow((rxrx+ryry)15) (rx^2 + ry^2) raised to the power of 32co = Gm[mi]m[j]fx = fx + corxrpfy = fy + coryrp

f = Mathsqrt(fxfx + fyfy)

dirf = Mathatan(fyfx)angdirf = (dirf(22070))1800 Systemoutprintf(ldquoThe resultant force on particle [ldquo+ mi +rdquo] due to the other masses is

14e N 22f degnrdquofangdirf)dff = new javatextDecimalFormat(ldquoE00rdquo)dfangdirf = new DecimalFormat(ldquo000rdquo)

jTextField2setText(ldquordquo + mi) jTextField3setText(ldquordquo + dffformat(f)) jTextField4setText(ldquordquo + dfangdirfformat(angdirf))

Note

Brown = automatically generatedBlue = the original code from ldquoGravityAttract07ajavardquoGreen = coding from ldquoGravityAttract07ajavardquo which were being disabled due to

incompatibilities with NetbeansRed = a new inserted coding a few of them is to rephrasesubstitute the green one

Javascripts command line useBufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin))String mass=Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))try mass=dataInreadLine()catch(IOException e)Systemoutprintln(Not a valid input value)

int mi = IntegerparseInt(mass)

to read any input from the keyboard typed by the user while Netbeans uses egldquoIntegerparseInt(jTextField1getText())rdquo etc Javascripts command line uses ldquoSystemoutprintfrdquo to format the printed output whileNetbeans uses more complicated way to do the same job (see below)

Javascript command line

Systemoutprintf(The resultant force on particle [+ mi +] due to the other masses is 14e N22f degnfangdirf)

Netbeansdff = new javatextDecimalFormat(ldquo0E00rdquo)df di f D i lF t(ldquo0 00rdquo)

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3558

jTextField2setText(ldquordquo + mi) jTextField3setText(ldquordquo + dffformat(f)) jTextField4setText(ldquordquo + dfangdirfformat(angdirf))

ldquodffrdquo and ldquodfangdirfrdquo are new variables that should be declared and added to format thenumbers into decimal format ie ldquo0E00rdquo to get a scientific format eg ldquo20019E-10rdquo and ldquo000rdquoto get a decimal number eg ldquo-1570rdquo

To use DecimalFormat you have to put ldquoimport javatextDecimalFormatrdquo in the top of thecoding as shown in Figure 8

Figure 8

To reset the calculation you need to put coding into the ldquoresetrdquo button (this is ldquoJButton2rdquo)First click on the ldquoresetrdquo button right click choose Events and then choose Action thenactionPerformed (similar like in Figure 6) It will bring you to where you should insert your coding(Figure 9) Write the following coding (Figure 9)

Figure 9

To exit the program you need to put coding into the ldquoexitrdquo button (this is ldquoJButton3rdquo) Firstclick on the ldquoexitrdquo button right click choose Events and then choose Action then actionPerformed(similar like in Figure 6) It will bring you to where you should insert your coding (Figure 10) Writethe following coding (Figure 10)

Figure 10

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3658

Once you finished now you can run and see the result If everything is fine then the output wilbe look like this (Figure 11)

Figure 11

Remember when you compile and run ldquoGravityAttract07ajavardquo using Javascripts commandline once you compile and run the result was looked like this

On which particle you want to calculate the resulting force 2The resultant force on particle [2] due to the other masses is 30673e-10 N 8837 deg

SEE THE DIFFERENCES AT THE USER INTERFACE

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3758

8th Step

Written by Mohd Khalid Kasmin

Monday 27 August 2007

Programming using command line interface (CLI) has its advantages but todays computer users are so used to graphical user

interface (GUI) that we just can no longer be contented with writing application that do not use the GUI Hence starting from this 8thstep we are going to embark on learning how to make java application that utilises the the GUI

It is possible to write a GUI application with the text based tools that we have been using for these past 7 steps but the job is so

messy that most of you will definitely be turnoff by it So to make life slightly easier well use an integrated development

environment (IDE) known as NetBeans specifically NetBeans 551

0 First ssh to the our server Next once logged in just type nice -n15 netbeans amp on the terminal If you had sshed with the -Y

switch after a few tens of seconds NetBeans should emerge on your display screen

One important point to note Since our server can readily handle up to 100 users (maybe even

more) doing work in CLI mode this machine will certainly be taxed to its maximum capability once

we start using graphical based application NetBeans 551 uses a geat deal of computer memory

and with the 4Gbytes RAM server it can only accomodate up to 25 users concurrently using

NetBeans if the users use the application wisely What is meant by wisely here is that (a) when

starting the NetBeans application please use it together with the nice -n15 command (b) in using

the NetBeans please make sure you only have one project open at a time and (c) please close your

project everytime before quitting the NetBeans

Home Profile Teaching Links Contact Me My Work

Newsflash

Yesterday all servers in the US went out on strike in a bid to get more RAM and better CPUs A spokes person said that the

need for better RAM was due to some fool increasing the front-side bus speed In future busses will be told to slow down in

residential motherboards

search

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3858

1

2

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3958

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4058

[ Back ]

5

Last Updated ( Wednesday 31 October 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4158

8th Step (Cont)

Written by Mohd Khalid Kasmin

Monday 27 August 2007

Up to this point we have define the name of our project Next we construct a class and to follow what we already know we name the class the sam

project name latihan01

6

7

Home Profile Teaching Links Contact Me My Work

Newsflash

Joomla 10 - Experience the Freedom It has never been easier to create your own dynamic site Manage all your content from

the best CMS admin interface

search

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4258

8

9

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4358

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4458

8th Step (Cont still)

Written by Mohd Khalid Kasmin

Monday 27 August 2007

In item (9) we see that Netbeans does not provide us with the main method So to follow what we already know we now put in the

main method

10

Home Profile Teaching Links Contact Me My Work

Newsflash

Aoccdrnig to a rscheearch at an Elingsh uinervtisy it deosnt mttaer in waht oredr the ltteers in a wrod are the olny iprmoetnt

tihng is taht frist and lsat ltteer is at the rghit pclae The rset can be a toatl mses and you can sitll raed it wouthit porbelm Tihs is

bcuseae we do not raed ervey lteter by itslef but the wrod as a wlohe

search

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4558

11

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4658

12

13

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4758

14

15

Last Updated ( Monday 27 August 2007 )

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4858

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4958

9th Step

Thursday 06 September 2007

Step by Step into Java Programming

for

University Physics Student

By

Mohd Khalid Kasmin

A Building graphical user interface - preparing the JFrame Form

At this stage of our programming experience with Java and NetBeans please follow strictly the steps outline in this article First and

make a new GUI project make sure there is no other project that already been loaded by Netbeans Please close any project displayed

1 Create new project

- On the main menu click on File --gt New Project (note Close first any previous project)

- Specify project name amp project location - Untick Create Main Class item

- Click on the Finish button

Home Profile Teaching Links Contact Me My Work

Newsflash

Aoccdrnig to a rscheearch at an Elingsh uinervtisy it deosnt mttaer in waht oredr the ltteers in a wrod are the olny iprmoetnt

tihng is taht frist and lsat ltteer is at the rghit pclae The rset can be a toatl mses and you can sitll raed it wouthit porbelm Tihs is

bcuseae we do not raed ervey lteter by itslef but the wrod as a wlohe

search

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5058

Figure 91

2 Create UI class

- Right click on the project name in the Project pane

on the popup menu select New --gt JFrame Form

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5158

Figure 92

- In the New JFrame Form dialog specify the Class Name and the Package name

eg Class Name multiplierUI Package mymultiplier

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5258

Figure 93

After clicking on the Finish button we should get something like the following

Figure 94

Last Updated ( Tuesday 18 September 2007 )

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5358

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5458

9th Step (Cont)

Written by Mohd Khalid Kasmin

Tuesday 11 September 2007

Step by Step into Java Programming

for University Physics Student

By

Mohd Khalid Kasmin

B Building user interface (UI) - putting in the components

Now that we have a proper blank form we can start filling the form with the suitable graphical components For introduction to

GUI programming an appropriate application to build (for a physics student) is a simple solver So our first exercise is to build an

application that takes two numbers and multiply them and display the result

With GUI we usually give input values to an application via input boxes that are labelled When all inputs are typed in properlywe click on a button to ask the application to execute the job Hence in this exercise well do just that The target now is to

construct a user interface that looks like Figure 95 below

Figure 95

Home Profile Teaching Links Contact Me My Work

Newsflash

Joomla 10 - Experience the Freedom It has never been easier to create your own dynamic site Manage all your content from

the best CMS admin interface

search

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5558

[ Back ]

1 Problemapplication description box

- In the palette pane click on Swing component named JTextPane (Note click just once) Move the mouse pointer on top of

your JFrame (Note just move not drag) When you see a little white box with orange border position it somewhere near the

top part of your JFrame and click once The JTextPane is then attached on the JFrame Addjust its size with the help of the

sizing hook around the perimeter of the JTextPane

- Edit the text property of the JTextPane with the description of your application

2 Input boxes

- In the palette pane click on Swing component named JTextField Next move the mouse pointer on top of your JFrame

When you see a white box with the word jTextField1 position it to the left and under the JTextPane1

- Click on Swing component JTextField in the palette pane again Move the mouse pointer on top of the JFrame you should

get a second JTextField named jTextField2 Position it to the right of jTextField1

Last Updated ( Tuesday 11 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5658

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5758

multiplication if a b and c were first declared as double or float or int variables So the code of the calculation should look like the

following

double a b c

a =

b =

c = a b

Clearly variables a and b need to be assigned with values of typed double Now looking back at inputboxes A and B we made

those boxes using the JTextField component meaning that any data or value written in a JTextField is of typed Text or String even

if the value displayed in the box is ldquo125rdquo But f irst here there is this task of getting to that Text (or String) values ldquo125rdquo and ldquo72rdquo

In Java we can get to a text in JTextField component using the getText() method So to retrieve values in jTextField1 and

jTextField2 we can use

jTextField1getText()

and

jTextField2getText()

But those values are still in text mode and we need another method to convert the data mode f rom text to double For that we can

use the method DoubleparseDouble() Thus the whole process of retrieve text -gt convert text to double -gt assign to double

variable can be done using

a = DoubleparseDouble( jTextField1getText() )

and

b = DoubleparseDouble( jTextField2getText() )

Hence the code for performing the multiplication becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

Up to this point we have the variable c as the result of the multiplication of a and b So the next thing to do is to display this

result on the JLabel that we have already prepared before

A x B = _____

The text A x B = ______ was made using JLabel component and its variable name is jLabel3 Since the variable c is of type

double and text property of JLabel is of type String we need to convert it first to String data type The method to convert a double

to String is DoubletoString() So we can write the code as DoubletoString(c) After getting the text we can now use it to be the

text displayed on jLabel3 To do that we use the method setText() Hence the appropriate code for displaying the result is

jLabel3setText( A x B = + DoubletoString(c) )

And the full code for our calculation becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

jLabel3setText( A x B = + DoubletoString(c) )

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5858

[ Back ]

Last Updated ( Tuesday 18 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

Page 27: Computional Physics

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2758

How to create a user interface (UI) using Netbeans

Based on the ldquoGravityAttract07ajavardquo source code (shown below) build a user interface usingNetbeans The design of the UI could be look like this (Figure1)

Figure 1

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2858

ldquoGravityAttract07ajavardquo source code

Java class for calculating the resultant force on any object(ie either on m1m2 m3 m4 or m5)

import static javalangMathimport javaiopublic class GravityAttract07a

public static void main(String args[])

double[] m = new double[6] mass of objectdouble[] x = new double[6] x-coor of objectdouble[] y = new double[6] y-coor of objectdouble f resultant forcedouble fxfy x amp y component of forcedouble rx ry rp co G dirfangdirf

int j

G = 667e-11m[1]=25 x[1]=12 y[1]=25m[2]=15 x[2]=15 y[2]=15m[3]=75 x[3]=02 y[3]=55m[4]=92 x[4]=52 y[4]=25m[5]=66 x[5]=42 y[5]=45BufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin))String mass=Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))try mass=dataInreadLine()

catch(IOException e)Systemoutprintln(Not a valid input value)int mi = IntegerparseInt(mass)fx = 00fy = 00

for(j=1 jlt=5 j++)

if (j=mi)rx = x[j]-x[mi]ry = y[j]-y[mi]rp = pow((rxrx+ryry)15) (rx^2 + ry^2) raised to the power of 32co = Gm[mi]m[j]

fx = fx + corxrpfy = fy + coryrp

f = sqrt(fxfx + fyfy)dirf = toDegrees(atan(fyfx))Systemoutprintf(The force on m[+ mi +] due to the other masses is eN fdegnfdirf)

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2958

This example will not show you step by step details procedure since it already explained inthe class and in the e-learning Please refer to the e-learning about how to create a project how tocreate JFrame Form and as well how to create JButton JTextField JLabel JTextPane and so on

First create a new project and give a name eg ldquogravityrdquo and then create JFrame Form andthe following JButton JTextField JLabel JTextPane etc by using ldquoswing paletterdquo as shown in Figure2

Figure 2

To insert a picture into JLabel2 click on the JLabel2 go to properties and select ldquoiconrdquo Onthe icon editor choose file and then select file to locate the directory of your graphicpicture file(Figure 3) Select the file and click OK when finished Make sure you have already created your ownpicture file and save it as jpg png bmp etc format eg ldquogravityjpgrdquo Use any graphic package todraw your diagrampicture or use drawing tools available in Open Office or Microsoft Word

JTextPane1JLabel1

JLabel2

JLabel3JLabel4

JLabel5

JLabel6 JLabel7

JLabel8

JTextField2 JTextField3 JTextField4

JTextF

JButton1JButton2

JButton3

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3058

Figure 3

To write documentation or question eg in JTextPane1 click on the JTextPane1 go toproperties and select ldquotextrdquo Type the documentation in the text editor as shown in Figure 4 Click OKwhen finished

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3158

To do formatting on the text such as superscript and subscript eg in jLabel3 click on the

JLabel3 go to properties and select ldquotextrdquo Type the documentationmessage on the text editor asshown in Figure 5 as follows

eg

lthtmlgtOn which particle (mltsubgt1ltsubgtmltsubgt5ltsubgt) you want to calculate theresultant force

Use sup instead if you want a superscriptThe result will look like this

Click OK when finished

Figure 5

To do the calculation you need to put the coding similar like ldquoGravityAttract07ajavardquo and put itinto the ldquocalculaterdquo button (this is ldquoJButton1rdquo) You can copy ldquoGravityAttract07a javardquo source code

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3258

paste it and then do a few alterations First click on the ldquocalculaterdquo button right click choose Eventsand then choose Action then actionPerformed (Figure 6) It will bring you to where you should insertyour coding (Figure 7) You can write your own coding or copypaste any coding from somewhereelse (in this case from ldquoGravityAttract07ajavardquo)

Figure 6

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3358

Figure 7

Copy ldquoGravityAttract07ajavardquo source code and paste to the place after ldquo TODO add youhandling code hererdquo see Figure 7 To see which part has been modified the coding in Figure 7 hasbeen color coded for better understanding as follows

private void jButton1ActionPerformed(javaawteventActionEvent evt) TODO add your handling code here

double[] m = new double[6] mass of objectdouble[] x = new double[6] x-coor of objectdouble[] y = new double[6] y-coor of objectdouble f resultant forcedouble fxfy x amp y component of forcedouble rx ry rp co G dirfangdirfDecimalFormat dff dfangdirfint j mi

G = 667e-11m[1]=25 x[1]=12 y[1]=25m[2]=15 x[2]=15 y[2]=15m[3]=75 x[3]=02 y[3]=55m[4]=92 x[4]=52 y[4]=25m[5]=66 x[5]=42 y[5]=45

BufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin)) String mass= Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3458

catch(IOException e)Systemoutprintln(Not a valid input value) int mi = IntegerparseInt(mass)mi = IntegerparseInt(jTextField1getText())fx = 00fy = 00for(j=1 jlt=5 j++)

if (j=mi)rx = x[j]-x[mi]ry = y[j]-y[mi]rp = Mathpow((rxrx+ryry)15) (rx^2 + ry^2) raised to the power of 32co = Gm[mi]m[j]fx = fx + corxrpfy = fy + coryrp

f = Mathsqrt(fxfx + fyfy)

dirf = Mathatan(fyfx)angdirf = (dirf(22070))1800 Systemoutprintf(ldquoThe resultant force on particle [ldquo+ mi +rdquo] due to the other masses is

14e N 22f degnrdquofangdirf)dff = new javatextDecimalFormat(ldquoE00rdquo)dfangdirf = new DecimalFormat(ldquo000rdquo)

jTextField2setText(ldquordquo + mi) jTextField3setText(ldquordquo + dffformat(f)) jTextField4setText(ldquordquo + dfangdirfformat(angdirf))

Note

Brown = automatically generatedBlue = the original code from ldquoGravityAttract07ajavardquoGreen = coding from ldquoGravityAttract07ajavardquo which were being disabled due to

incompatibilities with NetbeansRed = a new inserted coding a few of them is to rephrasesubstitute the green one

Javascripts command line useBufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin))String mass=Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))try mass=dataInreadLine()catch(IOException e)Systemoutprintln(Not a valid input value)

int mi = IntegerparseInt(mass)

to read any input from the keyboard typed by the user while Netbeans uses egldquoIntegerparseInt(jTextField1getText())rdquo etc Javascripts command line uses ldquoSystemoutprintfrdquo to format the printed output whileNetbeans uses more complicated way to do the same job (see below)

Javascript command line

Systemoutprintf(The resultant force on particle [+ mi +] due to the other masses is 14e N22f degnfangdirf)

Netbeansdff = new javatextDecimalFormat(ldquo0E00rdquo)df di f D i lF t(ldquo0 00rdquo)

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3558

jTextField2setText(ldquordquo + mi) jTextField3setText(ldquordquo + dffformat(f)) jTextField4setText(ldquordquo + dfangdirfformat(angdirf))

ldquodffrdquo and ldquodfangdirfrdquo are new variables that should be declared and added to format thenumbers into decimal format ie ldquo0E00rdquo to get a scientific format eg ldquo20019E-10rdquo and ldquo000rdquoto get a decimal number eg ldquo-1570rdquo

To use DecimalFormat you have to put ldquoimport javatextDecimalFormatrdquo in the top of thecoding as shown in Figure 8

Figure 8

To reset the calculation you need to put coding into the ldquoresetrdquo button (this is ldquoJButton2rdquo)First click on the ldquoresetrdquo button right click choose Events and then choose Action thenactionPerformed (similar like in Figure 6) It will bring you to where you should insert your coding(Figure 9) Write the following coding (Figure 9)

Figure 9

To exit the program you need to put coding into the ldquoexitrdquo button (this is ldquoJButton3rdquo) Firstclick on the ldquoexitrdquo button right click choose Events and then choose Action then actionPerformed(similar like in Figure 6) It will bring you to where you should insert your coding (Figure 10) Writethe following coding (Figure 10)

Figure 10

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3658

Once you finished now you can run and see the result If everything is fine then the output wilbe look like this (Figure 11)

Figure 11

Remember when you compile and run ldquoGravityAttract07ajavardquo using Javascripts commandline once you compile and run the result was looked like this

On which particle you want to calculate the resulting force 2The resultant force on particle [2] due to the other masses is 30673e-10 N 8837 deg

SEE THE DIFFERENCES AT THE USER INTERFACE

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3758

8th Step

Written by Mohd Khalid Kasmin

Monday 27 August 2007

Programming using command line interface (CLI) has its advantages but todays computer users are so used to graphical user

interface (GUI) that we just can no longer be contented with writing application that do not use the GUI Hence starting from this 8thstep we are going to embark on learning how to make java application that utilises the the GUI

It is possible to write a GUI application with the text based tools that we have been using for these past 7 steps but the job is so

messy that most of you will definitely be turnoff by it So to make life slightly easier well use an integrated development

environment (IDE) known as NetBeans specifically NetBeans 551

0 First ssh to the our server Next once logged in just type nice -n15 netbeans amp on the terminal If you had sshed with the -Y

switch after a few tens of seconds NetBeans should emerge on your display screen

One important point to note Since our server can readily handle up to 100 users (maybe even

more) doing work in CLI mode this machine will certainly be taxed to its maximum capability once

we start using graphical based application NetBeans 551 uses a geat deal of computer memory

and with the 4Gbytes RAM server it can only accomodate up to 25 users concurrently using

NetBeans if the users use the application wisely What is meant by wisely here is that (a) when

starting the NetBeans application please use it together with the nice -n15 command (b) in using

the NetBeans please make sure you only have one project open at a time and (c) please close your

project everytime before quitting the NetBeans

Home Profile Teaching Links Contact Me My Work

Newsflash

Yesterday all servers in the US went out on strike in a bid to get more RAM and better CPUs A spokes person said that the

need for better RAM was due to some fool increasing the front-side bus speed In future busses will be told to slow down in

residential motherboards

search

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3858

1

2

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3958

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4058

[ Back ]

5

Last Updated ( Wednesday 31 October 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4158

8th Step (Cont)

Written by Mohd Khalid Kasmin

Monday 27 August 2007

Up to this point we have define the name of our project Next we construct a class and to follow what we already know we name the class the sam

project name latihan01

6

7

Home Profile Teaching Links Contact Me My Work

Newsflash

Joomla 10 - Experience the Freedom It has never been easier to create your own dynamic site Manage all your content from

the best CMS admin interface

search

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4258

8

9

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4358

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4458

8th Step (Cont still)

Written by Mohd Khalid Kasmin

Monday 27 August 2007

In item (9) we see that Netbeans does not provide us with the main method So to follow what we already know we now put in the

main method

10

Home Profile Teaching Links Contact Me My Work

Newsflash

Aoccdrnig to a rscheearch at an Elingsh uinervtisy it deosnt mttaer in waht oredr the ltteers in a wrod are the olny iprmoetnt

tihng is taht frist and lsat ltteer is at the rghit pclae The rset can be a toatl mses and you can sitll raed it wouthit porbelm Tihs is

bcuseae we do not raed ervey lteter by itslef but the wrod as a wlohe

search

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4558

11

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4658

12

13

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4758

14

15

Last Updated ( Monday 27 August 2007 )

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4858

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4958

9th Step

Thursday 06 September 2007

Step by Step into Java Programming

for

University Physics Student

By

Mohd Khalid Kasmin

A Building graphical user interface - preparing the JFrame Form

At this stage of our programming experience with Java and NetBeans please follow strictly the steps outline in this article First and

make a new GUI project make sure there is no other project that already been loaded by Netbeans Please close any project displayed

1 Create new project

- On the main menu click on File --gt New Project (note Close first any previous project)

- Specify project name amp project location - Untick Create Main Class item

- Click on the Finish button

Home Profile Teaching Links Contact Me My Work

Newsflash

Aoccdrnig to a rscheearch at an Elingsh uinervtisy it deosnt mttaer in waht oredr the ltteers in a wrod are the olny iprmoetnt

tihng is taht frist and lsat ltteer is at the rghit pclae The rset can be a toatl mses and you can sitll raed it wouthit porbelm Tihs is

bcuseae we do not raed ervey lteter by itslef but the wrod as a wlohe

search

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5058

Figure 91

2 Create UI class

- Right click on the project name in the Project pane

on the popup menu select New --gt JFrame Form

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5158

Figure 92

- In the New JFrame Form dialog specify the Class Name and the Package name

eg Class Name multiplierUI Package mymultiplier

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5258

Figure 93

After clicking on the Finish button we should get something like the following

Figure 94

Last Updated ( Tuesday 18 September 2007 )

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5358

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5458

9th Step (Cont)

Written by Mohd Khalid Kasmin

Tuesday 11 September 2007

Step by Step into Java Programming

for University Physics Student

By

Mohd Khalid Kasmin

B Building user interface (UI) - putting in the components

Now that we have a proper blank form we can start filling the form with the suitable graphical components For introduction to

GUI programming an appropriate application to build (for a physics student) is a simple solver So our first exercise is to build an

application that takes two numbers and multiply them and display the result

With GUI we usually give input values to an application via input boxes that are labelled When all inputs are typed in properlywe click on a button to ask the application to execute the job Hence in this exercise well do just that The target now is to

construct a user interface that looks like Figure 95 below

Figure 95

Home Profile Teaching Links Contact Me My Work

Newsflash

Joomla 10 - Experience the Freedom It has never been easier to create your own dynamic site Manage all your content from

the best CMS admin interface

search

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5558

[ Back ]

1 Problemapplication description box

- In the palette pane click on Swing component named JTextPane (Note click just once) Move the mouse pointer on top of

your JFrame (Note just move not drag) When you see a little white box with orange border position it somewhere near the

top part of your JFrame and click once The JTextPane is then attached on the JFrame Addjust its size with the help of the

sizing hook around the perimeter of the JTextPane

- Edit the text property of the JTextPane with the description of your application

2 Input boxes

- In the palette pane click on Swing component named JTextField Next move the mouse pointer on top of your JFrame

When you see a white box with the word jTextField1 position it to the left and under the JTextPane1

- Click on Swing component JTextField in the palette pane again Move the mouse pointer on top of the JFrame you should

get a second JTextField named jTextField2 Position it to the right of jTextField1

Last Updated ( Tuesday 11 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5658

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5758

multiplication if a b and c were first declared as double or float or int variables So the code of the calculation should look like the

following

double a b c

a =

b =

c = a b

Clearly variables a and b need to be assigned with values of typed double Now looking back at inputboxes A and B we made

those boxes using the JTextField component meaning that any data or value written in a JTextField is of typed Text or String even

if the value displayed in the box is ldquo125rdquo But f irst here there is this task of getting to that Text (or String) values ldquo125rdquo and ldquo72rdquo

In Java we can get to a text in JTextField component using the getText() method So to retrieve values in jTextField1 and

jTextField2 we can use

jTextField1getText()

and

jTextField2getText()

But those values are still in text mode and we need another method to convert the data mode f rom text to double For that we can

use the method DoubleparseDouble() Thus the whole process of retrieve text -gt convert text to double -gt assign to double

variable can be done using

a = DoubleparseDouble( jTextField1getText() )

and

b = DoubleparseDouble( jTextField2getText() )

Hence the code for performing the multiplication becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

Up to this point we have the variable c as the result of the multiplication of a and b So the next thing to do is to display this

result on the JLabel that we have already prepared before

A x B = _____

The text A x B = ______ was made using JLabel component and its variable name is jLabel3 Since the variable c is of type

double and text property of JLabel is of type String we need to convert it first to String data type The method to convert a double

to String is DoubletoString() So we can write the code as DoubletoString(c) After getting the text we can now use it to be the

text displayed on jLabel3 To do that we use the method setText() Hence the appropriate code for displaying the result is

jLabel3setText( A x B = + DoubletoString(c) )

And the full code for our calculation becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

jLabel3setText( A x B = + DoubletoString(c) )

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5858

[ Back ]

Last Updated ( Tuesday 18 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

Page 28: Computional Physics

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2858

ldquoGravityAttract07ajavardquo source code

Java class for calculating the resultant force on any object(ie either on m1m2 m3 m4 or m5)

import static javalangMathimport javaiopublic class GravityAttract07a

public static void main(String args[])

double[] m = new double[6] mass of objectdouble[] x = new double[6] x-coor of objectdouble[] y = new double[6] y-coor of objectdouble f resultant forcedouble fxfy x amp y component of forcedouble rx ry rp co G dirfangdirf

int j

G = 667e-11m[1]=25 x[1]=12 y[1]=25m[2]=15 x[2]=15 y[2]=15m[3]=75 x[3]=02 y[3]=55m[4]=92 x[4]=52 y[4]=25m[5]=66 x[5]=42 y[5]=45BufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin))String mass=Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))try mass=dataInreadLine()

catch(IOException e)Systemoutprintln(Not a valid input value)int mi = IntegerparseInt(mass)fx = 00fy = 00

for(j=1 jlt=5 j++)

if (j=mi)rx = x[j]-x[mi]ry = y[j]-y[mi]rp = pow((rxrx+ryry)15) (rx^2 + ry^2) raised to the power of 32co = Gm[mi]m[j]

fx = fx + corxrpfy = fy + coryrp

f = sqrt(fxfx + fyfy)dirf = toDegrees(atan(fyfx))Systemoutprintf(The force on m[+ mi +] due to the other masses is eN fdegnfdirf)

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2958

This example will not show you step by step details procedure since it already explained inthe class and in the e-learning Please refer to the e-learning about how to create a project how tocreate JFrame Form and as well how to create JButton JTextField JLabel JTextPane and so on

First create a new project and give a name eg ldquogravityrdquo and then create JFrame Form andthe following JButton JTextField JLabel JTextPane etc by using ldquoswing paletterdquo as shown in Figure2

Figure 2

To insert a picture into JLabel2 click on the JLabel2 go to properties and select ldquoiconrdquo Onthe icon editor choose file and then select file to locate the directory of your graphicpicture file(Figure 3) Select the file and click OK when finished Make sure you have already created your ownpicture file and save it as jpg png bmp etc format eg ldquogravityjpgrdquo Use any graphic package todraw your diagrampicture or use drawing tools available in Open Office or Microsoft Word

JTextPane1JLabel1

JLabel2

JLabel3JLabel4

JLabel5

JLabel6 JLabel7

JLabel8

JTextField2 JTextField3 JTextField4

JTextF

JButton1JButton2

JButton3

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3058

Figure 3

To write documentation or question eg in JTextPane1 click on the JTextPane1 go toproperties and select ldquotextrdquo Type the documentation in the text editor as shown in Figure 4 Click OKwhen finished

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3158

To do formatting on the text such as superscript and subscript eg in jLabel3 click on the

JLabel3 go to properties and select ldquotextrdquo Type the documentationmessage on the text editor asshown in Figure 5 as follows

eg

lthtmlgtOn which particle (mltsubgt1ltsubgtmltsubgt5ltsubgt) you want to calculate theresultant force

Use sup instead if you want a superscriptThe result will look like this

Click OK when finished

Figure 5

To do the calculation you need to put the coding similar like ldquoGravityAttract07ajavardquo and put itinto the ldquocalculaterdquo button (this is ldquoJButton1rdquo) You can copy ldquoGravityAttract07a javardquo source code

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3258

paste it and then do a few alterations First click on the ldquocalculaterdquo button right click choose Eventsand then choose Action then actionPerformed (Figure 6) It will bring you to where you should insertyour coding (Figure 7) You can write your own coding or copypaste any coding from somewhereelse (in this case from ldquoGravityAttract07ajavardquo)

Figure 6

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3358

Figure 7

Copy ldquoGravityAttract07ajavardquo source code and paste to the place after ldquo TODO add youhandling code hererdquo see Figure 7 To see which part has been modified the coding in Figure 7 hasbeen color coded for better understanding as follows

private void jButton1ActionPerformed(javaawteventActionEvent evt) TODO add your handling code here

double[] m = new double[6] mass of objectdouble[] x = new double[6] x-coor of objectdouble[] y = new double[6] y-coor of objectdouble f resultant forcedouble fxfy x amp y component of forcedouble rx ry rp co G dirfangdirfDecimalFormat dff dfangdirfint j mi

G = 667e-11m[1]=25 x[1]=12 y[1]=25m[2]=15 x[2]=15 y[2]=15m[3]=75 x[3]=02 y[3]=55m[4]=92 x[4]=52 y[4]=25m[5]=66 x[5]=42 y[5]=45

BufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin)) String mass= Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3458

catch(IOException e)Systemoutprintln(Not a valid input value) int mi = IntegerparseInt(mass)mi = IntegerparseInt(jTextField1getText())fx = 00fy = 00for(j=1 jlt=5 j++)

if (j=mi)rx = x[j]-x[mi]ry = y[j]-y[mi]rp = Mathpow((rxrx+ryry)15) (rx^2 + ry^2) raised to the power of 32co = Gm[mi]m[j]fx = fx + corxrpfy = fy + coryrp

f = Mathsqrt(fxfx + fyfy)

dirf = Mathatan(fyfx)angdirf = (dirf(22070))1800 Systemoutprintf(ldquoThe resultant force on particle [ldquo+ mi +rdquo] due to the other masses is

14e N 22f degnrdquofangdirf)dff = new javatextDecimalFormat(ldquoE00rdquo)dfangdirf = new DecimalFormat(ldquo000rdquo)

jTextField2setText(ldquordquo + mi) jTextField3setText(ldquordquo + dffformat(f)) jTextField4setText(ldquordquo + dfangdirfformat(angdirf))

Note

Brown = automatically generatedBlue = the original code from ldquoGravityAttract07ajavardquoGreen = coding from ldquoGravityAttract07ajavardquo which were being disabled due to

incompatibilities with NetbeansRed = a new inserted coding a few of them is to rephrasesubstitute the green one

Javascripts command line useBufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin))String mass=Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))try mass=dataInreadLine()catch(IOException e)Systemoutprintln(Not a valid input value)

int mi = IntegerparseInt(mass)

to read any input from the keyboard typed by the user while Netbeans uses egldquoIntegerparseInt(jTextField1getText())rdquo etc Javascripts command line uses ldquoSystemoutprintfrdquo to format the printed output whileNetbeans uses more complicated way to do the same job (see below)

Javascript command line

Systemoutprintf(The resultant force on particle [+ mi +] due to the other masses is 14e N22f degnfangdirf)

Netbeansdff = new javatextDecimalFormat(ldquo0E00rdquo)df di f D i lF t(ldquo0 00rdquo)

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3558

jTextField2setText(ldquordquo + mi) jTextField3setText(ldquordquo + dffformat(f)) jTextField4setText(ldquordquo + dfangdirfformat(angdirf))

ldquodffrdquo and ldquodfangdirfrdquo are new variables that should be declared and added to format thenumbers into decimal format ie ldquo0E00rdquo to get a scientific format eg ldquo20019E-10rdquo and ldquo000rdquoto get a decimal number eg ldquo-1570rdquo

To use DecimalFormat you have to put ldquoimport javatextDecimalFormatrdquo in the top of thecoding as shown in Figure 8

Figure 8

To reset the calculation you need to put coding into the ldquoresetrdquo button (this is ldquoJButton2rdquo)First click on the ldquoresetrdquo button right click choose Events and then choose Action thenactionPerformed (similar like in Figure 6) It will bring you to where you should insert your coding(Figure 9) Write the following coding (Figure 9)

Figure 9

To exit the program you need to put coding into the ldquoexitrdquo button (this is ldquoJButton3rdquo) Firstclick on the ldquoexitrdquo button right click choose Events and then choose Action then actionPerformed(similar like in Figure 6) It will bring you to where you should insert your coding (Figure 10) Writethe following coding (Figure 10)

Figure 10

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3658

Once you finished now you can run and see the result If everything is fine then the output wilbe look like this (Figure 11)

Figure 11

Remember when you compile and run ldquoGravityAttract07ajavardquo using Javascripts commandline once you compile and run the result was looked like this

On which particle you want to calculate the resulting force 2The resultant force on particle [2] due to the other masses is 30673e-10 N 8837 deg

SEE THE DIFFERENCES AT THE USER INTERFACE

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3758

8th Step

Written by Mohd Khalid Kasmin

Monday 27 August 2007

Programming using command line interface (CLI) has its advantages but todays computer users are so used to graphical user

interface (GUI) that we just can no longer be contented with writing application that do not use the GUI Hence starting from this 8thstep we are going to embark on learning how to make java application that utilises the the GUI

It is possible to write a GUI application with the text based tools that we have been using for these past 7 steps but the job is so

messy that most of you will definitely be turnoff by it So to make life slightly easier well use an integrated development

environment (IDE) known as NetBeans specifically NetBeans 551

0 First ssh to the our server Next once logged in just type nice -n15 netbeans amp on the terminal If you had sshed with the -Y

switch after a few tens of seconds NetBeans should emerge on your display screen

One important point to note Since our server can readily handle up to 100 users (maybe even

more) doing work in CLI mode this machine will certainly be taxed to its maximum capability once

we start using graphical based application NetBeans 551 uses a geat deal of computer memory

and with the 4Gbytes RAM server it can only accomodate up to 25 users concurrently using

NetBeans if the users use the application wisely What is meant by wisely here is that (a) when

starting the NetBeans application please use it together with the nice -n15 command (b) in using

the NetBeans please make sure you only have one project open at a time and (c) please close your

project everytime before quitting the NetBeans

Home Profile Teaching Links Contact Me My Work

Newsflash

Yesterday all servers in the US went out on strike in a bid to get more RAM and better CPUs A spokes person said that the

need for better RAM was due to some fool increasing the front-side bus speed In future busses will be told to slow down in

residential motherboards

search

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3858

1

2

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3958

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4058

[ Back ]

5

Last Updated ( Wednesday 31 October 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4158

8th Step (Cont)

Written by Mohd Khalid Kasmin

Monday 27 August 2007

Up to this point we have define the name of our project Next we construct a class and to follow what we already know we name the class the sam

project name latihan01

6

7

Home Profile Teaching Links Contact Me My Work

Newsflash

Joomla 10 - Experience the Freedom It has never been easier to create your own dynamic site Manage all your content from

the best CMS admin interface

search

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4258

8

9

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4358

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4458

8th Step (Cont still)

Written by Mohd Khalid Kasmin

Monday 27 August 2007

In item (9) we see that Netbeans does not provide us with the main method So to follow what we already know we now put in the

main method

10

Home Profile Teaching Links Contact Me My Work

Newsflash

Aoccdrnig to a rscheearch at an Elingsh uinervtisy it deosnt mttaer in waht oredr the ltteers in a wrod are the olny iprmoetnt

tihng is taht frist and lsat ltteer is at the rghit pclae The rset can be a toatl mses and you can sitll raed it wouthit porbelm Tihs is

bcuseae we do not raed ervey lteter by itslef but the wrod as a wlohe

search

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4558

11

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4658

12

13

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4758

14

15

Last Updated ( Monday 27 August 2007 )

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4858

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4958

9th Step

Thursday 06 September 2007

Step by Step into Java Programming

for

University Physics Student

By

Mohd Khalid Kasmin

A Building graphical user interface - preparing the JFrame Form

At this stage of our programming experience with Java and NetBeans please follow strictly the steps outline in this article First and

make a new GUI project make sure there is no other project that already been loaded by Netbeans Please close any project displayed

1 Create new project

- On the main menu click on File --gt New Project (note Close first any previous project)

- Specify project name amp project location - Untick Create Main Class item

- Click on the Finish button

Home Profile Teaching Links Contact Me My Work

Newsflash

Aoccdrnig to a rscheearch at an Elingsh uinervtisy it deosnt mttaer in waht oredr the ltteers in a wrod are the olny iprmoetnt

tihng is taht frist and lsat ltteer is at the rghit pclae The rset can be a toatl mses and you can sitll raed it wouthit porbelm Tihs is

bcuseae we do not raed ervey lteter by itslef but the wrod as a wlohe

search

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5058

Figure 91

2 Create UI class

- Right click on the project name in the Project pane

on the popup menu select New --gt JFrame Form

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5158

Figure 92

- In the New JFrame Form dialog specify the Class Name and the Package name

eg Class Name multiplierUI Package mymultiplier

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5258

Figure 93

After clicking on the Finish button we should get something like the following

Figure 94

Last Updated ( Tuesday 18 September 2007 )

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5358

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5458

9th Step (Cont)

Written by Mohd Khalid Kasmin

Tuesday 11 September 2007

Step by Step into Java Programming

for University Physics Student

By

Mohd Khalid Kasmin

B Building user interface (UI) - putting in the components

Now that we have a proper blank form we can start filling the form with the suitable graphical components For introduction to

GUI programming an appropriate application to build (for a physics student) is a simple solver So our first exercise is to build an

application that takes two numbers and multiply them and display the result

With GUI we usually give input values to an application via input boxes that are labelled When all inputs are typed in properlywe click on a button to ask the application to execute the job Hence in this exercise well do just that The target now is to

construct a user interface that looks like Figure 95 below

Figure 95

Home Profile Teaching Links Contact Me My Work

Newsflash

Joomla 10 - Experience the Freedom It has never been easier to create your own dynamic site Manage all your content from

the best CMS admin interface

search

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5558

[ Back ]

1 Problemapplication description box

- In the palette pane click on Swing component named JTextPane (Note click just once) Move the mouse pointer on top of

your JFrame (Note just move not drag) When you see a little white box with orange border position it somewhere near the

top part of your JFrame and click once The JTextPane is then attached on the JFrame Addjust its size with the help of the

sizing hook around the perimeter of the JTextPane

- Edit the text property of the JTextPane with the description of your application

2 Input boxes

- In the palette pane click on Swing component named JTextField Next move the mouse pointer on top of your JFrame

When you see a white box with the word jTextField1 position it to the left and under the JTextPane1

- Click on Swing component JTextField in the palette pane again Move the mouse pointer on top of the JFrame you should

get a second JTextField named jTextField2 Position it to the right of jTextField1

Last Updated ( Tuesday 11 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5658

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5758

multiplication if a b and c were first declared as double or float or int variables So the code of the calculation should look like the

following

double a b c

a =

b =

c = a b

Clearly variables a and b need to be assigned with values of typed double Now looking back at inputboxes A and B we made

those boxes using the JTextField component meaning that any data or value written in a JTextField is of typed Text or String even

if the value displayed in the box is ldquo125rdquo But f irst here there is this task of getting to that Text (or String) values ldquo125rdquo and ldquo72rdquo

In Java we can get to a text in JTextField component using the getText() method So to retrieve values in jTextField1 and

jTextField2 we can use

jTextField1getText()

and

jTextField2getText()

But those values are still in text mode and we need another method to convert the data mode f rom text to double For that we can

use the method DoubleparseDouble() Thus the whole process of retrieve text -gt convert text to double -gt assign to double

variable can be done using

a = DoubleparseDouble( jTextField1getText() )

and

b = DoubleparseDouble( jTextField2getText() )

Hence the code for performing the multiplication becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

Up to this point we have the variable c as the result of the multiplication of a and b So the next thing to do is to display this

result on the JLabel that we have already prepared before

A x B = _____

The text A x B = ______ was made using JLabel component and its variable name is jLabel3 Since the variable c is of type

double and text property of JLabel is of type String we need to convert it first to String data type The method to convert a double

to String is DoubletoString() So we can write the code as DoubletoString(c) After getting the text we can now use it to be the

text displayed on jLabel3 To do that we use the method setText() Hence the appropriate code for displaying the result is

jLabel3setText( A x B = + DoubletoString(c) )

And the full code for our calculation becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

jLabel3setText( A x B = + DoubletoString(c) )

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5858

[ Back ]

Last Updated ( Tuesday 18 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

Page 29: Computional Physics

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 2958

This example will not show you step by step details procedure since it already explained inthe class and in the e-learning Please refer to the e-learning about how to create a project how tocreate JFrame Form and as well how to create JButton JTextField JLabel JTextPane and so on

First create a new project and give a name eg ldquogravityrdquo and then create JFrame Form andthe following JButton JTextField JLabel JTextPane etc by using ldquoswing paletterdquo as shown in Figure2

Figure 2

To insert a picture into JLabel2 click on the JLabel2 go to properties and select ldquoiconrdquo Onthe icon editor choose file and then select file to locate the directory of your graphicpicture file(Figure 3) Select the file and click OK when finished Make sure you have already created your ownpicture file and save it as jpg png bmp etc format eg ldquogravityjpgrdquo Use any graphic package todraw your diagrampicture or use drawing tools available in Open Office or Microsoft Word

JTextPane1JLabel1

JLabel2

JLabel3JLabel4

JLabel5

JLabel6 JLabel7

JLabel8

JTextField2 JTextField3 JTextField4

JTextF

JButton1JButton2

JButton3

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3058

Figure 3

To write documentation or question eg in JTextPane1 click on the JTextPane1 go toproperties and select ldquotextrdquo Type the documentation in the text editor as shown in Figure 4 Click OKwhen finished

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3158

To do formatting on the text such as superscript and subscript eg in jLabel3 click on the

JLabel3 go to properties and select ldquotextrdquo Type the documentationmessage on the text editor asshown in Figure 5 as follows

eg

lthtmlgtOn which particle (mltsubgt1ltsubgtmltsubgt5ltsubgt) you want to calculate theresultant force

Use sup instead if you want a superscriptThe result will look like this

Click OK when finished

Figure 5

To do the calculation you need to put the coding similar like ldquoGravityAttract07ajavardquo and put itinto the ldquocalculaterdquo button (this is ldquoJButton1rdquo) You can copy ldquoGravityAttract07a javardquo source code

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3258

paste it and then do a few alterations First click on the ldquocalculaterdquo button right click choose Eventsand then choose Action then actionPerformed (Figure 6) It will bring you to where you should insertyour coding (Figure 7) You can write your own coding or copypaste any coding from somewhereelse (in this case from ldquoGravityAttract07ajavardquo)

Figure 6

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3358

Figure 7

Copy ldquoGravityAttract07ajavardquo source code and paste to the place after ldquo TODO add youhandling code hererdquo see Figure 7 To see which part has been modified the coding in Figure 7 hasbeen color coded for better understanding as follows

private void jButton1ActionPerformed(javaawteventActionEvent evt) TODO add your handling code here

double[] m = new double[6] mass of objectdouble[] x = new double[6] x-coor of objectdouble[] y = new double[6] y-coor of objectdouble f resultant forcedouble fxfy x amp y component of forcedouble rx ry rp co G dirfangdirfDecimalFormat dff dfangdirfint j mi

G = 667e-11m[1]=25 x[1]=12 y[1]=25m[2]=15 x[2]=15 y[2]=15m[3]=75 x[3]=02 y[3]=55m[4]=92 x[4]=52 y[4]=25m[5]=66 x[5]=42 y[5]=45

BufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin)) String mass= Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3458

catch(IOException e)Systemoutprintln(Not a valid input value) int mi = IntegerparseInt(mass)mi = IntegerparseInt(jTextField1getText())fx = 00fy = 00for(j=1 jlt=5 j++)

if (j=mi)rx = x[j]-x[mi]ry = y[j]-y[mi]rp = Mathpow((rxrx+ryry)15) (rx^2 + ry^2) raised to the power of 32co = Gm[mi]m[j]fx = fx + corxrpfy = fy + coryrp

f = Mathsqrt(fxfx + fyfy)

dirf = Mathatan(fyfx)angdirf = (dirf(22070))1800 Systemoutprintf(ldquoThe resultant force on particle [ldquo+ mi +rdquo] due to the other masses is

14e N 22f degnrdquofangdirf)dff = new javatextDecimalFormat(ldquoE00rdquo)dfangdirf = new DecimalFormat(ldquo000rdquo)

jTextField2setText(ldquordquo + mi) jTextField3setText(ldquordquo + dffformat(f)) jTextField4setText(ldquordquo + dfangdirfformat(angdirf))

Note

Brown = automatically generatedBlue = the original code from ldquoGravityAttract07ajavardquoGreen = coding from ldquoGravityAttract07ajavardquo which were being disabled due to

incompatibilities with NetbeansRed = a new inserted coding a few of them is to rephrasesubstitute the green one

Javascripts command line useBufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin))String mass=Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))try mass=dataInreadLine()catch(IOException e)Systemoutprintln(Not a valid input value)

int mi = IntegerparseInt(mass)

to read any input from the keyboard typed by the user while Netbeans uses egldquoIntegerparseInt(jTextField1getText())rdquo etc Javascripts command line uses ldquoSystemoutprintfrdquo to format the printed output whileNetbeans uses more complicated way to do the same job (see below)

Javascript command line

Systemoutprintf(The resultant force on particle [+ mi +] due to the other masses is 14e N22f degnfangdirf)

Netbeansdff = new javatextDecimalFormat(ldquo0E00rdquo)df di f D i lF t(ldquo0 00rdquo)

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3558

jTextField2setText(ldquordquo + mi) jTextField3setText(ldquordquo + dffformat(f)) jTextField4setText(ldquordquo + dfangdirfformat(angdirf))

ldquodffrdquo and ldquodfangdirfrdquo are new variables that should be declared and added to format thenumbers into decimal format ie ldquo0E00rdquo to get a scientific format eg ldquo20019E-10rdquo and ldquo000rdquoto get a decimal number eg ldquo-1570rdquo

To use DecimalFormat you have to put ldquoimport javatextDecimalFormatrdquo in the top of thecoding as shown in Figure 8

Figure 8

To reset the calculation you need to put coding into the ldquoresetrdquo button (this is ldquoJButton2rdquo)First click on the ldquoresetrdquo button right click choose Events and then choose Action thenactionPerformed (similar like in Figure 6) It will bring you to where you should insert your coding(Figure 9) Write the following coding (Figure 9)

Figure 9

To exit the program you need to put coding into the ldquoexitrdquo button (this is ldquoJButton3rdquo) Firstclick on the ldquoexitrdquo button right click choose Events and then choose Action then actionPerformed(similar like in Figure 6) It will bring you to where you should insert your coding (Figure 10) Writethe following coding (Figure 10)

Figure 10

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3658

Once you finished now you can run and see the result If everything is fine then the output wilbe look like this (Figure 11)

Figure 11

Remember when you compile and run ldquoGravityAttract07ajavardquo using Javascripts commandline once you compile and run the result was looked like this

On which particle you want to calculate the resulting force 2The resultant force on particle [2] due to the other masses is 30673e-10 N 8837 deg

SEE THE DIFFERENCES AT THE USER INTERFACE

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3758

8th Step

Written by Mohd Khalid Kasmin

Monday 27 August 2007

Programming using command line interface (CLI) has its advantages but todays computer users are so used to graphical user

interface (GUI) that we just can no longer be contented with writing application that do not use the GUI Hence starting from this 8thstep we are going to embark on learning how to make java application that utilises the the GUI

It is possible to write a GUI application with the text based tools that we have been using for these past 7 steps but the job is so

messy that most of you will definitely be turnoff by it So to make life slightly easier well use an integrated development

environment (IDE) known as NetBeans specifically NetBeans 551

0 First ssh to the our server Next once logged in just type nice -n15 netbeans amp on the terminal If you had sshed with the -Y

switch after a few tens of seconds NetBeans should emerge on your display screen

One important point to note Since our server can readily handle up to 100 users (maybe even

more) doing work in CLI mode this machine will certainly be taxed to its maximum capability once

we start using graphical based application NetBeans 551 uses a geat deal of computer memory

and with the 4Gbytes RAM server it can only accomodate up to 25 users concurrently using

NetBeans if the users use the application wisely What is meant by wisely here is that (a) when

starting the NetBeans application please use it together with the nice -n15 command (b) in using

the NetBeans please make sure you only have one project open at a time and (c) please close your

project everytime before quitting the NetBeans

Home Profile Teaching Links Contact Me My Work

Newsflash

Yesterday all servers in the US went out on strike in a bid to get more RAM and better CPUs A spokes person said that the

need for better RAM was due to some fool increasing the front-side bus speed In future busses will be told to slow down in

residential motherboards

search

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3858

1

2

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3958

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4058

[ Back ]

5

Last Updated ( Wednesday 31 October 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4158

8th Step (Cont)

Written by Mohd Khalid Kasmin

Monday 27 August 2007

Up to this point we have define the name of our project Next we construct a class and to follow what we already know we name the class the sam

project name latihan01

6

7

Home Profile Teaching Links Contact Me My Work

Newsflash

Joomla 10 - Experience the Freedom It has never been easier to create your own dynamic site Manage all your content from

the best CMS admin interface

search

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4258

8

9

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4358

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4458

8th Step (Cont still)

Written by Mohd Khalid Kasmin

Monday 27 August 2007

In item (9) we see that Netbeans does not provide us with the main method So to follow what we already know we now put in the

main method

10

Home Profile Teaching Links Contact Me My Work

Newsflash

Aoccdrnig to a rscheearch at an Elingsh uinervtisy it deosnt mttaer in waht oredr the ltteers in a wrod are the olny iprmoetnt

tihng is taht frist and lsat ltteer is at the rghit pclae The rset can be a toatl mses and you can sitll raed it wouthit porbelm Tihs is

bcuseae we do not raed ervey lteter by itslef but the wrod as a wlohe

search

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4558

11

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4658

12

13

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4758

14

15

Last Updated ( Monday 27 August 2007 )

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4858

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4958

9th Step

Thursday 06 September 2007

Step by Step into Java Programming

for

University Physics Student

By

Mohd Khalid Kasmin

A Building graphical user interface - preparing the JFrame Form

At this stage of our programming experience with Java and NetBeans please follow strictly the steps outline in this article First and

make a new GUI project make sure there is no other project that already been loaded by Netbeans Please close any project displayed

1 Create new project

- On the main menu click on File --gt New Project (note Close first any previous project)

- Specify project name amp project location - Untick Create Main Class item

- Click on the Finish button

Home Profile Teaching Links Contact Me My Work

Newsflash

Aoccdrnig to a rscheearch at an Elingsh uinervtisy it deosnt mttaer in waht oredr the ltteers in a wrod are the olny iprmoetnt

tihng is taht frist and lsat ltteer is at the rghit pclae The rset can be a toatl mses and you can sitll raed it wouthit porbelm Tihs is

bcuseae we do not raed ervey lteter by itslef but the wrod as a wlohe

search

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5058

Figure 91

2 Create UI class

- Right click on the project name in the Project pane

on the popup menu select New --gt JFrame Form

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5158

Figure 92

- In the New JFrame Form dialog specify the Class Name and the Package name

eg Class Name multiplierUI Package mymultiplier

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5258

Figure 93

After clicking on the Finish button we should get something like the following

Figure 94

Last Updated ( Tuesday 18 September 2007 )

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5358

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5458

9th Step (Cont)

Written by Mohd Khalid Kasmin

Tuesday 11 September 2007

Step by Step into Java Programming

for University Physics Student

By

Mohd Khalid Kasmin

B Building user interface (UI) - putting in the components

Now that we have a proper blank form we can start filling the form with the suitable graphical components For introduction to

GUI programming an appropriate application to build (for a physics student) is a simple solver So our first exercise is to build an

application that takes two numbers and multiply them and display the result

With GUI we usually give input values to an application via input boxes that are labelled When all inputs are typed in properlywe click on a button to ask the application to execute the job Hence in this exercise well do just that The target now is to

construct a user interface that looks like Figure 95 below

Figure 95

Home Profile Teaching Links Contact Me My Work

Newsflash

Joomla 10 - Experience the Freedom It has never been easier to create your own dynamic site Manage all your content from

the best CMS admin interface

search

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5558

[ Back ]

1 Problemapplication description box

- In the palette pane click on Swing component named JTextPane (Note click just once) Move the mouse pointer on top of

your JFrame (Note just move not drag) When you see a little white box with orange border position it somewhere near the

top part of your JFrame and click once The JTextPane is then attached on the JFrame Addjust its size with the help of the

sizing hook around the perimeter of the JTextPane

- Edit the text property of the JTextPane with the description of your application

2 Input boxes

- In the palette pane click on Swing component named JTextField Next move the mouse pointer on top of your JFrame

When you see a white box with the word jTextField1 position it to the left and under the JTextPane1

- Click on Swing component JTextField in the palette pane again Move the mouse pointer on top of the JFrame you should

get a second JTextField named jTextField2 Position it to the right of jTextField1

Last Updated ( Tuesday 11 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5658

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5758

multiplication if a b and c were first declared as double or float or int variables So the code of the calculation should look like the

following

double a b c

a =

b =

c = a b

Clearly variables a and b need to be assigned with values of typed double Now looking back at inputboxes A and B we made

those boxes using the JTextField component meaning that any data or value written in a JTextField is of typed Text or String even

if the value displayed in the box is ldquo125rdquo But f irst here there is this task of getting to that Text (or String) values ldquo125rdquo and ldquo72rdquo

In Java we can get to a text in JTextField component using the getText() method So to retrieve values in jTextField1 and

jTextField2 we can use

jTextField1getText()

and

jTextField2getText()

But those values are still in text mode and we need another method to convert the data mode f rom text to double For that we can

use the method DoubleparseDouble() Thus the whole process of retrieve text -gt convert text to double -gt assign to double

variable can be done using

a = DoubleparseDouble( jTextField1getText() )

and

b = DoubleparseDouble( jTextField2getText() )

Hence the code for performing the multiplication becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

Up to this point we have the variable c as the result of the multiplication of a and b So the next thing to do is to display this

result on the JLabel that we have already prepared before

A x B = _____

The text A x B = ______ was made using JLabel component and its variable name is jLabel3 Since the variable c is of type

double and text property of JLabel is of type String we need to convert it first to String data type The method to convert a double

to String is DoubletoString() So we can write the code as DoubletoString(c) After getting the text we can now use it to be the

text displayed on jLabel3 To do that we use the method setText() Hence the appropriate code for displaying the result is

jLabel3setText( A x B = + DoubletoString(c) )

And the full code for our calculation becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

jLabel3setText( A x B = + DoubletoString(c) )

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5858

[ Back ]

Last Updated ( Tuesday 18 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

Page 30: Computional Physics

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3058

Figure 3

To write documentation or question eg in JTextPane1 click on the JTextPane1 go toproperties and select ldquotextrdquo Type the documentation in the text editor as shown in Figure 4 Click OKwhen finished

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3158

To do formatting on the text such as superscript and subscript eg in jLabel3 click on the

JLabel3 go to properties and select ldquotextrdquo Type the documentationmessage on the text editor asshown in Figure 5 as follows

eg

lthtmlgtOn which particle (mltsubgt1ltsubgtmltsubgt5ltsubgt) you want to calculate theresultant force

Use sup instead if you want a superscriptThe result will look like this

Click OK when finished

Figure 5

To do the calculation you need to put the coding similar like ldquoGravityAttract07ajavardquo and put itinto the ldquocalculaterdquo button (this is ldquoJButton1rdquo) You can copy ldquoGravityAttract07a javardquo source code

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3258

paste it and then do a few alterations First click on the ldquocalculaterdquo button right click choose Eventsand then choose Action then actionPerformed (Figure 6) It will bring you to where you should insertyour coding (Figure 7) You can write your own coding or copypaste any coding from somewhereelse (in this case from ldquoGravityAttract07ajavardquo)

Figure 6

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3358

Figure 7

Copy ldquoGravityAttract07ajavardquo source code and paste to the place after ldquo TODO add youhandling code hererdquo see Figure 7 To see which part has been modified the coding in Figure 7 hasbeen color coded for better understanding as follows

private void jButton1ActionPerformed(javaawteventActionEvent evt) TODO add your handling code here

double[] m = new double[6] mass of objectdouble[] x = new double[6] x-coor of objectdouble[] y = new double[6] y-coor of objectdouble f resultant forcedouble fxfy x amp y component of forcedouble rx ry rp co G dirfangdirfDecimalFormat dff dfangdirfint j mi

G = 667e-11m[1]=25 x[1]=12 y[1]=25m[2]=15 x[2]=15 y[2]=15m[3]=75 x[3]=02 y[3]=55m[4]=92 x[4]=52 y[4]=25m[5]=66 x[5]=42 y[5]=45

BufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin)) String mass= Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3458

catch(IOException e)Systemoutprintln(Not a valid input value) int mi = IntegerparseInt(mass)mi = IntegerparseInt(jTextField1getText())fx = 00fy = 00for(j=1 jlt=5 j++)

if (j=mi)rx = x[j]-x[mi]ry = y[j]-y[mi]rp = Mathpow((rxrx+ryry)15) (rx^2 + ry^2) raised to the power of 32co = Gm[mi]m[j]fx = fx + corxrpfy = fy + coryrp

f = Mathsqrt(fxfx + fyfy)

dirf = Mathatan(fyfx)angdirf = (dirf(22070))1800 Systemoutprintf(ldquoThe resultant force on particle [ldquo+ mi +rdquo] due to the other masses is

14e N 22f degnrdquofangdirf)dff = new javatextDecimalFormat(ldquoE00rdquo)dfangdirf = new DecimalFormat(ldquo000rdquo)

jTextField2setText(ldquordquo + mi) jTextField3setText(ldquordquo + dffformat(f)) jTextField4setText(ldquordquo + dfangdirfformat(angdirf))

Note

Brown = automatically generatedBlue = the original code from ldquoGravityAttract07ajavardquoGreen = coding from ldquoGravityAttract07ajavardquo which were being disabled due to

incompatibilities with NetbeansRed = a new inserted coding a few of them is to rephrasesubstitute the green one

Javascripts command line useBufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin))String mass=Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))try mass=dataInreadLine()catch(IOException e)Systemoutprintln(Not a valid input value)

int mi = IntegerparseInt(mass)

to read any input from the keyboard typed by the user while Netbeans uses egldquoIntegerparseInt(jTextField1getText())rdquo etc Javascripts command line uses ldquoSystemoutprintfrdquo to format the printed output whileNetbeans uses more complicated way to do the same job (see below)

Javascript command line

Systemoutprintf(The resultant force on particle [+ mi +] due to the other masses is 14e N22f degnfangdirf)

Netbeansdff = new javatextDecimalFormat(ldquo0E00rdquo)df di f D i lF t(ldquo0 00rdquo)

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3558

jTextField2setText(ldquordquo + mi) jTextField3setText(ldquordquo + dffformat(f)) jTextField4setText(ldquordquo + dfangdirfformat(angdirf))

ldquodffrdquo and ldquodfangdirfrdquo are new variables that should be declared and added to format thenumbers into decimal format ie ldquo0E00rdquo to get a scientific format eg ldquo20019E-10rdquo and ldquo000rdquoto get a decimal number eg ldquo-1570rdquo

To use DecimalFormat you have to put ldquoimport javatextDecimalFormatrdquo in the top of thecoding as shown in Figure 8

Figure 8

To reset the calculation you need to put coding into the ldquoresetrdquo button (this is ldquoJButton2rdquo)First click on the ldquoresetrdquo button right click choose Events and then choose Action thenactionPerformed (similar like in Figure 6) It will bring you to where you should insert your coding(Figure 9) Write the following coding (Figure 9)

Figure 9

To exit the program you need to put coding into the ldquoexitrdquo button (this is ldquoJButton3rdquo) Firstclick on the ldquoexitrdquo button right click choose Events and then choose Action then actionPerformed(similar like in Figure 6) It will bring you to where you should insert your coding (Figure 10) Writethe following coding (Figure 10)

Figure 10

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3658

Once you finished now you can run and see the result If everything is fine then the output wilbe look like this (Figure 11)

Figure 11

Remember when you compile and run ldquoGravityAttract07ajavardquo using Javascripts commandline once you compile and run the result was looked like this

On which particle you want to calculate the resulting force 2The resultant force on particle [2] due to the other masses is 30673e-10 N 8837 deg

SEE THE DIFFERENCES AT THE USER INTERFACE

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3758

8th Step

Written by Mohd Khalid Kasmin

Monday 27 August 2007

Programming using command line interface (CLI) has its advantages but todays computer users are so used to graphical user

interface (GUI) that we just can no longer be contented with writing application that do not use the GUI Hence starting from this 8thstep we are going to embark on learning how to make java application that utilises the the GUI

It is possible to write a GUI application with the text based tools that we have been using for these past 7 steps but the job is so

messy that most of you will definitely be turnoff by it So to make life slightly easier well use an integrated development

environment (IDE) known as NetBeans specifically NetBeans 551

0 First ssh to the our server Next once logged in just type nice -n15 netbeans amp on the terminal If you had sshed with the -Y

switch after a few tens of seconds NetBeans should emerge on your display screen

One important point to note Since our server can readily handle up to 100 users (maybe even

more) doing work in CLI mode this machine will certainly be taxed to its maximum capability once

we start using graphical based application NetBeans 551 uses a geat deal of computer memory

and with the 4Gbytes RAM server it can only accomodate up to 25 users concurrently using

NetBeans if the users use the application wisely What is meant by wisely here is that (a) when

starting the NetBeans application please use it together with the nice -n15 command (b) in using

the NetBeans please make sure you only have one project open at a time and (c) please close your

project everytime before quitting the NetBeans

Home Profile Teaching Links Contact Me My Work

Newsflash

Yesterday all servers in the US went out on strike in a bid to get more RAM and better CPUs A spokes person said that the

need for better RAM was due to some fool increasing the front-side bus speed In future busses will be told to slow down in

residential motherboards

search

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3858

1

2

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3958

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4058

[ Back ]

5

Last Updated ( Wednesday 31 October 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4158

8th Step (Cont)

Written by Mohd Khalid Kasmin

Monday 27 August 2007

Up to this point we have define the name of our project Next we construct a class and to follow what we already know we name the class the sam

project name latihan01

6

7

Home Profile Teaching Links Contact Me My Work

Newsflash

Joomla 10 - Experience the Freedom It has never been easier to create your own dynamic site Manage all your content from

the best CMS admin interface

search

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4258

8

9

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4358

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4458

8th Step (Cont still)

Written by Mohd Khalid Kasmin

Monday 27 August 2007

In item (9) we see that Netbeans does not provide us with the main method So to follow what we already know we now put in the

main method

10

Home Profile Teaching Links Contact Me My Work

Newsflash

Aoccdrnig to a rscheearch at an Elingsh uinervtisy it deosnt mttaer in waht oredr the ltteers in a wrod are the olny iprmoetnt

tihng is taht frist and lsat ltteer is at the rghit pclae The rset can be a toatl mses and you can sitll raed it wouthit porbelm Tihs is

bcuseae we do not raed ervey lteter by itslef but the wrod as a wlohe

search

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4558

11

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4658

12

13

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4758

14

15

Last Updated ( Monday 27 August 2007 )

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4858

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4958

9th Step

Thursday 06 September 2007

Step by Step into Java Programming

for

University Physics Student

By

Mohd Khalid Kasmin

A Building graphical user interface - preparing the JFrame Form

At this stage of our programming experience with Java and NetBeans please follow strictly the steps outline in this article First and

make a new GUI project make sure there is no other project that already been loaded by Netbeans Please close any project displayed

1 Create new project

- On the main menu click on File --gt New Project (note Close first any previous project)

- Specify project name amp project location - Untick Create Main Class item

- Click on the Finish button

Home Profile Teaching Links Contact Me My Work

Newsflash

Aoccdrnig to a rscheearch at an Elingsh uinervtisy it deosnt mttaer in waht oredr the ltteers in a wrod are the olny iprmoetnt

tihng is taht frist and lsat ltteer is at the rghit pclae The rset can be a toatl mses and you can sitll raed it wouthit porbelm Tihs is

bcuseae we do not raed ervey lteter by itslef but the wrod as a wlohe

search

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5058

Figure 91

2 Create UI class

- Right click on the project name in the Project pane

on the popup menu select New --gt JFrame Form

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5158

Figure 92

- In the New JFrame Form dialog specify the Class Name and the Package name

eg Class Name multiplierUI Package mymultiplier

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5258

Figure 93

After clicking on the Finish button we should get something like the following

Figure 94

Last Updated ( Tuesday 18 September 2007 )

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5358

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5458

9th Step (Cont)

Written by Mohd Khalid Kasmin

Tuesday 11 September 2007

Step by Step into Java Programming

for University Physics Student

By

Mohd Khalid Kasmin

B Building user interface (UI) - putting in the components

Now that we have a proper blank form we can start filling the form with the suitable graphical components For introduction to

GUI programming an appropriate application to build (for a physics student) is a simple solver So our first exercise is to build an

application that takes two numbers and multiply them and display the result

With GUI we usually give input values to an application via input boxes that are labelled When all inputs are typed in properlywe click on a button to ask the application to execute the job Hence in this exercise well do just that The target now is to

construct a user interface that looks like Figure 95 below

Figure 95

Home Profile Teaching Links Contact Me My Work

Newsflash

Joomla 10 - Experience the Freedom It has never been easier to create your own dynamic site Manage all your content from

the best CMS admin interface

search

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5558

[ Back ]

1 Problemapplication description box

- In the palette pane click on Swing component named JTextPane (Note click just once) Move the mouse pointer on top of

your JFrame (Note just move not drag) When you see a little white box with orange border position it somewhere near the

top part of your JFrame and click once The JTextPane is then attached on the JFrame Addjust its size with the help of the

sizing hook around the perimeter of the JTextPane

- Edit the text property of the JTextPane with the description of your application

2 Input boxes

- In the palette pane click on Swing component named JTextField Next move the mouse pointer on top of your JFrame

When you see a white box with the word jTextField1 position it to the left and under the JTextPane1

- Click on Swing component JTextField in the palette pane again Move the mouse pointer on top of the JFrame you should

get a second JTextField named jTextField2 Position it to the right of jTextField1

Last Updated ( Tuesday 11 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5658

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5758

multiplication if a b and c were first declared as double or float or int variables So the code of the calculation should look like the

following

double a b c

a =

b =

c = a b

Clearly variables a and b need to be assigned with values of typed double Now looking back at inputboxes A and B we made

those boxes using the JTextField component meaning that any data or value written in a JTextField is of typed Text or String even

if the value displayed in the box is ldquo125rdquo But f irst here there is this task of getting to that Text (or String) values ldquo125rdquo and ldquo72rdquo

In Java we can get to a text in JTextField component using the getText() method So to retrieve values in jTextField1 and

jTextField2 we can use

jTextField1getText()

and

jTextField2getText()

But those values are still in text mode and we need another method to convert the data mode f rom text to double For that we can

use the method DoubleparseDouble() Thus the whole process of retrieve text -gt convert text to double -gt assign to double

variable can be done using

a = DoubleparseDouble( jTextField1getText() )

and

b = DoubleparseDouble( jTextField2getText() )

Hence the code for performing the multiplication becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

Up to this point we have the variable c as the result of the multiplication of a and b So the next thing to do is to display this

result on the JLabel that we have already prepared before

A x B = _____

The text A x B = ______ was made using JLabel component and its variable name is jLabel3 Since the variable c is of type

double and text property of JLabel is of type String we need to convert it first to String data type The method to convert a double

to String is DoubletoString() So we can write the code as DoubletoString(c) After getting the text we can now use it to be the

text displayed on jLabel3 To do that we use the method setText() Hence the appropriate code for displaying the result is

jLabel3setText( A x B = + DoubletoString(c) )

And the full code for our calculation becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

jLabel3setText( A x B = + DoubletoString(c) )

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5858

[ Back ]

Last Updated ( Tuesday 18 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

Page 31: Computional Physics

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3158

To do formatting on the text such as superscript and subscript eg in jLabel3 click on the

JLabel3 go to properties and select ldquotextrdquo Type the documentationmessage on the text editor asshown in Figure 5 as follows

eg

lthtmlgtOn which particle (mltsubgt1ltsubgtmltsubgt5ltsubgt) you want to calculate theresultant force

Use sup instead if you want a superscriptThe result will look like this

Click OK when finished

Figure 5

To do the calculation you need to put the coding similar like ldquoGravityAttract07ajavardquo and put itinto the ldquocalculaterdquo button (this is ldquoJButton1rdquo) You can copy ldquoGravityAttract07a javardquo source code

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3258

paste it and then do a few alterations First click on the ldquocalculaterdquo button right click choose Eventsand then choose Action then actionPerformed (Figure 6) It will bring you to where you should insertyour coding (Figure 7) You can write your own coding or copypaste any coding from somewhereelse (in this case from ldquoGravityAttract07ajavardquo)

Figure 6

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3358

Figure 7

Copy ldquoGravityAttract07ajavardquo source code and paste to the place after ldquo TODO add youhandling code hererdquo see Figure 7 To see which part has been modified the coding in Figure 7 hasbeen color coded for better understanding as follows

private void jButton1ActionPerformed(javaawteventActionEvent evt) TODO add your handling code here

double[] m = new double[6] mass of objectdouble[] x = new double[6] x-coor of objectdouble[] y = new double[6] y-coor of objectdouble f resultant forcedouble fxfy x amp y component of forcedouble rx ry rp co G dirfangdirfDecimalFormat dff dfangdirfint j mi

G = 667e-11m[1]=25 x[1]=12 y[1]=25m[2]=15 x[2]=15 y[2]=15m[3]=75 x[3]=02 y[3]=55m[4]=92 x[4]=52 y[4]=25m[5]=66 x[5]=42 y[5]=45

BufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin)) String mass= Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3458

catch(IOException e)Systemoutprintln(Not a valid input value) int mi = IntegerparseInt(mass)mi = IntegerparseInt(jTextField1getText())fx = 00fy = 00for(j=1 jlt=5 j++)

if (j=mi)rx = x[j]-x[mi]ry = y[j]-y[mi]rp = Mathpow((rxrx+ryry)15) (rx^2 + ry^2) raised to the power of 32co = Gm[mi]m[j]fx = fx + corxrpfy = fy + coryrp

f = Mathsqrt(fxfx + fyfy)

dirf = Mathatan(fyfx)angdirf = (dirf(22070))1800 Systemoutprintf(ldquoThe resultant force on particle [ldquo+ mi +rdquo] due to the other masses is

14e N 22f degnrdquofangdirf)dff = new javatextDecimalFormat(ldquoE00rdquo)dfangdirf = new DecimalFormat(ldquo000rdquo)

jTextField2setText(ldquordquo + mi) jTextField3setText(ldquordquo + dffformat(f)) jTextField4setText(ldquordquo + dfangdirfformat(angdirf))

Note

Brown = automatically generatedBlue = the original code from ldquoGravityAttract07ajavardquoGreen = coding from ldquoGravityAttract07ajavardquo which were being disabled due to

incompatibilities with NetbeansRed = a new inserted coding a few of them is to rephrasesubstitute the green one

Javascripts command line useBufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin))String mass=Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))try mass=dataInreadLine()catch(IOException e)Systemoutprintln(Not a valid input value)

int mi = IntegerparseInt(mass)

to read any input from the keyboard typed by the user while Netbeans uses egldquoIntegerparseInt(jTextField1getText())rdquo etc Javascripts command line uses ldquoSystemoutprintfrdquo to format the printed output whileNetbeans uses more complicated way to do the same job (see below)

Javascript command line

Systemoutprintf(The resultant force on particle [+ mi +] due to the other masses is 14e N22f degnfangdirf)

Netbeansdff = new javatextDecimalFormat(ldquo0E00rdquo)df di f D i lF t(ldquo0 00rdquo)

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3558

jTextField2setText(ldquordquo + mi) jTextField3setText(ldquordquo + dffformat(f)) jTextField4setText(ldquordquo + dfangdirfformat(angdirf))

ldquodffrdquo and ldquodfangdirfrdquo are new variables that should be declared and added to format thenumbers into decimal format ie ldquo0E00rdquo to get a scientific format eg ldquo20019E-10rdquo and ldquo000rdquoto get a decimal number eg ldquo-1570rdquo

To use DecimalFormat you have to put ldquoimport javatextDecimalFormatrdquo in the top of thecoding as shown in Figure 8

Figure 8

To reset the calculation you need to put coding into the ldquoresetrdquo button (this is ldquoJButton2rdquo)First click on the ldquoresetrdquo button right click choose Events and then choose Action thenactionPerformed (similar like in Figure 6) It will bring you to where you should insert your coding(Figure 9) Write the following coding (Figure 9)

Figure 9

To exit the program you need to put coding into the ldquoexitrdquo button (this is ldquoJButton3rdquo) Firstclick on the ldquoexitrdquo button right click choose Events and then choose Action then actionPerformed(similar like in Figure 6) It will bring you to where you should insert your coding (Figure 10) Writethe following coding (Figure 10)

Figure 10

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3658

Once you finished now you can run and see the result If everything is fine then the output wilbe look like this (Figure 11)

Figure 11

Remember when you compile and run ldquoGravityAttract07ajavardquo using Javascripts commandline once you compile and run the result was looked like this

On which particle you want to calculate the resulting force 2The resultant force on particle [2] due to the other masses is 30673e-10 N 8837 deg

SEE THE DIFFERENCES AT THE USER INTERFACE

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3758

8th Step

Written by Mohd Khalid Kasmin

Monday 27 August 2007

Programming using command line interface (CLI) has its advantages but todays computer users are so used to graphical user

interface (GUI) that we just can no longer be contented with writing application that do not use the GUI Hence starting from this 8thstep we are going to embark on learning how to make java application that utilises the the GUI

It is possible to write a GUI application with the text based tools that we have been using for these past 7 steps but the job is so

messy that most of you will definitely be turnoff by it So to make life slightly easier well use an integrated development

environment (IDE) known as NetBeans specifically NetBeans 551

0 First ssh to the our server Next once logged in just type nice -n15 netbeans amp on the terminal If you had sshed with the -Y

switch after a few tens of seconds NetBeans should emerge on your display screen

One important point to note Since our server can readily handle up to 100 users (maybe even

more) doing work in CLI mode this machine will certainly be taxed to its maximum capability once

we start using graphical based application NetBeans 551 uses a geat deal of computer memory

and with the 4Gbytes RAM server it can only accomodate up to 25 users concurrently using

NetBeans if the users use the application wisely What is meant by wisely here is that (a) when

starting the NetBeans application please use it together with the nice -n15 command (b) in using

the NetBeans please make sure you only have one project open at a time and (c) please close your

project everytime before quitting the NetBeans

Home Profile Teaching Links Contact Me My Work

Newsflash

Yesterday all servers in the US went out on strike in a bid to get more RAM and better CPUs A spokes person said that the

need for better RAM was due to some fool increasing the front-side bus speed In future busses will be told to slow down in

residential motherboards

search

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3858

1

2

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3958

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4058

[ Back ]

5

Last Updated ( Wednesday 31 October 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4158

8th Step (Cont)

Written by Mohd Khalid Kasmin

Monday 27 August 2007

Up to this point we have define the name of our project Next we construct a class and to follow what we already know we name the class the sam

project name latihan01

6

7

Home Profile Teaching Links Contact Me My Work

Newsflash

Joomla 10 - Experience the Freedom It has never been easier to create your own dynamic site Manage all your content from

the best CMS admin interface

search

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4258

8

9

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4358

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4458

8th Step (Cont still)

Written by Mohd Khalid Kasmin

Monday 27 August 2007

In item (9) we see that Netbeans does not provide us with the main method So to follow what we already know we now put in the

main method

10

Home Profile Teaching Links Contact Me My Work

Newsflash

Aoccdrnig to a rscheearch at an Elingsh uinervtisy it deosnt mttaer in waht oredr the ltteers in a wrod are the olny iprmoetnt

tihng is taht frist and lsat ltteer is at the rghit pclae The rset can be a toatl mses and you can sitll raed it wouthit porbelm Tihs is

bcuseae we do not raed ervey lteter by itslef but the wrod as a wlohe

search

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4558

11

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4658

12

13

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4758

14

15

Last Updated ( Monday 27 August 2007 )

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4858

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4958

9th Step

Thursday 06 September 2007

Step by Step into Java Programming

for

University Physics Student

By

Mohd Khalid Kasmin

A Building graphical user interface - preparing the JFrame Form

At this stage of our programming experience with Java and NetBeans please follow strictly the steps outline in this article First and

make a new GUI project make sure there is no other project that already been loaded by Netbeans Please close any project displayed

1 Create new project

- On the main menu click on File --gt New Project (note Close first any previous project)

- Specify project name amp project location - Untick Create Main Class item

- Click on the Finish button

Home Profile Teaching Links Contact Me My Work

Newsflash

Aoccdrnig to a rscheearch at an Elingsh uinervtisy it deosnt mttaer in waht oredr the ltteers in a wrod are the olny iprmoetnt

tihng is taht frist and lsat ltteer is at the rghit pclae The rset can be a toatl mses and you can sitll raed it wouthit porbelm Tihs is

bcuseae we do not raed ervey lteter by itslef but the wrod as a wlohe

search

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5058

Figure 91

2 Create UI class

- Right click on the project name in the Project pane

on the popup menu select New --gt JFrame Form

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5158

Figure 92

- In the New JFrame Form dialog specify the Class Name and the Package name

eg Class Name multiplierUI Package mymultiplier

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5258

Figure 93

After clicking on the Finish button we should get something like the following

Figure 94

Last Updated ( Tuesday 18 September 2007 )

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5358

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5458

9th Step (Cont)

Written by Mohd Khalid Kasmin

Tuesday 11 September 2007

Step by Step into Java Programming

for University Physics Student

By

Mohd Khalid Kasmin

B Building user interface (UI) - putting in the components

Now that we have a proper blank form we can start filling the form with the suitable graphical components For introduction to

GUI programming an appropriate application to build (for a physics student) is a simple solver So our first exercise is to build an

application that takes two numbers and multiply them and display the result

With GUI we usually give input values to an application via input boxes that are labelled When all inputs are typed in properlywe click on a button to ask the application to execute the job Hence in this exercise well do just that The target now is to

construct a user interface that looks like Figure 95 below

Figure 95

Home Profile Teaching Links Contact Me My Work

Newsflash

Joomla 10 - Experience the Freedom It has never been easier to create your own dynamic site Manage all your content from

the best CMS admin interface

search

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5558

[ Back ]

1 Problemapplication description box

- In the palette pane click on Swing component named JTextPane (Note click just once) Move the mouse pointer on top of

your JFrame (Note just move not drag) When you see a little white box with orange border position it somewhere near the

top part of your JFrame and click once The JTextPane is then attached on the JFrame Addjust its size with the help of the

sizing hook around the perimeter of the JTextPane

- Edit the text property of the JTextPane with the description of your application

2 Input boxes

- In the palette pane click on Swing component named JTextField Next move the mouse pointer on top of your JFrame

When you see a white box with the word jTextField1 position it to the left and under the JTextPane1

- Click on Swing component JTextField in the palette pane again Move the mouse pointer on top of the JFrame you should

get a second JTextField named jTextField2 Position it to the right of jTextField1

Last Updated ( Tuesday 11 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5658

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5758

multiplication if a b and c were first declared as double or float or int variables So the code of the calculation should look like the

following

double a b c

a =

b =

c = a b

Clearly variables a and b need to be assigned with values of typed double Now looking back at inputboxes A and B we made

those boxes using the JTextField component meaning that any data or value written in a JTextField is of typed Text or String even

if the value displayed in the box is ldquo125rdquo But f irst here there is this task of getting to that Text (or String) values ldquo125rdquo and ldquo72rdquo

In Java we can get to a text in JTextField component using the getText() method So to retrieve values in jTextField1 and

jTextField2 we can use

jTextField1getText()

and

jTextField2getText()

But those values are still in text mode and we need another method to convert the data mode f rom text to double For that we can

use the method DoubleparseDouble() Thus the whole process of retrieve text -gt convert text to double -gt assign to double

variable can be done using

a = DoubleparseDouble( jTextField1getText() )

and

b = DoubleparseDouble( jTextField2getText() )

Hence the code for performing the multiplication becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

Up to this point we have the variable c as the result of the multiplication of a and b So the next thing to do is to display this

result on the JLabel that we have already prepared before

A x B = _____

The text A x B = ______ was made using JLabel component and its variable name is jLabel3 Since the variable c is of type

double and text property of JLabel is of type String we need to convert it first to String data type The method to convert a double

to String is DoubletoString() So we can write the code as DoubletoString(c) After getting the text we can now use it to be the

text displayed on jLabel3 To do that we use the method setText() Hence the appropriate code for displaying the result is

jLabel3setText( A x B = + DoubletoString(c) )

And the full code for our calculation becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

jLabel3setText( A x B = + DoubletoString(c) )

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5858

[ Back ]

Last Updated ( Tuesday 18 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

Page 32: Computional Physics

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3258

paste it and then do a few alterations First click on the ldquocalculaterdquo button right click choose Eventsand then choose Action then actionPerformed (Figure 6) It will bring you to where you should insertyour coding (Figure 7) You can write your own coding or copypaste any coding from somewhereelse (in this case from ldquoGravityAttract07ajavardquo)

Figure 6

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3358

Figure 7

Copy ldquoGravityAttract07ajavardquo source code and paste to the place after ldquo TODO add youhandling code hererdquo see Figure 7 To see which part has been modified the coding in Figure 7 hasbeen color coded for better understanding as follows

private void jButton1ActionPerformed(javaawteventActionEvent evt) TODO add your handling code here

double[] m = new double[6] mass of objectdouble[] x = new double[6] x-coor of objectdouble[] y = new double[6] y-coor of objectdouble f resultant forcedouble fxfy x amp y component of forcedouble rx ry rp co G dirfangdirfDecimalFormat dff dfangdirfint j mi

G = 667e-11m[1]=25 x[1]=12 y[1]=25m[2]=15 x[2]=15 y[2]=15m[3]=75 x[3]=02 y[3]=55m[4]=92 x[4]=52 y[4]=25m[5]=66 x[5]=42 y[5]=45

BufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin)) String mass= Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3458

catch(IOException e)Systemoutprintln(Not a valid input value) int mi = IntegerparseInt(mass)mi = IntegerparseInt(jTextField1getText())fx = 00fy = 00for(j=1 jlt=5 j++)

if (j=mi)rx = x[j]-x[mi]ry = y[j]-y[mi]rp = Mathpow((rxrx+ryry)15) (rx^2 + ry^2) raised to the power of 32co = Gm[mi]m[j]fx = fx + corxrpfy = fy + coryrp

f = Mathsqrt(fxfx + fyfy)

dirf = Mathatan(fyfx)angdirf = (dirf(22070))1800 Systemoutprintf(ldquoThe resultant force on particle [ldquo+ mi +rdquo] due to the other masses is

14e N 22f degnrdquofangdirf)dff = new javatextDecimalFormat(ldquoE00rdquo)dfangdirf = new DecimalFormat(ldquo000rdquo)

jTextField2setText(ldquordquo + mi) jTextField3setText(ldquordquo + dffformat(f)) jTextField4setText(ldquordquo + dfangdirfformat(angdirf))

Note

Brown = automatically generatedBlue = the original code from ldquoGravityAttract07ajavardquoGreen = coding from ldquoGravityAttract07ajavardquo which were being disabled due to

incompatibilities with NetbeansRed = a new inserted coding a few of them is to rephrasesubstitute the green one

Javascripts command line useBufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin))String mass=Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))try mass=dataInreadLine()catch(IOException e)Systemoutprintln(Not a valid input value)

int mi = IntegerparseInt(mass)

to read any input from the keyboard typed by the user while Netbeans uses egldquoIntegerparseInt(jTextField1getText())rdquo etc Javascripts command line uses ldquoSystemoutprintfrdquo to format the printed output whileNetbeans uses more complicated way to do the same job (see below)

Javascript command line

Systemoutprintf(The resultant force on particle [+ mi +] due to the other masses is 14e N22f degnfangdirf)

Netbeansdff = new javatextDecimalFormat(ldquo0E00rdquo)df di f D i lF t(ldquo0 00rdquo)

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3558

jTextField2setText(ldquordquo + mi) jTextField3setText(ldquordquo + dffformat(f)) jTextField4setText(ldquordquo + dfangdirfformat(angdirf))

ldquodffrdquo and ldquodfangdirfrdquo are new variables that should be declared and added to format thenumbers into decimal format ie ldquo0E00rdquo to get a scientific format eg ldquo20019E-10rdquo and ldquo000rdquoto get a decimal number eg ldquo-1570rdquo

To use DecimalFormat you have to put ldquoimport javatextDecimalFormatrdquo in the top of thecoding as shown in Figure 8

Figure 8

To reset the calculation you need to put coding into the ldquoresetrdquo button (this is ldquoJButton2rdquo)First click on the ldquoresetrdquo button right click choose Events and then choose Action thenactionPerformed (similar like in Figure 6) It will bring you to where you should insert your coding(Figure 9) Write the following coding (Figure 9)

Figure 9

To exit the program you need to put coding into the ldquoexitrdquo button (this is ldquoJButton3rdquo) Firstclick on the ldquoexitrdquo button right click choose Events and then choose Action then actionPerformed(similar like in Figure 6) It will bring you to where you should insert your coding (Figure 10) Writethe following coding (Figure 10)

Figure 10

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3658

Once you finished now you can run and see the result If everything is fine then the output wilbe look like this (Figure 11)

Figure 11

Remember when you compile and run ldquoGravityAttract07ajavardquo using Javascripts commandline once you compile and run the result was looked like this

On which particle you want to calculate the resulting force 2The resultant force on particle [2] due to the other masses is 30673e-10 N 8837 deg

SEE THE DIFFERENCES AT THE USER INTERFACE

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3758

8th Step

Written by Mohd Khalid Kasmin

Monday 27 August 2007

Programming using command line interface (CLI) has its advantages but todays computer users are so used to graphical user

interface (GUI) that we just can no longer be contented with writing application that do not use the GUI Hence starting from this 8thstep we are going to embark on learning how to make java application that utilises the the GUI

It is possible to write a GUI application with the text based tools that we have been using for these past 7 steps but the job is so

messy that most of you will definitely be turnoff by it So to make life slightly easier well use an integrated development

environment (IDE) known as NetBeans specifically NetBeans 551

0 First ssh to the our server Next once logged in just type nice -n15 netbeans amp on the terminal If you had sshed with the -Y

switch after a few tens of seconds NetBeans should emerge on your display screen

One important point to note Since our server can readily handle up to 100 users (maybe even

more) doing work in CLI mode this machine will certainly be taxed to its maximum capability once

we start using graphical based application NetBeans 551 uses a geat deal of computer memory

and with the 4Gbytes RAM server it can only accomodate up to 25 users concurrently using

NetBeans if the users use the application wisely What is meant by wisely here is that (a) when

starting the NetBeans application please use it together with the nice -n15 command (b) in using

the NetBeans please make sure you only have one project open at a time and (c) please close your

project everytime before quitting the NetBeans

Home Profile Teaching Links Contact Me My Work

Newsflash

Yesterday all servers in the US went out on strike in a bid to get more RAM and better CPUs A spokes person said that the

need for better RAM was due to some fool increasing the front-side bus speed In future busses will be told to slow down in

residential motherboards

search

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3858

1

2

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3958

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4058

[ Back ]

5

Last Updated ( Wednesday 31 October 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4158

8th Step (Cont)

Written by Mohd Khalid Kasmin

Monday 27 August 2007

Up to this point we have define the name of our project Next we construct a class and to follow what we already know we name the class the sam

project name latihan01

6

7

Home Profile Teaching Links Contact Me My Work

Newsflash

Joomla 10 - Experience the Freedom It has never been easier to create your own dynamic site Manage all your content from

the best CMS admin interface

search

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4258

8

9

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4358

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4458

8th Step (Cont still)

Written by Mohd Khalid Kasmin

Monday 27 August 2007

In item (9) we see that Netbeans does not provide us with the main method So to follow what we already know we now put in the

main method

10

Home Profile Teaching Links Contact Me My Work

Newsflash

Aoccdrnig to a rscheearch at an Elingsh uinervtisy it deosnt mttaer in waht oredr the ltteers in a wrod are the olny iprmoetnt

tihng is taht frist and lsat ltteer is at the rghit pclae The rset can be a toatl mses and you can sitll raed it wouthit porbelm Tihs is

bcuseae we do not raed ervey lteter by itslef but the wrod as a wlohe

search

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4558

11

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4658

12

13

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4758

14

15

Last Updated ( Monday 27 August 2007 )

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4858

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4958

9th Step

Thursday 06 September 2007

Step by Step into Java Programming

for

University Physics Student

By

Mohd Khalid Kasmin

A Building graphical user interface - preparing the JFrame Form

At this stage of our programming experience with Java and NetBeans please follow strictly the steps outline in this article First and

make a new GUI project make sure there is no other project that already been loaded by Netbeans Please close any project displayed

1 Create new project

- On the main menu click on File --gt New Project (note Close first any previous project)

- Specify project name amp project location - Untick Create Main Class item

- Click on the Finish button

Home Profile Teaching Links Contact Me My Work

Newsflash

Aoccdrnig to a rscheearch at an Elingsh uinervtisy it deosnt mttaer in waht oredr the ltteers in a wrod are the olny iprmoetnt

tihng is taht frist and lsat ltteer is at the rghit pclae The rset can be a toatl mses and you can sitll raed it wouthit porbelm Tihs is

bcuseae we do not raed ervey lteter by itslef but the wrod as a wlohe

search

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5058

Figure 91

2 Create UI class

- Right click on the project name in the Project pane

on the popup menu select New --gt JFrame Form

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5158

Figure 92

- In the New JFrame Form dialog specify the Class Name and the Package name

eg Class Name multiplierUI Package mymultiplier

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5258

Figure 93

After clicking on the Finish button we should get something like the following

Figure 94

Last Updated ( Tuesday 18 September 2007 )

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5358

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5458

9th Step (Cont)

Written by Mohd Khalid Kasmin

Tuesday 11 September 2007

Step by Step into Java Programming

for University Physics Student

By

Mohd Khalid Kasmin

B Building user interface (UI) - putting in the components

Now that we have a proper blank form we can start filling the form with the suitable graphical components For introduction to

GUI programming an appropriate application to build (for a physics student) is a simple solver So our first exercise is to build an

application that takes two numbers and multiply them and display the result

With GUI we usually give input values to an application via input boxes that are labelled When all inputs are typed in properlywe click on a button to ask the application to execute the job Hence in this exercise well do just that The target now is to

construct a user interface that looks like Figure 95 below

Figure 95

Home Profile Teaching Links Contact Me My Work

Newsflash

Joomla 10 - Experience the Freedom It has never been easier to create your own dynamic site Manage all your content from

the best CMS admin interface

search

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5558

[ Back ]

1 Problemapplication description box

- In the palette pane click on Swing component named JTextPane (Note click just once) Move the mouse pointer on top of

your JFrame (Note just move not drag) When you see a little white box with orange border position it somewhere near the

top part of your JFrame and click once The JTextPane is then attached on the JFrame Addjust its size with the help of the

sizing hook around the perimeter of the JTextPane

- Edit the text property of the JTextPane with the description of your application

2 Input boxes

- In the palette pane click on Swing component named JTextField Next move the mouse pointer on top of your JFrame

When you see a white box with the word jTextField1 position it to the left and under the JTextPane1

- Click on Swing component JTextField in the palette pane again Move the mouse pointer on top of the JFrame you should

get a second JTextField named jTextField2 Position it to the right of jTextField1

Last Updated ( Tuesday 11 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5658

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5758

multiplication if a b and c were first declared as double or float or int variables So the code of the calculation should look like the

following

double a b c

a =

b =

c = a b

Clearly variables a and b need to be assigned with values of typed double Now looking back at inputboxes A and B we made

those boxes using the JTextField component meaning that any data or value written in a JTextField is of typed Text or String even

if the value displayed in the box is ldquo125rdquo But f irst here there is this task of getting to that Text (or String) values ldquo125rdquo and ldquo72rdquo

In Java we can get to a text in JTextField component using the getText() method So to retrieve values in jTextField1 and

jTextField2 we can use

jTextField1getText()

and

jTextField2getText()

But those values are still in text mode and we need another method to convert the data mode f rom text to double For that we can

use the method DoubleparseDouble() Thus the whole process of retrieve text -gt convert text to double -gt assign to double

variable can be done using

a = DoubleparseDouble( jTextField1getText() )

and

b = DoubleparseDouble( jTextField2getText() )

Hence the code for performing the multiplication becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

Up to this point we have the variable c as the result of the multiplication of a and b So the next thing to do is to display this

result on the JLabel that we have already prepared before

A x B = _____

The text A x B = ______ was made using JLabel component and its variable name is jLabel3 Since the variable c is of type

double and text property of JLabel is of type String we need to convert it first to String data type The method to convert a double

to String is DoubletoString() So we can write the code as DoubletoString(c) After getting the text we can now use it to be the

text displayed on jLabel3 To do that we use the method setText() Hence the appropriate code for displaying the result is

jLabel3setText( A x B = + DoubletoString(c) )

And the full code for our calculation becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

jLabel3setText( A x B = + DoubletoString(c) )

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5858

[ Back ]

Last Updated ( Tuesday 18 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

Page 33: Computional Physics

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3358

Figure 7

Copy ldquoGravityAttract07ajavardquo source code and paste to the place after ldquo TODO add youhandling code hererdquo see Figure 7 To see which part has been modified the coding in Figure 7 hasbeen color coded for better understanding as follows

private void jButton1ActionPerformed(javaawteventActionEvent evt) TODO add your handling code here

double[] m = new double[6] mass of objectdouble[] x = new double[6] x-coor of objectdouble[] y = new double[6] y-coor of objectdouble f resultant forcedouble fxfy x amp y component of forcedouble rx ry rp co G dirfangdirfDecimalFormat dff dfangdirfint j mi

G = 667e-11m[1]=25 x[1]=12 y[1]=25m[2]=15 x[2]=15 y[2]=15m[3]=75 x[3]=02 y[3]=55m[4]=92 x[4]=52 y[4]=25m[5]=66 x[5]=42 y[5]=45

BufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin)) String mass= Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3458

catch(IOException e)Systemoutprintln(Not a valid input value) int mi = IntegerparseInt(mass)mi = IntegerparseInt(jTextField1getText())fx = 00fy = 00for(j=1 jlt=5 j++)

if (j=mi)rx = x[j]-x[mi]ry = y[j]-y[mi]rp = Mathpow((rxrx+ryry)15) (rx^2 + ry^2) raised to the power of 32co = Gm[mi]m[j]fx = fx + corxrpfy = fy + coryrp

f = Mathsqrt(fxfx + fyfy)

dirf = Mathatan(fyfx)angdirf = (dirf(22070))1800 Systemoutprintf(ldquoThe resultant force on particle [ldquo+ mi +rdquo] due to the other masses is

14e N 22f degnrdquofangdirf)dff = new javatextDecimalFormat(ldquoE00rdquo)dfangdirf = new DecimalFormat(ldquo000rdquo)

jTextField2setText(ldquordquo + mi) jTextField3setText(ldquordquo + dffformat(f)) jTextField4setText(ldquordquo + dfangdirfformat(angdirf))

Note

Brown = automatically generatedBlue = the original code from ldquoGravityAttract07ajavardquoGreen = coding from ldquoGravityAttract07ajavardquo which were being disabled due to

incompatibilities with NetbeansRed = a new inserted coding a few of them is to rephrasesubstitute the green one

Javascripts command line useBufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin))String mass=Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))try mass=dataInreadLine()catch(IOException e)Systemoutprintln(Not a valid input value)

int mi = IntegerparseInt(mass)

to read any input from the keyboard typed by the user while Netbeans uses egldquoIntegerparseInt(jTextField1getText())rdquo etc Javascripts command line uses ldquoSystemoutprintfrdquo to format the printed output whileNetbeans uses more complicated way to do the same job (see below)

Javascript command line

Systemoutprintf(The resultant force on particle [+ mi +] due to the other masses is 14e N22f degnfangdirf)

Netbeansdff = new javatextDecimalFormat(ldquo0E00rdquo)df di f D i lF t(ldquo0 00rdquo)

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3558

jTextField2setText(ldquordquo + mi) jTextField3setText(ldquordquo + dffformat(f)) jTextField4setText(ldquordquo + dfangdirfformat(angdirf))

ldquodffrdquo and ldquodfangdirfrdquo are new variables that should be declared and added to format thenumbers into decimal format ie ldquo0E00rdquo to get a scientific format eg ldquo20019E-10rdquo and ldquo000rdquoto get a decimal number eg ldquo-1570rdquo

To use DecimalFormat you have to put ldquoimport javatextDecimalFormatrdquo in the top of thecoding as shown in Figure 8

Figure 8

To reset the calculation you need to put coding into the ldquoresetrdquo button (this is ldquoJButton2rdquo)First click on the ldquoresetrdquo button right click choose Events and then choose Action thenactionPerformed (similar like in Figure 6) It will bring you to where you should insert your coding(Figure 9) Write the following coding (Figure 9)

Figure 9

To exit the program you need to put coding into the ldquoexitrdquo button (this is ldquoJButton3rdquo) Firstclick on the ldquoexitrdquo button right click choose Events and then choose Action then actionPerformed(similar like in Figure 6) It will bring you to where you should insert your coding (Figure 10) Writethe following coding (Figure 10)

Figure 10

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3658

Once you finished now you can run and see the result If everything is fine then the output wilbe look like this (Figure 11)

Figure 11

Remember when you compile and run ldquoGravityAttract07ajavardquo using Javascripts commandline once you compile and run the result was looked like this

On which particle you want to calculate the resulting force 2The resultant force on particle [2] due to the other masses is 30673e-10 N 8837 deg

SEE THE DIFFERENCES AT THE USER INTERFACE

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3758

8th Step

Written by Mohd Khalid Kasmin

Monday 27 August 2007

Programming using command line interface (CLI) has its advantages but todays computer users are so used to graphical user

interface (GUI) that we just can no longer be contented with writing application that do not use the GUI Hence starting from this 8thstep we are going to embark on learning how to make java application that utilises the the GUI

It is possible to write a GUI application with the text based tools that we have been using for these past 7 steps but the job is so

messy that most of you will definitely be turnoff by it So to make life slightly easier well use an integrated development

environment (IDE) known as NetBeans specifically NetBeans 551

0 First ssh to the our server Next once logged in just type nice -n15 netbeans amp on the terminal If you had sshed with the -Y

switch after a few tens of seconds NetBeans should emerge on your display screen

One important point to note Since our server can readily handle up to 100 users (maybe even

more) doing work in CLI mode this machine will certainly be taxed to its maximum capability once

we start using graphical based application NetBeans 551 uses a geat deal of computer memory

and with the 4Gbytes RAM server it can only accomodate up to 25 users concurrently using

NetBeans if the users use the application wisely What is meant by wisely here is that (a) when

starting the NetBeans application please use it together with the nice -n15 command (b) in using

the NetBeans please make sure you only have one project open at a time and (c) please close your

project everytime before quitting the NetBeans

Home Profile Teaching Links Contact Me My Work

Newsflash

Yesterday all servers in the US went out on strike in a bid to get more RAM and better CPUs A spokes person said that the

need for better RAM was due to some fool increasing the front-side bus speed In future busses will be told to slow down in

residential motherboards

search

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3858

1

2

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3958

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4058

[ Back ]

5

Last Updated ( Wednesday 31 October 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4158

8th Step (Cont)

Written by Mohd Khalid Kasmin

Monday 27 August 2007

Up to this point we have define the name of our project Next we construct a class and to follow what we already know we name the class the sam

project name latihan01

6

7

Home Profile Teaching Links Contact Me My Work

Newsflash

Joomla 10 - Experience the Freedom It has never been easier to create your own dynamic site Manage all your content from

the best CMS admin interface

search

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4258

8

9

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4358

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4458

8th Step (Cont still)

Written by Mohd Khalid Kasmin

Monday 27 August 2007

In item (9) we see that Netbeans does not provide us with the main method So to follow what we already know we now put in the

main method

10

Home Profile Teaching Links Contact Me My Work

Newsflash

Aoccdrnig to a rscheearch at an Elingsh uinervtisy it deosnt mttaer in waht oredr the ltteers in a wrod are the olny iprmoetnt

tihng is taht frist and lsat ltteer is at the rghit pclae The rset can be a toatl mses and you can sitll raed it wouthit porbelm Tihs is

bcuseae we do not raed ervey lteter by itslef but the wrod as a wlohe

search

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4558

11

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4658

12

13

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4758

14

15

Last Updated ( Monday 27 August 2007 )

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4858

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4958

9th Step

Thursday 06 September 2007

Step by Step into Java Programming

for

University Physics Student

By

Mohd Khalid Kasmin

A Building graphical user interface - preparing the JFrame Form

At this stage of our programming experience with Java and NetBeans please follow strictly the steps outline in this article First and

make a new GUI project make sure there is no other project that already been loaded by Netbeans Please close any project displayed

1 Create new project

- On the main menu click on File --gt New Project (note Close first any previous project)

- Specify project name amp project location - Untick Create Main Class item

- Click on the Finish button

Home Profile Teaching Links Contact Me My Work

Newsflash

Aoccdrnig to a rscheearch at an Elingsh uinervtisy it deosnt mttaer in waht oredr the ltteers in a wrod are the olny iprmoetnt

tihng is taht frist and lsat ltteer is at the rghit pclae The rset can be a toatl mses and you can sitll raed it wouthit porbelm Tihs is

bcuseae we do not raed ervey lteter by itslef but the wrod as a wlohe

search

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5058

Figure 91

2 Create UI class

- Right click on the project name in the Project pane

on the popup menu select New --gt JFrame Form

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5158

Figure 92

- In the New JFrame Form dialog specify the Class Name and the Package name

eg Class Name multiplierUI Package mymultiplier

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5258

Figure 93

After clicking on the Finish button we should get something like the following

Figure 94

Last Updated ( Tuesday 18 September 2007 )

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5358

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5458

9th Step (Cont)

Written by Mohd Khalid Kasmin

Tuesday 11 September 2007

Step by Step into Java Programming

for University Physics Student

By

Mohd Khalid Kasmin

B Building user interface (UI) - putting in the components

Now that we have a proper blank form we can start filling the form with the suitable graphical components For introduction to

GUI programming an appropriate application to build (for a physics student) is a simple solver So our first exercise is to build an

application that takes two numbers and multiply them and display the result

With GUI we usually give input values to an application via input boxes that are labelled When all inputs are typed in properlywe click on a button to ask the application to execute the job Hence in this exercise well do just that The target now is to

construct a user interface that looks like Figure 95 below

Figure 95

Home Profile Teaching Links Contact Me My Work

Newsflash

Joomla 10 - Experience the Freedom It has never been easier to create your own dynamic site Manage all your content from

the best CMS admin interface

search

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5558

[ Back ]

1 Problemapplication description box

- In the palette pane click on Swing component named JTextPane (Note click just once) Move the mouse pointer on top of

your JFrame (Note just move not drag) When you see a little white box with orange border position it somewhere near the

top part of your JFrame and click once The JTextPane is then attached on the JFrame Addjust its size with the help of the

sizing hook around the perimeter of the JTextPane

- Edit the text property of the JTextPane with the description of your application

2 Input boxes

- In the palette pane click on Swing component named JTextField Next move the mouse pointer on top of your JFrame

When you see a white box with the word jTextField1 position it to the left and under the JTextPane1

- Click on Swing component JTextField in the palette pane again Move the mouse pointer on top of the JFrame you should

get a second JTextField named jTextField2 Position it to the right of jTextField1

Last Updated ( Tuesday 11 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5658

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5758

multiplication if a b and c were first declared as double or float or int variables So the code of the calculation should look like the

following

double a b c

a =

b =

c = a b

Clearly variables a and b need to be assigned with values of typed double Now looking back at inputboxes A and B we made

those boxes using the JTextField component meaning that any data or value written in a JTextField is of typed Text or String even

if the value displayed in the box is ldquo125rdquo But f irst here there is this task of getting to that Text (or String) values ldquo125rdquo and ldquo72rdquo

In Java we can get to a text in JTextField component using the getText() method So to retrieve values in jTextField1 and

jTextField2 we can use

jTextField1getText()

and

jTextField2getText()

But those values are still in text mode and we need another method to convert the data mode f rom text to double For that we can

use the method DoubleparseDouble() Thus the whole process of retrieve text -gt convert text to double -gt assign to double

variable can be done using

a = DoubleparseDouble( jTextField1getText() )

and

b = DoubleparseDouble( jTextField2getText() )

Hence the code for performing the multiplication becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

Up to this point we have the variable c as the result of the multiplication of a and b So the next thing to do is to display this

result on the JLabel that we have already prepared before

A x B = _____

The text A x B = ______ was made using JLabel component and its variable name is jLabel3 Since the variable c is of type

double and text property of JLabel is of type String we need to convert it first to String data type The method to convert a double

to String is DoubletoString() So we can write the code as DoubletoString(c) After getting the text we can now use it to be the

text displayed on jLabel3 To do that we use the method setText() Hence the appropriate code for displaying the result is

jLabel3setText( A x B = + DoubletoString(c) )

And the full code for our calculation becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

jLabel3setText( A x B = + DoubletoString(c) )

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5858

[ Back ]

Last Updated ( Tuesday 18 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

Page 34: Computional Physics

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3458

catch(IOException e)Systemoutprintln(Not a valid input value) int mi = IntegerparseInt(mass)mi = IntegerparseInt(jTextField1getText())fx = 00fy = 00for(j=1 jlt=5 j++)

if (j=mi)rx = x[j]-x[mi]ry = y[j]-y[mi]rp = Mathpow((rxrx+ryry)15) (rx^2 + ry^2) raised to the power of 32co = Gm[mi]m[j]fx = fx + corxrpfy = fy + coryrp

f = Mathsqrt(fxfx + fyfy)

dirf = Mathatan(fyfx)angdirf = (dirf(22070))1800 Systemoutprintf(ldquoThe resultant force on particle [ldquo+ mi +rdquo] due to the other masses is

14e N 22f degnrdquofangdirf)dff = new javatextDecimalFormat(ldquoE00rdquo)dfangdirf = new DecimalFormat(ldquo000rdquo)

jTextField2setText(ldquordquo + mi) jTextField3setText(ldquordquo + dffformat(f)) jTextField4setText(ldquordquo + dfangdirfformat(angdirf))

Note

Brown = automatically generatedBlue = the original code from ldquoGravityAttract07ajavardquoGreen = coding from ldquoGravityAttract07ajavardquo which were being disabled due to

incompatibilities with NetbeansRed = a new inserted coding a few of them is to rephrasesubstitute the green one

Javascripts command line useBufferedReader dataIn=new BufferedReader(new InputStreamReader(Systemin))String mass=Systemoutprintln(On which mass you want to calculate the net force(1234 or 5))try mass=dataInreadLine()catch(IOException e)Systemoutprintln(Not a valid input value)

int mi = IntegerparseInt(mass)

to read any input from the keyboard typed by the user while Netbeans uses egldquoIntegerparseInt(jTextField1getText())rdquo etc Javascripts command line uses ldquoSystemoutprintfrdquo to format the printed output whileNetbeans uses more complicated way to do the same job (see below)

Javascript command line

Systemoutprintf(The resultant force on particle [+ mi +] due to the other masses is 14e N22f degnfangdirf)

Netbeansdff = new javatextDecimalFormat(ldquo0E00rdquo)df di f D i lF t(ldquo0 00rdquo)

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3558

jTextField2setText(ldquordquo + mi) jTextField3setText(ldquordquo + dffformat(f)) jTextField4setText(ldquordquo + dfangdirfformat(angdirf))

ldquodffrdquo and ldquodfangdirfrdquo are new variables that should be declared and added to format thenumbers into decimal format ie ldquo0E00rdquo to get a scientific format eg ldquo20019E-10rdquo and ldquo000rdquoto get a decimal number eg ldquo-1570rdquo

To use DecimalFormat you have to put ldquoimport javatextDecimalFormatrdquo in the top of thecoding as shown in Figure 8

Figure 8

To reset the calculation you need to put coding into the ldquoresetrdquo button (this is ldquoJButton2rdquo)First click on the ldquoresetrdquo button right click choose Events and then choose Action thenactionPerformed (similar like in Figure 6) It will bring you to where you should insert your coding(Figure 9) Write the following coding (Figure 9)

Figure 9

To exit the program you need to put coding into the ldquoexitrdquo button (this is ldquoJButton3rdquo) Firstclick on the ldquoexitrdquo button right click choose Events and then choose Action then actionPerformed(similar like in Figure 6) It will bring you to where you should insert your coding (Figure 10) Writethe following coding (Figure 10)

Figure 10

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3658

Once you finished now you can run and see the result If everything is fine then the output wilbe look like this (Figure 11)

Figure 11

Remember when you compile and run ldquoGravityAttract07ajavardquo using Javascripts commandline once you compile and run the result was looked like this

On which particle you want to calculate the resulting force 2The resultant force on particle [2] due to the other masses is 30673e-10 N 8837 deg

SEE THE DIFFERENCES AT THE USER INTERFACE

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3758

8th Step

Written by Mohd Khalid Kasmin

Monday 27 August 2007

Programming using command line interface (CLI) has its advantages but todays computer users are so used to graphical user

interface (GUI) that we just can no longer be contented with writing application that do not use the GUI Hence starting from this 8thstep we are going to embark on learning how to make java application that utilises the the GUI

It is possible to write a GUI application with the text based tools that we have been using for these past 7 steps but the job is so

messy that most of you will definitely be turnoff by it So to make life slightly easier well use an integrated development

environment (IDE) known as NetBeans specifically NetBeans 551

0 First ssh to the our server Next once logged in just type nice -n15 netbeans amp on the terminal If you had sshed with the -Y

switch after a few tens of seconds NetBeans should emerge on your display screen

One important point to note Since our server can readily handle up to 100 users (maybe even

more) doing work in CLI mode this machine will certainly be taxed to its maximum capability once

we start using graphical based application NetBeans 551 uses a geat deal of computer memory

and with the 4Gbytes RAM server it can only accomodate up to 25 users concurrently using

NetBeans if the users use the application wisely What is meant by wisely here is that (a) when

starting the NetBeans application please use it together with the nice -n15 command (b) in using

the NetBeans please make sure you only have one project open at a time and (c) please close your

project everytime before quitting the NetBeans

Home Profile Teaching Links Contact Me My Work

Newsflash

Yesterday all servers in the US went out on strike in a bid to get more RAM and better CPUs A spokes person said that the

need for better RAM was due to some fool increasing the front-side bus speed In future busses will be told to slow down in

residential motherboards

search

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3858

1

2

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3958

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4058

[ Back ]

5

Last Updated ( Wednesday 31 October 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4158

8th Step (Cont)

Written by Mohd Khalid Kasmin

Monday 27 August 2007

Up to this point we have define the name of our project Next we construct a class and to follow what we already know we name the class the sam

project name latihan01

6

7

Home Profile Teaching Links Contact Me My Work

Newsflash

Joomla 10 - Experience the Freedom It has never been easier to create your own dynamic site Manage all your content from

the best CMS admin interface

search

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4258

8

9

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4358

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4458

8th Step (Cont still)

Written by Mohd Khalid Kasmin

Monday 27 August 2007

In item (9) we see that Netbeans does not provide us with the main method So to follow what we already know we now put in the

main method

10

Home Profile Teaching Links Contact Me My Work

Newsflash

Aoccdrnig to a rscheearch at an Elingsh uinervtisy it deosnt mttaer in waht oredr the ltteers in a wrod are the olny iprmoetnt

tihng is taht frist and lsat ltteer is at the rghit pclae The rset can be a toatl mses and you can sitll raed it wouthit porbelm Tihs is

bcuseae we do not raed ervey lteter by itslef but the wrod as a wlohe

search

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4558

11

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4658

12

13

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4758

14

15

Last Updated ( Monday 27 August 2007 )

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4858

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4958

9th Step

Thursday 06 September 2007

Step by Step into Java Programming

for

University Physics Student

By

Mohd Khalid Kasmin

A Building graphical user interface - preparing the JFrame Form

At this stage of our programming experience with Java and NetBeans please follow strictly the steps outline in this article First and

make a new GUI project make sure there is no other project that already been loaded by Netbeans Please close any project displayed

1 Create new project

- On the main menu click on File --gt New Project (note Close first any previous project)

- Specify project name amp project location - Untick Create Main Class item

- Click on the Finish button

Home Profile Teaching Links Contact Me My Work

Newsflash

Aoccdrnig to a rscheearch at an Elingsh uinervtisy it deosnt mttaer in waht oredr the ltteers in a wrod are the olny iprmoetnt

tihng is taht frist and lsat ltteer is at the rghit pclae The rset can be a toatl mses and you can sitll raed it wouthit porbelm Tihs is

bcuseae we do not raed ervey lteter by itslef but the wrod as a wlohe

search

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5058

Figure 91

2 Create UI class

- Right click on the project name in the Project pane

on the popup menu select New --gt JFrame Form

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5158

Figure 92

- In the New JFrame Form dialog specify the Class Name and the Package name

eg Class Name multiplierUI Package mymultiplier

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5258

Figure 93

After clicking on the Finish button we should get something like the following

Figure 94

Last Updated ( Tuesday 18 September 2007 )

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5358

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5458

9th Step (Cont)

Written by Mohd Khalid Kasmin

Tuesday 11 September 2007

Step by Step into Java Programming

for University Physics Student

By

Mohd Khalid Kasmin

B Building user interface (UI) - putting in the components

Now that we have a proper blank form we can start filling the form with the suitable graphical components For introduction to

GUI programming an appropriate application to build (for a physics student) is a simple solver So our first exercise is to build an

application that takes two numbers and multiply them and display the result

With GUI we usually give input values to an application via input boxes that are labelled When all inputs are typed in properlywe click on a button to ask the application to execute the job Hence in this exercise well do just that The target now is to

construct a user interface that looks like Figure 95 below

Figure 95

Home Profile Teaching Links Contact Me My Work

Newsflash

Joomla 10 - Experience the Freedom It has never been easier to create your own dynamic site Manage all your content from

the best CMS admin interface

search

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5558

[ Back ]

1 Problemapplication description box

- In the palette pane click on Swing component named JTextPane (Note click just once) Move the mouse pointer on top of

your JFrame (Note just move not drag) When you see a little white box with orange border position it somewhere near the

top part of your JFrame and click once The JTextPane is then attached on the JFrame Addjust its size with the help of the

sizing hook around the perimeter of the JTextPane

- Edit the text property of the JTextPane with the description of your application

2 Input boxes

- In the palette pane click on Swing component named JTextField Next move the mouse pointer on top of your JFrame

When you see a white box with the word jTextField1 position it to the left and under the JTextPane1

- Click on Swing component JTextField in the palette pane again Move the mouse pointer on top of the JFrame you should

get a second JTextField named jTextField2 Position it to the right of jTextField1

Last Updated ( Tuesday 11 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5658

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5758

multiplication if a b and c were first declared as double or float or int variables So the code of the calculation should look like the

following

double a b c

a =

b =

c = a b

Clearly variables a and b need to be assigned with values of typed double Now looking back at inputboxes A and B we made

those boxes using the JTextField component meaning that any data or value written in a JTextField is of typed Text or String even

if the value displayed in the box is ldquo125rdquo But f irst here there is this task of getting to that Text (or String) values ldquo125rdquo and ldquo72rdquo

In Java we can get to a text in JTextField component using the getText() method So to retrieve values in jTextField1 and

jTextField2 we can use

jTextField1getText()

and

jTextField2getText()

But those values are still in text mode and we need another method to convert the data mode f rom text to double For that we can

use the method DoubleparseDouble() Thus the whole process of retrieve text -gt convert text to double -gt assign to double

variable can be done using

a = DoubleparseDouble( jTextField1getText() )

and

b = DoubleparseDouble( jTextField2getText() )

Hence the code for performing the multiplication becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

Up to this point we have the variable c as the result of the multiplication of a and b So the next thing to do is to display this

result on the JLabel that we have already prepared before

A x B = _____

The text A x B = ______ was made using JLabel component and its variable name is jLabel3 Since the variable c is of type

double and text property of JLabel is of type String we need to convert it first to String data type The method to convert a double

to String is DoubletoString() So we can write the code as DoubletoString(c) After getting the text we can now use it to be the

text displayed on jLabel3 To do that we use the method setText() Hence the appropriate code for displaying the result is

jLabel3setText( A x B = + DoubletoString(c) )

And the full code for our calculation becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

jLabel3setText( A x B = + DoubletoString(c) )

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5858

[ Back ]

Last Updated ( Tuesday 18 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

Page 35: Computional Physics

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3558

jTextField2setText(ldquordquo + mi) jTextField3setText(ldquordquo + dffformat(f)) jTextField4setText(ldquordquo + dfangdirfformat(angdirf))

ldquodffrdquo and ldquodfangdirfrdquo are new variables that should be declared and added to format thenumbers into decimal format ie ldquo0E00rdquo to get a scientific format eg ldquo20019E-10rdquo and ldquo000rdquoto get a decimal number eg ldquo-1570rdquo

To use DecimalFormat you have to put ldquoimport javatextDecimalFormatrdquo in the top of thecoding as shown in Figure 8

Figure 8

To reset the calculation you need to put coding into the ldquoresetrdquo button (this is ldquoJButton2rdquo)First click on the ldquoresetrdquo button right click choose Events and then choose Action thenactionPerformed (similar like in Figure 6) It will bring you to where you should insert your coding(Figure 9) Write the following coding (Figure 9)

Figure 9

To exit the program you need to put coding into the ldquoexitrdquo button (this is ldquoJButton3rdquo) Firstclick on the ldquoexitrdquo button right click choose Events and then choose Action then actionPerformed(similar like in Figure 6) It will bring you to where you should insert your coding (Figure 10) Writethe following coding (Figure 10)

Figure 10

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3658

Once you finished now you can run and see the result If everything is fine then the output wilbe look like this (Figure 11)

Figure 11

Remember when you compile and run ldquoGravityAttract07ajavardquo using Javascripts commandline once you compile and run the result was looked like this

On which particle you want to calculate the resulting force 2The resultant force on particle [2] due to the other masses is 30673e-10 N 8837 deg

SEE THE DIFFERENCES AT THE USER INTERFACE

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3758

8th Step

Written by Mohd Khalid Kasmin

Monday 27 August 2007

Programming using command line interface (CLI) has its advantages but todays computer users are so used to graphical user

interface (GUI) that we just can no longer be contented with writing application that do not use the GUI Hence starting from this 8thstep we are going to embark on learning how to make java application that utilises the the GUI

It is possible to write a GUI application with the text based tools that we have been using for these past 7 steps but the job is so

messy that most of you will definitely be turnoff by it So to make life slightly easier well use an integrated development

environment (IDE) known as NetBeans specifically NetBeans 551

0 First ssh to the our server Next once logged in just type nice -n15 netbeans amp on the terminal If you had sshed with the -Y

switch after a few tens of seconds NetBeans should emerge on your display screen

One important point to note Since our server can readily handle up to 100 users (maybe even

more) doing work in CLI mode this machine will certainly be taxed to its maximum capability once

we start using graphical based application NetBeans 551 uses a geat deal of computer memory

and with the 4Gbytes RAM server it can only accomodate up to 25 users concurrently using

NetBeans if the users use the application wisely What is meant by wisely here is that (a) when

starting the NetBeans application please use it together with the nice -n15 command (b) in using

the NetBeans please make sure you only have one project open at a time and (c) please close your

project everytime before quitting the NetBeans

Home Profile Teaching Links Contact Me My Work

Newsflash

Yesterday all servers in the US went out on strike in a bid to get more RAM and better CPUs A spokes person said that the

need for better RAM was due to some fool increasing the front-side bus speed In future busses will be told to slow down in

residential motherboards

search

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3858

1

2

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3958

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4058

[ Back ]

5

Last Updated ( Wednesday 31 October 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4158

8th Step (Cont)

Written by Mohd Khalid Kasmin

Monday 27 August 2007

Up to this point we have define the name of our project Next we construct a class and to follow what we already know we name the class the sam

project name latihan01

6

7

Home Profile Teaching Links Contact Me My Work

Newsflash

Joomla 10 - Experience the Freedom It has never been easier to create your own dynamic site Manage all your content from

the best CMS admin interface

search

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4258

8

9

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4358

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4458

8th Step (Cont still)

Written by Mohd Khalid Kasmin

Monday 27 August 2007

In item (9) we see that Netbeans does not provide us with the main method So to follow what we already know we now put in the

main method

10

Home Profile Teaching Links Contact Me My Work

Newsflash

Aoccdrnig to a rscheearch at an Elingsh uinervtisy it deosnt mttaer in waht oredr the ltteers in a wrod are the olny iprmoetnt

tihng is taht frist and lsat ltteer is at the rghit pclae The rset can be a toatl mses and you can sitll raed it wouthit porbelm Tihs is

bcuseae we do not raed ervey lteter by itslef but the wrod as a wlohe

search

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4558

11

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4658

12

13

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4758

14

15

Last Updated ( Monday 27 August 2007 )

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4858

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4958

9th Step

Thursday 06 September 2007

Step by Step into Java Programming

for

University Physics Student

By

Mohd Khalid Kasmin

A Building graphical user interface - preparing the JFrame Form

At this stage of our programming experience with Java and NetBeans please follow strictly the steps outline in this article First and

make a new GUI project make sure there is no other project that already been loaded by Netbeans Please close any project displayed

1 Create new project

- On the main menu click on File --gt New Project (note Close first any previous project)

- Specify project name amp project location - Untick Create Main Class item

- Click on the Finish button

Home Profile Teaching Links Contact Me My Work

Newsflash

Aoccdrnig to a rscheearch at an Elingsh uinervtisy it deosnt mttaer in waht oredr the ltteers in a wrod are the olny iprmoetnt

tihng is taht frist and lsat ltteer is at the rghit pclae The rset can be a toatl mses and you can sitll raed it wouthit porbelm Tihs is

bcuseae we do not raed ervey lteter by itslef but the wrod as a wlohe

search

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5058

Figure 91

2 Create UI class

- Right click on the project name in the Project pane

on the popup menu select New --gt JFrame Form

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5158

Figure 92

- In the New JFrame Form dialog specify the Class Name and the Package name

eg Class Name multiplierUI Package mymultiplier

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5258

Figure 93

After clicking on the Finish button we should get something like the following

Figure 94

Last Updated ( Tuesday 18 September 2007 )

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5358

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5458

9th Step (Cont)

Written by Mohd Khalid Kasmin

Tuesday 11 September 2007

Step by Step into Java Programming

for University Physics Student

By

Mohd Khalid Kasmin

B Building user interface (UI) - putting in the components

Now that we have a proper blank form we can start filling the form with the suitable graphical components For introduction to

GUI programming an appropriate application to build (for a physics student) is a simple solver So our first exercise is to build an

application that takes two numbers and multiply them and display the result

With GUI we usually give input values to an application via input boxes that are labelled When all inputs are typed in properlywe click on a button to ask the application to execute the job Hence in this exercise well do just that The target now is to

construct a user interface that looks like Figure 95 below

Figure 95

Home Profile Teaching Links Contact Me My Work

Newsflash

Joomla 10 - Experience the Freedom It has never been easier to create your own dynamic site Manage all your content from

the best CMS admin interface

search

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5558

[ Back ]

1 Problemapplication description box

- In the palette pane click on Swing component named JTextPane (Note click just once) Move the mouse pointer on top of

your JFrame (Note just move not drag) When you see a little white box with orange border position it somewhere near the

top part of your JFrame and click once The JTextPane is then attached on the JFrame Addjust its size with the help of the

sizing hook around the perimeter of the JTextPane

- Edit the text property of the JTextPane with the description of your application

2 Input boxes

- In the palette pane click on Swing component named JTextField Next move the mouse pointer on top of your JFrame

When you see a white box with the word jTextField1 position it to the left and under the JTextPane1

- Click on Swing component JTextField in the palette pane again Move the mouse pointer on top of the JFrame you should

get a second JTextField named jTextField2 Position it to the right of jTextField1

Last Updated ( Tuesday 11 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5658

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5758

multiplication if a b and c were first declared as double or float or int variables So the code of the calculation should look like the

following

double a b c

a =

b =

c = a b

Clearly variables a and b need to be assigned with values of typed double Now looking back at inputboxes A and B we made

those boxes using the JTextField component meaning that any data or value written in a JTextField is of typed Text or String even

if the value displayed in the box is ldquo125rdquo But f irst here there is this task of getting to that Text (or String) values ldquo125rdquo and ldquo72rdquo

In Java we can get to a text in JTextField component using the getText() method So to retrieve values in jTextField1 and

jTextField2 we can use

jTextField1getText()

and

jTextField2getText()

But those values are still in text mode and we need another method to convert the data mode f rom text to double For that we can

use the method DoubleparseDouble() Thus the whole process of retrieve text -gt convert text to double -gt assign to double

variable can be done using

a = DoubleparseDouble( jTextField1getText() )

and

b = DoubleparseDouble( jTextField2getText() )

Hence the code for performing the multiplication becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

Up to this point we have the variable c as the result of the multiplication of a and b So the next thing to do is to display this

result on the JLabel that we have already prepared before

A x B = _____

The text A x B = ______ was made using JLabel component and its variable name is jLabel3 Since the variable c is of type

double and text property of JLabel is of type String we need to convert it first to String data type The method to convert a double

to String is DoubletoString() So we can write the code as DoubletoString(c) After getting the text we can now use it to be the

text displayed on jLabel3 To do that we use the method setText() Hence the appropriate code for displaying the result is

jLabel3setText( A x B = + DoubletoString(c) )

And the full code for our calculation becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

jLabel3setText( A x B = + DoubletoString(c) )

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5858

[ Back ]

Last Updated ( Tuesday 18 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

Page 36: Computional Physics

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3658

Once you finished now you can run and see the result If everything is fine then the output wilbe look like this (Figure 11)

Figure 11

Remember when you compile and run ldquoGravityAttract07ajavardquo using Javascripts commandline once you compile and run the result was looked like this

On which particle you want to calculate the resulting force 2The resultant force on particle [2] due to the other masses is 30673e-10 N 8837 deg

SEE THE DIFFERENCES AT THE USER INTERFACE

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3758

8th Step

Written by Mohd Khalid Kasmin

Monday 27 August 2007

Programming using command line interface (CLI) has its advantages but todays computer users are so used to graphical user

interface (GUI) that we just can no longer be contented with writing application that do not use the GUI Hence starting from this 8thstep we are going to embark on learning how to make java application that utilises the the GUI

It is possible to write a GUI application with the text based tools that we have been using for these past 7 steps but the job is so

messy that most of you will definitely be turnoff by it So to make life slightly easier well use an integrated development

environment (IDE) known as NetBeans specifically NetBeans 551

0 First ssh to the our server Next once logged in just type nice -n15 netbeans amp on the terminal If you had sshed with the -Y

switch after a few tens of seconds NetBeans should emerge on your display screen

One important point to note Since our server can readily handle up to 100 users (maybe even

more) doing work in CLI mode this machine will certainly be taxed to its maximum capability once

we start using graphical based application NetBeans 551 uses a geat deal of computer memory

and with the 4Gbytes RAM server it can only accomodate up to 25 users concurrently using

NetBeans if the users use the application wisely What is meant by wisely here is that (a) when

starting the NetBeans application please use it together with the nice -n15 command (b) in using

the NetBeans please make sure you only have one project open at a time and (c) please close your

project everytime before quitting the NetBeans

Home Profile Teaching Links Contact Me My Work

Newsflash

Yesterday all servers in the US went out on strike in a bid to get more RAM and better CPUs A spokes person said that the

need for better RAM was due to some fool increasing the front-side bus speed In future busses will be told to slow down in

residential motherboards

search

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3858

1

2

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3958

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4058

[ Back ]

5

Last Updated ( Wednesday 31 October 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4158

8th Step (Cont)

Written by Mohd Khalid Kasmin

Monday 27 August 2007

Up to this point we have define the name of our project Next we construct a class and to follow what we already know we name the class the sam

project name latihan01

6

7

Home Profile Teaching Links Contact Me My Work

Newsflash

Joomla 10 - Experience the Freedom It has never been easier to create your own dynamic site Manage all your content from

the best CMS admin interface

search

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4258

8

9

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4358

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4458

8th Step (Cont still)

Written by Mohd Khalid Kasmin

Monday 27 August 2007

In item (9) we see that Netbeans does not provide us with the main method So to follow what we already know we now put in the

main method

10

Home Profile Teaching Links Contact Me My Work

Newsflash

Aoccdrnig to a rscheearch at an Elingsh uinervtisy it deosnt mttaer in waht oredr the ltteers in a wrod are the olny iprmoetnt

tihng is taht frist and lsat ltteer is at the rghit pclae The rset can be a toatl mses and you can sitll raed it wouthit porbelm Tihs is

bcuseae we do not raed ervey lteter by itslef but the wrod as a wlohe

search

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4558

11

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4658

12

13

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4758

14

15

Last Updated ( Monday 27 August 2007 )

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4858

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4958

9th Step

Thursday 06 September 2007

Step by Step into Java Programming

for

University Physics Student

By

Mohd Khalid Kasmin

A Building graphical user interface - preparing the JFrame Form

At this stage of our programming experience with Java and NetBeans please follow strictly the steps outline in this article First and

make a new GUI project make sure there is no other project that already been loaded by Netbeans Please close any project displayed

1 Create new project

- On the main menu click on File --gt New Project (note Close first any previous project)

- Specify project name amp project location - Untick Create Main Class item

- Click on the Finish button

Home Profile Teaching Links Contact Me My Work

Newsflash

Aoccdrnig to a rscheearch at an Elingsh uinervtisy it deosnt mttaer in waht oredr the ltteers in a wrod are the olny iprmoetnt

tihng is taht frist and lsat ltteer is at the rghit pclae The rset can be a toatl mses and you can sitll raed it wouthit porbelm Tihs is

bcuseae we do not raed ervey lteter by itslef but the wrod as a wlohe

search

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5058

Figure 91

2 Create UI class

- Right click on the project name in the Project pane

on the popup menu select New --gt JFrame Form

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5158

Figure 92

- In the New JFrame Form dialog specify the Class Name and the Package name

eg Class Name multiplierUI Package mymultiplier

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5258

Figure 93

After clicking on the Finish button we should get something like the following

Figure 94

Last Updated ( Tuesday 18 September 2007 )

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5358

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5458

9th Step (Cont)

Written by Mohd Khalid Kasmin

Tuesday 11 September 2007

Step by Step into Java Programming

for University Physics Student

By

Mohd Khalid Kasmin

B Building user interface (UI) - putting in the components

Now that we have a proper blank form we can start filling the form with the suitable graphical components For introduction to

GUI programming an appropriate application to build (for a physics student) is a simple solver So our first exercise is to build an

application that takes two numbers and multiply them and display the result

With GUI we usually give input values to an application via input boxes that are labelled When all inputs are typed in properlywe click on a button to ask the application to execute the job Hence in this exercise well do just that The target now is to

construct a user interface that looks like Figure 95 below

Figure 95

Home Profile Teaching Links Contact Me My Work

Newsflash

Joomla 10 - Experience the Freedom It has never been easier to create your own dynamic site Manage all your content from

the best CMS admin interface

search

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5558

[ Back ]

1 Problemapplication description box

- In the palette pane click on Swing component named JTextPane (Note click just once) Move the mouse pointer on top of

your JFrame (Note just move not drag) When you see a little white box with orange border position it somewhere near the

top part of your JFrame and click once The JTextPane is then attached on the JFrame Addjust its size with the help of the

sizing hook around the perimeter of the JTextPane

- Edit the text property of the JTextPane with the description of your application

2 Input boxes

- In the palette pane click on Swing component named JTextField Next move the mouse pointer on top of your JFrame

When you see a white box with the word jTextField1 position it to the left and under the JTextPane1

- Click on Swing component JTextField in the palette pane again Move the mouse pointer on top of the JFrame you should

get a second JTextField named jTextField2 Position it to the right of jTextField1

Last Updated ( Tuesday 11 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5658

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5758

multiplication if a b and c were first declared as double or float or int variables So the code of the calculation should look like the

following

double a b c

a =

b =

c = a b

Clearly variables a and b need to be assigned with values of typed double Now looking back at inputboxes A and B we made

those boxes using the JTextField component meaning that any data or value written in a JTextField is of typed Text or String even

if the value displayed in the box is ldquo125rdquo But f irst here there is this task of getting to that Text (or String) values ldquo125rdquo and ldquo72rdquo

In Java we can get to a text in JTextField component using the getText() method So to retrieve values in jTextField1 and

jTextField2 we can use

jTextField1getText()

and

jTextField2getText()

But those values are still in text mode and we need another method to convert the data mode f rom text to double For that we can

use the method DoubleparseDouble() Thus the whole process of retrieve text -gt convert text to double -gt assign to double

variable can be done using

a = DoubleparseDouble( jTextField1getText() )

and

b = DoubleparseDouble( jTextField2getText() )

Hence the code for performing the multiplication becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

Up to this point we have the variable c as the result of the multiplication of a and b So the next thing to do is to display this

result on the JLabel that we have already prepared before

A x B = _____

The text A x B = ______ was made using JLabel component and its variable name is jLabel3 Since the variable c is of type

double and text property of JLabel is of type String we need to convert it first to String data type The method to convert a double

to String is DoubletoString() So we can write the code as DoubletoString(c) After getting the text we can now use it to be the

text displayed on jLabel3 To do that we use the method setText() Hence the appropriate code for displaying the result is

jLabel3setText( A x B = + DoubletoString(c) )

And the full code for our calculation becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

jLabel3setText( A x B = + DoubletoString(c) )

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5858

[ Back ]

Last Updated ( Tuesday 18 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

Page 37: Computional Physics

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3758

8th Step

Written by Mohd Khalid Kasmin

Monday 27 August 2007

Programming using command line interface (CLI) has its advantages but todays computer users are so used to graphical user

interface (GUI) that we just can no longer be contented with writing application that do not use the GUI Hence starting from this 8thstep we are going to embark on learning how to make java application that utilises the the GUI

It is possible to write a GUI application with the text based tools that we have been using for these past 7 steps but the job is so

messy that most of you will definitely be turnoff by it So to make life slightly easier well use an integrated development

environment (IDE) known as NetBeans specifically NetBeans 551

0 First ssh to the our server Next once logged in just type nice -n15 netbeans amp on the terminal If you had sshed with the -Y

switch after a few tens of seconds NetBeans should emerge on your display screen

One important point to note Since our server can readily handle up to 100 users (maybe even

more) doing work in CLI mode this machine will certainly be taxed to its maximum capability once

we start using graphical based application NetBeans 551 uses a geat deal of computer memory

and with the 4Gbytes RAM server it can only accomodate up to 25 users concurrently using

NetBeans if the users use the application wisely What is meant by wisely here is that (a) when

starting the NetBeans application please use it together with the nice -n15 command (b) in using

the NetBeans please make sure you only have one project open at a time and (c) please close your

project everytime before quitting the NetBeans

Home Profile Teaching Links Contact Me My Work

Newsflash

Yesterday all servers in the US went out on strike in a bid to get more RAM and better CPUs A spokes person said that the

need for better RAM was due to some fool increasing the front-side bus speed In future busses will be told to slow down in

residential motherboards

search

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3858

1

2

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3958

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4058

[ Back ]

5

Last Updated ( Wednesday 31 October 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4158

8th Step (Cont)

Written by Mohd Khalid Kasmin

Monday 27 August 2007

Up to this point we have define the name of our project Next we construct a class and to follow what we already know we name the class the sam

project name latihan01

6

7

Home Profile Teaching Links Contact Me My Work

Newsflash

Joomla 10 - Experience the Freedom It has never been easier to create your own dynamic site Manage all your content from

the best CMS admin interface

search

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4258

8

9

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4358

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4458

8th Step (Cont still)

Written by Mohd Khalid Kasmin

Monday 27 August 2007

In item (9) we see that Netbeans does not provide us with the main method So to follow what we already know we now put in the

main method

10

Home Profile Teaching Links Contact Me My Work

Newsflash

Aoccdrnig to a rscheearch at an Elingsh uinervtisy it deosnt mttaer in waht oredr the ltteers in a wrod are the olny iprmoetnt

tihng is taht frist and lsat ltteer is at the rghit pclae The rset can be a toatl mses and you can sitll raed it wouthit porbelm Tihs is

bcuseae we do not raed ervey lteter by itslef but the wrod as a wlohe

search

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4558

11

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4658

12

13

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4758

14

15

Last Updated ( Monday 27 August 2007 )

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4858

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4958

9th Step

Thursday 06 September 2007

Step by Step into Java Programming

for

University Physics Student

By

Mohd Khalid Kasmin

A Building graphical user interface - preparing the JFrame Form

At this stage of our programming experience with Java and NetBeans please follow strictly the steps outline in this article First and

make a new GUI project make sure there is no other project that already been loaded by Netbeans Please close any project displayed

1 Create new project

- On the main menu click on File --gt New Project (note Close first any previous project)

- Specify project name amp project location - Untick Create Main Class item

- Click on the Finish button

Home Profile Teaching Links Contact Me My Work

Newsflash

Aoccdrnig to a rscheearch at an Elingsh uinervtisy it deosnt mttaer in waht oredr the ltteers in a wrod are the olny iprmoetnt

tihng is taht frist and lsat ltteer is at the rghit pclae The rset can be a toatl mses and you can sitll raed it wouthit porbelm Tihs is

bcuseae we do not raed ervey lteter by itslef but the wrod as a wlohe

search

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5058

Figure 91

2 Create UI class

- Right click on the project name in the Project pane

on the popup menu select New --gt JFrame Form

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5158

Figure 92

- In the New JFrame Form dialog specify the Class Name and the Package name

eg Class Name multiplierUI Package mymultiplier

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5258

Figure 93

After clicking on the Finish button we should get something like the following

Figure 94

Last Updated ( Tuesday 18 September 2007 )

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5358

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5458

9th Step (Cont)

Written by Mohd Khalid Kasmin

Tuesday 11 September 2007

Step by Step into Java Programming

for University Physics Student

By

Mohd Khalid Kasmin

B Building user interface (UI) - putting in the components

Now that we have a proper blank form we can start filling the form with the suitable graphical components For introduction to

GUI programming an appropriate application to build (for a physics student) is a simple solver So our first exercise is to build an

application that takes two numbers and multiply them and display the result

With GUI we usually give input values to an application via input boxes that are labelled When all inputs are typed in properlywe click on a button to ask the application to execute the job Hence in this exercise well do just that The target now is to

construct a user interface that looks like Figure 95 below

Figure 95

Home Profile Teaching Links Contact Me My Work

Newsflash

Joomla 10 - Experience the Freedom It has never been easier to create your own dynamic site Manage all your content from

the best CMS admin interface

search

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5558

[ Back ]

1 Problemapplication description box

- In the palette pane click on Swing component named JTextPane (Note click just once) Move the mouse pointer on top of

your JFrame (Note just move not drag) When you see a little white box with orange border position it somewhere near the

top part of your JFrame and click once The JTextPane is then attached on the JFrame Addjust its size with the help of the

sizing hook around the perimeter of the JTextPane

- Edit the text property of the JTextPane with the description of your application

2 Input boxes

- In the palette pane click on Swing component named JTextField Next move the mouse pointer on top of your JFrame

When you see a white box with the word jTextField1 position it to the left and under the JTextPane1

- Click on Swing component JTextField in the palette pane again Move the mouse pointer on top of the JFrame you should

get a second JTextField named jTextField2 Position it to the right of jTextField1

Last Updated ( Tuesday 11 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5658

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5758

multiplication if a b and c were first declared as double or float or int variables So the code of the calculation should look like the

following

double a b c

a =

b =

c = a b

Clearly variables a and b need to be assigned with values of typed double Now looking back at inputboxes A and B we made

those boxes using the JTextField component meaning that any data or value written in a JTextField is of typed Text or String even

if the value displayed in the box is ldquo125rdquo But f irst here there is this task of getting to that Text (or String) values ldquo125rdquo and ldquo72rdquo

In Java we can get to a text in JTextField component using the getText() method So to retrieve values in jTextField1 and

jTextField2 we can use

jTextField1getText()

and

jTextField2getText()

But those values are still in text mode and we need another method to convert the data mode f rom text to double For that we can

use the method DoubleparseDouble() Thus the whole process of retrieve text -gt convert text to double -gt assign to double

variable can be done using

a = DoubleparseDouble( jTextField1getText() )

and

b = DoubleparseDouble( jTextField2getText() )

Hence the code for performing the multiplication becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

Up to this point we have the variable c as the result of the multiplication of a and b So the next thing to do is to display this

result on the JLabel that we have already prepared before

A x B = _____

The text A x B = ______ was made using JLabel component and its variable name is jLabel3 Since the variable c is of type

double and text property of JLabel is of type String we need to convert it first to String data type The method to convert a double

to String is DoubletoString() So we can write the code as DoubletoString(c) After getting the text we can now use it to be the

text displayed on jLabel3 To do that we use the method setText() Hence the appropriate code for displaying the result is

jLabel3setText( A x B = + DoubletoString(c) )

And the full code for our calculation becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

jLabel3setText( A x B = + DoubletoString(c) )

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5858

[ Back ]

Last Updated ( Tuesday 18 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

Page 38: Computional Physics

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3858

1

2

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3958

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4058

[ Back ]

5

Last Updated ( Wednesday 31 October 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4158

8th Step (Cont)

Written by Mohd Khalid Kasmin

Monday 27 August 2007

Up to this point we have define the name of our project Next we construct a class and to follow what we already know we name the class the sam

project name latihan01

6

7

Home Profile Teaching Links Contact Me My Work

Newsflash

Joomla 10 - Experience the Freedom It has never been easier to create your own dynamic site Manage all your content from

the best CMS admin interface

search

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4258

8

9

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4358

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4458

8th Step (Cont still)

Written by Mohd Khalid Kasmin

Monday 27 August 2007

In item (9) we see that Netbeans does not provide us with the main method So to follow what we already know we now put in the

main method

10

Home Profile Teaching Links Contact Me My Work

Newsflash

Aoccdrnig to a rscheearch at an Elingsh uinervtisy it deosnt mttaer in waht oredr the ltteers in a wrod are the olny iprmoetnt

tihng is taht frist and lsat ltteer is at the rghit pclae The rset can be a toatl mses and you can sitll raed it wouthit porbelm Tihs is

bcuseae we do not raed ervey lteter by itslef but the wrod as a wlohe

search

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4558

11

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4658

12

13

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4758

14

15

Last Updated ( Monday 27 August 2007 )

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4858

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4958

9th Step

Thursday 06 September 2007

Step by Step into Java Programming

for

University Physics Student

By

Mohd Khalid Kasmin

A Building graphical user interface - preparing the JFrame Form

At this stage of our programming experience with Java and NetBeans please follow strictly the steps outline in this article First and

make a new GUI project make sure there is no other project that already been loaded by Netbeans Please close any project displayed

1 Create new project

- On the main menu click on File --gt New Project (note Close first any previous project)

- Specify project name amp project location - Untick Create Main Class item

- Click on the Finish button

Home Profile Teaching Links Contact Me My Work

Newsflash

Aoccdrnig to a rscheearch at an Elingsh uinervtisy it deosnt mttaer in waht oredr the ltteers in a wrod are the olny iprmoetnt

tihng is taht frist and lsat ltteer is at the rghit pclae The rset can be a toatl mses and you can sitll raed it wouthit porbelm Tihs is

bcuseae we do not raed ervey lteter by itslef but the wrod as a wlohe

search

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5058

Figure 91

2 Create UI class

- Right click on the project name in the Project pane

on the popup menu select New --gt JFrame Form

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5158

Figure 92

- In the New JFrame Form dialog specify the Class Name and the Package name

eg Class Name multiplierUI Package mymultiplier

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5258

Figure 93

After clicking on the Finish button we should get something like the following

Figure 94

Last Updated ( Tuesday 18 September 2007 )

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5358

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5458

9th Step (Cont)

Written by Mohd Khalid Kasmin

Tuesday 11 September 2007

Step by Step into Java Programming

for University Physics Student

By

Mohd Khalid Kasmin

B Building user interface (UI) - putting in the components

Now that we have a proper blank form we can start filling the form with the suitable graphical components For introduction to

GUI programming an appropriate application to build (for a physics student) is a simple solver So our first exercise is to build an

application that takes two numbers and multiply them and display the result

With GUI we usually give input values to an application via input boxes that are labelled When all inputs are typed in properlywe click on a button to ask the application to execute the job Hence in this exercise well do just that The target now is to

construct a user interface that looks like Figure 95 below

Figure 95

Home Profile Teaching Links Contact Me My Work

Newsflash

Joomla 10 - Experience the Freedom It has never been easier to create your own dynamic site Manage all your content from

the best CMS admin interface

search

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5558

[ Back ]

1 Problemapplication description box

- In the palette pane click on Swing component named JTextPane (Note click just once) Move the mouse pointer on top of

your JFrame (Note just move not drag) When you see a little white box with orange border position it somewhere near the

top part of your JFrame and click once The JTextPane is then attached on the JFrame Addjust its size with the help of the

sizing hook around the perimeter of the JTextPane

- Edit the text property of the JTextPane with the description of your application

2 Input boxes

- In the palette pane click on Swing component named JTextField Next move the mouse pointer on top of your JFrame

When you see a white box with the word jTextField1 position it to the left and under the JTextPane1

- Click on Swing component JTextField in the palette pane again Move the mouse pointer on top of the JFrame you should

get a second JTextField named jTextField2 Position it to the right of jTextField1

Last Updated ( Tuesday 11 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5658

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5758

multiplication if a b and c were first declared as double or float or int variables So the code of the calculation should look like the

following

double a b c

a =

b =

c = a b

Clearly variables a and b need to be assigned with values of typed double Now looking back at inputboxes A and B we made

those boxes using the JTextField component meaning that any data or value written in a JTextField is of typed Text or String even

if the value displayed in the box is ldquo125rdquo But f irst here there is this task of getting to that Text (or String) values ldquo125rdquo and ldquo72rdquo

In Java we can get to a text in JTextField component using the getText() method So to retrieve values in jTextField1 and

jTextField2 we can use

jTextField1getText()

and

jTextField2getText()

But those values are still in text mode and we need another method to convert the data mode f rom text to double For that we can

use the method DoubleparseDouble() Thus the whole process of retrieve text -gt convert text to double -gt assign to double

variable can be done using

a = DoubleparseDouble( jTextField1getText() )

and

b = DoubleparseDouble( jTextField2getText() )

Hence the code for performing the multiplication becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

Up to this point we have the variable c as the result of the multiplication of a and b So the next thing to do is to display this

result on the JLabel that we have already prepared before

A x B = _____

The text A x B = ______ was made using JLabel component and its variable name is jLabel3 Since the variable c is of type

double and text property of JLabel is of type String we need to convert it first to String data type The method to convert a double

to String is DoubletoString() So we can write the code as DoubletoString(c) After getting the text we can now use it to be the

text displayed on jLabel3 To do that we use the method setText() Hence the appropriate code for displaying the result is

jLabel3setText( A x B = + DoubletoString(c) )

And the full code for our calculation becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

jLabel3setText( A x B = + DoubletoString(c) )

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5858

[ Back ]

Last Updated ( Tuesday 18 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

Page 39: Computional Physics

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 3958

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4058

[ Back ]

5

Last Updated ( Wednesday 31 October 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4158

8th Step (Cont)

Written by Mohd Khalid Kasmin

Monday 27 August 2007

Up to this point we have define the name of our project Next we construct a class and to follow what we already know we name the class the sam

project name latihan01

6

7

Home Profile Teaching Links Contact Me My Work

Newsflash

Joomla 10 - Experience the Freedom It has never been easier to create your own dynamic site Manage all your content from

the best CMS admin interface

search

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4258

8

9

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4358

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4458

8th Step (Cont still)

Written by Mohd Khalid Kasmin

Monday 27 August 2007

In item (9) we see that Netbeans does not provide us with the main method So to follow what we already know we now put in the

main method

10

Home Profile Teaching Links Contact Me My Work

Newsflash

Aoccdrnig to a rscheearch at an Elingsh uinervtisy it deosnt mttaer in waht oredr the ltteers in a wrod are the olny iprmoetnt

tihng is taht frist and lsat ltteer is at the rghit pclae The rset can be a toatl mses and you can sitll raed it wouthit porbelm Tihs is

bcuseae we do not raed ervey lteter by itslef but the wrod as a wlohe

search

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4558

11

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4658

12

13

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4758

14

15

Last Updated ( Monday 27 August 2007 )

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4858

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4958

9th Step

Thursday 06 September 2007

Step by Step into Java Programming

for

University Physics Student

By

Mohd Khalid Kasmin

A Building graphical user interface - preparing the JFrame Form

At this stage of our programming experience with Java and NetBeans please follow strictly the steps outline in this article First and

make a new GUI project make sure there is no other project that already been loaded by Netbeans Please close any project displayed

1 Create new project

- On the main menu click on File --gt New Project (note Close first any previous project)

- Specify project name amp project location - Untick Create Main Class item

- Click on the Finish button

Home Profile Teaching Links Contact Me My Work

Newsflash

Aoccdrnig to a rscheearch at an Elingsh uinervtisy it deosnt mttaer in waht oredr the ltteers in a wrod are the olny iprmoetnt

tihng is taht frist and lsat ltteer is at the rghit pclae The rset can be a toatl mses and you can sitll raed it wouthit porbelm Tihs is

bcuseae we do not raed ervey lteter by itslef but the wrod as a wlohe

search

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5058

Figure 91

2 Create UI class

- Right click on the project name in the Project pane

on the popup menu select New --gt JFrame Form

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5158

Figure 92

- In the New JFrame Form dialog specify the Class Name and the Package name

eg Class Name multiplierUI Package mymultiplier

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5258

Figure 93

After clicking on the Finish button we should get something like the following

Figure 94

Last Updated ( Tuesday 18 September 2007 )

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5358

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5458

9th Step (Cont)

Written by Mohd Khalid Kasmin

Tuesday 11 September 2007

Step by Step into Java Programming

for University Physics Student

By

Mohd Khalid Kasmin

B Building user interface (UI) - putting in the components

Now that we have a proper blank form we can start filling the form with the suitable graphical components For introduction to

GUI programming an appropriate application to build (for a physics student) is a simple solver So our first exercise is to build an

application that takes two numbers and multiply them and display the result

With GUI we usually give input values to an application via input boxes that are labelled When all inputs are typed in properlywe click on a button to ask the application to execute the job Hence in this exercise well do just that The target now is to

construct a user interface that looks like Figure 95 below

Figure 95

Home Profile Teaching Links Contact Me My Work

Newsflash

Joomla 10 - Experience the Freedom It has never been easier to create your own dynamic site Manage all your content from

the best CMS admin interface

search

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5558

[ Back ]

1 Problemapplication description box

- In the palette pane click on Swing component named JTextPane (Note click just once) Move the mouse pointer on top of

your JFrame (Note just move not drag) When you see a little white box with orange border position it somewhere near the

top part of your JFrame and click once The JTextPane is then attached on the JFrame Addjust its size with the help of the

sizing hook around the perimeter of the JTextPane

- Edit the text property of the JTextPane with the description of your application

2 Input boxes

- In the palette pane click on Swing component named JTextField Next move the mouse pointer on top of your JFrame

When you see a white box with the word jTextField1 position it to the left and under the JTextPane1

- Click on Swing component JTextField in the palette pane again Move the mouse pointer on top of the JFrame you should

get a second JTextField named jTextField2 Position it to the right of jTextField1

Last Updated ( Tuesday 11 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5658

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5758

multiplication if a b and c were first declared as double or float or int variables So the code of the calculation should look like the

following

double a b c

a =

b =

c = a b

Clearly variables a and b need to be assigned with values of typed double Now looking back at inputboxes A and B we made

those boxes using the JTextField component meaning that any data or value written in a JTextField is of typed Text or String even

if the value displayed in the box is ldquo125rdquo But f irst here there is this task of getting to that Text (or String) values ldquo125rdquo and ldquo72rdquo

In Java we can get to a text in JTextField component using the getText() method So to retrieve values in jTextField1 and

jTextField2 we can use

jTextField1getText()

and

jTextField2getText()

But those values are still in text mode and we need another method to convert the data mode f rom text to double For that we can

use the method DoubleparseDouble() Thus the whole process of retrieve text -gt convert text to double -gt assign to double

variable can be done using

a = DoubleparseDouble( jTextField1getText() )

and

b = DoubleparseDouble( jTextField2getText() )

Hence the code for performing the multiplication becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

Up to this point we have the variable c as the result of the multiplication of a and b So the next thing to do is to display this

result on the JLabel that we have already prepared before

A x B = _____

The text A x B = ______ was made using JLabel component and its variable name is jLabel3 Since the variable c is of type

double and text property of JLabel is of type String we need to convert it first to String data type The method to convert a double

to String is DoubletoString() So we can write the code as DoubletoString(c) After getting the text we can now use it to be the

text displayed on jLabel3 To do that we use the method setText() Hence the appropriate code for displaying the result is

jLabel3setText( A x B = + DoubletoString(c) )

And the full code for our calculation becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

jLabel3setText( A x B = + DoubletoString(c) )

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5858

[ Back ]

Last Updated ( Tuesday 18 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

Page 40: Computional Physics

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4058

[ Back ]

5

Last Updated ( Wednesday 31 October 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4158

8th Step (Cont)

Written by Mohd Khalid Kasmin

Monday 27 August 2007

Up to this point we have define the name of our project Next we construct a class and to follow what we already know we name the class the sam

project name latihan01

6

7

Home Profile Teaching Links Contact Me My Work

Newsflash

Joomla 10 - Experience the Freedom It has never been easier to create your own dynamic site Manage all your content from

the best CMS admin interface

search

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4258

8

9

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4358

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4458

8th Step (Cont still)

Written by Mohd Khalid Kasmin

Monday 27 August 2007

In item (9) we see that Netbeans does not provide us with the main method So to follow what we already know we now put in the

main method

10

Home Profile Teaching Links Contact Me My Work

Newsflash

Aoccdrnig to a rscheearch at an Elingsh uinervtisy it deosnt mttaer in waht oredr the ltteers in a wrod are the olny iprmoetnt

tihng is taht frist and lsat ltteer is at the rghit pclae The rset can be a toatl mses and you can sitll raed it wouthit porbelm Tihs is

bcuseae we do not raed ervey lteter by itslef but the wrod as a wlohe

search

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4558

11

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4658

12

13

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4758

14

15

Last Updated ( Monday 27 August 2007 )

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4858

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4958

9th Step

Thursday 06 September 2007

Step by Step into Java Programming

for

University Physics Student

By

Mohd Khalid Kasmin

A Building graphical user interface - preparing the JFrame Form

At this stage of our programming experience with Java and NetBeans please follow strictly the steps outline in this article First and

make a new GUI project make sure there is no other project that already been loaded by Netbeans Please close any project displayed

1 Create new project

- On the main menu click on File --gt New Project (note Close first any previous project)

- Specify project name amp project location - Untick Create Main Class item

- Click on the Finish button

Home Profile Teaching Links Contact Me My Work

Newsflash

Aoccdrnig to a rscheearch at an Elingsh uinervtisy it deosnt mttaer in waht oredr the ltteers in a wrod are the olny iprmoetnt

tihng is taht frist and lsat ltteer is at the rghit pclae The rset can be a toatl mses and you can sitll raed it wouthit porbelm Tihs is

bcuseae we do not raed ervey lteter by itslef but the wrod as a wlohe

search

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5058

Figure 91

2 Create UI class

- Right click on the project name in the Project pane

on the popup menu select New --gt JFrame Form

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5158

Figure 92

- In the New JFrame Form dialog specify the Class Name and the Package name

eg Class Name multiplierUI Package mymultiplier

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5258

Figure 93

After clicking on the Finish button we should get something like the following

Figure 94

Last Updated ( Tuesday 18 September 2007 )

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5358

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5458

9th Step (Cont)

Written by Mohd Khalid Kasmin

Tuesday 11 September 2007

Step by Step into Java Programming

for University Physics Student

By

Mohd Khalid Kasmin

B Building user interface (UI) - putting in the components

Now that we have a proper blank form we can start filling the form with the suitable graphical components For introduction to

GUI programming an appropriate application to build (for a physics student) is a simple solver So our first exercise is to build an

application that takes two numbers and multiply them and display the result

With GUI we usually give input values to an application via input boxes that are labelled When all inputs are typed in properlywe click on a button to ask the application to execute the job Hence in this exercise well do just that The target now is to

construct a user interface that looks like Figure 95 below

Figure 95

Home Profile Teaching Links Contact Me My Work

Newsflash

Joomla 10 - Experience the Freedom It has never been easier to create your own dynamic site Manage all your content from

the best CMS admin interface

search

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5558

[ Back ]

1 Problemapplication description box

- In the palette pane click on Swing component named JTextPane (Note click just once) Move the mouse pointer on top of

your JFrame (Note just move not drag) When you see a little white box with orange border position it somewhere near the

top part of your JFrame and click once The JTextPane is then attached on the JFrame Addjust its size with the help of the

sizing hook around the perimeter of the JTextPane

- Edit the text property of the JTextPane with the description of your application

2 Input boxes

- In the palette pane click on Swing component named JTextField Next move the mouse pointer on top of your JFrame

When you see a white box with the word jTextField1 position it to the left and under the JTextPane1

- Click on Swing component JTextField in the palette pane again Move the mouse pointer on top of the JFrame you should

get a second JTextField named jTextField2 Position it to the right of jTextField1

Last Updated ( Tuesday 11 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5658

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5758

multiplication if a b and c were first declared as double or float or int variables So the code of the calculation should look like the

following

double a b c

a =

b =

c = a b

Clearly variables a and b need to be assigned with values of typed double Now looking back at inputboxes A and B we made

those boxes using the JTextField component meaning that any data or value written in a JTextField is of typed Text or String even

if the value displayed in the box is ldquo125rdquo But f irst here there is this task of getting to that Text (or String) values ldquo125rdquo and ldquo72rdquo

In Java we can get to a text in JTextField component using the getText() method So to retrieve values in jTextField1 and

jTextField2 we can use

jTextField1getText()

and

jTextField2getText()

But those values are still in text mode and we need another method to convert the data mode f rom text to double For that we can

use the method DoubleparseDouble() Thus the whole process of retrieve text -gt convert text to double -gt assign to double

variable can be done using

a = DoubleparseDouble( jTextField1getText() )

and

b = DoubleparseDouble( jTextField2getText() )

Hence the code for performing the multiplication becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

Up to this point we have the variable c as the result of the multiplication of a and b So the next thing to do is to display this

result on the JLabel that we have already prepared before

A x B = _____

The text A x B = ______ was made using JLabel component and its variable name is jLabel3 Since the variable c is of type

double and text property of JLabel is of type String we need to convert it first to String data type The method to convert a double

to String is DoubletoString() So we can write the code as DoubletoString(c) After getting the text we can now use it to be the

text displayed on jLabel3 To do that we use the method setText() Hence the appropriate code for displaying the result is

jLabel3setText( A x B = + DoubletoString(c) )

And the full code for our calculation becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

jLabel3setText( A x B = + DoubletoString(c) )

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5858

[ Back ]

Last Updated ( Tuesday 18 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

Page 41: Computional Physics

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4158

8th Step (Cont)

Written by Mohd Khalid Kasmin

Monday 27 August 2007

Up to this point we have define the name of our project Next we construct a class and to follow what we already know we name the class the sam

project name latihan01

6

7

Home Profile Teaching Links Contact Me My Work

Newsflash

Joomla 10 - Experience the Freedom It has never been easier to create your own dynamic site Manage all your content from

the best CMS admin interface

search

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4258

8

9

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4358

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4458

8th Step (Cont still)

Written by Mohd Khalid Kasmin

Monday 27 August 2007

In item (9) we see that Netbeans does not provide us with the main method So to follow what we already know we now put in the

main method

10

Home Profile Teaching Links Contact Me My Work

Newsflash

Aoccdrnig to a rscheearch at an Elingsh uinervtisy it deosnt mttaer in waht oredr the ltteers in a wrod are the olny iprmoetnt

tihng is taht frist and lsat ltteer is at the rghit pclae The rset can be a toatl mses and you can sitll raed it wouthit porbelm Tihs is

bcuseae we do not raed ervey lteter by itslef but the wrod as a wlohe

search

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4558

11

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4658

12

13

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4758

14

15

Last Updated ( Monday 27 August 2007 )

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4858

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4958

9th Step

Thursday 06 September 2007

Step by Step into Java Programming

for

University Physics Student

By

Mohd Khalid Kasmin

A Building graphical user interface - preparing the JFrame Form

At this stage of our programming experience with Java and NetBeans please follow strictly the steps outline in this article First and

make a new GUI project make sure there is no other project that already been loaded by Netbeans Please close any project displayed

1 Create new project

- On the main menu click on File --gt New Project (note Close first any previous project)

- Specify project name amp project location - Untick Create Main Class item

- Click on the Finish button

Home Profile Teaching Links Contact Me My Work

Newsflash

Aoccdrnig to a rscheearch at an Elingsh uinervtisy it deosnt mttaer in waht oredr the ltteers in a wrod are the olny iprmoetnt

tihng is taht frist and lsat ltteer is at the rghit pclae The rset can be a toatl mses and you can sitll raed it wouthit porbelm Tihs is

bcuseae we do not raed ervey lteter by itslef but the wrod as a wlohe

search

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5058

Figure 91

2 Create UI class

- Right click on the project name in the Project pane

on the popup menu select New --gt JFrame Form

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5158

Figure 92

- In the New JFrame Form dialog specify the Class Name and the Package name

eg Class Name multiplierUI Package mymultiplier

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5258

Figure 93

After clicking on the Finish button we should get something like the following

Figure 94

Last Updated ( Tuesday 18 September 2007 )

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5358

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5458

9th Step (Cont)

Written by Mohd Khalid Kasmin

Tuesday 11 September 2007

Step by Step into Java Programming

for University Physics Student

By

Mohd Khalid Kasmin

B Building user interface (UI) - putting in the components

Now that we have a proper blank form we can start filling the form with the suitable graphical components For introduction to

GUI programming an appropriate application to build (for a physics student) is a simple solver So our first exercise is to build an

application that takes two numbers and multiply them and display the result

With GUI we usually give input values to an application via input boxes that are labelled When all inputs are typed in properlywe click on a button to ask the application to execute the job Hence in this exercise well do just that The target now is to

construct a user interface that looks like Figure 95 below

Figure 95

Home Profile Teaching Links Contact Me My Work

Newsflash

Joomla 10 - Experience the Freedom It has never been easier to create your own dynamic site Manage all your content from

the best CMS admin interface

search

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5558

[ Back ]

1 Problemapplication description box

- In the palette pane click on Swing component named JTextPane (Note click just once) Move the mouse pointer on top of

your JFrame (Note just move not drag) When you see a little white box with orange border position it somewhere near the

top part of your JFrame and click once The JTextPane is then attached on the JFrame Addjust its size with the help of the

sizing hook around the perimeter of the JTextPane

- Edit the text property of the JTextPane with the description of your application

2 Input boxes

- In the palette pane click on Swing component named JTextField Next move the mouse pointer on top of your JFrame

When you see a white box with the word jTextField1 position it to the left and under the JTextPane1

- Click on Swing component JTextField in the palette pane again Move the mouse pointer on top of the JFrame you should

get a second JTextField named jTextField2 Position it to the right of jTextField1

Last Updated ( Tuesday 11 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5658

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5758

multiplication if a b and c were first declared as double or float or int variables So the code of the calculation should look like the

following

double a b c

a =

b =

c = a b

Clearly variables a and b need to be assigned with values of typed double Now looking back at inputboxes A and B we made

those boxes using the JTextField component meaning that any data or value written in a JTextField is of typed Text or String even

if the value displayed in the box is ldquo125rdquo But f irst here there is this task of getting to that Text (or String) values ldquo125rdquo and ldquo72rdquo

In Java we can get to a text in JTextField component using the getText() method So to retrieve values in jTextField1 and

jTextField2 we can use

jTextField1getText()

and

jTextField2getText()

But those values are still in text mode and we need another method to convert the data mode f rom text to double For that we can

use the method DoubleparseDouble() Thus the whole process of retrieve text -gt convert text to double -gt assign to double

variable can be done using

a = DoubleparseDouble( jTextField1getText() )

and

b = DoubleparseDouble( jTextField2getText() )

Hence the code for performing the multiplication becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

Up to this point we have the variable c as the result of the multiplication of a and b So the next thing to do is to display this

result on the JLabel that we have already prepared before

A x B = _____

The text A x B = ______ was made using JLabel component and its variable name is jLabel3 Since the variable c is of type

double and text property of JLabel is of type String we need to convert it first to String data type The method to convert a double

to String is DoubletoString() So we can write the code as DoubletoString(c) After getting the text we can now use it to be the

text displayed on jLabel3 To do that we use the method setText() Hence the appropriate code for displaying the result is

jLabel3setText( A x B = + DoubletoString(c) )

And the full code for our calculation becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

jLabel3setText( A x B = + DoubletoString(c) )

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5858

[ Back ]

Last Updated ( Tuesday 18 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

Page 42: Computional Physics

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4258

8

9

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4358

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4458

8th Step (Cont still)

Written by Mohd Khalid Kasmin

Monday 27 August 2007

In item (9) we see that Netbeans does not provide us with the main method So to follow what we already know we now put in the

main method

10

Home Profile Teaching Links Contact Me My Work

Newsflash

Aoccdrnig to a rscheearch at an Elingsh uinervtisy it deosnt mttaer in waht oredr the ltteers in a wrod are the olny iprmoetnt

tihng is taht frist and lsat ltteer is at the rghit pclae The rset can be a toatl mses and you can sitll raed it wouthit porbelm Tihs is

bcuseae we do not raed ervey lteter by itslef but the wrod as a wlohe

search

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4558

11

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4658

12

13

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4758

14

15

Last Updated ( Monday 27 August 2007 )

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4858

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4958

9th Step

Thursday 06 September 2007

Step by Step into Java Programming

for

University Physics Student

By

Mohd Khalid Kasmin

A Building graphical user interface - preparing the JFrame Form

At this stage of our programming experience with Java and NetBeans please follow strictly the steps outline in this article First and

make a new GUI project make sure there is no other project that already been loaded by Netbeans Please close any project displayed

1 Create new project

- On the main menu click on File --gt New Project (note Close first any previous project)

- Specify project name amp project location - Untick Create Main Class item

- Click on the Finish button

Home Profile Teaching Links Contact Me My Work

Newsflash

Aoccdrnig to a rscheearch at an Elingsh uinervtisy it deosnt mttaer in waht oredr the ltteers in a wrod are the olny iprmoetnt

tihng is taht frist and lsat ltteer is at the rghit pclae The rset can be a toatl mses and you can sitll raed it wouthit porbelm Tihs is

bcuseae we do not raed ervey lteter by itslef but the wrod as a wlohe

search

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5058

Figure 91

2 Create UI class

- Right click on the project name in the Project pane

on the popup menu select New --gt JFrame Form

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5158

Figure 92

- In the New JFrame Form dialog specify the Class Name and the Package name

eg Class Name multiplierUI Package mymultiplier

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5258

Figure 93

After clicking on the Finish button we should get something like the following

Figure 94

Last Updated ( Tuesday 18 September 2007 )

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5358

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5458

9th Step (Cont)

Written by Mohd Khalid Kasmin

Tuesday 11 September 2007

Step by Step into Java Programming

for University Physics Student

By

Mohd Khalid Kasmin

B Building user interface (UI) - putting in the components

Now that we have a proper blank form we can start filling the form with the suitable graphical components For introduction to

GUI programming an appropriate application to build (for a physics student) is a simple solver So our first exercise is to build an

application that takes two numbers and multiply them and display the result

With GUI we usually give input values to an application via input boxes that are labelled When all inputs are typed in properlywe click on a button to ask the application to execute the job Hence in this exercise well do just that The target now is to

construct a user interface that looks like Figure 95 below

Figure 95

Home Profile Teaching Links Contact Me My Work

Newsflash

Joomla 10 - Experience the Freedom It has never been easier to create your own dynamic site Manage all your content from

the best CMS admin interface

search

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5558

[ Back ]

1 Problemapplication description box

- In the palette pane click on Swing component named JTextPane (Note click just once) Move the mouse pointer on top of

your JFrame (Note just move not drag) When you see a little white box with orange border position it somewhere near the

top part of your JFrame and click once The JTextPane is then attached on the JFrame Addjust its size with the help of the

sizing hook around the perimeter of the JTextPane

- Edit the text property of the JTextPane with the description of your application

2 Input boxes

- In the palette pane click on Swing component named JTextField Next move the mouse pointer on top of your JFrame

When you see a white box with the word jTextField1 position it to the left and under the JTextPane1

- Click on Swing component JTextField in the palette pane again Move the mouse pointer on top of the JFrame you should

get a second JTextField named jTextField2 Position it to the right of jTextField1

Last Updated ( Tuesday 11 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5658

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5758

multiplication if a b and c were first declared as double or float or int variables So the code of the calculation should look like the

following

double a b c

a =

b =

c = a b

Clearly variables a and b need to be assigned with values of typed double Now looking back at inputboxes A and B we made

those boxes using the JTextField component meaning that any data or value written in a JTextField is of typed Text or String even

if the value displayed in the box is ldquo125rdquo But f irst here there is this task of getting to that Text (or String) values ldquo125rdquo and ldquo72rdquo

In Java we can get to a text in JTextField component using the getText() method So to retrieve values in jTextField1 and

jTextField2 we can use

jTextField1getText()

and

jTextField2getText()

But those values are still in text mode and we need another method to convert the data mode f rom text to double For that we can

use the method DoubleparseDouble() Thus the whole process of retrieve text -gt convert text to double -gt assign to double

variable can be done using

a = DoubleparseDouble( jTextField1getText() )

and

b = DoubleparseDouble( jTextField2getText() )

Hence the code for performing the multiplication becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

Up to this point we have the variable c as the result of the multiplication of a and b So the next thing to do is to display this

result on the JLabel that we have already prepared before

A x B = _____

The text A x B = ______ was made using JLabel component and its variable name is jLabel3 Since the variable c is of type

double and text property of JLabel is of type String we need to convert it first to String data type The method to convert a double

to String is DoubletoString() So we can write the code as DoubletoString(c) After getting the text we can now use it to be the

text displayed on jLabel3 To do that we use the method setText() Hence the appropriate code for displaying the result is

jLabel3setText( A x B = + DoubletoString(c) )

And the full code for our calculation becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

jLabel3setText( A x B = + DoubletoString(c) )

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5858

[ Back ]

Last Updated ( Tuesday 18 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

Page 43: Computional Physics

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4358

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4458

8th Step (Cont still)

Written by Mohd Khalid Kasmin

Monday 27 August 2007

In item (9) we see that Netbeans does not provide us with the main method So to follow what we already know we now put in the

main method

10

Home Profile Teaching Links Contact Me My Work

Newsflash

Aoccdrnig to a rscheearch at an Elingsh uinervtisy it deosnt mttaer in waht oredr the ltteers in a wrod are the olny iprmoetnt

tihng is taht frist and lsat ltteer is at the rghit pclae The rset can be a toatl mses and you can sitll raed it wouthit porbelm Tihs is

bcuseae we do not raed ervey lteter by itslef but the wrod as a wlohe

search

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4558

11

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4658

12

13

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4758

14

15

Last Updated ( Monday 27 August 2007 )

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4858

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4958

9th Step

Thursday 06 September 2007

Step by Step into Java Programming

for

University Physics Student

By

Mohd Khalid Kasmin

A Building graphical user interface - preparing the JFrame Form

At this stage of our programming experience with Java and NetBeans please follow strictly the steps outline in this article First and

make a new GUI project make sure there is no other project that already been loaded by Netbeans Please close any project displayed

1 Create new project

- On the main menu click on File --gt New Project (note Close first any previous project)

- Specify project name amp project location - Untick Create Main Class item

- Click on the Finish button

Home Profile Teaching Links Contact Me My Work

Newsflash

Aoccdrnig to a rscheearch at an Elingsh uinervtisy it deosnt mttaer in waht oredr the ltteers in a wrod are the olny iprmoetnt

tihng is taht frist and lsat ltteer is at the rghit pclae The rset can be a toatl mses and you can sitll raed it wouthit porbelm Tihs is

bcuseae we do not raed ervey lteter by itslef but the wrod as a wlohe

search

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5058

Figure 91

2 Create UI class

- Right click on the project name in the Project pane

on the popup menu select New --gt JFrame Form

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5158

Figure 92

- In the New JFrame Form dialog specify the Class Name and the Package name

eg Class Name multiplierUI Package mymultiplier

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5258

Figure 93

After clicking on the Finish button we should get something like the following

Figure 94

Last Updated ( Tuesday 18 September 2007 )

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5358

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5458

9th Step (Cont)

Written by Mohd Khalid Kasmin

Tuesday 11 September 2007

Step by Step into Java Programming

for University Physics Student

By

Mohd Khalid Kasmin

B Building user interface (UI) - putting in the components

Now that we have a proper blank form we can start filling the form with the suitable graphical components For introduction to

GUI programming an appropriate application to build (for a physics student) is a simple solver So our first exercise is to build an

application that takes two numbers and multiply them and display the result

With GUI we usually give input values to an application via input boxes that are labelled When all inputs are typed in properlywe click on a button to ask the application to execute the job Hence in this exercise well do just that The target now is to

construct a user interface that looks like Figure 95 below

Figure 95

Home Profile Teaching Links Contact Me My Work

Newsflash

Joomla 10 - Experience the Freedom It has never been easier to create your own dynamic site Manage all your content from

the best CMS admin interface

search

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5558

[ Back ]

1 Problemapplication description box

- In the palette pane click on Swing component named JTextPane (Note click just once) Move the mouse pointer on top of

your JFrame (Note just move not drag) When you see a little white box with orange border position it somewhere near the

top part of your JFrame and click once The JTextPane is then attached on the JFrame Addjust its size with the help of the

sizing hook around the perimeter of the JTextPane

- Edit the text property of the JTextPane with the description of your application

2 Input boxes

- In the palette pane click on Swing component named JTextField Next move the mouse pointer on top of your JFrame

When you see a white box with the word jTextField1 position it to the left and under the JTextPane1

- Click on Swing component JTextField in the palette pane again Move the mouse pointer on top of the JFrame you should

get a second JTextField named jTextField2 Position it to the right of jTextField1

Last Updated ( Tuesday 11 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5658

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5758

multiplication if a b and c were first declared as double or float or int variables So the code of the calculation should look like the

following

double a b c

a =

b =

c = a b

Clearly variables a and b need to be assigned with values of typed double Now looking back at inputboxes A and B we made

those boxes using the JTextField component meaning that any data or value written in a JTextField is of typed Text or String even

if the value displayed in the box is ldquo125rdquo But f irst here there is this task of getting to that Text (or String) values ldquo125rdquo and ldquo72rdquo

In Java we can get to a text in JTextField component using the getText() method So to retrieve values in jTextField1 and

jTextField2 we can use

jTextField1getText()

and

jTextField2getText()

But those values are still in text mode and we need another method to convert the data mode f rom text to double For that we can

use the method DoubleparseDouble() Thus the whole process of retrieve text -gt convert text to double -gt assign to double

variable can be done using

a = DoubleparseDouble( jTextField1getText() )

and

b = DoubleparseDouble( jTextField2getText() )

Hence the code for performing the multiplication becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

Up to this point we have the variable c as the result of the multiplication of a and b So the next thing to do is to display this

result on the JLabel that we have already prepared before

A x B = _____

The text A x B = ______ was made using JLabel component and its variable name is jLabel3 Since the variable c is of type

double and text property of JLabel is of type String we need to convert it first to String data type The method to convert a double

to String is DoubletoString() So we can write the code as DoubletoString(c) After getting the text we can now use it to be the

text displayed on jLabel3 To do that we use the method setText() Hence the appropriate code for displaying the result is

jLabel3setText( A x B = + DoubletoString(c) )

And the full code for our calculation becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

jLabel3setText( A x B = + DoubletoString(c) )

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5858

[ Back ]

Last Updated ( Tuesday 18 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

Page 44: Computional Physics

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4458

8th Step (Cont still)

Written by Mohd Khalid Kasmin

Monday 27 August 2007

In item (9) we see that Netbeans does not provide us with the main method So to follow what we already know we now put in the

main method

10

Home Profile Teaching Links Contact Me My Work

Newsflash

Aoccdrnig to a rscheearch at an Elingsh uinervtisy it deosnt mttaer in waht oredr the ltteers in a wrod are the olny iprmoetnt

tihng is taht frist and lsat ltteer is at the rghit pclae The rset can be a toatl mses and you can sitll raed it wouthit porbelm Tihs is

bcuseae we do not raed ervey lteter by itslef but the wrod as a wlohe

search

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4558

11

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4658

12

13

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4758

14

15

Last Updated ( Monday 27 August 2007 )

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4858

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4958

9th Step

Thursday 06 September 2007

Step by Step into Java Programming

for

University Physics Student

By

Mohd Khalid Kasmin

A Building graphical user interface - preparing the JFrame Form

At this stage of our programming experience with Java and NetBeans please follow strictly the steps outline in this article First and

make a new GUI project make sure there is no other project that already been loaded by Netbeans Please close any project displayed

1 Create new project

- On the main menu click on File --gt New Project (note Close first any previous project)

- Specify project name amp project location - Untick Create Main Class item

- Click on the Finish button

Home Profile Teaching Links Contact Me My Work

Newsflash

Aoccdrnig to a rscheearch at an Elingsh uinervtisy it deosnt mttaer in waht oredr the ltteers in a wrod are the olny iprmoetnt

tihng is taht frist and lsat ltteer is at the rghit pclae The rset can be a toatl mses and you can sitll raed it wouthit porbelm Tihs is

bcuseae we do not raed ervey lteter by itslef but the wrod as a wlohe

search

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5058

Figure 91

2 Create UI class

- Right click on the project name in the Project pane

on the popup menu select New --gt JFrame Form

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5158

Figure 92

- In the New JFrame Form dialog specify the Class Name and the Package name

eg Class Name multiplierUI Package mymultiplier

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5258

Figure 93

After clicking on the Finish button we should get something like the following

Figure 94

Last Updated ( Tuesday 18 September 2007 )

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5358

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5458

9th Step (Cont)

Written by Mohd Khalid Kasmin

Tuesday 11 September 2007

Step by Step into Java Programming

for University Physics Student

By

Mohd Khalid Kasmin

B Building user interface (UI) - putting in the components

Now that we have a proper blank form we can start filling the form with the suitable graphical components For introduction to

GUI programming an appropriate application to build (for a physics student) is a simple solver So our first exercise is to build an

application that takes two numbers and multiply them and display the result

With GUI we usually give input values to an application via input boxes that are labelled When all inputs are typed in properlywe click on a button to ask the application to execute the job Hence in this exercise well do just that The target now is to

construct a user interface that looks like Figure 95 below

Figure 95

Home Profile Teaching Links Contact Me My Work

Newsflash

Joomla 10 - Experience the Freedom It has never been easier to create your own dynamic site Manage all your content from

the best CMS admin interface

search

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5558

[ Back ]

1 Problemapplication description box

- In the palette pane click on Swing component named JTextPane (Note click just once) Move the mouse pointer on top of

your JFrame (Note just move not drag) When you see a little white box with orange border position it somewhere near the

top part of your JFrame and click once The JTextPane is then attached on the JFrame Addjust its size with the help of the

sizing hook around the perimeter of the JTextPane

- Edit the text property of the JTextPane with the description of your application

2 Input boxes

- In the palette pane click on Swing component named JTextField Next move the mouse pointer on top of your JFrame

When you see a white box with the word jTextField1 position it to the left and under the JTextPane1

- Click on Swing component JTextField in the palette pane again Move the mouse pointer on top of the JFrame you should

get a second JTextField named jTextField2 Position it to the right of jTextField1

Last Updated ( Tuesday 11 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5658

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5758

multiplication if a b and c were first declared as double or float or int variables So the code of the calculation should look like the

following

double a b c

a =

b =

c = a b

Clearly variables a and b need to be assigned with values of typed double Now looking back at inputboxes A and B we made

those boxes using the JTextField component meaning that any data or value written in a JTextField is of typed Text or String even

if the value displayed in the box is ldquo125rdquo But f irst here there is this task of getting to that Text (or String) values ldquo125rdquo and ldquo72rdquo

In Java we can get to a text in JTextField component using the getText() method So to retrieve values in jTextField1 and

jTextField2 we can use

jTextField1getText()

and

jTextField2getText()

But those values are still in text mode and we need another method to convert the data mode f rom text to double For that we can

use the method DoubleparseDouble() Thus the whole process of retrieve text -gt convert text to double -gt assign to double

variable can be done using

a = DoubleparseDouble( jTextField1getText() )

and

b = DoubleparseDouble( jTextField2getText() )

Hence the code for performing the multiplication becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

Up to this point we have the variable c as the result of the multiplication of a and b So the next thing to do is to display this

result on the JLabel that we have already prepared before

A x B = _____

The text A x B = ______ was made using JLabel component and its variable name is jLabel3 Since the variable c is of type

double and text property of JLabel is of type String we need to convert it first to String data type The method to convert a double

to String is DoubletoString() So we can write the code as DoubletoString(c) After getting the text we can now use it to be the

text displayed on jLabel3 To do that we use the method setText() Hence the appropriate code for displaying the result is

jLabel3setText( A x B = + DoubletoString(c) )

And the full code for our calculation becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

jLabel3setText( A x B = + DoubletoString(c) )

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5858

[ Back ]

Last Updated ( Tuesday 18 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

Page 45: Computional Physics

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4558

11

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4658

12

13

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4758

14

15

Last Updated ( Monday 27 August 2007 )

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4858

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4958

9th Step

Thursday 06 September 2007

Step by Step into Java Programming

for

University Physics Student

By

Mohd Khalid Kasmin

A Building graphical user interface - preparing the JFrame Form

At this stage of our programming experience with Java and NetBeans please follow strictly the steps outline in this article First and

make a new GUI project make sure there is no other project that already been loaded by Netbeans Please close any project displayed

1 Create new project

- On the main menu click on File --gt New Project (note Close first any previous project)

- Specify project name amp project location - Untick Create Main Class item

- Click on the Finish button

Home Profile Teaching Links Contact Me My Work

Newsflash

Aoccdrnig to a rscheearch at an Elingsh uinervtisy it deosnt mttaer in waht oredr the ltteers in a wrod are the olny iprmoetnt

tihng is taht frist and lsat ltteer is at the rghit pclae The rset can be a toatl mses and you can sitll raed it wouthit porbelm Tihs is

bcuseae we do not raed ervey lteter by itslef but the wrod as a wlohe

search

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5058

Figure 91

2 Create UI class

- Right click on the project name in the Project pane

on the popup menu select New --gt JFrame Form

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5158

Figure 92

- In the New JFrame Form dialog specify the Class Name and the Package name

eg Class Name multiplierUI Package mymultiplier

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5258

Figure 93

After clicking on the Finish button we should get something like the following

Figure 94

Last Updated ( Tuesday 18 September 2007 )

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5358

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5458

9th Step (Cont)

Written by Mohd Khalid Kasmin

Tuesday 11 September 2007

Step by Step into Java Programming

for University Physics Student

By

Mohd Khalid Kasmin

B Building user interface (UI) - putting in the components

Now that we have a proper blank form we can start filling the form with the suitable graphical components For introduction to

GUI programming an appropriate application to build (for a physics student) is a simple solver So our first exercise is to build an

application that takes two numbers and multiply them and display the result

With GUI we usually give input values to an application via input boxes that are labelled When all inputs are typed in properlywe click on a button to ask the application to execute the job Hence in this exercise well do just that The target now is to

construct a user interface that looks like Figure 95 below

Figure 95

Home Profile Teaching Links Contact Me My Work

Newsflash

Joomla 10 - Experience the Freedom It has never been easier to create your own dynamic site Manage all your content from

the best CMS admin interface

search

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5558

[ Back ]

1 Problemapplication description box

- In the palette pane click on Swing component named JTextPane (Note click just once) Move the mouse pointer on top of

your JFrame (Note just move not drag) When you see a little white box with orange border position it somewhere near the

top part of your JFrame and click once The JTextPane is then attached on the JFrame Addjust its size with the help of the

sizing hook around the perimeter of the JTextPane

- Edit the text property of the JTextPane with the description of your application

2 Input boxes

- In the palette pane click on Swing component named JTextField Next move the mouse pointer on top of your JFrame

When you see a white box with the word jTextField1 position it to the left and under the JTextPane1

- Click on Swing component JTextField in the palette pane again Move the mouse pointer on top of the JFrame you should

get a second JTextField named jTextField2 Position it to the right of jTextField1

Last Updated ( Tuesday 11 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5658

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5758

multiplication if a b and c were first declared as double or float or int variables So the code of the calculation should look like the

following

double a b c

a =

b =

c = a b

Clearly variables a and b need to be assigned with values of typed double Now looking back at inputboxes A and B we made

those boxes using the JTextField component meaning that any data or value written in a JTextField is of typed Text or String even

if the value displayed in the box is ldquo125rdquo But f irst here there is this task of getting to that Text (or String) values ldquo125rdquo and ldquo72rdquo

In Java we can get to a text in JTextField component using the getText() method So to retrieve values in jTextField1 and

jTextField2 we can use

jTextField1getText()

and

jTextField2getText()

But those values are still in text mode and we need another method to convert the data mode f rom text to double For that we can

use the method DoubleparseDouble() Thus the whole process of retrieve text -gt convert text to double -gt assign to double

variable can be done using

a = DoubleparseDouble( jTextField1getText() )

and

b = DoubleparseDouble( jTextField2getText() )

Hence the code for performing the multiplication becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

Up to this point we have the variable c as the result of the multiplication of a and b So the next thing to do is to display this

result on the JLabel that we have already prepared before

A x B = _____

The text A x B = ______ was made using JLabel component and its variable name is jLabel3 Since the variable c is of type

double and text property of JLabel is of type String we need to convert it first to String data type The method to convert a double

to String is DoubletoString() So we can write the code as DoubletoString(c) After getting the text we can now use it to be the

text displayed on jLabel3 To do that we use the method setText() Hence the appropriate code for displaying the result is

jLabel3setText( A x B = + DoubletoString(c) )

And the full code for our calculation becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

jLabel3setText( A x B = + DoubletoString(c) )

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5858

[ Back ]

Last Updated ( Tuesday 18 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

Page 46: Computional Physics

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4658

12

13

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4758

14

15

Last Updated ( Monday 27 August 2007 )

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4858

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4958

9th Step

Thursday 06 September 2007

Step by Step into Java Programming

for

University Physics Student

By

Mohd Khalid Kasmin

A Building graphical user interface - preparing the JFrame Form

At this stage of our programming experience with Java and NetBeans please follow strictly the steps outline in this article First and

make a new GUI project make sure there is no other project that already been loaded by Netbeans Please close any project displayed

1 Create new project

- On the main menu click on File --gt New Project (note Close first any previous project)

- Specify project name amp project location - Untick Create Main Class item

- Click on the Finish button

Home Profile Teaching Links Contact Me My Work

Newsflash

Aoccdrnig to a rscheearch at an Elingsh uinervtisy it deosnt mttaer in waht oredr the ltteers in a wrod are the olny iprmoetnt

tihng is taht frist and lsat ltteer is at the rghit pclae The rset can be a toatl mses and you can sitll raed it wouthit porbelm Tihs is

bcuseae we do not raed ervey lteter by itslef but the wrod as a wlohe

search

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5058

Figure 91

2 Create UI class

- Right click on the project name in the Project pane

on the popup menu select New --gt JFrame Form

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5158

Figure 92

- In the New JFrame Form dialog specify the Class Name and the Package name

eg Class Name multiplierUI Package mymultiplier

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5258

Figure 93

After clicking on the Finish button we should get something like the following

Figure 94

Last Updated ( Tuesday 18 September 2007 )

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5358

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5458

9th Step (Cont)

Written by Mohd Khalid Kasmin

Tuesday 11 September 2007

Step by Step into Java Programming

for University Physics Student

By

Mohd Khalid Kasmin

B Building user interface (UI) - putting in the components

Now that we have a proper blank form we can start filling the form with the suitable graphical components For introduction to

GUI programming an appropriate application to build (for a physics student) is a simple solver So our first exercise is to build an

application that takes two numbers and multiply them and display the result

With GUI we usually give input values to an application via input boxes that are labelled When all inputs are typed in properlywe click on a button to ask the application to execute the job Hence in this exercise well do just that The target now is to

construct a user interface that looks like Figure 95 below

Figure 95

Home Profile Teaching Links Contact Me My Work

Newsflash

Joomla 10 - Experience the Freedom It has never been easier to create your own dynamic site Manage all your content from

the best CMS admin interface

search

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5558

[ Back ]

1 Problemapplication description box

- In the palette pane click on Swing component named JTextPane (Note click just once) Move the mouse pointer on top of

your JFrame (Note just move not drag) When you see a little white box with orange border position it somewhere near the

top part of your JFrame and click once The JTextPane is then attached on the JFrame Addjust its size with the help of the

sizing hook around the perimeter of the JTextPane

- Edit the text property of the JTextPane with the description of your application

2 Input boxes

- In the palette pane click on Swing component named JTextField Next move the mouse pointer on top of your JFrame

When you see a white box with the word jTextField1 position it to the left and under the JTextPane1

- Click on Swing component JTextField in the palette pane again Move the mouse pointer on top of the JFrame you should

get a second JTextField named jTextField2 Position it to the right of jTextField1

Last Updated ( Tuesday 11 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5658

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5758

multiplication if a b and c were first declared as double or float or int variables So the code of the calculation should look like the

following

double a b c

a =

b =

c = a b

Clearly variables a and b need to be assigned with values of typed double Now looking back at inputboxes A and B we made

those boxes using the JTextField component meaning that any data or value written in a JTextField is of typed Text or String even

if the value displayed in the box is ldquo125rdquo But f irst here there is this task of getting to that Text (or String) values ldquo125rdquo and ldquo72rdquo

In Java we can get to a text in JTextField component using the getText() method So to retrieve values in jTextField1 and

jTextField2 we can use

jTextField1getText()

and

jTextField2getText()

But those values are still in text mode and we need another method to convert the data mode f rom text to double For that we can

use the method DoubleparseDouble() Thus the whole process of retrieve text -gt convert text to double -gt assign to double

variable can be done using

a = DoubleparseDouble( jTextField1getText() )

and

b = DoubleparseDouble( jTextField2getText() )

Hence the code for performing the multiplication becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

Up to this point we have the variable c as the result of the multiplication of a and b So the next thing to do is to display this

result on the JLabel that we have already prepared before

A x B = _____

The text A x B = ______ was made using JLabel component and its variable name is jLabel3 Since the variable c is of type

double and text property of JLabel is of type String we need to convert it first to String data type The method to convert a double

to String is DoubletoString() So we can write the code as DoubletoString(c) After getting the text we can now use it to be the

text displayed on jLabel3 To do that we use the method setText() Hence the appropriate code for displaying the result is

jLabel3setText( A x B = + DoubletoString(c) )

And the full code for our calculation becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

jLabel3setText( A x B = + DoubletoString(c) )

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5858

[ Back ]

Last Updated ( Tuesday 18 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

Page 47: Computional Physics

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4758

14

15

Last Updated ( Monday 27 August 2007 )

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4858

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4958

9th Step

Thursday 06 September 2007

Step by Step into Java Programming

for

University Physics Student

By

Mohd Khalid Kasmin

A Building graphical user interface - preparing the JFrame Form

At this stage of our programming experience with Java and NetBeans please follow strictly the steps outline in this article First and

make a new GUI project make sure there is no other project that already been loaded by Netbeans Please close any project displayed

1 Create new project

- On the main menu click on File --gt New Project (note Close first any previous project)

- Specify project name amp project location - Untick Create Main Class item

- Click on the Finish button

Home Profile Teaching Links Contact Me My Work

Newsflash

Aoccdrnig to a rscheearch at an Elingsh uinervtisy it deosnt mttaer in waht oredr the ltteers in a wrod are the olny iprmoetnt

tihng is taht frist and lsat ltteer is at the rghit pclae The rset can be a toatl mses and you can sitll raed it wouthit porbelm Tihs is

bcuseae we do not raed ervey lteter by itslef but the wrod as a wlohe

search

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5058

Figure 91

2 Create UI class

- Right click on the project name in the Project pane

on the popup menu select New --gt JFrame Form

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5158

Figure 92

- In the New JFrame Form dialog specify the Class Name and the Package name

eg Class Name multiplierUI Package mymultiplier

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5258

Figure 93

After clicking on the Finish button we should get something like the following

Figure 94

Last Updated ( Tuesday 18 September 2007 )

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5358

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5458

9th Step (Cont)

Written by Mohd Khalid Kasmin

Tuesday 11 September 2007

Step by Step into Java Programming

for University Physics Student

By

Mohd Khalid Kasmin

B Building user interface (UI) - putting in the components

Now that we have a proper blank form we can start filling the form with the suitable graphical components For introduction to

GUI programming an appropriate application to build (for a physics student) is a simple solver So our first exercise is to build an

application that takes two numbers and multiply them and display the result

With GUI we usually give input values to an application via input boxes that are labelled When all inputs are typed in properlywe click on a button to ask the application to execute the job Hence in this exercise well do just that The target now is to

construct a user interface that looks like Figure 95 below

Figure 95

Home Profile Teaching Links Contact Me My Work

Newsflash

Joomla 10 - Experience the Freedom It has never been easier to create your own dynamic site Manage all your content from

the best CMS admin interface

search

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5558

[ Back ]

1 Problemapplication description box

- In the palette pane click on Swing component named JTextPane (Note click just once) Move the mouse pointer on top of

your JFrame (Note just move not drag) When you see a little white box with orange border position it somewhere near the

top part of your JFrame and click once The JTextPane is then attached on the JFrame Addjust its size with the help of the

sizing hook around the perimeter of the JTextPane

- Edit the text property of the JTextPane with the description of your application

2 Input boxes

- In the palette pane click on Swing component named JTextField Next move the mouse pointer on top of your JFrame

When you see a white box with the word jTextField1 position it to the left and under the JTextPane1

- Click on Swing component JTextField in the palette pane again Move the mouse pointer on top of the JFrame you should

get a second JTextField named jTextField2 Position it to the right of jTextField1

Last Updated ( Tuesday 11 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5658

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5758

multiplication if a b and c were first declared as double or float or int variables So the code of the calculation should look like the

following

double a b c

a =

b =

c = a b

Clearly variables a and b need to be assigned with values of typed double Now looking back at inputboxes A and B we made

those boxes using the JTextField component meaning that any data or value written in a JTextField is of typed Text or String even

if the value displayed in the box is ldquo125rdquo But f irst here there is this task of getting to that Text (or String) values ldquo125rdquo and ldquo72rdquo

In Java we can get to a text in JTextField component using the getText() method So to retrieve values in jTextField1 and

jTextField2 we can use

jTextField1getText()

and

jTextField2getText()

But those values are still in text mode and we need another method to convert the data mode f rom text to double For that we can

use the method DoubleparseDouble() Thus the whole process of retrieve text -gt convert text to double -gt assign to double

variable can be done using

a = DoubleparseDouble( jTextField1getText() )

and

b = DoubleparseDouble( jTextField2getText() )

Hence the code for performing the multiplication becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

Up to this point we have the variable c as the result of the multiplication of a and b So the next thing to do is to display this

result on the JLabel that we have already prepared before

A x B = _____

The text A x B = ______ was made using JLabel component and its variable name is jLabel3 Since the variable c is of type

double and text property of JLabel is of type String we need to convert it first to String data type The method to convert a double

to String is DoubletoString() So we can write the code as DoubletoString(c) After getting the text we can now use it to be the

text displayed on jLabel3 To do that we use the method setText() Hence the appropriate code for displaying the result is

jLabel3setText( A x B = + DoubletoString(c) )

And the full code for our calculation becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

jLabel3setText( A x B = + DoubletoString(c) )

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5858

[ Back ]

Last Updated ( Tuesday 18 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

Page 48: Computional Physics

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4858

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 8th Step (Cont sti ll) fi le Epengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4958

9th Step

Thursday 06 September 2007

Step by Step into Java Programming

for

University Physics Student

By

Mohd Khalid Kasmin

A Building graphical user interface - preparing the JFrame Form

At this stage of our programming experience with Java and NetBeans please follow strictly the steps outline in this article First and

make a new GUI project make sure there is no other project that already been loaded by Netbeans Please close any project displayed

1 Create new project

- On the main menu click on File --gt New Project (note Close first any previous project)

- Specify project name amp project location - Untick Create Main Class item

- Click on the Finish button

Home Profile Teaching Links Contact Me My Work

Newsflash

Aoccdrnig to a rscheearch at an Elingsh uinervtisy it deosnt mttaer in waht oredr the ltteers in a wrod are the olny iprmoetnt

tihng is taht frist and lsat ltteer is at the rghit pclae The rset can be a toatl mses and you can sitll raed it wouthit porbelm Tihs is

bcuseae we do not raed ervey lteter by itslef but the wrod as a wlohe

search

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5058

Figure 91

2 Create UI class

- Right click on the project name in the Project pane

on the popup menu select New --gt JFrame Form

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5158

Figure 92

- In the New JFrame Form dialog specify the Class Name and the Package name

eg Class Name multiplierUI Package mymultiplier

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5258

Figure 93

After clicking on the Finish button we should get something like the following

Figure 94

Last Updated ( Tuesday 18 September 2007 )

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5358

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5458

9th Step (Cont)

Written by Mohd Khalid Kasmin

Tuesday 11 September 2007

Step by Step into Java Programming

for University Physics Student

By

Mohd Khalid Kasmin

B Building user interface (UI) - putting in the components

Now that we have a proper blank form we can start filling the form with the suitable graphical components For introduction to

GUI programming an appropriate application to build (for a physics student) is a simple solver So our first exercise is to build an

application that takes two numbers and multiply them and display the result

With GUI we usually give input values to an application via input boxes that are labelled When all inputs are typed in properlywe click on a button to ask the application to execute the job Hence in this exercise well do just that The target now is to

construct a user interface that looks like Figure 95 below

Figure 95

Home Profile Teaching Links Contact Me My Work

Newsflash

Joomla 10 - Experience the Freedom It has never been easier to create your own dynamic site Manage all your content from

the best CMS admin interface

search

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5558

[ Back ]

1 Problemapplication description box

- In the palette pane click on Swing component named JTextPane (Note click just once) Move the mouse pointer on top of

your JFrame (Note just move not drag) When you see a little white box with orange border position it somewhere near the

top part of your JFrame and click once The JTextPane is then attached on the JFrame Addjust its size with the help of the

sizing hook around the perimeter of the JTextPane

- Edit the text property of the JTextPane with the description of your application

2 Input boxes

- In the palette pane click on Swing component named JTextField Next move the mouse pointer on top of your JFrame

When you see a white box with the word jTextField1 position it to the left and under the JTextPane1

- Click on Swing component JTextField in the palette pane again Move the mouse pointer on top of the JFrame you should

get a second JTextField named jTextField2 Position it to the right of jTextField1

Last Updated ( Tuesday 11 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5658

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5758

multiplication if a b and c were first declared as double or float or int variables So the code of the calculation should look like the

following

double a b c

a =

b =

c = a b

Clearly variables a and b need to be assigned with values of typed double Now looking back at inputboxes A and B we made

those boxes using the JTextField component meaning that any data or value written in a JTextField is of typed Text or String even

if the value displayed in the box is ldquo125rdquo But f irst here there is this task of getting to that Text (or String) values ldquo125rdquo and ldquo72rdquo

In Java we can get to a text in JTextField component using the getText() method So to retrieve values in jTextField1 and

jTextField2 we can use

jTextField1getText()

and

jTextField2getText()

But those values are still in text mode and we need another method to convert the data mode f rom text to double For that we can

use the method DoubleparseDouble() Thus the whole process of retrieve text -gt convert text to double -gt assign to double

variable can be done using

a = DoubleparseDouble( jTextField1getText() )

and

b = DoubleparseDouble( jTextField2getText() )

Hence the code for performing the multiplication becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

Up to this point we have the variable c as the result of the multiplication of a and b So the next thing to do is to display this

result on the JLabel that we have already prepared before

A x B = _____

The text A x B = ______ was made using JLabel component and its variable name is jLabel3 Since the variable c is of type

double and text property of JLabel is of type String we need to convert it first to String data type The method to convert a double

to String is DoubletoString() So we can write the code as DoubletoString(c) After getting the text we can now use it to be the

text displayed on jLabel3 To do that we use the method setText() Hence the appropriate code for displaying the result is

jLabel3setText( A x B = + DoubletoString(c) )

And the full code for our calculation becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

jLabel3setText( A x B = + DoubletoString(c) )

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5858

[ Back ]

Last Updated ( Tuesday 18 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

Page 49: Computional Physics

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 4958

9th Step

Thursday 06 September 2007

Step by Step into Java Programming

for

University Physics Student

By

Mohd Khalid Kasmin

A Building graphical user interface - preparing the JFrame Form

At this stage of our programming experience with Java and NetBeans please follow strictly the steps outline in this article First and

make a new GUI project make sure there is no other project that already been loaded by Netbeans Please close any project displayed

1 Create new project

- On the main menu click on File --gt New Project (note Close first any previous project)

- Specify project name amp project location - Untick Create Main Class item

- Click on the Finish button

Home Profile Teaching Links Contact Me My Work

Newsflash

Aoccdrnig to a rscheearch at an Elingsh uinervtisy it deosnt mttaer in waht oredr the ltteers in a wrod are the olny iprmoetnt

tihng is taht frist and lsat ltteer is at the rghit pclae The rset can be a toatl mses and you can sitll raed it wouthit porbelm Tihs is

bcuseae we do not raed ervey lteter by itslef but the wrod as a wlohe

search

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5058

Figure 91

2 Create UI class

- Right click on the project name in the Project pane

on the popup menu select New --gt JFrame Form

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5158

Figure 92

- In the New JFrame Form dialog specify the Class Name and the Package name

eg Class Name multiplierUI Package mymultiplier

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5258

Figure 93

After clicking on the Finish button we should get something like the following

Figure 94

Last Updated ( Tuesday 18 September 2007 )

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5358

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5458

9th Step (Cont)

Written by Mohd Khalid Kasmin

Tuesday 11 September 2007

Step by Step into Java Programming

for University Physics Student

By

Mohd Khalid Kasmin

B Building user interface (UI) - putting in the components

Now that we have a proper blank form we can start filling the form with the suitable graphical components For introduction to

GUI programming an appropriate application to build (for a physics student) is a simple solver So our first exercise is to build an

application that takes two numbers and multiply them and display the result

With GUI we usually give input values to an application via input boxes that are labelled When all inputs are typed in properlywe click on a button to ask the application to execute the job Hence in this exercise well do just that The target now is to

construct a user interface that looks like Figure 95 below

Figure 95

Home Profile Teaching Links Contact Me My Work

Newsflash

Joomla 10 - Experience the Freedom It has never been easier to create your own dynamic site Manage all your content from

the best CMS admin interface

search

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5558

[ Back ]

1 Problemapplication description box

- In the palette pane click on Swing component named JTextPane (Note click just once) Move the mouse pointer on top of

your JFrame (Note just move not drag) When you see a little white box with orange border position it somewhere near the

top part of your JFrame and click once The JTextPane is then attached on the JFrame Addjust its size with the help of the

sizing hook around the perimeter of the JTextPane

- Edit the text property of the JTextPane with the description of your application

2 Input boxes

- In the palette pane click on Swing component named JTextField Next move the mouse pointer on top of your JFrame

When you see a white box with the word jTextField1 position it to the left and under the JTextPane1

- Click on Swing component JTextField in the palette pane again Move the mouse pointer on top of the JFrame you should

get a second JTextField named jTextField2 Position it to the right of jTextField1

Last Updated ( Tuesday 11 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5658

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5758

multiplication if a b and c were first declared as double or float or int variables So the code of the calculation should look like the

following

double a b c

a =

b =

c = a b

Clearly variables a and b need to be assigned with values of typed double Now looking back at inputboxes A and B we made

those boxes using the JTextField component meaning that any data or value written in a JTextField is of typed Text or String even

if the value displayed in the box is ldquo125rdquo But f irst here there is this task of getting to that Text (or String) values ldquo125rdquo and ldquo72rdquo

In Java we can get to a text in JTextField component using the getText() method So to retrieve values in jTextField1 and

jTextField2 we can use

jTextField1getText()

and

jTextField2getText()

But those values are still in text mode and we need another method to convert the data mode f rom text to double For that we can

use the method DoubleparseDouble() Thus the whole process of retrieve text -gt convert text to double -gt assign to double

variable can be done using

a = DoubleparseDouble( jTextField1getText() )

and

b = DoubleparseDouble( jTextField2getText() )

Hence the code for performing the multiplication becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

Up to this point we have the variable c as the result of the multiplication of a and b So the next thing to do is to display this

result on the JLabel that we have already prepared before

A x B = _____

The text A x B = ______ was made using JLabel component and its variable name is jLabel3 Since the variable c is of type

double and text property of JLabel is of type String we need to convert it first to String data type The method to convert a double

to String is DoubletoString() So we can write the code as DoubletoString(c) After getting the text we can now use it to be the

text displayed on jLabel3 To do that we use the method setText() Hence the appropriate code for displaying the result is

jLabel3setText( A x B = + DoubletoString(c) )

And the full code for our calculation becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

jLabel3setText( A x B = + DoubletoString(c) )

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5858

[ Back ]

Last Updated ( Tuesday 18 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

Page 50: Computional Physics

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5058

Figure 91

2 Create UI class

- Right click on the project name in the Project pane

on the popup menu select New --gt JFrame Form

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5158

Figure 92

- In the New JFrame Form dialog specify the Class Name and the Package name

eg Class Name multiplierUI Package mymultiplier

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5258

Figure 93

After clicking on the Finish button we should get something like the following

Figure 94

Last Updated ( Tuesday 18 September 2007 )

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5358

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5458

9th Step (Cont)

Written by Mohd Khalid Kasmin

Tuesday 11 September 2007

Step by Step into Java Programming

for University Physics Student

By

Mohd Khalid Kasmin

B Building user interface (UI) - putting in the components

Now that we have a proper blank form we can start filling the form with the suitable graphical components For introduction to

GUI programming an appropriate application to build (for a physics student) is a simple solver So our first exercise is to build an

application that takes two numbers and multiply them and display the result

With GUI we usually give input values to an application via input boxes that are labelled When all inputs are typed in properlywe click on a button to ask the application to execute the job Hence in this exercise well do just that The target now is to

construct a user interface that looks like Figure 95 below

Figure 95

Home Profile Teaching Links Contact Me My Work

Newsflash

Joomla 10 - Experience the Freedom It has never been easier to create your own dynamic site Manage all your content from

the best CMS admin interface

search

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5558

[ Back ]

1 Problemapplication description box

- In the palette pane click on Swing component named JTextPane (Note click just once) Move the mouse pointer on top of

your JFrame (Note just move not drag) When you see a little white box with orange border position it somewhere near the

top part of your JFrame and click once The JTextPane is then attached on the JFrame Addjust its size with the help of the

sizing hook around the perimeter of the JTextPane

- Edit the text property of the JTextPane with the description of your application

2 Input boxes

- In the palette pane click on Swing component named JTextField Next move the mouse pointer on top of your JFrame

When you see a white box with the word jTextField1 position it to the left and under the JTextPane1

- Click on Swing component JTextField in the palette pane again Move the mouse pointer on top of the JFrame you should

get a second JTextField named jTextField2 Position it to the right of jTextField1

Last Updated ( Tuesday 11 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5658

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5758

multiplication if a b and c were first declared as double or float or int variables So the code of the calculation should look like the

following

double a b c

a =

b =

c = a b

Clearly variables a and b need to be assigned with values of typed double Now looking back at inputboxes A and B we made

those boxes using the JTextField component meaning that any data or value written in a JTextField is of typed Text or String even

if the value displayed in the box is ldquo125rdquo But f irst here there is this task of getting to that Text (or String) values ldquo125rdquo and ldquo72rdquo

In Java we can get to a text in JTextField component using the getText() method So to retrieve values in jTextField1 and

jTextField2 we can use

jTextField1getText()

and

jTextField2getText()

But those values are still in text mode and we need another method to convert the data mode f rom text to double For that we can

use the method DoubleparseDouble() Thus the whole process of retrieve text -gt convert text to double -gt assign to double

variable can be done using

a = DoubleparseDouble( jTextField1getText() )

and

b = DoubleparseDouble( jTextField2getText() )

Hence the code for performing the multiplication becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

Up to this point we have the variable c as the result of the multiplication of a and b So the next thing to do is to display this

result on the JLabel that we have already prepared before

A x B = _____

The text A x B = ______ was made using JLabel component and its variable name is jLabel3 Since the variable c is of type

double and text property of JLabel is of type String we need to convert it first to String data type The method to convert a double

to String is DoubletoString() So we can write the code as DoubletoString(c) After getting the text we can now use it to be the

text displayed on jLabel3 To do that we use the method setText() Hence the appropriate code for displaying the result is

jLabel3setText( A x B = + DoubletoString(c) )

And the full code for our calculation becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

jLabel3setText( A x B = + DoubletoString(c) )

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5858

[ Back ]

Last Updated ( Tuesday 18 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

Page 51: Computional Physics

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5158

Figure 92

- In the New JFrame Form dialog specify the Class Name and the Package name

eg Class Name multiplierUI Package mymultiplier

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5258

Figure 93

After clicking on the Finish button we should get something like the following

Figure 94

Last Updated ( Tuesday 18 September 2007 )

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5358

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5458

9th Step (Cont)

Written by Mohd Khalid Kasmin

Tuesday 11 September 2007

Step by Step into Java Programming

for University Physics Student

By

Mohd Khalid Kasmin

B Building user interface (UI) - putting in the components

Now that we have a proper blank form we can start filling the form with the suitable graphical components For introduction to

GUI programming an appropriate application to build (for a physics student) is a simple solver So our first exercise is to build an

application that takes two numbers and multiply them and display the result

With GUI we usually give input values to an application via input boxes that are labelled When all inputs are typed in properlywe click on a button to ask the application to execute the job Hence in this exercise well do just that The target now is to

construct a user interface that looks like Figure 95 below

Figure 95

Home Profile Teaching Links Contact Me My Work

Newsflash

Joomla 10 - Experience the Freedom It has never been easier to create your own dynamic site Manage all your content from

the best CMS admin interface

search

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5558

[ Back ]

1 Problemapplication description box

- In the palette pane click on Swing component named JTextPane (Note click just once) Move the mouse pointer on top of

your JFrame (Note just move not drag) When you see a little white box with orange border position it somewhere near the

top part of your JFrame and click once The JTextPane is then attached on the JFrame Addjust its size with the help of the

sizing hook around the perimeter of the JTextPane

- Edit the text property of the JTextPane with the description of your application

2 Input boxes

- In the palette pane click on Swing component named JTextField Next move the mouse pointer on top of your JFrame

When you see a white box with the word jTextField1 position it to the left and under the JTextPane1

- Click on Swing component JTextField in the palette pane again Move the mouse pointer on top of the JFrame you should

get a second JTextField named jTextField2 Position it to the right of jTextField1

Last Updated ( Tuesday 11 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5658

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5758

multiplication if a b and c were first declared as double or float or int variables So the code of the calculation should look like the

following

double a b c

a =

b =

c = a b

Clearly variables a and b need to be assigned with values of typed double Now looking back at inputboxes A and B we made

those boxes using the JTextField component meaning that any data or value written in a JTextField is of typed Text or String even

if the value displayed in the box is ldquo125rdquo But f irst here there is this task of getting to that Text (or String) values ldquo125rdquo and ldquo72rdquo

In Java we can get to a text in JTextField component using the getText() method So to retrieve values in jTextField1 and

jTextField2 we can use

jTextField1getText()

and

jTextField2getText()

But those values are still in text mode and we need another method to convert the data mode f rom text to double For that we can

use the method DoubleparseDouble() Thus the whole process of retrieve text -gt convert text to double -gt assign to double

variable can be done using

a = DoubleparseDouble( jTextField1getText() )

and

b = DoubleparseDouble( jTextField2getText() )

Hence the code for performing the multiplication becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

Up to this point we have the variable c as the result of the multiplication of a and b So the next thing to do is to display this

result on the JLabel that we have already prepared before

A x B = _____

The text A x B = ______ was made using JLabel component and its variable name is jLabel3 Since the variable c is of type

double and text property of JLabel is of type String we need to convert it first to String data type The method to convert a double

to String is DoubletoString() So we can write the code as DoubletoString(c) After getting the text we can now use it to be the

text displayed on jLabel3 To do that we use the method setText() Hence the appropriate code for displaying the result is

jLabel3setText( A x B = + DoubletoString(c) )

And the full code for our calculation becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

jLabel3setText( A x B = + DoubletoString(c) )

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5858

[ Back ]

Last Updated ( Tuesday 18 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

Page 52: Computional Physics

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5258

Figure 93

After clicking on the Finish button we should get something like the following

Figure 94

Last Updated ( Tuesday 18 September 2007 )

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5358

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5458

9th Step (Cont)

Written by Mohd Khalid Kasmin

Tuesday 11 September 2007

Step by Step into Java Programming

for University Physics Student

By

Mohd Khalid Kasmin

B Building user interface (UI) - putting in the components

Now that we have a proper blank form we can start filling the form with the suitable graphical components For introduction to

GUI programming an appropriate application to build (for a physics student) is a simple solver So our first exercise is to build an

application that takes two numbers and multiply them and display the result

With GUI we usually give input values to an application via input boxes that are labelled When all inputs are typed in properlywe click on a button to ask the application to execute the job Hence in this exercise well do just that The target now is to

construct a user interface that looks like Figure 95 below

Figure 95

Home Profile Teaching Links Contact Me My Work

Newsflash

Joomla 10 - Experience the Freedom It has never been easier to create your own dynamic site Manage all your content from

the best CMS admin interface

search

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5558

[ Back ]

1 Problemapplication description box

- In the palette pane click on Swing component named JTextPane (Note click just once) Move the mouse pointer on top of

your JFrame (Note just move not drag) When you see a little white box with orange border position it somewhere near the

top part of your JFrame and click once The JTextPane is then attached on the JFrame Addjust its size with the help of the

sizing hook around the perimeter of the JTextPane

- Edit the text property of the JTextPane with the description of your application

2 Input boxes

- In the palette pane click on Swing component named JTextField Next move the mouse pointer on top of your JFrame

When you see a white box with the word jTextField1 position it to the left and under the JTextPane1

- Click on Swing component JTextField in the palette pane again Move the mouse pointer on top of the JFrame you should

get a second JTextField named jTextField2 Position it to the right of jTextField1

Last Updated ( Tuesday 11 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5658

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5758

multiplication if a b and c were first declared as double or float or int variables So the code of the calculation should look like the

following

double a b c

a =

b =

c = a b

Clearly variables a and b need to be assigned with values of typed double Now looking back at inputboxes A and B we made

those boxes using the JTextField component meaning that any data or value written in a JTextField is of typed Text or String even

if the value displayed in the box is ldquo125rdquo But f irst here there is this task of getting to that Text (or String) values ldquo125rdquo and ldquo72rdquo

In Java we can get to a text in JTextField component using the getText() method So to retrieve values in jTextField1 and

jTextField2 we can use

jTextField1getText()

and

jTextField2getText()

But those values are still in text mode and we need another method to convert the data mode f rom text to double For that we can

use the method DoubleparseDouble() Thus the whole process of retrieve text -gt convert text to double -gt assign to double

variable can be done using

a = DoubleparseDouble( jTextField1getText() )

and

b = DoubleparseDouble( jTextField2getText() )

Hence the code for performing the multiplication becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

Up to this point we have the variable c as the result of the multiplication of a and b So the next thing to do is to display this

result on the JLabel that we have already prepared before

A x B = _____

The text A x B = ______ was made using JLabel component and its variable name is jLabel3 Since the variable c is of type

double and text property of JLabel is of type String we need to convert it first to String data type The method to convert a double

to String is DoubletoString() So we can write the code as DoubletoString(c) After getting the text we can now use it to be the

text displayed on jLabel3 To do that we use the method setText() Hence the appropriate code for displaying the result is

jLabel3setText( A x B = + DoubletoString(c) )

And the full code for our calculation becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

jLabel3setText( A x B = + DoubletoString(c) )

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5858

[ Back ]

Last Updated ( Tuesday 18 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

Page 53: Computional Physics

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5358

[ Back ]

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5458

9th Step (Cont)

Written by Mohd Khalid Kasmin

Tuesday 11 September 2007

Step by Step into Java Programming

for University Physics Student

By

Mohd Khalid Kasmin

B Building user interface (UI) - putting in the components

Now that we have a proper blank form we can start filling the form with the suitable graphical components For introduction to

GUI programming an appropriate application to build (for a physics student) is a simple solver So our first exercise is to build an

application that takes two numbers and multiply them and display the result

With GUI we usually give input values to an application via input boxes that are labelled When all inputs are typed in properlywe click on a button to ask the application to execute the job Hence in this exercise well do just that The target now is to

construct a user interface that looks like Figure 95 below

Figure 95

Home Profile Teaching Links Contact Me My Work

Newsflash

Joomla 10 - Experience the Freedom It has never been easier to create your own dynamic site Manage all your content from

the best CMS admin interface

search

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5558

[ Back ]

1 Problemapplication description box

- In the palette pane click on Swing component named JTextPane (Note click just once) Move the mouse pointer on top of

your JFrame (Note just move not drag) When you see a little white box with orange border position it somewhere near the

top part of your JFrame and click once The JTextPane is then attached on the JFrame Addjust its size with the help of the

sizing hook around the perimeter of the JTextPane

- Edit the text property of the JTextPane with the description of your application

2 Input boxes

- In the palette pane click on Swing component named JTextField Next move the mouse pointer on top of your JFrame

When you see a white box with the word jTextField1 position it to the left and under the JTextPane1

- Click on Swing component JTextField in the palette pane again Move the mouse pointer on top of the JFrame you should

get a second JTextField named jTextField2 Position it to the right of jTextField1

Last Updated ( Tuesday 11 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5658

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5758

multiplication if a b and c were first declared as double or float or int variables So the code of the calculation should look like the

following

double a b c

a =

b =

c = a b

Clearly variables a and b need to be assigned with values of typed double Now looking back at inputboxes A and B we made

those boxes using the JTextField component meaning that any data or value written in a JTextField is of typed Text or String even

if the value displayed in the box is ldquo125rdquo But f irst here there is this task of getting to that Text (or String) values ldquo125rdquo and ldquo72rdquo

In Java we can get to a text in JTextField component using the getText() method So to retrieve values in jTextField1 and

jTextField2 we can use

jTextField1getText()

and

jTextField2getText()

But those values are still in text mode and we need another method to convert the data mode f rom text to double For that we can

use the method DoubleparseDouble() Thus the whole process of retrieve text -gt convert text to double -gt assign to double

variable can be done using

a = DoubleparseDouble( jTextField1getText() )

and

b = DoubleparseDouble( jTextField2getText() )

Hence the code for performing the multiplication becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

Up to this point we have the variable c as the result of the multiplication of a and b So the next thing to do is to display this

result on the JLabel that we have already prepared before

A x B = _____

The text A x B = ______ was made using JLabel component and its variable name is jLabel3 Since the variable c is of type

double and text property of JLabel is of type String we need to convert it first to String data type The method to convert a double

to String is DoubletoString() So we can write the code as DoubletoString(c) After getting the text we can now use it to be the

text displayed on jLabel3 To do that we use the method setText() Hence the appropriate code for displaying the result is

jLabel3setText( A x B = + DoubletoString(c) )

And the full code for our calculation becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

jLabel3setText( A x B = + DoubletoString(c) )

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5858

[ Back ]

Last Updated ( Tuesday 18 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

Page 54: Computional Physics

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5458

9th Step (Cont)

Written by Mohd Khalid Kasmin

Tuesday 11 September 2007

Step by Step into Java Programming

for University Physics Student

By

Mohd Khalid Kasmin

B Building user interface (UI) - putting in the components

Now that we have a proper blank form we can start filling the form with the suitable graphical components For introduction to

GUI programming an appropriate application to build (for a physics student) is a simple solver So our first exercise is to build an

application that takes two numbers and multiply them and display the result

With GUI we usually give input values to an application via input boxes that are labelled When all inputs are typed in properlywe click on a button to ask the application to execute the job Hence in this exercise well do just that The target now is to

construct a user interface that looks like Figure 95 below

Figure 95

Home Profile Teaching Links Contact Me My Work

Newsflash

Joomla 10 - Experience the Freedom It has never been easier to create your own dynamic site Manage all your content from

the best CMS admin interface

search

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5558

[ Back ]

1 Problemapplication description box

- In the palette pane click on Swing component named JTextPane (Note click just once) Move the mouse pointer on top of

your JFrame (Note just move not drag) When you see a little white box with orange border position it somewhere near the

top part of your JFrame and click once The JTextPane is then attached on the JFrame Addjust its size with the help of the

sizing hook around the perimeter of the JTextPane

- Edit the text property of the JTextPane with the description of your application

2 Input boxes

- In the palette pane click on Swing component named JTextField Next move the mouse pointer on top of your JFrame

When you see a white box with the word jTextField1 position it to the left and under the JTextPane1

- Click on Swing component JTextField in the palette pane again Move the mouse pointer on top of the JFrame you should

get a second JTextField named jTextField2 Position it to the right of jTextField1

Last Updated ( Tuesday 11 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5658

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5758

multiplication if a b and c were first declared as double or float or int variables So the code of the calculation should look like the

following

double a b c

a =

b =

c = a b

Clearly variables a and b need to be assigned with values of typed double Now looking back at inputboxes A and B we made

those boxes using the JTextField component meaning that any data or value written in a JTextField is of typed Text or String even

if the value displayed in the box is ldquo125rdquo But f irst here there is this task of getting to that Text (or String) values ldquo125rdquo and ldquo72rdquo

In Java we can get to a text in JTextField component using the getText() method So to retrieve values in jTextField1 and

jTextField2 we can use

jTextField1getText()

and

jTextField2getText()

But those values are still in text mode and we need another method to convert the data mode f rom text to double For that we can

use the method DoubleparseDouble() Thus the whole process of retrieve text -gt convert text to double -gt assign to double

variable can be done using

a = DoubleparseDouble( jTextField1getText() )

and

b = DoubleparseDouble( jTextField2getText() )

Hence the code for performing the multiplication becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

Up to this point we have the variable c as the result of the multiplication of a and b So the next thing to do is to display this

result on the JLabel that we have already prepared before

A x B = _____

The text A x B = ______ was made using JLabel component and its variable name is jLabel3 Since the variable c is of type

double and text property of JLabel is of type String we need to convert it first to String data type The method to convert a double

to String is DoubletoString() So we can write the code as DoubletoString(c) After getting the text we can now use it to be the

text displayed on jLabel3 To do that we use the method setText() Hence the appropriate code for displaying the result is

jLabel3setText( A x B = + DoubletoString(c) )

And the full code for our calculation becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

jLabel3setText( A x B = + DoubletoString(c) )

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5858

[ Back ]

Last Updated ( Tuesday 18 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

Page 55: Computional Physics

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5558

[ Back ]

1 Problemapplication description box

- In the palette pane click on Swing component named JTextPane (Note click just once) Move the mouse pointer on top of

your JFrame (Note just move not drag) When you see a little white box with orange border position it somewhere near the

top part of your JFrame and click once The JTextPane is then attached on the JFrame Addjust its size with the help of the

sizing hook around the perimeter of the JTextPane

- Edit the text property of the JTextPane with the description of your application

2 Input boxes

- In the palette pane click on Swing component named JTextField Next move the mouse pointer on top of your JFrame

When you see a white box with the word jTextField1 position it to the left and under the JTextPane1

- Click on Swing component JTextField in the palette pane again Move the mouse pointer on top of the JFrame you should

get a second JTextField named jTextField2 Position it to the right of jTextField1

Last Updated ( Tuesday 11 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 9th Step (Cont) fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5658

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5758

multiplication if a b and c were first declared as double or float or int variables So the code of the calculation should look like the

following

double a b c

a =

b =

c = a b

Clearly variables a and b need to be assigned with values of typed double Now looking back at inputboxes A and B we made

those boxes using the JTextField component meaning that any data or value written in a JTextField is of typed Text or String even

if the value displayed in the box is ldquo125rdquo But f irst here there is this task of getting to that Text (or String) values ldquo125rdquo and ldquo72rdquo

In Java we can get to a text in JTextField component using the getText() method So to retrieve values in jTextField1 and

jTextField2 we can use

jTextField1getText()

and

jTextField2getText()

But those values are still in text mode and we need another method to convert the data mode f rom text to double For that we can

use the method DoubleparseDouble() Thus the whole process of retrieve text -gt convert text to double -gt assign to double

variable can be done using

a = DoubleparseDouble( jTextField1getText() )

and

b = DoubleparseDouble( jTextField2getText() )

Hence the code for performing the multiplication becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

Up to this point we have the variable c as the result of the multiplication of a and b So the next thing to do is to display this

result on the JLabel that we have already prepared before

A x B = _____

The text A x B = ______ was made using JLabel component and its variable name is jLabel3 Since the variable c is of type

double and text property of JLabel is of type String we need to convert it first to String data type The method to convert a double

to String is DoubletoString() So we can write the code as DoubletoString(c) After getting the text we can now use it to be the

text displayed on jLabel3 To do that we use the method setText() Hence the appropriate code for displaying the result is

jLabel3setText( A x B = + DoubletoString(c) )

And the full code for our calculation becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

jLabel3setText( A x B = + DoubletoString(c) )

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5858

[ Back ]

Last Updated ( Tuesday 18 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

Page 56: Computional Physics

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5658

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5758

multiplication if a b and c were first declared as double or float or int variables So the code of the calculation should look like the

following

double a b c

a =

b =

c = a b

Clearly variables a and b need to be assigned with values of typed double Now looking back at inputboxes A and B we made

those boxes using the JTextField component meaning that any data or value written in a JTextField is of typed Text or String even

if the value displayed in the box is ldquo125rdquo But f irst here there is this task of getting to that Text (or String) values ldquo125rdquo and ldquo72rdquo

In Java we can get to a text in JTextField component using the getText() method So to retrieve values in jTextField1 and

jTextField2 we can use

jTextField1getText()

and

jTextField2getText()

But those values are still in text mode and we need another method to convert the data mode f rom text to double For that we can

use the method DoubleparseDouble() Thus the whole process of retrieve text -gt convert text to double -gt assign to double

variable can be done using

a = DoubleparseDouble( jTextField1getText() )

and

b = DoubleparseDouble( jTextField2getText() )

Hence the code for performing the multiplication becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

Up to this point we have the variable c as the result of the multiplication of a and b So the next thing to do is to display this

result on the JLabel that we have already prepared before

A x B = _____

The text A x B = ______ was made using JLabel component and its variable name is jLabel3 Since the variable c is of type

double and text property of JLabel is of type String we need to convert it first to String data type The method to convert a double

to String is DoubletoString() So we can write the code as DoubletoString(c) After getting the text we can now use it to be the

text displayed on jLabel3 To do that we use the method setText() Hence the appropriate code for displaying the result is

jLabel3setText( A x B = + DoubletoString(c) )

And the full code for our calculation becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

jLabel3setText( A x B = + DoubletoString(c) )

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5858

[ Back ]

Last Updated ( Tuesday 18 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

Page 57: Computional Physics

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5758

multiplication if a b and c were first declared as double or float or int variables So the code of the calculation should look like the

following

double a b c

a =

b =

c = a b

Clearly variables a and b need to be assigned with values of typed double Now looking back at inputboxes A and B we made

those boxes using the JTextField component meaning that any data or value written in a JTextField is of typed Text or String even

if the value displayed in the box is ldquo125rdquo But f irst here there is this task of getting to that Text (or String) values ldquo125rdquo and ldquo72rdquo

In Java we can get to a text in JTextField component using the getText() method So to retrieve values in jTextField1 and

jTextField2 we can use

jTextField1getText()

and

jTextField2getText()

But those values are still in text mode and we need another method to convert the data mode f rom text to double For that we can

use the method DoubleparseDouble() Thus the whole process of retrieve text -gt convert text to double -gt assign to double

variable can be done using

a = DoubleparseDouble( jTextField1getText() )

and

b = DoubleparseDouble( jTextField2getText() )

Hence the code for performing the multiplication becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

Up to this point we have the variable c as the result of the multiplication of a and b So the next thing to do is to display this

result on the JLabel that we have already prepared before

A x B = _____

The text A x B = ______ was made using JLabel component and its variable name is jLabel3 Since the variable c is of type

double and text property of JLabel is of type String we need to convert it first to String data type The method to convert a double

to String is DoubletoString() So we can write the code as DoubletoString(c) After getting the text we can now use it to be the

text displayed on jLabel3 To do that we use the method setText() Hence the appropriate code for displaying the result is

jLabel3setText( A x B = + DoubletoString(c) )

And the full code for our calculation becomes

double a b c

a = DoubleparseDouble(jTextField1getText())

b = DoubleparseDouble(jTextField1getText())

c = a b

jLabel3setText( A x B = + DoubletoString(c) )

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

031220

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5858

[ Back ]

Last Updated ( Tuesday 18 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122

Page 58: Computional Physics

8132019 Computional Physics

httpslidepdfcomreaderfullcomputional-physics 5858

[ Back ]

Last Updated ( Tuesday 18 September 2007 )

lt Prev Next gt

Home Contact Us News Links

Copyright copy 2008 Web Pages of Mohd Khalid Joomla is Free Software released under the GNUGPL License

Pages of Mohd Khalid - 10th Step fileEpengajaran_pengurusanComputational_physic SSP2122