Escolar Documentos
Profissional Documentos
Cultura Documentos
INSTRUCTED BY: MR. THARANGA GUNARATHNE NAME: SENEVIRATNE W.D.J.G.C. STUDENT ID: EN12ME3052 GROUP NO: Q FIELD: MECHATRONICS DATE OF PER: 2012-01-05
1
1. Introduction
During my first lab session I was able to compile my first java program using Eclipse. Eclipse is software which includes an Integrated Development Environment (IDE). Its widely used to develop java programs. After some basic java exercises, we started to compile programs which were developed using a java package (library) called swing. Swing, was used to display messages and to collect user data from a window screen. Swing package contains classes for doing things with JOptionPane. This class was referred as JOptionPane.class. This report contains the exercises that were done during the lab 1 and the homework.
Figure.1: source code for the lab exercise 2.1 System.out.println (Hello World); The term System refers to the class and out is the object and println is the method used to print in the console. In java we use the ; to represent the end of the statement to the compiler.
2.2 Lab Exercise 2 When doing the above lab exercise i was able to recognize some major differences between using command line and Eclipse IDE. When you get into the use of eclipse, its very hard to go back to the use of command line because Eclipses environment is very user friendly. Following are some advantages and disadvantages that I came across when doing lab exercises in eclipse. When writing programs
Text editors
Have to use two programs, one to compile and one to write the program source level debugging is not available
Eclipse IDE
Have to remember keywords and have to keep in track of the symbols that are being used Syntax Checking not available Not user friendly When compiling programs
Command line
Debugging is hard (as need to go through source code from the beginning) Compiler integration is not available Need few commands before compiling
Eclipse IDE
Easy to debug programs
When executing programs Command Line Cant execute applets Errors are not further describe Eclipse Even applets can be execute in the same window Errors are specifically describe and shown clearlt
In this lab exercise, I was able to write a simple program called Name Printer which displays my name inside a box (box is made using symbols) in that console.
Figure 3: Source code of the program Name Printer During this lab exercise I had to used to System.out.println( ); for 3 times. Two of them were used to indicate the top and botom of the box and one to include the name. Below is a screen shot of the output from the lab exercise
Figure 5: Eclipse clearly indicate compilation errors As you can see in Figure 5 I made a mistake when typing this code (I forgot to add the ; at the end) But thanks to Eclipse it clearly indicate my mistake.
This lab exercise is similar to the previous lab exercise (Lab Exercise 2.4) However; during this exercise JOptionPane.showInputDialog was called. The name written inside the text field is assigned to a string variable, which is then printed in the console. Figure 8, Figure 9 and Figure 10 shows the screen shots of the source code and the output respectively.
10
Lab Exercise 2.6 This lab exercise is like a combination of lab exercise 2.4 and 2.5. This
program asks the users name and then asks if the program can do anything for the user, then it denies anything said by the user. It uses both, text fields and message boxes. Below is the source code for the program.
Figure 12: Source code of the program I have assigned the users name to String name variable and what user likes to do to String like variable. Below are some screen shots of the exercise.
11
Figure 15: Message box displaying the denial of the users command
12
// Note that a this() invocation must be the BEGINNING of // statement body of constructor public Point2d (Point2d pt) { // Another consructor x = pt.getX(); y = pt.getY(); // a better method would be to replace the above code with // this (pt.getX(), pt.getY()); // especially since the above code does not initialize the // variable debug. } public void dprint (String s) { // print the debugging string only if the "debug" // data member is true if (debug) System.out.println("Debug: " + s); } public void setDebug (boolean b) { debug = b; } public void setX(double px) { dprint ("setX(): Changing value of X from " + x + " to " + px ); x = px; } public double getX() { return x; } public void setY(double py) { dprint ("setY(): Changing value of Y from " + y + " to " + py ); y = py; } public double getY() { return y; } public void setXY(double px, double py) { setX(px); setY(py); } public double distanceFrom (Point2d pt) { double dx = Math.abs(x - pt.getX()); double dy = Math.abs(y - pt.getY()); // check out the use of dprint() dprint ("distanceFrom(): deltaX = " + dx); dprint ("distanceFrom(): deltaY = " + dy); return Math.sqrt((dx * dx) + (dy * dy)); public double distanceFromOrigin () { return distanceFrom (new Point2d ( )); } public String toStringForXY() { String str = "(" + x + ", " + y; return str; } public String toString() { String str = toStringForXY() + ")"; return str; } }
13
04. Conclusion
In sum up, developing java using Eclipses integrated development environment is much easier than using text editor and Command line. As many are using Eclipse to develop java programs, help and support is vitally available in the net. From the lab exercises, we learned how java uses its classes, object and methods in object oriented programming environment. Got familiar with how to handle JOptionPane when calling for message boxes and input boxes. Due to carelessness some syntax and compile errors occurred but they were taken care of later.
05. References
Big Java, 3rd edition by C. Horstmann http://www2.cs.uic.edu/~sloan/CLASSES/java/ http://docs.oracle.com/javase/tutorial/essential/exceptions/handling.html
14