You are on page 1of 33

PROGRAM NO - 1

Design a GUI application having student details data form as shown below.

Code:

Double click on Generate button and write the following code…..

String text1 = titleTextField.getText();


String text2 = firstNameTextField.getText();
String text3 = lastNameTextField.getText();
String text4 = classTextField.getText();
String text5 = sectionTextField.getText();

textArea.setText(text1 +'.' +text2 +'.'+text3 +"is studying in" +text4 +text5);

1
PROGRAM NO - 2

Design an application for a Football Match’s Scoreboard. Each team’s score should be
maintained. Whenever a team scores a goal, it should be reflected on the Scoreboard. When
the result is declared, depending upon the scoreboard, the winning team should be declared.

Code:

Double click on Declare Result button and write the following code…..

String v1 = tAGoals.getText(); String v2 = tBGoals.getText();


int ga = Integer.parseInt(v1); int gb = Integer.parseInt(v2);

String res = (ga>gb ? "Team A wins" : (ga<gb) ? "Team B wins" : "Match Draw");
result.setText(res);

2
PROGRAM NO - 3

Calculate commission for the salesmen. The commission is calculated according to


following rates.
Sales Commission Rate
30,001/- onwards 15%
22,001/- to 30,000/- 10%
12,001/- to 22,000/- 7%
5,001/- to 12,000/- 3%
0 to 5,000/- 0%
Code:
Double click on Calculate Commission button and write the following code…..
//obtaining input
String txt = SalesTF.getText();
double sales, comm;
sales = Double.parseDouble(txt);
// calculate commission
if(sales>30000)
comm = sales * 0.15;
else if(sales > 22000)
comm = sales * 0.10;
else if(sales > 12000)
comm = sales * 0.07;
else if(sales > 5000)
comm = sales * 0.03;
else
comm=0;
//display output
CommLabel.setText("The commission is Rs." +comm);

3
PROGRAM NO – 4

Design a special sale at a store, a 10% discount is taken on purchases over Rs. 1000/-. Write
a program that asks for the amount of purchases, then calculate the discounted price.

Code:

Double click on Calculate button and write the following code…..

double amt = Double.parseDouble(AmtTF.getText());


double disc, discount, discamt;

// Calculate Discount

if (amt > 1000)


disc = 10;
else
disc = 0;

discount = amt * disc/100;


discamt = amt - discount;

//display result

DiscLabel.setText("Discount =" + disc +"%");


result.setText("" + discamt);

4
PROGRAM NO - 5

Given a GUI application having a layout as shown below:

Code:

Double click on Print Button and write the following code…..

String val = inputTF.getText();


int n = Integer.parseInt(val);
int i=2;
for(; i<=n; i+=2)
{
String txt = reslbl.getText();
reslbl.setText(txt +" "+i);
}

5
PROGRAM NO - 6

Design a GUI application that performs arithmetic operations ( +, -, *, / and %).


Code:

Double click on Add Button and write the following code…..

double num1 = Double.parseDouble(num1TF.getText());


double num2 = Double.parseDouble(num2TF.getText());
double num3 = num1 + num2;
num3TF.setText(""+num3);

Subtract , Multiplication, Divide, Mod BUTTONS copy and paste the same block of code
and change the desired operator to get the result.

Clear Button:

num1TF.setText(null);
num2TF.setText(null);
num3TF.setText(null);

Exit Button:

System.exit(0);

6
PROGRAM NO - 7

Design a GUI application having interface as having if statements check to find the largest of
3 numbers.

Code:

Double click on Find Largest button and write the following code…….

int x = Integer.parseInt (number1.getText());


int y = Integer.parseInt (number2.getText());
int z = Integer.parseInt (number3.getText());

//determining largest number


int max = 0;
max = x;
if(y>max)
max = y;
if(z>max)
max = z;

//setting output

largeResult.setText(""+max);

7
PROGRAM NO - 8

Design a GUI application interface for creating “multiplication table” of a given number.

Code:

Double Click on Result Button and write the following code…………….

int num1 = Integer.parseInt(input.getText());


int q=1;
while(q<=10)
{
output.append(num1+ " x " +q +" = " +num1 * q + "\n");
q++;
}

