JAVA Project for class XII
1. Write a java program to add and subtract two numbers.
private void jButton1ActionPerformed([Link] evt) {
// TODO add your handling code
here:
float a,b;
float sum;
a=
[Link]([Link]());
b=
[Link]([Link]());
sum = a+b;
[Link](""+sum);
}
private void
jButton2ActionPerformed([Link] evt) {
// TODO add your handling code here:
float a,b;
float sub;
a = [Link]([Link]());
b= [Link]([Link]());
sub = a-b;
[Link](""+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([Link] evt) {
// TODO add your handling code here:
int i,j,sum;
j=0;
sum=0;
String str =new String();
i=[Link]([Link]());
while(i>0)
{
j=i%10;
i=i/10;
str=str+j;
sum=sum+j;
}
[Link](str);
[Link](""+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([Link] evt) {
// TODO add your handling code here:
//declaring necessary variables
int l,b;
double area,perimeter;
//getting values form user through IDE
l=[Link]([Link]());
b=[Link]([Link]());
//method call and passing values
area=getArea(l,b);
perimeter=getPerimeter(l,b);
//displaying processed result
[Link](""+area);
[Link](""+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([Link] evt) {
// TODO add your handling code here:
String str1=new String();
String str2=new String();
String str3=new String();
String str4=new String();
str1=[Link]();
str2=[Link]();
str3=[Link]();
//use of library method concat()
str4=[Link](" "+str2);
//string joining using + operator
str4=str3+" "+str4;
[Link](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([Link] evt) {
// TODO add your handling code here:
String str1=new String();
String str2=new String();
String str3=new String();
String str4=new String();
str1=[Link]();
str2=[Link]();
str3=[Link]();
//use of library method concat()
str4=[Link](" "+str2);
//string joining using + operator
str4=str3+" "+str4;
[Link](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([Link] evt) {
// TODO add your handling code here:
//declare necessary variables
String str= new String();
int loc;
char c;
//processing
str=[Link]();
loc=[Link]([Link]());
//to check whether the entered location is negative or 0 if so it will display an error
if(loc<=0)
{
[Link]("Invalid entry ! ! Plz try again");
}
else if(loc<=[Link]())
{
//loc is decreased 1 as length() counts form 0
loc--;
c=[Link](loc);
if(c==' ')
{
//loc increased to original(entered) value and displayed
loc++;
[Link]("The character at: "+loc+" is Space");
}
else
{
loc++;
[Link]("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
{
[Link]("Invalid entry ! ! Plz try again");
}
}
7. Write a program to accept two strings and check whether they are equal or not.
private void jButton1ActionPerformed([Link] evt) {
// TODO add your handling code here:
String str1=new String();
String str2=new String();
boolean bool;
str1=[Link]();
str2=[Link]();
bool=[Link](str2);
if(bool==true)
{
[Link]("entered strings are equal");
}
else
{
[Link]("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([Link] evt) {
// TODO add your handling code here:
String str1=new String();
String str2=new String();
str1=[Link]();
str2=[Link]('a', '@');
[Link](str2);
}
private void jButton2ActionPerformed([Link] evt) {
// TODO add your handling code here:
String str1=new String();
str1=[Link]();
[Link](str1);
}
9. Write a java program to accept a string and display it into upper case and lower case.
private void jButton1ActionPerformed([Link] evt) {
// TODO add your handling code here:
String str_1, str_2;
str_1 = [Link]();
str_2 = str_1.toUpperCase();
[Link](str_2);
private void jButton2ActionPerformed([Link] evt) {
// TODO add your handling code here:
String str_1, str_2;
str_1 = [Link]();
str_2 = str_1.toLowerCase();
[Link](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([Link] evt) {
// Declaring necessary variables
String str=new String();
int start,end;
//fetching values from GUI(from user) and assigned to respective variables.
str=[Link]();
start=[Link]([Link]());
end=[Link]([Link]());
//starting index is reduced 1 as it counts form 0.
start--;
//checking for faulty entry
if(start<=0||end>[Link]())
{
[Link]("invalid entry plz try again");
}
//application of substring() to get desired part of the sentence.
else
{
[Link]("your sub string is: \""+[Link](start, end)+"\"");
}
}
11. Write a java program to accept a number and display the factorial of the number
private void jButton1ActionPerformed([Link] evt) {
// TODO add your handling code here:
int a, fact;
int i;
fact = 1;
a = [Link]([Link]());
for(i=a; i>=1; i--)
{
fact = fact*i;
}
[Link](""+fact);
}