0% found this document useful (0 votes)
27 views

11_IT Unit-5 Java

Uploaded by

M Safi Ikram
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

11_IT Unit-5 Java

Uploaded by

M Safi Ikram
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Class 11 IT – JAVA Programming using NetBeans IDE

NetBeans IDE is used to create java applications very easily using the efficient GUI
builder.

Creating a New Project


The steps to create a new project are:
1. Select New Project from the File menu. You can also click the New Project button in
the IDE toolbar.
2. In the Categories pane, select the General node. In the Projects pane, choose the Java
Application type. Click the Next button.
3. Enter the name of the project in the Project Name field and specify the project
location. Do not create a Main class here.
4. Click the Finish button.

JAVA SWING
Swing is a set of classes that provides more powerful and flexible components than
are possible with AWT (Abstract Windows Toolkit). It supplies buttons, chekboxes,
labels, tabbed panes, scrool pans, trees, tables, dialog boxes etc.
The swing controls/components are categorized as:
 Components : Self contained graphic entity ( eg. JLabel, JBotten, JTextField etc)
 Containers: Component that can hold other components (eg. JPanel, JFrame,
JDialog, JWindow)

COMMONLY AVAILABLE SWING CONTROLS IN JAVA


 jFrame: A Frame is a container control, in which all the controls can be lace.
 jLabel: JLable allows placing un-editable text on the Frame/Panel
 jTextField: JTextFeild allows placing editable text on the Frame/Pane. User can
enter text in a text field during runtime.
 jbutton: is used to initiate an action when it is clicked.
 jList: is a group of values or items from which one or more selections can be
made.
 jComboBox: jComboBox is similar to jList but also allow to enter editable text
 during run time. It is a combination of jTextFiled and jList.
 jRadioButton: Allow us to choose a single item from a group of jRadioButton
 options.
 jCheckBox: Allow us to choose one or more items from a group of jCheckBox
 options.
 jPasswordField: Allow us to enter a text during the run time but shows an
encrypted text instead of the original text.
 jTextArea: JTextArea is a multi-line text component to enter or edit text.

Swing Controls Methods and Properties:


Methods are used to perform some action on the object. For example to display
something in a textfield you can use its setText() method, to extract the contents of a
textfield you can use its getText() method.
Methods can be divided into two categories- getters and setters.
• Getters are the methods which extract some information from the object and return it
to the program. Getters start with the word get.
Examples of getters are: getText(), getForeground(), getModel(), isEditable etc.
• Setters are the methods which set some properties of the object so that the object's
appearance changes. Setters start with the word set.
Examples of setters are: setText(), setForground(), setModel() etc.
Events:
Events are the actions which are performed on controls.
Examples of events are: mouseClick, mouseMoved,keyPressed etc.
When the user performs any action on a control, an event happens and that event
invokes (sends a call to) the corresponding part of the code and the application behaves
accordingly.

Q&A
1. Which window is used to designed the form.
Ans: Design window

2. Which window contains the Swing Controls components.


Ans: Palette window

3. What is the most suitable component to accept multiline text.


Ans: Text Area

4. What will be the output of the following command?


Learning.concat(“Java”)
Ans: Error

5. What will be the output of the following command? “Learning”.concat(“Java”)


Ans: LearningJava

6. Name the different list type controls offered by Java Swing.


Ans:
(i) jListBox
(ii) jComboBox

7. Name any two commonly used method of ListBox.


Ans: getSelectedIndex() and getSelectedValue()
8. What is jOptionPane?
Ans: We use JOptionPane when we want to request information from the user, display
information to the user or a combination of both. It requires an import statement at the
top of the program.
import javax.swing.JOptionPane;
OR
import javax.swing.*;

9. What is a variable?
Ans: Variables are containers used to store the values for some input, intermediate
result or the final result of an operation. The characteristics of a variable are:
 It has a name.
 It is capable of storing values.
 It provides temporary storage.
 It is capable of changing its value during program execution.

10.By default a combo box does not offer editing feature.How would you make a
combo box editable.
Ans: By setting its editable property to false.

11. Write Name the component classes of Swing API for the following
components-
a) frame
(b) button
Ans: (a) JFrame (b) JButton

12.What is the name of event listener interface for action events ?


Ans: ActionPerformed

13.What does getpassword() on a password field return ?


Ans: A character array.

14.What is event driven programming?


Ans: This programming style responds to the user events and is driven by the
occurrence of user events.

15.What are containers? Give examples.


Ans: Containers are those controls inside them e.g., frame (JFrame), Panel (JPanel), label
(JLabel) etc. are containers.

16.Which method of list is used to determine the value of selected item, if only
one item is selected?
Ans: getSelectedValue()

17.Which type of storage is provided by variables?


Ans: Temporary