Double Click on Clear Button and write the following code…………….

output.setText("");
input.setText("");

8
PROGRAM NO - 9

Design a GUI application having interface, the loop for the frame should print numbers from
lowest to highest on the basis of given two numbers, upon clicking at “Count” button.

Code:

Double click on the Count Button and write the following code……..

int start = Integer.parseInt(startNumber.getText());


int end = Integer.parseInt(endNumber.getText());

for (int i= start; i<=end; i+=1)


{
textArea.setText(textArea.getText()+i +" ");
}

Double click on the Reset Button and write the following code…….
startNumber.setText(null);
endNumber.setText(null);
textArea.setText(null);

Double click on the Exit Button and write the following code…….
System.exit(0);

9
PROGRAM NO - 10

Design a GUI application having interface for the percentage marks are to be entered in the
text box and upon clicking at the button, corresponding grade (as per the following rules)
should be displayed in the picture box below command button.
Marks % Grade
>=90 A++
80 – 90 A+
75 – 80 A
60 – 75 B
50 – 60 C
40 – 50 D
<40 Fail
Code:
Double click on Calculate Final Grade Button and write the following code…………..
int per = Integer.parseInt(TextField1.getText());
if(per>=90)
result.setText(" A++");
else if(per>=80 && per<90)
result.setText(" A+ ");
else if(per>=75 && per<80)
result.setText(" A ");
else if(per>=60 && per<75)
result.setText(" B ");
else if(per>=50 && per<60)
result.setText(" C ");
else if(per>=50 && per<60)
result.setText(" D ");
else
result.setText(" Fail ");

10
PROGRAM NO – 11

Design a GUI application that has a list and a few controls such as a Label, a button and a
text field. The list displays some colors. When a user selects a color from the list, the
background of the appropriate control (depending upon user’s choice) should be changed.

Code:

Since we need to work with colors, thus we need to import Color class of java.awt.
Thus, in the source editor, at the top of all the code, type…………….

import java.awt.Color;

Select Colors List - Right click - Events - ListSelection - ValueChanged in the


code editor type the following code………..

int i;
Color x = Color.WHITE;
i = colorsLst.getSelectedIndex();
switch(i)
{
case 0: x = Color.RED;
break;
case 1: x = Color.BLUE;
break;
case 2: x = Color.GREEN;
break;
case 3: x = Color.MAGENTA;
break;
case 4: x = Color.CYAN;
break;
case 5: x = Color.YELLOW;

break;
case 6: x = Color.GRAY;
break;
}

11
if (lblChk.isSelected())
Lbl.setBackground(x);
else
Lbl.setBackground(Color.WHITE);
if(btnChk.isSelected())
Btn.setBackground(x);
else
Btn.setBackground(Color.WHITE);
if(tfChk.isSelected())
TF.setBackground(x);
else
TF.setBackground(Color.WHITE);

Checkbox 1: Apply on Label:-


Add the same code to CheckBox created for Apply on Label  Right click - Events -
Action - actionPerformed.

Checkbox 2: Apply on Button:-


Add the same code to CheckBox created for Apply on Button right click - Events -
Action - actionPerformed.

Checkbox 3: Apply on TextField:-


Add the same code to CheckBox created for Apply on TextFieldright click - Events -
Action - actionPerformed.

12
PROGRAM NO – 12

Design the GUI interface for the ComBox. The user should enter a city name in text field.
After entering the city name in the text field, when the user press a button to add city to
combox, the list of city names to be added to the combox.

Code:

Double click on the Add to Com Box button and the write the following code……………

String city = TextField1.getText();

String sel = (String) CityCB.getSelectedItem();


CityCB.addItem(city);

13
PROGRAM NO - 13

Design a GUI application that obtains the price and quantity of an item, calculates the sale-
value, discount and net payable amount. Discount is calculated as 10% of the sale-value.
Code:

Double click on the Calculate Button and write the following code…………………..

String txt1 = priceTextField.getText();


double price = Double.parseDouble(txt1);

String txt2 = qtyTextField.getText();


double qty = Double.parseDouble(txt2);

double salAmt = price*qty;


double disc = salAmt * 0.10;
double netAmt = salAmt - disc;

