Class Notes
Class: XI
Topic: Unit-5:
Subject: Information Technology (802) Understand Integrated Development Environment
(NETBEANS)
Control Structures
We use control structures when we want to control the flow of the program. There are types of
control structures: Selection statements and Iteration statements.
Selection Statements:A selection statement selects among a set of statements depending on the value of a
controlling expression. The selection statements are the if statement and the switch statement,
which are discussed below:
Simple if Statement - The if statement allows selection (decision making) depending upon the
outcome of a condition. If the condition evaluates to true then the statement immediately
following if will be executed and otherwise if the condition evaluates to false then the
statements following the else clause will be executed. The selection statements are also called
conditional statements or decision statements.
The syntax of if statement is as shown below:
if (conditional expression)
{
Statement Block;
}
else
{
Statement Block;
}
Write a JAVA code to accept a year and check if it is a leap year.
int year = Integer.parseInt(JTF1.getText());
int r = year % 4 ;
if ( r == 0) {
JTF2.setText(" It is a leap year");
}
else {
JTF2.setText(" It is NOT a leap year");
}
JOptionPane Message:
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.*;
Method Description
showMessageDialog() Shows a one-button, modal dialog box that gives the user some information.
Example : JOptionPane.showMessageDialog(this,"Java and NetBeans");
showConfirmDialog() Shows a three-button modal dialog that asks the user a question.
User can respond by pressing any of the suitable buttons.
Example: Confirm=JOptionPane.showConfirmDialog(null,"quit?")
showInputDialog() Shows a modal dialog that prompts the user for input.
It prompts the user with a text box in which the user can enter the relevant input.
Example : name= JOptionPane.showInputDialog(this,"Name:");
WAP to check whether a person is eligibile to vote or not using ELSE condition
import javax.swing.JOptionPane;
int a= Integer.parseInt(JTF1.getText());
if (a >=18)
JOptionPane.showMessageDialog(null,"You are eligible To VOTE");
else
JOptionPane.showMessageDialog(null,"You are NOT eligible To VOTE"); }
Radio buttons are groups of buttons in which, by convention, only one button at a time can be selected.
The radio buttons are used to provide the user several choices and allow him to select one of the choices
(the radio buttons belong to a group allowing the user to select single option). But radio buttons occupy
a lot of space. Radio Button is created through jRadioButton component. (The radio button must be
attached to a button group).
The ButtonGroup takes care of unselecting the previously selected button when the user selects another
button in the group.
Method Description
getText() Retrieves the text displayed by radio button.
String str = <radiobutton-name>.getText();
isSelected() Returns true if the component is checked else returns false.
boolean b = <radiobutton-name>.isSelected();
setText() Changes the display text at runtime.
<radiobutton-name>.setText(String t);
setSelected() Checks(true) or unchecks the radio button.
<radiobutton-name>.setSelected(boolean b);
Design a JAVA application to display the result of arithmetic operations based on the option
selected by the user through RADIO Button.
int n1=Integer.parseInt(JTF1.getText());
int n2=Integer.parseInt(JTF2.getText());
int r1=0;
if (JRBAdd.isSelected()==true){
r1=n1+n2;
}
else if (JRBSub.isSelected()==true){
r1=n1-n2;
}
else if (JRBMul.isSelected()==true){
r1=n1*n2;
}
else if (JRBDiv.isSelected()==true){
r1=n1/n2;
}
result.setText(""+r1);
Using the Text Area Component and CheckBox
CheckBox is a small box like component that is either marked or unmarked. When it is clicked, it
changes from checked to unchecked or vice versa automatically.
Method Description
getText() Retrieves the text typed in String str = <checkbox-name>.getText();
isSelected() Returns true if the component is checked else returns false. boolean b = <checkbox-
name>.isSelected();
setText() Changes the display text at runtime. <checkbox-name>.setText(String t);
setSelected() Checks(true) or unchecks the checkbox. <checkbox-name>.setSelected(boolean b);
The Text Area component is used if we want to accept multiline input or want to display multiline
output. This component automatically adds vertical or horizontal scroll bars as and when required
during run time.
Handling a Password Field Component
The Password Field if we want that the text input by the user should not be displayed as characters but
as special characters (so that it is not readable by anyone). This component allows confidential input
like passwords which are single line.
We can use the Password Field if we want that the text input by the user should not be displayed as
characters but as special characters (so that it is not readable by anyone). This component allows
confidential input like passwords which are single line. Let us design a simple application which
displays a simple message when the user inputs a user name and password.
Variable Naming Conventions
Each variable needs to have a name so that it can be referenced anywhere during the application. Each
programming language has its own set of rules for naming variables. The rules and conventions for
naming variables in Java are summarized below:
● Variable names are case sensitive.
● Keywords or words, which have special meaning in java, should not be used as
the variable names.
● Variable names should be short and meaningful.
● All variable names must begin with a letter, an underscore (_) or a dollar sign ($).
The convention is to always use a letter and avoid starting variable names with
underscore (_) and dollar sign ($).
● After the first initial letter, variable names may contain letters and digits (0 to 9) and ( _, $), but no
spaces or special characters are allowed.
Switch Statement
This selection statement allows us to test the value of an expression with a series of character or integer
values. On finding a matching value the control jumps to the statement pertaining to that value and the
statement is executed, till the break statement is encountered or the end of switch is reached. The
expression must either evaluate to an integer value or a character value. It cannot be a string or a real
number.
The syntax of the switch statement is as follows:
switch (Variable/Expression)
{
case Value1:statements1 ;
break ;
case Value2:statements2 ;
break ;
.
.
default:statements3 ;
}
Write a program to display weekday using Switch Case statement.
int a = Integer.parseInt(JTF1.getText());
switch (a) {
case 1:
JTF2.setText("Monday");
break;
case 2:
JTF2.setText("Tuesday");
break;
case 3:
JTF2.setText("Wednesday");
break;
case 4:
JTF2.setText("Thursday");
break;
case 5:
JTF2.setText("Friday");
break;
case 6:
JTF2.setText("Saturday");
break;
case 7:
JTF2.setText("Sunday");
break;
default :
JTF2.setText("Invalid day No.");
break;
}
Combo Box: It allows only single item selection. It takes less space initially and drops down when the
user clicks on its arrow. User can edit if he/she wishes. It is a cross between a text field and a list.
Method Description
getSelectedItem() Retrieves the selected item.
Object result = <combobox-name>.getSelectedItem();
getSelectedIndex() Retrieves the index of the selected item.
int result = <combobox-name>.getSelectedIndex();
setModel() Sets the data model that the combo box uses to get its list of elements.
<combobox-name>.setModel (ComboBoxModel aModel);
List Box: It allows us to select more than one item. It does’nt have a drop down menu. Users can select
items directly.
Method Description
getSelectedValue() Returns the selected value when only a single item is selected,
if multiple items are selected then returns first selected value.
Returns null in case no item selected
Object result= <list-name>.getSelectedValue();
isSelectedIndex() Returns true if specified index is selected.
boolean b = <list-name>.isSelectedIndex(int index)
WAP to display the admission fee of selected Class through LIST box
String s=(String)JL.getSelectedValue();
if (s.equals("IX")==true){
JTF.setText("15000");
}
else if (s.equals("X")==true){
JTF.setText("17000");
}
else if (s.equals("XI")==true){
JTF.setText("21000");
}
else {JTF.setText("25000");
}
Repetition Structures
The ability of a computer to perform the same set of actions again and again is called looping. The
sequence of statements that is repeated again and again is called the body of the loop.
The test conditions that determine whether a loop is entered or exited is constructed using relational and
logical operators. A single pass through the loop is called an iteration.
For example, a loop that repeats the execution of the body three times goes through three iterations.
Java provides three statements – the while statement, the do while statement, and the for statement for
looping.
The for Statement
The for loop is the most widely used Java loop construct. The structure of the Java for statement is as
below:
for (counter=initial_value; test_condition;change counter)
{
statements
}
Semicolons separate the three parts of a for loop:
The initial_value initializes the value of the loop counter.
The test_condition tests whether the loop should be executed again.
The loop is exited when the test condition fails.
The step updates the counter in each loop iteration.
WAP to display the values from 1 to 5 using FOR loop
int i;
for (i=1; i <=5; i++)
{
System.out.println(“” + i);
}
WAP to display the values 5, 4, 3, 2, 1 using FOR loop.
int i;
for (i=5; i >=1; i--)
{
System.out.println(i);
}
WAP to display first 5 Even numbers using FOR loop.
int i;
for (i=2; i <=10; i=i+2)
{
System.out.println(i);
}
WAP to display first 5 Odd numbers in descending order using FOR loop.
int i;
for (i=9; i >=1; i=i-2)
{
System.out.println(i);
}
Use of Break statement inside loop
int i;
for (i=1; i <=5; i++)
{
if (i ==3) {
break;
}
System.out.println(i);
}
Use of Continous statement inside loop
int i;
for (i=1; i <=5; i++)
{
if (i ==3) {
continue;
}
System.out.println(i);
}
The while statement evaluates the test before executing the body of a loop. A while loop is an entry
controlled loop. The structure of the Java while statement is as shown:
The While Statement
while (expression)
{
statements
}
WAP to print first 5 natural numbers using WHILE loop
int i = 1;
while (i<=5)
{
System.out.println("" + i);
i++;
}
The Do While Statement
The do while statement evaluates the test after executing the body of a loop.
A do-while loop is an exit control loop.
The structure of the Java do while statement is as shown:
do
{
statements
} while (expression);
WAP to print first 5 natural numbers using do while loop
int i = 1;
do
{
System.out.println("" + i);
i++;
}
while (i <=5);
Note: ‘Content developed/prepared absolutely from home.