for engineers and scientists

by Meera Joseph, University of Johannesburg

Java has revolutionised software development in the IT fi eld for many years. This article which takes a tutorial approach and object reusability would benefi t scientists and engineers with some programming experience.

Engineering, science and creative programming require a lot of /* Calculating and displaying the fi nal velocity */ imagination, analysis and computation. With top-down design for ( double mi=20000.0; mi<31000.0; mi=mi+1000.0 ) methodology and easy introduction to simple GUI for inputs and outputs { Java is used from cell phones to smart cards. Unlike C++ and Excel, Java double fi nvel = Math.fl oor(vt + vrel*(Math.log(mi/mf))); can create good graphical user interfaces with less lines of code. The JOptionPane.showMessageDialog(null,”Java for engineers and fact is use of Java technology in the engineering and scientifi c fi eld has Scientists\n”+ become unavoidable. ”\nTSIOLKOVSKII’S EQUATION\n”+ ”\nRocket initial velocity\t R”+ vt + “m/s” + Baby steps in Java programming ”\nGas relative velocity\t “ + vrel + “m/s” + ”\nRocket fi nal mass\t “ + mf + “kg”+”\nRocket initial mass\t “ + mi + Java is an object-oriented portable programming language and is a ”kg”+”\n\nRocket fi nal velocity “ consequence of a research project funded by Sun Microsystems and +fi nvel+ “m/s”,”Tsiolkovskii’s Equation”, developed in a C++ based language named Oak in 1991. Sun later JOptionPane.INFORMATION_MESSAGE); renamed it as Java. Java programs consist of classes which include } methods. However programmers use existing classes from Java class } libraries (Java API) or create their own. Reusability of the classes helps } programmers build reliable systems. Bungee jumping is a popular Operating systematically sport. A cord of fi xed length D is attached to an elastic Here are some of the engineering/scientifi c examples solved using Java cord of unstretched length coding with relevant outputs. l and force constant k. The Tsiolkovskii’s equation applies Newton’s law of motion to rocket fl ight. static cord is attached to the The following equation is applied sequentially to each rocket stage to ankles of the jumper. For a calculate fi nal velocity. jumper of mass m, the value of the maximum length of the Final velocity of the rocket - Initial velocity of the rocket = Velocity of stretched bungee system, the rocket motor exhaust gas relative to the rocket * log (Initial mass of the rocket / fi nal mass of the rocket) where g is the Output of Tsiolkovskii’s equation. acceleration of gravity. The Java source code Tsiolkovskii’s equation.java is used to solve this The following problem engineering problem. computes ymax for m,k and l for D =1,5 to 10m in steps of 0,5 /* Tsiolkovskii’s equation.java*/ (The values m, k and l are given in the program.) /* @author Meerajoseph */ Java coding for is given in Bunjee jumpers.java. /* java packages – collection of classes*/ import javax..*; /* Bunjee jumpers beware.java*/ import javax.swing.JOptionPane; // program uses JOptionPane /** * * @author MeeraJoseph * * **/ public class eq { import javax.swing.*; /* main method begins execution of Java application*/ import javax.swing.JOptionPane; public static void main(String Tsiolkovskii[]){ public class bungeejump{ /*JOptionPane class asking user to input data in a dialog box*/ /* main method begins execution of Java application*/ String s11=JOptionPane.showInputDialog(“Rocket initial velocity”); public static void main(String init[]){ String s22=JOptionPane.showInputDialog(“Gas relative velocity”); String s11=JOptionPane.showInputDialog(“Weight of the bungee String s33=JOptionPane.showInputDialog(“Rocket fi nal mass”); jumper”); double vt=Double.parseDouble(s11); double m=Double.parseDouble(s11); double vrel=Double.parseDouble(s22); double g=9.8; double l=10.0; double k=10; /* convert numbers from type String to type Double*/ for ( double D=1.5; D<=10.0; D=D+0.5 ) double mf=Double.parseDouble(s33); {

Elektron May 2005 59 //calls the Math class method for calculation (int)(Math.random()*156)));// set random drawing color double Ymax = Math.fl oor(l + D + (m*g/k) + ((1/k) * (Math.sqrt(2 * gr.fi ll(s); } } // end method paint Math.pow(m,2) * Math.pow(g,2) + 2*k*m*g *(l+D))))); Readers are welcome to try Java coding for the graphics output 2. JOptionPane.showMessageDialog(null,”Java for engineers and Scientists\n”+ Java can also be used to generate charts like Excel. The link http://www. ”\nBUNGEE JUMPERS’ BEWARE\n”+ jfree.org/jfreechart/index.html contains the free for ”\nWeight of the jumper\t R”+ m + “m/s” + generating charts. ”Length D”+ D+ An alternative for graphing other than using Java 2D is Java Objects for ”\n\n mAX: LENGTH OF THE STRETCHED BUNJEE SYSTEM\nYmax Science (JSci), an open-source package which lets you create 2D bar graphs, = l + D pie charts, and line graphs in Swing. The classes used to create graphs in + mg/k + 1/k * (2m2g2 + 2 kmg (l+D))(1/2)“ Swing are located in the JSci.swing package. More information in http://jsci. +Ymax+ “m/s”,”Bunjee jumpers beware”, sourceforge.net/ JOptionPane.INFORMATION_MESSAGE); } } } A sample line graph created using JSci.swing.JLineGraph API component Graphics with Java is shown in Line Graph [1] To begin drawing you need an understanding of the coordinate system Applets (using JApplet) are Java programs embedded in a HyperText and graphics context. Markup Language (HTML) which are downloaded into a Web Browser A part of the Java code which creates the Output of graphics problem 1. for execution.

Output of graphics problem 1. Line graph [1].

/* @author Meerjose – Draw shapes with Java2D*/ A sample Applet which calculates monthly public void paint( Graphics g) installment is shown { in Monthly instalment super.paint(g); // calls super class paint calculator. Graphics2D g2d = ( Graphics2D ) g; int xP[]={60,72,114,78,88,60,32,42,6,48}; (This program involves int yP[]={5,41,41,59,101,77,101,59,41,41}; a lot of mathematical Graphics2D gr = (Graphics2D) g; calculations). GeneralPath s=new GeneralPath(); Conclusion s.moveTo(xP[0]*5,yP[0]*7); //set the initial coordinates of General path All developers/ Monthly instalment calculator. for (int ct=5;ct

References 1. http://www-106.ibm.com/developerworks/java/library/j-2dgraphics/index. html?dwzone=java 2. www.dietel.com 3. http://www.jfree.org/jfreechart/index.html 4. www.exepc.com/~culp/rockets/rct_eqn.html 5. http://www.efunda.com/formulae/formula_index.cfm 6. To download a powerful Java integrated development environment use the following link- http://developers.sun.com/ 7. S.C.Bloch, C.2003, Excel for Engineers and Scientists, Second Edition, Wiley, USA Contact Meera Joseph, University of Johannesburg, Graphics output 2. Tel (011) 406-2255, [email protected] ‰

60 Elektron May 2005