saleAmtTextField.setText(""+salAmt);
discTextField.setText(""+disc);
netAmtTextField.setText(""+netAmt);

14
PROGRAM NO – 14

Department of Human Resources along with Department of Higher Education wants to


conduct an online survey of school student’s time spending habits and their subject choices.
It will help them design a better education policy and create better placement opportunities.
Design a GUI application for this online survey.

Code:

Double click on Cancel Button and write the following code……….


nameTF.setText("");
eduLst.clearSelection();
subLst.clearSelection();
tvCB.setSelected(false);
movCB.setSelected(false);
readCB.setSelected(false);
artCB.setSelected(false);
playCB.setSelected(false);
cookCB.setSelected(false);
advCB.setSelected(false);
indCB.setSelected(false);
netCB.setSelected(false);
socCB.setSelected(false);
othCB.setSelected(false);

15
PROGRAM NO - 15

FRX Consultancy is registering job seekers for placement. Their basic entry form has the
following features.
(a) All the characters entered in text field should be converted into uppercase characters.
(b) When submit button SubmitBtn is clicked the following things should happen:
1) If checkbox PgtCB is selected checkboxes GrandCB and InterCB should also get
selected.
2) If GrandCB is selected InterCB should also be selected.
3) A message box should display “Hello Mr……….. you are registered” for male and
“Hello Miss ………. You are selected”.
(c) When clear button ClearBtn is clicked the textbox, checkboxes and the option button
should be set to Male.

Code:

(a) Right Click NameTF text field - Events - Focus - FocusLost and write the
following code to convert input text in to UpperCase as given in question.

String str = NameTF.getText().toUpperCase();


NameTF.setText(str);

(b) Double click on the SubmitBtn and write the following code……

if(PgtCB.isSelected() == true)
{
GrandCB.setSelected(true);
InterCB.setSelected(true);
}
if(GrandCB.isSelected() == true)
{
InterCB.setSelected(true);
}
String str = null;
if(MaleRB.isSelected())
{
str = "Hello Mr. " + NameTF.getText() + " you are registered.";
}
else if(FemaleRB.isSelected())
{
str = "Hello Miss. " + NameTF.getText() + " you are selected.";
}
JOptionPane.showMessageDialog(null, str);
16
(c) Double click on the ClearBtn and write the following code……

GrandCB.setSelected(false);
InterCB.setSelected(false);
PgtCB.setSelected(false);
NameTF.setText(null);
MaleRB.setSelected(true);
FemaleRB.setSelected(false);

17
PROGRAM NO – 16

The given form calculates the GCD(HCF) of two numbers. Write code for the command button
(cmdGcd) to print the GCD in the label (lblGcd).

Code:

Double click on GCDBtn and write the following code……

int n1 = Integer.parseInt(Num1TF.getText().toString());
int n2 = Integer.parseInt(Num2TF.getText().toString());
int t;
while (n2!=0)
{
t = n2;
n2 = n1%t;
n1 = t;
}
gcdTF.setText("" + n1);

18
PROGRAM NO – 17

Write a program to print the sum of digits of the given number


