III Sem BCom JAVA LAB (GDC) 1
/*-------------------------------------------------------------------------------------------------------------
Program No. 1.:
Write a Java program to find whether the given number is palindrome or not.
------------------------------------------------------------------------------------------------------------*/
import [Link].*;
class palin
{
public static void main(String args[])
{
Scanner in=new Scanner([Link]);
int n,n1=0,d,rev=0;
[Link]("Enter a number");
n=[Link]();
n1=n;
while(n!=0)
{
d=n%10;
rev=rev*10+d;
n=n/10;
}
if(n1==rev)
[Link](n1+" is palindrome");
else
[Link](n1+" is not a palindrome");
}
}
OUTPUT:
H:\Java_BCom>javac [Link]
H:\Java_BCom>java palin
Enter a number
151
151 is palindrome
H:\Java_BCom>java palin
Enter a number
321
321 is not a palindrome
III Sem BCom JAVA LAB (GDC) 2
/*-------------------------------------------------------------------------------------------------------------
Program No.2.
Write a program to initialize an integer array and find the maximum and minimum
value of the array.
------------------------------------------------------------------------------------------------------------*/
import [Link].*;
class maxmin
{
public static void main(String args[])
{
Scanner in=new Scanner([Link]);
int n,i;
int a[]=new int[10];
[Link]("Enter how many numbers?:");
n=[Link]();
[Link]("Enter the elements:");
for(i=0;i<n;i++)
{
a[i]=[Link]();
}
for(i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
int temp=a[i];
a[i]=a[j];
a[j]=temp;
}}}
int min=a[0];
int max=a[n-1];
[Link]("maximum="+max+" minimum="+min);
}
}
OUTPUT:
D:\Java_BCom>javac [Link]
D:\Java_BCom>java maxmin
Enter how many numbers?:
5
Enter the elements:
20
-32
6
0
98
maximum=98 minimum=-32
III Sem BCom JAVA LAB (GDC) 3
/*---------------------------------------------------------------------------------------------------------
Program No:3
Given two strings, a and b, print a new string which is made of the following
combination-first character of a, the first character of b, second character of a, second
character of b and so on. Any characters left, will go to the end of the result.
Sample Example: i. Input string: Hello,World ii. Output string: HWeolrllod */
/*---------------------------------------------------------------------------------------------------------
import [Link].*;
import [Link].*;
class Combine
{
public static void main(String args[])
{
Scanner in=new Scanner([Link]);
[Link]("Enter two strings");
String a=[Link]();
String b=[Link]();
String result=" ";
[Link]("Two strings are");
[Link]("String 1:" +a);
[Link]("String 2:" +b);
[Link]("Combined String:");
for( int i=0; i<[Link]() || i<[Link](); i++)
{
if( i<[Link]())
result+=[Link](i);
if( i<[Link]())
result+=[Link](i);
}
[Link](result);
}
}
OUTPUT:
H:\Java_BCom>javac [Link]
H:\Java_BCom>java Combine
Enter two strings
Hello
World
Two strings are
String 1:Hello
String 2:World
Combined String:
HWeolrllod
III Sem BCom JAVA LAB (GDC) 4
/*---------------------------------------------------------------------------------------------------------
Program No.4:
Create a class Box that uses a parameterized constructor to initialize the dimensions of
a box. The dimensions of the Box are width, height, depth. The class should have a
method that can return the volume of the box.
------------------------------------------------------------------------------------------------------------*/
class box
{
int width,height,depth;
box(int x, int y, int z)
{
width=x;
height=y;
depth=z;
}
int display()
{
return(width*height*depth);
}
}
class boxvol
{
public static void main(String args[])
{
box b1=new box(5,6,7) ;
int volume=[Link]();
[Link]("volume of box="+volume);
}
}
OUTPUT:
D:\Java_BCom>javac [Link]
D:\Java_BCom>java boxvol
volume of box=210
III Sem BCom JAVA LAB (GDC) 5
/*---------------------------------------------------------------------------------------------------------
Program No.5:
Create a school application with a class called Person. Create name and Date of Birth
as member variables. Create a class called Teacher that inherits from the Person class.
The teacher will have additional properties like salary, and the subject that the teacher
teaches. Create a class called Student that inherits from Person class. This class will
have a member variable called student-Id. Create a class called College Student that
inherits from Student class. This class will have college Name, the year in which the
student is studying (first/second/third/fourth) etc. Create objects of each of these
classes, invoke and test the methods that are available in these classes.
--------------------------------------------------------------------------------------------------------- ---*/
class person
{
String name,dob;
person(String n,String d)
{
name=n;
dob=d;
}
}
class Teacher extends person
{
int sal;
String sub;
Teacher(String n,String d,int s,String su)
{
super(n,d);
sal=s;
sub=su;
}
void Tdisplay()
{
[Link]("teacher name="+name);
[Link]("dob="+dob);
[Link]("salary="+sal);
[Link]("subject="+sub);
}
}
class student extends person
{
String Id;
student(String n,String d,String i)
{
super(n,d);
Id=i;
}
}
III Sem BCom JAVA LAB (GDC) 6
class cstudent extends student
{
String cname,year;
cstudent(String n,String d,String i,String c,String y)
{
super(n,d,i);
cname=c;
year=y;
}
void Sdisplay()
{
[Link]("student name="+name);
[Link]("student dob="+dob);
[Link]("student Id="+Id);
[Link]("student year="+year);
[Link]("student cname="+cname);
}
}
class Inher
{
public static void main(String args[])
{
Teacher t1=new Teacher("shreya","12-12-1995",50000,"computer");
cstudent cs=new cstudent("neha","19-04-2003","21306","GDC","2nd bcom");
[Link]();
[Link]();
}
}
OUTPUT:
D:\Java_BCom>javac [Link]
D:\Java_BCom>javac [Link]
D:\Java_BCom>java Inher
teacher name=shreya
dob=12-12-1995
salary=50000
subject=computer
student name=neha
student dob=19-04-2003
student Id=21306
student year=2nd bcom
student cname=GDC
III Sem BCom JAVA LAB (GDC) 7
/*---------------------------------------------------------------------------------------------------------
Program No.6:
Write a Menu drive Java program to perform following operation using Vector. a.
INSERT an ITEM b. DELETE a SPECFIC ITEM c. DISPLAY ITEM [Link] */
---------------------------------------------------------------------------------------------------------*/
import [Link].*;
class vectexample
{
public static void main(String args[])
{
Vector list=new Vector();
Scanner in=new Scanner([Link]);
int ch=0;
do
{
[Link]("[Link] item");
[Link]("[Link] item");
[Link]("[Link]");
[Link]("[Link]");
[Link]("enter your choice");
ch=[Link]();
[Link]();
String item;
switch(ch)
{
case 1:
[Link]("enter an item");
item=[Link]();
[Link](item);
break;
case 2:
if([Link]())
[Link]("no item in the list");
else
{
[Link]("enter the element to remove\n");
item=[Link]();
[Link](item);
}
break;
case 3:
if([Link]())
[Link]("no item in the list");
else
{
[Link]("element in the list");
for(int i=0;i<[Link]();i++)
III Sem BCom JAVA LAB (GDC) 8
[Link]([Link](i));
}
break;
default:[Link]("invalid choice\n");
break;
}
}
while(ch<4);
}}
D:\2Bcom_2024\JAVA>java vectexample
[Link] item
[Link] item
[Link]
[Link]
enter your choice
3
no item in the list
[Link] item
[Link] item
[Link]
[Link]
enter your choice
1
enter an item
pen
[Link] item
[Link] item
[Link]
[Link]
enter your choice
1
enter an item
pencil
[Link] item
[Link] item
[Link]
[Link]
enter your choice
3
element in the list
pen
pencil
[Link] item
[Link] item
[Link]
[Link]
III Sem BCom JAVA LAB (GDC) 9
enter your choice
2
enter the element to remove
pen
[Link] item
[Link] item
[Link]
[Link]
enter your choice
3
element in the list
pencil
[Link] item
[Link] item
[Link]
[Link]
enter your choice
4
invalid choice
III Sem BCom JAVA LAB (GDC) 10
/*-------------------------------------------------------------------------------------------------------
Program No. : 7
Java program using package to convert Celsius to Fahrenheit
------------------------------------------------------------------------------------------------------- */
Steps:-
1. Create a sub folder temp
2. Create source file CTOF and compile ([Link])*/
//Contents of [Link] file:-
package temp;
public class CTOF
{
float c;
public void getc(float x)
{
c=x;
}
public void convert()
{
double f;
f=(1.8)*c+32;
[Link]("Temperature in Fahrenheit"+f);
}
}
/*Step :-
1. Compile the [Link] file [javac [Link]]
2. Go back to the previous directory [2bcom]
3. Create the main source file [Link] [[Link]]*/
//Contents of [Link] file: -
import [Link];
import [Link].*;
import [Link].*;
class PTest
{
public static void main(String args[])
{
Scanner in=new Scanner([Link]);
float x;
CTOF c=new CTOF();
[Link]("Temperature Conversion");
[Link]("Enter the temperature in celcius:");
x=[Link]();
[Link](x);
[Link]();
}
}
III Sem BCom JAVA LAB (GDC) 11
OUTPUT:
D:\2024\Java_BCom\temp>java CTOF
D:\2024\Java_BCom>java PTest
III Sem BCom JAVA LAB (GDC) 12
/*---------------------------------------------------------------------------------------------------------
Program No. 8
Write a Java program to read number form [Link] file and count the number of
zero’s, positive and negative numbers and display the result.
--------------------------------------------------------------------------------------------------------------*/
import [Link].*;
class ReadWrite
{
public static void main(String args[]) throws IOException
{
File num=new File("[Link]");
int n=0,c=0;
int x;
int p=0;
int ne=0;
int z=0;
DataInputStream in=new DataInputStream([Link]);
FileOutputStream fos=new FileOutputStream(num);
DataOutputStream dos=new DataOutputStream(fos);
[Link]("Enter how many numbers");
try
{
c=[Link]([Link]());
[Link]("Enter "+c+" numbers");
for(int i=0;i<c;i++)
{
x=[Link]([Link]());
[Link](x);
}}catch(Exception e){}
[Link]();
[Link]();
FileInputStream fis=new FileInputStream(num);
DataInputStream dis=new DataInputStream(fis);
for(int i=0;i<c;i++)
{
n=[Link]();
if(n>0)
p=p+1;
else if(n<0)
ne=ne+1;
else if(n==0)
z=z+1;
}
[Link]("No. of Positive Integers "+p);
[Link]("No. of Negative Integers "+ne);
[Link]("No of zeros "+z);
[Link]();
III Sem BCom JAVA LAB (GDC) 13
[Link]();
}
}
OUTPUT
D:\2024\Java_BCom>javac [Link]
D:\2024\Java_BCom>java ReadWrite
Enter how many numbers
6
Enter 6 numbers
80
-54
36
0
-34
45
No. of Positive Integers 3
No. of Negative Integers 2
No of zeros 1
---------------------------------------------------------------------------------------------------------------