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

Java Project Xii

Uploaded by

aryanmaharana714
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Java Project Xii

Uploaded by

aryanmaharana714
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

JAVA Project for class XII

1. Write a java program to add and subtract two numbers.


private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code
here:
float a,b;
float sum;
a=
Float.parseFloat(jTextField1.getText());
b=
Float.parseFloat(jTextField2.getText());
sum = a+b;
jLabel3.setText(""+sum);
}

private void

jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
float a,b;
float sub;
a = Float.parseFloat(jTextField1.getText());
b= Float.parseFloat(jTextField2.getText());
sub = a-b;
jLabel3.setText(""+sub);
}
2. Write a program in java using netbeans IDE to show the reverse of a number and find the sum of digits of
the same as well?

Java code
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
int i,j,sum;
j=0;
sum=0;
String str =new String();
i=Integer.parseInt(jTextField1.getText());
while(i>0)
{
j=i%10;
i=i/10;
str=str+j;
sum=sum+j;
}
jLabel3.setText(str);
jLabel5.setText(""+sum);
}
3. Write a program to find out the area and perimeter of a rectangle by calling a user defined method.

public double getArea(int x,int y){


double Area=x*y;
return Area;
}
public double getPerimeter(int x,int y){
double Perimeter=2*(x+y);
return Perimeter;
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
//declaring necessary variables
int l,b;
double area,perimeter;
//getting values form user through IDE
l=Integer.parseInt(jTextField1.getText());
b=Integer.parseInt(jTextField2.getText());
//method call and passing values
area=getArea(l,b);
perimeter=getPerimeter(l,b);
//displaying processed result
jLabel4.setText(""+area);
jLabel7.setText(""+perimeter);
}
4. Write a java program to accept two string and display them by joining into one string (using concate()).
And take a greeting expression as third string prefix it by + operator.

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

// TODO add your handling code here:


String str1=new String();
String str2=new String();
String str3=new String();
String str4=new String();
str1=jTextField1.getText();
str2=jTextField2.getText();
str3=jTextField3.getText();
//use of library method concat()
str4=str1.concat(" "+str2);
//string joining using + operator
str4=str3+" "+str4;
jLabel4.setText(str4);
}
5. Write a program to accept a sentence and two locations (start and end) form the user. Display part of the
sentence using library methods.

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


// TODO add your handling code here:
String str1=new String();
String str2=new String();
String str3=new String();
String str4=new String();
str1=jTextField1.getText();
str2=jTextField2.getText();
str3=jTextField3.getText();
//use of library method concat()
str4=str1.concat(" "+str2);
//string joining using + operator
str4=str3+" "+str4;
jLabel4.setText(str4);
}
6. Write a program to accept a sentence and a location from user, display the character at that location by
using string methods. Add necessary codes to prevent errors.

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


// TODO add your handling code here:
//declare necessary variables
String str= new String();
int loc;
char c;
//processing
str=jTextField1.getText();
loc=Integer.parseInt(jTextField2.getText());
//to check whether the entered location is negative or 0 if so it will display an error
if(loc<=0)
{
jLabel3.setText("Invalid entry ! ! Plz try again");
}
else if(loc<=str.length())
{
//loc is decreased 1 as length() counts form 0
loc--;
c=str.charAt(loc);
if(c==' ')
{
//loc increased to original(entered) value and displayed
loc++;
jLabel3.setText("The character at: "+loc+" is Space");
}
else
{
loc++;
jLabel3.setText("The character at: "+loc+" is "+c);
}
}
/*if loc is nether 0 nor negative nor within the string length then it must exceed string length thus invalid location*/
else
{
jLabel3.setText("Invalid entry ! ! Plz try again");
}
}
7. Write a program to accept two strings and check whether they are equal or not.

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


// TODO add your handling code here:
String str1=new String();
String str2=new String();
boolean bool;
str1=jTextField1.getText();
str2=jTextField2.getText();
bool=str1.equals(str2);
if(bool==true)
{
jLabel3.setText("entered strings are equal");
}
else
{
jLabel3.setText("entered strings are not equal");
}
}
8. Write a program to accept a string and replace all the ‘a’ of that string in to @. Provide a reset option to
get the original string as well.

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


// TODO add your handling code here:
String str1=new String();
String str2=new String();
str1=jTextField1.getText();
str2=str1.replace('a', '@');
jLabel2.setText(str2);
}

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


// TODO add your handling code here:
String str1=new String();
str1=jTextField1.getText();
jLabel2.setText(str1);
}
9. Write a java program to accept a string and display it into upper case and lower case.

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


// TODO add your handling code here:
String str_1, str_2;
str_1 = jTextField1.getText();
str_2 = str_1.toUpperCase();
jLabel2.setText(str_2);

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


// TODO add your handling code here:
String str_1, str_2;
str_1 = jTextField1.getText();
str_2 = str_1.toLowerCase();
jLabel2.setText(str_2);
}
10. Write a program to accept a sentence and two locations (start and end) form the user. Display part of the
sentence using library methods.

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


// Declaring necessary variables
String str=new String();
int start,end;
//fetching values from GUI(from user) and assigned to respective variables.
str=jTextField1.getText();
start=Integer.parseInt(jTextField2.getText());
end=Integer.parseInt(jTextField3.getText());
//starting index is reduced 1 as it counts form 0.
start--;
//checking for faulty entry
if(start<=0||end>str.length())
{
jLabel4.setText("invalid entry plz try again");
}
//application of substring() to get desired part of the sentence.
else
{
jLabel4.setText("your sub string is: \""+str.substring(start, end)+"\"");
}
}
11. Write a java program to accept a number and display the factorial of the number

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


// TODO add your handling code here:
int a, fact;
int i;
fact = 1;
a = Integer.parseInt(jTextField1.getText());
for(i=a; i>=1; i--)
{
fact = fact*i;
}
jLabel2.setText(""+fact);
}

You might also like