(For example if the input number is 123456, the program should calculate the sum
(1+2+3+4+5+6) and display the output as 21.

Code:

int num = Integer.parseInt(numTF.getText());


int sum=0;
int rem;
while(n>0)
{
rem = n % 10;
sum = sum + rem;
n = n / 10;
}
OutLbl.setText("Sum of its digits is :" +sum);

19
PROGRAM NO - 18

Write a program reverse of given number

Code:

int num = Integer.parseInt(numTF.getText());


int rev=0;
int rem;
while(n>0)
{
rem = n % 10;
rev = (rev * 10 ) + rem;
n = n / 10;
}
OutLbl.setText("Reverse of given number is :" +rev);

20
PROGRAM NO - 19

Write a program to find the factorial of a given number

Code:

int num = Integer.parseInt(n.getText());

int factorial = 1;

for(int i=1; i<=num; i++)

factorial = factorial * i;

output.setText("The factorial of given number is:" +factorial);

OUTPUT

21
PROGRAM NO - 20

Write a program to retrieve data from the database using JDBC. Design a GUI for table to
retrieve the data.

package jdbc;
import java.sql.*;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

Write the code by double clicking on the retrieve button.

DefaultTableModel model=(DefaultTableModel)employee12.getModel();
try
{
Class.forName("java.sql.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/prasad", "root",
"");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from employee12");

while(rs.next())
{
model.addRow(new Object[ ]
{
rs.getInt(1),rs.getString(2),rs.getString(3),rs.getString(4)}
);
}
rs.close();
st.close();
con.close();
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null, "Error in Connectivity" +e);

22
OUTPUT

23
PROGRAM NO - 21

Write HTML code to design the table.

<HTML>
<HEAD>
<STYLE>
TD,TH,TABLE
{
BORDER:2PX SOLID red;
}
</STYLE>
</HEAD>
<img align = "right" border = 5px solid
src = "C:\Users\Chaitanya\Desktop\sclogo.jpg" width = 100 height = 100>
</img>
<H1><CENTER>MY STUDY TIME TABLE FOR THE WEEK </CENTER></H1>
<H2><CENTER>SRI CHAITANYA COLLEGE</CENTER></H2>
<TABLE STYLE="WIDTH:1200PX">
<TR>
<TH>DAYS</TH>
<TH>08:20 - 09:10</TH>
<TH>09:10 - 10:00</TH>
<TH>10:10 - 11:00</TH>
<TH>11:00 - 11:50</TH>
<TH>11:50 - 12:40</TH>
<TH>01:10 - 02:00</TH>
<TH>02:00 - 02:50</TH>
<TH>03:00 - 03:50</TH>
<TH>03:50 - 04:40</TH>
<TH>04:40 - 05:20</TH>
</TR>
<TR>
<TH>Monday</TH>
<TH>ENGLISH</TH>
<TH>MATH</TH>
<TH>PHYSICS</TH>
<TH>CHEMISTRY</TH>
<TH>BIOLOGY</TH>
<TH>COMP.SCIENCE</TH>
24
<TH>LANG</TH>
<TH>IP</TH>
<TH>LAB-PHY</TH>
<TH>LAB-CHEM</TH>
</TR>
<TR>
<TH>Tuesday</TH>
<TH>ENGLISH</TH>
<TH>MATH</TH>
<TH>PHYSICS</TH>
<TH>CHEMISTRY</TH>
<TH>BIOLOGY</TH>
<TH>COMP.SCIENCE</TH>
<TH>LANG</TH>
<TH>IP</TH>
<TH>LAB-PHY</TH>
<TH>LAB-CHEM</TH>
</TR>
</TABLE>
<MARQUEE DIRECTION="right"> ENJOY THE LIFE AS A STUDENT!!!</MARQUEE>
</BODY>
</HTML>

25
PROGRAM NO - 22

Write a HTML code to design the form

<html>
<head>
<title> forms creation </title>
</head>
<img align = "right" border = 5px solid src = "C:\Users\Chaitanya\Desktop\sclogo.jpg"
width = 100 height = 100>
</img>
<center> <h1> APPLICATION FORM </h1> </center>

<form method="get" action="forms.html">


Enter Name:
<input type="text" name="fname" size="30" maxlength="60"> <br> <br>

Password:
<input type="password" name="pass" size="30" maxlength="15"> <br> <br>

Address:
<textarea name="add" rows="3" cols="20" maxlength=250>
</textarea> <br> <br>
<input type="checkbox" name="paper1" value="paper1">
<label for="paper1"> I LANG </label>

<input type="checkbox">
<label for="paper2"> II LANG </label> <br>

<input type="radio" name="p1" value="p1">


paper 1
<input type="radio" name="paper2" value="paper 2">
paper 2 <br> <br>

Examination Centers
<select name="Examination Centers" size="1">
<option value="Bng"> BANGLORE
<option value="Tpt"> TIRUPATHI
<option value="mas"> CHENNAI
<option value="cst"> MUMBAI
26
</select> <br> <br>

Location
<select name="Examination Centers" size="1">
<optgroup label="Bangalore">
<option value="seshadripuram"> Seshadripuram
<option value="rt nagar"> R.T.Nagar
<option value="hebbal"> Hebbala
<option value="Bangsouth"> Koramangala
<option value="Jayanagar"> Jayanagar
<optgroup label="Tirupathi"> TIRUPATHI
<option value="tptcity"> City
<option value="outcuts"> Outcuts
<optgroup label="chennai"> CHENNAI
<option value="Adayar"> Adayar
<option value="T.Nagar"> T.Nagar
<optgroup label="Hyderabad"> HYDERABAD
<option value="Hitech City"> Hitechs
<option value="Dsnr"> Dilsukhnagar
</select> <br>
<center>
<input type="submit" value="SUBMIT">
<br>
<br>
<input type="reset" value="CLEAR">
</form>
</body>
</html>

27
PROGRAM NO - 23

Consider a database “LOANS” with the following data.

Accno Cust_Name Loan_Amount Installments Int_Rate Start_Date


1 RK Gupta 300000 36 12.00 2009-07-19
2 SP Sharma 500000 48 10.00 2008-03-22
3 KP Jain 300000 36 NULL 2007-03-08
4 MP Yadav 800000 60 10.00 2008-12-06
5 SP Sinha 200000 36 12.50 2010-01-03
6 P Sharma 700000 60 12.50 2008-06-05
7 KS Dhall 500000 48 NULL 2008-03-05

1) Create the database loans