18.What will be the output of the following code segment:


String firstName = “Abc “;
String lastName = “Xyz”;
String fullName = firstName + lastName;
jTextField1.setText(“Full Name: “);
jTextField2.setText (fullName);
Ans: Full Name: AbcXyz

19.Which expression is used to print the value of a variable “x” of type int.
Ans: jTextField1.setText(“x = ” + x);

20.The statement i++; is equivalent to


Ans: i= i+1

21.Name the primitives datatypes in java.


Ans: Numeric type , Fractional type, Character type and Boolean type.
22.Which events gets fired when a user click a JButton and JRadioButton.
Ans: ActionPerformed

23.Which of the following is a selection construct?


a. do while Loop
b. for Loop
c. while Loop
d. None of these
Ans: d . None of these

24.What will be used if there are two or more possible options?


Ans: We can use if…..else conditional statement or switch……case statement.

25.Name the loop that never ends.


Ans: Infinite loop. For example:
for( k=1;k<=10;k++)
{
System.out.println(“It is infinite loop”); k=k- 1;
}

26. Which braces is used to enclose statements in a block statement.


Ans: { } Curly braces

27. Which of the following is an exit controlled loop?


a. for loop b. do while Loop c. while loop d. none of these
Ans: do…. while loop

28. Which process is used to translate a task into a series of commands that a
computer will use to perform that task.
Ans: Project design
29. Which of the following component is the best suited to accept the country of
the user?
A. List B Combo box C Radio button D Check box
Ans: List and combo box

30. Which construct will be used to find the sum of the first 10 natural numbers?
Ans: for loop

31. Which of the following is not a good programming guideline?


Ans: Using text fields to accept input of marital status.

32. Explain the following terms:


a) IDE
b) Form
Ans: IDE : IDE is an acronym for Integrated Development Environment which is a work
environment that integrates all tools necessary for Application Development and makes
them available as part of one environment.
b) Forms: Forms are used to accept data (input) and submit data to an external agent
for processing.

33. Design a GUI application with a STOP button. This application should close on
click of this button.

// Code for STOP Button:


private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}
34. Design a GUI application to display the message "Good Morning" on the click of
the Morning button and display the message "Good Evening" on the click of the
Evening button. Add another STOP button to end the application. (Use
JOptionPane for output)

// Code for Morning Button:


private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
JOptionPane.showMessageDialog(this,"Good Morning");
}

// Code for Evening Button:


private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
JOptionPane.showMessageDialog(this,"Good Evening");
}

// Code for Stop Button:


private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
System.exit(0);
}

35. Design a GUI application to display the message "Good Morning" on the click of
the Morning button and display the message "Good Evening" on the click of the
Evening button. Add another STOP button to end the application. (Use jTextField
for output)

// Variable name for jTextField1 is txt1 and the property of txt1 is set to Not editable
text ield
// Code for Morning Button:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
txt1.setText("Good Morning");
}

// Code for Evening Button:


private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
txt1.setText("Good Evening");
}

// Code for stop Button:


private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}

36. Design a GUI application to accept the name of the user in a Text Field placed
at the top and then display a personalized time-based greeting (greeting along
with the name of the user) in the Text Field placed at the bottom.

// Variable name for jTextField1 is txt1 and jTextField2 is txt. The property of txt2 is set
to Not editable text ield.

// Code for Morning Button:


private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
txt2.setText("Good Morning, "+txt1.getText());
}

// Code for Evening Button:


private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
txt2.setText("Good Evening, "+txt1.getText());
}

// Code for STOP Button:


private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}
37. Design a GUI application to display the title of the user (Mr. or Ms.) along with
the name input in the textbox using radio buttons.

// Variable name for jTextField1 is txt1 and jTextField2 is txt2. Added component
buttonGroup1 display name is gender. The property of txt2 is set as Not editable text
ield.

// Code for Radio Button Male is:


private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {
txt2.setText("Mr. "+txt1.getText());
}

// Code for Radio Button Female is:


private void jRadioButton2ActionPerformed(java.awt.event.ActionEvent evt) {
txt2.setText("Ms. "+txt1.getText());
}

38. Design a GUI application to display personalized message for friends and
colleagues using concatenation in a text area component.

// Variable name for jTextField1 is txt1 and jTextField2 is txt2. Also, the variable name
for jTextArea1 is txt3.
// Code for FRIENDS button
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
txt3.setText(txt1.getText()+"and "+txt2.getText()+" are true friends. \nA true friend
encourages you to live your dreams.");
}

// Code for COLLEUGES button


private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
txt3.setText(txt1.getText()+" and "+txt2.getText()+" are good colleagues, \nA good
colleague is one who support you at your work place");
}

// Code for STOP button


private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}

39. Design a GUI application which displays a simple message when the user
inputs a username and password.

