lab report 1st

15
LAB NO: 01 SOUTH ASIAN INSITUTE OF TECHNOLOGY AND MEDICINE (SAITM) FACULTY OF ENGINEERING DEPARTMENT OF MECHATRONICS ENGINEERING UG 20.01-Introduction to Computer Programming INTRODUCTION TO JAVA INSTRUCTED BY: MR. THARANGA GUNARATHNE NAME: SENEVIRATNE W.D.J.G.C. STUDENT ID: EN12ME3052 GROUP NO: Q FIELD: MECHATRONICS 1

Upload: jgayans

Post on 15-Oct-2014

44 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Lab Report 1st

LAB NO: 01

SOUTH ASIAN INSITUTE OF TECHNOLOGY AND MEDICINE (SAITM)

FACULTY OF ENGINEERING

DEPARTMENT OF MECHATRONICS ENGINEERING

UG 20.01-Introduction to Computer Programming

INTRODUCTION TO JAVA

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

DATE OF SUB: 2012-01- 12

1

Page 2: Lab Report 1st

01.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). It’s 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.

02. Lab Exercises

2.1 Lab Exercise 1

This exercise was done to understand the Eclipse’s integrated development environment. We create a simple java program to display “Hello World”. Doing this exercise made us realized about the benefits of using Eclipse. Programming and compiling in Eclipse is much easier than using a text editor and command line.

The following are the screen shots from the actions performed in exercise lab exercise 1

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

Page 3: Lab Report 1st

Figure 1.2: Output from the lab exercise 1

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, it’s very hard to go back to the use of command line because Eclipse’s 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 Eclipse IDEo Have to use two programs, one to

compile and one to write the programo Compile and writing all can be done

within Eclipse IDEo source level debugging is not available o source level debugging is availableo Code Completion option is not

availableo Code Completion option is available(no

need to specifically remember codes, can use the tab )

o Have to remember keywords and have to keep in track of the symbols that are being used

o No need to specifically memorize the key words

o Syntax Checking not available o Syntax Checking is availableo Not user friendly o User friendly environment When compiling programs

Command line Eclipse IDEo Debugging is hard (as need to go

through source code from the beginning)

o Easy to debug programs

o Compiler integration is not available o Compiler integration is availableo Need few commands before compiling o Just few clicks to compile a programs

3

Page 4: Lab Report 1st

When executing programs

Command Line EclipseCan’t execute applets Even applets can be execute in the same

windowErrors are not further describe Errors are specifically describe and shown

clearlt

Lab Exercise 2.3

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

4

Page 5: Lab Report 1st

Figure 4: output from the lab exercise 2.3

Lab Exercise 2.4

In this exercise I wrote a program to Show a message box which said “Hello World”. Using the extra java package “swing” which is used to draw windows.

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.

5

Page 6: Lab Report 1st

Figure 6: Debugged program

The line ‘Javax.swing.JOptionPane’ tells the java compiler to include the code for ‘JOptionPane’ class in our program. This lets us call the ‘JOptionPane’ class and tell it to do things with ‘OptionPanes’.

The line ‘JOptionPane.showMessageDialog(null, “Hello, World!”); ‘asks the ‘JOptionPane’ class to call its ‘showMessageBox’ method which shows a message box showing anything that is included inside quotations and word null means that the message box will appear in the center of the screen. ‘System.exit(0);‘ Tells the OS that the program is shutting down. The exit value 0 tells the system that the program ran without errors.

Below is a screenshot of the successful program

Figure 7: Program output for lab exercise 2.4

6

Page 7: Lab Report 1st

Lab Exercise 2.5

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.

Figure 8: Source code of the Lab exercise 2.5

Figure 9: Writing my name inside the text field

7

Page 8: Lab Report 1st

Figure 10: Output inside the console

The line, String name = JOptionPane.showInputDialog("What is your name?"); calls an text field and assigns the value entered into it to the string variable “name” and the line ‘System.out.println(name); ‘,prints it on the console.

Then we were asked to modify the program to display “Hello (name) “instead of just the ‘name’. Figure 11 shows the required change that has been done to satisfy the question.

Figure 11: modified source code with changes clearly indicating

Figure 12: Output, indicating the change

8

Page 9: Lab Report 1st

Lab Exercise 2.6

This lab exercise is like a combination of lab exercise 2.4 and 2.5. This program asks the user’s 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 user’s name to ‘String name ‘variable and what user likes to do to ‘String like’ variable. Below are some screen shots of the exercise.

Figure 13: Writing my name in the text field

9

Page 10: Lab Report 1st

Figure 14: Assigning ‘write’ to the ‘String like’ variable

Figure 15: Message box displaying the denial of the user’s command

03. Homework Exercises

For this exercise I used a 2D mathematical application connecting with real world basis, to compile and execute. I downloaded it from a website and run then didn’t get any errors or bugs.

Here is the source code of this program.

class Point2d { /* The X and Y coordinates of the point--instance variables */ private double x; private double y; private boolean debug; // A trick to help with debugging public Point2d (double px, double py) { // Constructor

x = px;y = py;debug = false; // turn off debugging

} public Point2d () { // Default constructor

this (0.0, 0.0); // Invokes 2 parameter Point2D constructor } // 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 trueif (debug) System.out.println("Debug: " + s); }

10

Page 11: Lab Report 1st

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;

}}

The result is given below.

11

Page 12: Lab Report 1st

04. Conclusion

In sum up, developing java using Eclipse’s 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

12