Mysql> create database loans;

2) Use the database loans


Mysql> use loans

3) Create the table loans


Mysql> create table loans (accno int, cust_name char(20), loan_amount int, installments int(2),
int_rate decimal(5,2), start_date date);

4) Show the description of the table


Mysql> desc loans;

5) Insert records into the table


Mysql> insert into loans values(1, ‘RK Gupta’, 300000, 36, 12.00, ‘2009-07-19’);

6) Display the details of the table loans


Mysql> select * from loans;
7) Display the accno, cust_name and loan_amount of all the loans
Mysql> select accno, cust_name, loan_amount from loans;

8) Display the details of all the loans with less than 40 installments
Mysql> select * from loans where installments < 40;

9) Display the accno, loan_amount of all the loans started before 2009-01-04
Mysql> select accno, loan_amount from loans where start_date < ’2009-01-04’;

28
10) Display the int_rate of all the loans started after 2009-01-04
Mysql> select int_rate from loans where start_date > ‘2009-01-04’;

11) Display the details of all the loans whose rate of interest is NULL
Mysql> select * from loans where int_rate is null;

12) Display the details of all the loans whose rate of interest is not NULL
Mysql> select * from loans where int_rate is not null;

13) Display the amounts of various loans from the table loans. Only a loan amount should
appear
Mysql> select distinct(loan_amount) from loans;

14) Display the details of all the loans started after 2008-12-31 for which the number of
installments are more than 36.
Mysql> select * from loans where start_date > ‘2008-12-31’ AND installments > 36;

15) Display the cust_Name and loan_amount for all the loans which don’t have number of
installments 36
Mysql> select cust_name, loan_amount from loans where installments < > 36;

16) Display the details of all the loans whose rate of interest is in the range of 11% to 12%
Mysql> select * from loans where int_rate >=11.00 AND int_rate <=12.00;

17) Display the cust_name and loan_amount for all the loans for which the number of
installments are 24,36 or 48 (using IN )
Mysql> select cust_name, loan_amount from loans where installments IN (24,36,48);

18) Display the details of all the loans whose loan_amount is in the range of 400000 to 500000
using(BETWEEN)
Mysql> select * from loans where loan_amount BETWEEN(400000 AND 500000);
19) Display the details of all the loans whose rate of interest is in the range of 11% to 12% (using
BETWEEN)
Mysql> select * from loans where int_rate BETWEEN (11 AND 12);

20) Display the accno, cust_name and loan_amount for all the loans for which the cust_name
ends with sharma
Mysql> select accno, cust_name, loan_amount from loans where cust_name LIKE ‘%sharma’;

29
21) Display the accno, cust_name and loan_amount for all the loans for which the cust_name
contains letter ‘a’
Mysql> select accno, cust_name, loan_amount from loans where cust_name LIKE ‘% a %’;

