Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

exercise-09-Java Network Programming-Lab Manual, Exercises of Java Programming

This lab manual is designed by Sarah Jane at Indraprastha Institute of Information Technology for subject Java Network Programming. It includes Creating, Interface, Directions, Interface, Test, Implementing, Errors, Event, Handling

Typology: Exercises

2011/2012

Uploaded on 07/03/2012

netu
netu 🇮🇳

4.5

(4)

55 documents

1 / 1

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Creating an Interface
Follow the directions
Create a project InterfaceTest
Create an interface InterFaceTest
Use the following code to create an interface
public class myFirstInterFace(){
pubic void aboutMe();
}
Save it and compile it
Implementing the interface
Following the directions
Create a class Test
add following to your class declaration
implements myFirstInterFace
add following code
public void aboutMe(){
System.out.println(“My first interface implemented by Test”);
}
Create main method
Create an object of Test
Call aboutMe method of class Test.
If you see “My first interface implemented by Test “ in output window you have successfully
implemented your first interface
Learning from errors
Comment the method aboutMe() method in your last program and look at the error in class view
pane
Event Handling
Knowing the events related to a component
Follow the directions
Create an applet or a frame using JBuilder
Open design tool
Add a command button and rename it to Click It
Click events tab (located at bottom right of content pane)
You can see the list of events related to this component
Return to the source code and add the following
[To the class definition]
implements ActionListener [to the class definition]
[at the end of your jbinit() method]
button1.addActionListener(this)
[Any where in the class but out of any method or constructor]
public void actionPerformed(ActionEvent event){
System.out.println("The Button is pressed");
}
Create an object of your frame in main method set its size and call its show method
Compile your program and click the button if you see “The Button is Pressed” you have
successfully implemented your first event handler
Re-Implement the code using an anonymous class
Create a frame and
Add three text fields with label Num1, Num2, Sum
Add a two buttons with labels Reset, Calculate
Write code to perform the following when Reset button is pressed
Clear all the text fields
Write code to perform the following when Calculate button is pressed
Add values in Num1 and Num2 and display result in Sum
Learning from JBuilder
Open source code of previous program
Some where in your code write name of a button you have created and press (. Dot )
Find method addActionListener ….see other methods for adding other listener , did you find any
method for adding some other listener
KeyListener interface has following methods
public void keyPressed(KeyEvent e);
public void keyReleased(KeyEvent e);
public void keyTyped(keyEvent e);
there are lots of constant which you can see by typing KeyListener (. Dot ) in JBuilder
Re-implement your calculation program with following modification
Remove the buttons
Implement the key listener such that
When escape key is pressed the field should be cleared
When enter key is pressed in Num1 the focus should be transferred to next field
When enter key is pressed in Num2 the result should be displayed in Sum
J
Ja
av
va
a-
-P
Pr
ro
og
gr
ra
am
mm
mi
in
ng
g
(
(D
Da
ay
y-
-0
06
6
E
Ex
xe
er
rc
ci
is
se
e-
-0
01
1)
)
Docsity.com

Partial preview of the text

Download exercise-09-Java Network Programming-Lab Manual and more Exercises Java Programming in PDF only on Docsity!

  • Creating an Interface  Follow the directions  Create a project InterfaceTest  Create an interface InterFaceTest  Use the following code to create an interface  public class myFirstInterFace(){  pubic void aboutMe();  }  Save it and compile it
  • Implementing the interface  Following the directions  Create a class Test  add following to your class declaration  implements myFirstInterFace  add following code  public void aboutMe(){  System.out.println(“My first interface implemented by Test”);  }  Create main method  Create an object of Test  Call aboutMe method of class Test.  If you see “My first interface implemented by Test “ in output window you have successfully implemented your first interface
  • Learning from errors  Comment the method aboutMe() method in your last program and look at the error in class view pane
  • Event Handling  Knowing the events related to a component  Follow the directions  Create an applet or a frame using JBuilder  Open design tool  Add a command button and rename it to Click It  Click events tab (located at bottom right of content pane)  You can see the list of events related to this component  Return to the source code and add the following [To the class definition] implements ActionListener [to the class definition]

[at the end of your jbinit() method] button1.addActionListener(this)

[Any where in the class but out of any method or constructor] public void actionPerformed(ActionEvent event){ System.out.println("The Button is pressed"); }  Create an object of your frame in main method set its size and call its show method  Compile your program and click the button if you see “The Button is Pressed” you have successfully implemented your first event handler  Re-Implement the code using an anonymous class

  • Create a frame and  Add three text fields with label Num1, Num2, Sum  Add a two buttons with labels Reset, Calculate  Write code to perform the following when Reset button is pressed  Clear all the text fields  Write code to perform the following when Calculate button is pressed  Add values in Num1 and Num2 and display result in Sum
  • Learning from JBuilder  Open source code of previous program  Some where in your code write name of a button you have created and press (. Dot )  Find method addActionListener ….see other methods for adding other listener , did you find any method for adding some other listener
  • KeyListener interface has following methods  public void keyPressed(KeyEvent e);  public void keyReleased(KeyEvent e);  public void keyTyped(keyEvent e);  there are lots of constant which you can see by typing KeyListener (. Dot ) in JBuilder
  • Re-implement your calculation program with following modification  Remove the buttons  Implement the key listener such that  When escape key is pressed the field should be cleared  When enter key is pressed in Num1 the focus should be transferred to next field  When enter key is pressed in Num2 the result should be displayed in Sum

J Jaavvaa--PPrrooggrraammmmiinngg ((DDaayy-- 0066 EExxeerrcciissee-- 0011 ))

Docsity.com