UNIT-II_Classes_and_Objects_Programs
UNIT-II_Classes_and_Objects_Programs
90.91
****STUDENT INFO*****
Roll NO:1010
Name:James
Marks:90.91
Program: Write a Java Program declare class book having data members
book id, name and price. Accept and display data for 2 objects.
import java.util.*;
class Book
{
int book_id;
String name;
float price;
void get_book_info()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter Book ID:");
book_id=sc.nextInt();
System.out.println("Enter Book Name:");
name=sc.next();
System.out.println("Enter Book Price:");
price=sc.nextFloat();
}
void disp_book_info()
{
System.out.println("Book ID:"+book_id);
System.out.println("Book Name:"+name);
System.out.println("Book Price:"+price);
}
public static void main(String args[])
{
Book b1=new Book();
Book b2=new Book();
b1.get_book_info();
b2.get_book_info();
b1.disp_book_info();
b2.disp_book_info();
}
}
OUTPUT:
=======
Enter Book ID:
1010
Enter Book Name:
Java
Enter Book Price:
850
Enter Book ID:
2020
Enter Book Name:
Python
Enter Book Price:
350
Book ID:1010
Book Name:Java
Book Price:850.0
Book ID:2020
Book Name:Python
Book Price:350.0
Program: Write a Java Program declare class Employee having data members emp id,
name and salary. Accept and display data for 5 objects.
import java.util.*;
/*
*/
import java.util.*;
class Employee
{
int emp_id;
String name;
float salary;
void get_emp_info()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter Employee ID:");
emp_id=sc.nextInt();
System.out.println("Enter Employee Name:");
name=sc.next();
System.out.println("Enter Employee Salary:");
salary=sc.nextFloat();
}
void disp_emp_info()
{
System.out.println(emp_id+"\t"+name+"\t"+salary);
}
public static void main(String args[])
{
Employee e[]=new Employee[5];
int i;
for(i=0;i<5;i++)
{
e[i]=new Employee();
e[i].get_emp_info();
}
System.out.println("******EMPLOYEE INFO******");
System.out.println("EMPID\tNAME\tSALARY");
System.out.println("========================");
for(i=0;i<5;i++)
{
e[i].disp_emp_info();
}
}
}
OUTPUT:
=======
Enter Employee ID:
1111
Enter Employee Name:
Dennis
Enter Employee Salary:
45000
Enter Employee ID:
2222
Enter Employee Name:
Bajrne
Enter Employee Salary:
3000
Enter Employee ID:
3333
Enter Employee Name:
James
Enter Employee Salary:
80000
Enter Employee ID:
4444
Enter Employee Name:
Lisa
Enter Employee Salary:
10000
Enter Employee ID:
5555
Enter Employee Name:
Yang
Enter Employee Salary:
35000
******EMPLOYEE INFO******
EMPID NAME SALARY
========================
1111 Dennis 45000.0
2222 Bajrne 3000.0
3333 James 80000.0
4444 Lisa 10000.0
5555 Yang 35000.0
Program: Write a Java Program declare class Company having data members Company
name and No of Employees. Accept and display data for 3 objects.
import java.util.*;
class Company
{
String company_name;
int no_of_emp;
void get_company_info()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter Company Name:");
company_name=sc.next();
System.out.println("Enter No of Employee:");
no_of_emp=sc.nextInt();
}
void disp_company_info()
{
System.out.println(company_name+"\t"+no_of_emp);
}
public static void main(String args[])
{
Company c[]=new Company[3];
int i;
for(i=0;i<3;i++)
{
c[i]=new Company();
c[i].get_company_info();
}
System.out.println("******COMPANY INFO******");
System.out.println("NAME\tNO OF EMP");
System.out.println("========================");
for(i=0;i<3;i++)
{
c[i].disp_company_info();
}
}
}
OUTPUT:
=======
Enter Company Name:
Infosys
Enter No of Employee:
23000
Enter Company Name:
Amdocs
Enter No of Employee:
89000
Enter Company Name:
Zensor
Enter No of Employee:
45000
******COMPANY INFO******
NAME NO OF EMP
========================
Infosys 23000
Amdocs 89000
Zensor 45000
class Addition1
{
int a,b,c;
Addition1()
{
a=100;
b=200;
}
void display()
{
c=a+b;
System.out.println("Addition="+c);
}
public static void main(String args[])
{
Addition1 a1=new Addition1();
a1.display();
}
}
Parameterized Constructor
class Addition2
{
int a,b,c;
Addition2(int x,int y)
{
a=x;
b=y;
}
void display()
{
c=a+b;
System.out.println("Addition="+c);
}
public static void main(String args[])
{
Addition2 a1=new Addition2(500,600);
a1.display();
}
}
Copy Constructor
class Addition3
{
int a,b,c;
Addition3()
{
a=100;
b=200;
}
Addition3(Addition3 m)
{
a=m.a;
b=m.b;
}
void display()
{
c=a+b;
System.out.println("Addition="+c);
}
Constructor Overloading
class Addition4
{
int a,b,c;
Addition4()
{
a=100;
b=200;
}
Addition4(int x,int y)
{
a=x;
b=y;
}
void display()
{
c=a+b;
System.out.println("Addition="+c);
}
public static void main(String args[])
{
Addition4 a1=new Addition4();
Addition4 a2=new Addition4(500,600);
a1.display();
a2.display();
}
}
class StaticDemo
{
int no;
static int count;
void getdata(int x)
{
no=x;
count++;
}
void display_no()
{
System.out.println("No:"+no);
}
static void display_count()
{
System.out.println("Count:"+count);
}
public static void main(String args[])
{
StaticDemo s1=new StaticDemo();
StaticDemo s2=new StaticDemo();
StaticDemo s3=new StaticDemo();
s1.getdata(100);
s2.getdata(200);
s3.getdata(300);
s1.display_no();
s2.display_no();
s3.display_no();
StaticDemo.display_count();
}
}
OUTPUT:
=======
No:100
No:200
No:300
Count:3
class OneDArray
{
public static void main(String args[])
{
int a[]=new int[5];
//int a[]={20,40,60,80,100};
int i;
a[0]=10;
a[1]=20;
a[2]=30;
a[3]=40;
a[4]=50;
for(i=0;i<a.length;i++)
{
System.out.println("Value present at index
"+i+" is "+a[i]);
}
}
}
/*
OUTPUT:
=======
Value present at index 0 is 10
Value present at index 1 is 20
Value present at index 2 is 30
Value present at index 3 is 40
Value present at index 4 is 50
import java.util.*;
class OneDArray1
{
public static void main(String args[])
{
int a[]=new int[5];
int i;
Scanner sc=new Scanner(System.in);
System.out.println("Enter 5 array elements:");
for(i=0;i<5;i++)
{
a[i]=sc.nextInt();
}
System.out.println("Array elements:");
for(i=0;i<5;i++)
{
System.out.print(a[i]+"\t");
}
}
}
/*
OUTPUT:
=======
Enter 5 array elements:
100
200
300
400
500
Array elements:
100 200 300 400 500
Array: Accept array elements from user and display it in reverse order
import java.util.*;
class OneDArray2
{
public static void main(String args[])
{
int a[]=new int[5];
int i;
Scanner sc=new Scanner(System.in);
System.out.println("Enter 5 array elements:");
for(i=0;i<5;i++)
{
a[i]=sc.nextInt();
}
System.out.println("Array elements in reverse order:");
for(i=4;i>=0;i--)
{
System.out.print(a[i]+"\t");
}
}
}
/*
OUTPUT:
=======
Enter 5 array elements:
100
200
300
400
500
Array elements in reverse order:
500 400 300 200 100
import java.util.*;
class TwoDArray
{
public static void main(String args[])
{
int a[][]=new int[3][3];
int i,j;
Scanner sc=new Scanner(System.in);
System.out.println("Enter 3*3 array elements:");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
a[i][j]=sc.nextInt();
}
}
System.out.println("Array elements:");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
System.out.print(a[i][j]+"\t");
}
System.out.print("\n");
}
}
}
OUTPUT:
=======
Enter 3*3 array elements:
10
20
30
40
50
60
70
80
90
Array elements:
10 20 30
40 50 60
70 80 90
import java.util.*;
class VectorDemo
{
public static void main(String args[])
{
Vector v1=new Vector();
v1.addElement(new Integer(10));
v1.addElement(new Integer(30));
v1.addElement(new Integer(60));
v1.addElement(new Integer(70));
v1.addElement(new Integer(80));
v1.addElement(new Integer(100));
System.out.println("Initial Vector Size = "+v1.size());
v1.removeElementAt(3);
v1.removeElementAt(4);
v1.insertElementAt(new Integer(150),3);
System.out.println("Final Vector Size = "+v1.size());
System.out.println(v1);
}
}
OUTPUT:
=======
Initial Vector Size = 6
Final Vector Size = 5
Vector Program
import java.util.*;
class VectorDemo1
{
public static void main(String args[])
{
Vector v1=new Vector();
for(int i=0;i<args.length;i++)
{
v1.addElement(args[i]);
}
System.out.println("Vector of Size = "+v1.size());
System.out.println("Your Vector Elements:");
for(int i=0;i<v1.size();i++)
{
System.out.println(v1.elementAt(i));
}
}
}
OUTPUT:
=======
C:\Javabatch2025\UNIT-II Programs>javac VectorDemo1
C:\Javabatch2025\UNIT-II Programs>java VectorDemo1 10 20 30
Vector of Size = 3
Your Vector Elements:
10
20
30
Wrapper class
import java.util.*;
class WrapperClassDemo
{
public static void main(String args[])
{
int a=10;
Integer i=new Integer(a);
System.out.println("Simple Data Type a:"+a);
System.out.println("Object i:"+i);
}
}
OUTPUT:
=======
Simple Data Type a:10
Object i:10
String class
class StringDemo
{
public static void main(String args[])
{
String str=new String("VJTech");
System.out.println("Value of str ="+str);
System.out.println("Length of str ="+str.length());
System.out.println("To Lower Case ="+str.toLowerCase());
System.out.println("To Upper Case ="+str.toUpperCase());
System.out.println("Character present at 2 index ="+str.charAt(2));
System.out.println("Concatenation="+str.concat("Academy"));
System.out.println("Index of e character ="+str.indexOf('e'));
System.out.println("Equals method ="+str.equals("VJTech"));
System.out.println("CompareTo method ="+str.compareTo("VJTech"));
}
}
OUTPUT:
=======
Value of str =VJTech
Length of str =6
To Lower Case =vjtech
To Upper Case =VJTECH
Character present at 2 index =T
Concatenation=VJTechAcademy
Index of e character =3
Equals method =true
CompareTo method =0
StringBuffer class
class StringBufferDemo
{
public static void main(String args[])
{
StringBuffer str=new StringBuffer("VJTech");