Java Programs To Be Written in Practical Files
Java Programs To Be Written in Practical Files
Output:
2. Write a Java Program to input the Principal Amount, Rate of Interest and Time(in years).
The Program will calculate the Simple and Compound Interest
A) private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
double p,r,t,i;
p=Double.parseDouble(jTextField1.getText());
r=Double.parseDouble(jTextField2.getText());
t=Double.parseDouble(jTextField3.getText());
i=p*r*t/100;
jTextField4.setText(" "+i);
}
Output:
3. Write a Java Program to input the Lower Limit and Upper Limit and print the sum of Series
between them(including them)
A) private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
int a,b,i,sum=0;
a=Integer.parseInt(jTextField1.getText());
b=Integer.parseInt(jTextField2.getText());
for(i=a;i<=b;i++) {
sum=sum+i;
}
jTextField3.setText(" "+sum);
}
Output:
4. Write a Java Program to input a number in a text field and calculate its Factorial in another
text field.
A) private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
int n,i;
long f=1;
n=Integer.parseInt(jTextField1.getText());
for(i=1;i<=n;i++) {
f=f*i; }
jTextField2.setText(" "+f);
}
Output:
5. Write a Java Program to input a number and check whether it is a Prime or Composite
Number.
A) private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
int n,f=0,i;
n=Integer.parseInt(jTextField1.getText());
for(i=1;i<=n;i++) {
if(n%i==0)
f++;}
if(f==2)
jTextField2.setText("PRIME");
else
jTextField2.setText("COMPOSITE");
}
Output:
6. Write a Java Program to input a number in a text field and print all its Factors in a text
area.
A) private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
int a,i;
a=Integer.parseInt(jTextField1.getText());
for(i=1;i<=a;i++){
if(a%i==0)
jTextArea1.append(String.valueOf(i)+"\n");
}}
Output:
7. Write a Java Program to input the terms and print the Fibonacci Series up to that terms.
For e.g. if the input is 7, then output is:
1 1 2 3 5 8 13
A) private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
int a=1,b=1,c=2;
int i,n;
n=Integer.parseInt(jTextField1.getText());
jTextArea1.append(String.valueOf(a)+" ");
jTextArea1.append(String.valueOf(b)+" ");
jTextArea1.append(String.valueOf(c)+" ");
for(i=1;i<=n-3;i++){
a=b;
b=c;
c=a+b;
jTextArea1.append(String.valueOf(c)+" "); }
Output:
8. Write a Java Program to input a multi-digit number and print the sum of the digits.
A) private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
int n,n1,sum=0;
n=Integer.parseInt(jTextField1.getText());
while(n!=0) {
n1=n%10;
sum=sum+n1;
n=n/10; }
jTextField2.setText(" "+sum);
}
Output:
9. Write a Java Program to input a multi-digit number in one text field and display the number
in reversed form in another text field.
A) private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
int n,n1,rev=0;
n=Integer.parseInt(jTextField1.getText());
while(n!=0) {
n1=n%10;
rev=rev*10+n1;
n=n/10;
}
jTextField2.setText(" "+rev);
}
Output:
10. Write a Java Program to input the purchase amount and calculate the payable amount
depending on the payment mode selected.
If Payment mode is CASH- 35% discount is offered.
If Payment mode is CARD- 20% discount is offered.
A) private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
double p,amt=0;
p=Double.parseDouble(jTextField1.getText());
if(jRadioButton1.isSelected())
amt=p-p*35.0/100;
else if(jRadioButton2.isSelected())
amt=p-p*20.0/100;
jTextField2.setText(" "+amt);
}
Output:
11. Write a Java Program to input the number of movie tickets purchased and calculate and
print the total bill as per the following:
General Ticket= Rs 50/person
DC = Rs 80/person
Box = Rs 100/person
Tax Free movie is sold with a discount of 30%
A) private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
int no;
double amt=0;
no=Integer.parseInt(jTextField1.getText());
if(jRadioButton1.isSelected())
amt=no*50;
else if(jRadioButton2.isSelected())
amt=no*80;
else if(jRadioButton3.isSelected())
amt=no*100;
if(jCheckBox1.isSelected())
amt=amt-amt*30.0/100;
jTextField2.setText(" "+amt);
}
Output:
12. Write a Java Program to select the medical tests to be done and calculate the total bill as
per the following:
Hemoglobin Test= Rs 70
Thyroid Test = Rs 350
Blood Sugar = Rs 100
SGPT = Rs 400
A) private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
int amount=0;
if(jCheckBox1.isSelected())
amount=amount+70;
if(jCheckBox2.isSelected())
amount=amount+350;
if(jCheckBox3.isSelected())
amount=amount+100;
if(jCheckBox4.isSelected())
amount=amount+400;
jTextField1.setText(" "+amount);
}
Output:
13. Write a Java Program to input the height in Centimeter and convert it into equivalent meter
and feet.
A) private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
double cm,m,feet;
cm=Double.parseDouble(jTextField1.getText());
m=cm/100.0;
feet=cm*0.032;
jTextField2.setText(" "+m);
jTextField3.setText(" "+feet);
}
Output:
14. Write a Java Program to enter the name, select class and enter the marks in 6 subjects.
The program will calculate a report display the total marks in a text field, percentage and
grade through dialog box. The program also has Clear button to clear all the text fields
and Close button to terminate the application.
A) // Code for Clear Button.
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
jTextField1.setText(" ");
jTextField2.setText(" ");
jTextField3.setText(" ");
jTextField4.setText(" ");
jTextField5.setText(" ");
jTextField6.setText(" ");
jTextField7.setText(" ");
jTextField8.setText(" ");
jTextField1.requestFocus();
}
Output:
15. Write a Java Program to input the name, principal loan amount, select the years of payment
(5 years, 10 years, 15 years, 20 years), enter the rate of payment. The program calculates
and displays the total payable amount in a text field and EMI to be paid through a dialog
box. The program also has Clear button to clear all the text fields and Close button to
terminate the application.
A) private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
jTextField1.setText(" ");
jTextField2.setText(" ");
jTextField3.setText(" ");
jTextField4.setText(" ");
jRadioButton1.setSelected(true);
jTextField1.requestFocus();
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
String name=jTextField1.getText();
double p,r=0,amount,emiperyear,emipermonth;
int t=0;
p=Double.parseDouble(jTextField2.getText());
if(jRadioButton1.isSelected()){
t=5;
r=7.5; }
else if(jRadioButton2.isSelected()) {
t=10;
r=8.75;
}
else if(jRadioButton3.isSelected()) {
t=15;
r=10.25;
}
else if(jRadioButton4.isSelected()) {
t=20;
r=13.75;
}
amount=p*Math.pow((1+r/100), t);
amount=Math.round(amount);
jTextField4.setText(" "+amount);
emiperyear=amount/t;
emipermonth=emiperyear/12.0;
emipermonth=Math.round(emipermonth);
JOptionPane.showMessageDialog(null,name+" "+"has to pay"+" "+"Rs"+" "+emipermonth+"
"+"per month");
}
Output:
---------------------------