// Code for LOGIN button


private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
JOptionPane.showMessageDialog(null, jTextField1.getText()+" Logged in Demo
Version Successfully \n"+"(Password Ignored - will be checked later)");
}

// Code for STOP button


private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}

40. Design a GUI application to calculate the price of fruit item based on the
following design. (Amount to be paid shall be displayed using a text ield.
// Variable name for jTextField1 is txt1 and jTextField2 is txt2.

// Code for One dozen button:


private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
txt2.setText(Integer.toString(12*Integer.parseInt(txt1.getText())));
}

// Code for two dozen button


private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
txt2.setText(Integer.toString(24*Integer.parseInt(txt1.getText())));
}

// Code for STOP button


private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}

41. Design a GUI application to calculate the bill amount based on price and
quantity as per the following layout.

// Variable name for jTextField1 is txt1, jTextField2 is txt2 and jTextField3 is txt3.

// Code for Calculate button


private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

txt3.setText(Double.toString(Double.parseDouble(txt1.getText())*Double.parseDouble(t
xt2.getText())));
}

// Code for STOP button


private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}

42. Develop a simple application to display the message entered by the user
surrounded by four different characters (*, #, %, “).

// Code for * button


private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// Concatenate * to the text in
char Star;
Star='*';
jTextField2.setText(Star+jTextField1.getText()+Star);

// Code for # button


private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// Concatenate # to the text in
char Hash;
Hash='#';
jTextField2.setText(Hash+jTextField1.getText()+Hash);

}
// Code for % button
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
// Concatenate % to the text in
char Percent;
Percent='%';
jTextField2.setText(Percent+jTextField1.getText()+Percent);

}
// Code for ‘’ button
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
// Concatenate " to the text in
char Quotes;
Quotes='"';
jTextField2.setText(Quotes+jTextField1.getText()+Quotes);

// Code for STOP button


private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);

43. Design a simple calculator with addition, subtraction, multiplication, and


division capability. Also, add Reset and Exit buttons.

// Code for + button


private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
double Number1,Number2,Result;
Number1=Double.parseDouble(jTextField1.getText());
Number2=Double.parseDouble(jTextField2.getText());
Result=Number1+Number2;
jTextField3.setText(Double.toString(Result));
}

// Code for - button


private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
double Number1,Number2,Result;
Number1=Double.parseDouble(jTextField1.getText());
Number2=Double.parseDouble(jTextField2.getText());
Result=Number1-Number2;
jTextField3.setText(Double.toString(Result));
}

// Code for * button


private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {
double Number1,Number2,Result;
Number1=Double.parseDouble(jTextField1.getText());
Number2=Double.parseDouble(jTextField2.getText());
Result=Number1*Number2;
jTextField3.setText(Double.toString(Result));
}

// Code for / button


private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {
double Number1,Number2,Result;
Number1=Double.parseDouble(jTextField1.getText());
Number2=Double.parseDouble(jTextField2.getText());
Result=Number1/Number2;
jTextField3.setText(Double.toString(Result));
}

// Code for Reset button


private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
jTextField1.setText("");
jTextField2.setText("");
jTextField3.setText("");
}

// Code for Exit button


private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}

44. Design a simple interest calculator application based on the following layout.

// Code for Calculate button


private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
double Principal,Rate,SInterest;
byte Time; //Expected value not more than 127 Years
Principal=Double.parseDouble(jTextField1.getText());
Rate=Double.parseDouble(jTextField2.getText());
Time=Byte.parseByte(jTextField3.getText());
SInterest=(Principal*Rate*Time)/100; //Formula to calculate SI
jTextField4.setText(Double.toString(SInterest));

// Code for STOP button


private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}

45. Design a GUI application to accept a number from the user in a text ield and
print using option pane whether it is a positive even number or not.

Variable name for jTextField1 is txt1

Code for CHECK Button:


private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
int number;
number = Integer.parseInt(txt1.getText());
if(number%2==0){
JOptionPane.showMessageDialog(this, "The number is even");
}
else{
JOptionPane.showMessageDialog(this, "The number is odd");
}
}

Code for Exit button:


private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}
46. Design a GUI application to accept two numbers from the user using text ield
input and show the greater number using option pane.

Code for Check button:


private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
int num1;
int num2;
num1=Integer.parseInt(txt1.getText());
num2=Integer.parseInt(txt2.getText());
if (num1>num2){
JOptionPane.showMessageDialog(this, "First number is greater");
}
else if (num2>num1){
JOptionPane.showMessageDialog(this, "Second number is greater");
}
else {
JOptionPane.showMessageDialog(this, "Both numbers are equal");
}

Code for Reset button:


private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
txt1.setText("");
txt2.setText("");
}

Code for Exit button:


private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}

You might also like