22) Display the accno, cust_name and loan_amount for all the loans for which the cust_name
does not contain letter ‘p’
Mysql> select accno, cust_name, loan_amount from loans where cust_name NOT LIKE
‘ % p %’;

23) Display the details of all the loans in the ascending order of their loan_amount
Mysql> select * from loans ORDER BY loan_amount ASC;

24) Display the details of all the loans in the descending order of their start_date
Mysql> select * from loans ORDER BY start_date DESC;

25) Change the interest rate as 11.50% for the loans which interest rate is NULL
Mysql> update loans SET int_rate = 11.50 where int_rate is NULL;

26) Increase the interest rate by 0.5% for all the loans for which the loan amount is more than
400000
Mysql> update loans SET int_rate = int_rate + 0.5 where loan_amount > 400000;

27) Delete the records of all the loans whose start date is before 2007
Mysql> delete from loans where year (start_date) < 2007;

28) Add the column category of type char(2) in the loan table
Mysql> alter table loans add(category char(2));

30
PROGRAM NO - 24

Write the OUTPUT of the following queries

1) Mysql> select cust_name, LENGTH(cust_name), LCASE(cust_name), UCASE(cust_name)


from loans where int_rate < 11.00;

2) Mysql> select RIGHT(cust_name,3), SUBSTR(cust_name,5), MID(cust_name,5) from loans;

3) Mysql> select DAYNAME(start_date) from loans;

4) Mysql> select POW(3,2);

5) Mysql> select ROUND(543.5694,2), ROUND(543.5694, -2);

6) Mysql> select CONCAT(‘HARRY’, ‘POTTER’);

7) Mysql> select YEAR (curdate( )), MONTH(curdate( )), DAY(curdate( ));

8) Mysql> select LEFT (‘UNICODE’, 3), RIGHT(‘UNICODE’, 4);

9) Mysql> select INSTR(‘UNICODE’, ‘CD’), INSTR(‘UNICODE’, ‘D’);

10) Mysql> select MID(‘INFORMATICS’, 3,4), SUBSTR(‘PRACTICES’, 3);

31
PROGRAM NO – 25

Consider the following two table’s items and bills and write the SQL statements for the
following queries

ITEMS TABLE
ICode Name Category Rate
1001 Masala Dosa South Indian 60
1002 Vada Sambar South Indian 40
1003 Idly Sambar South Indian 40
2001 Chowmein Chinese 80
2002 Dimsum Chinese 60
2003 Soup Chinese 50
3001 Pizza Italian 240
3002 Pasta Italian 125

BILLS TABLE
Bill No Date Item code Qty
1 2010-04-01 1002 2
1 2010-04-01 3001 1
2 2010-04-01 1001 3
2 2010-04-01 1002 1
2 2010-04-01 2003 2
3 2010-04-02 2002 1
4 2010-04-02 2002 4
4 2010-04-02 2003 2
5 2010-04-03 2003 2
5 2010-04-03 3001 1
5 2010-04-03 3002 3

32
1) Display the average rate of south Indian items
Mysql> select AVG(rate) from items where category = ‘south indian’;

2) Display the number of items in each category


Mysql> select category, COUNT( * ) from items GROUP BY category;

3) Display the total quantity sold for each item


Mysql> select name, SUM(Qty) from items I, bills B where I.icode=B.icode GROUP BY name;

4) Display total quantity of each item sold but do not display this data for the items whose total
quantity sold is less than 3
Mysql> select name, SUM(Qty) from items I, bills B where I.icode=B.icode GROUP BY name
HAVING SUM(Qty) > 3;

5) Display the details of bill records along with name of each corresponding item
Mysql> select name, billno, billcode, date, qty from items I, bills B where I.icode=B.icode;

6) Display the details of the bill records for which the item is ‘dosa’
Mysql> select name, billno, billcode, date, qty from items I, bills B where I.icode=B.icode AND
name=’dosa’;

7) Display the bill records for each Italian item sold


Mysql> select category, billno, billcode, date, qty from items I, bill B where I.icode=B.icode
AND category = ‘Italian’;

8) Display the total value of items sold for each bill


Mysql> select billno, SUM(qty * rate) from items I, bills B where I.icode = B.icode GROUP BY
billno;

33

You might also like