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

UNIT-II_Classes_and_Objects_Programs

The document contains a series of Java programming exercises focused on object-oriented concepts such as classes, objects, constructors, and arrays. Each section provides sample code for creating classes like Addition, Student, Book, Employee, and Company, along with their respective methods for data input and output. Additionally, it covers topics like static members, array manipulation, and vector operations, with examples and expected outputs for each program.

Uploaded by

republic260150
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

UNIT-II_Classes_and_Objects_Programs

The document contains a series of Java programming exercises focused on object-oriented concepts such as classes, objects, constructors, and arrays. Each section provides sample code for creating classes like Addition, Student, Book, Employee, and Company, along with their respective methods for data input and output. Additionally, it covers topics like static members, array manipulation, and vector operations, with examples and expected outputs for each program.

Uploaded by

republic260150
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

UNIT-I Programs

“Classes & Objects”

Contact Us: +91 7743909870 Website: https://www.vjtechacademy.in

Android Mobile App: https://play.google.com/store/apps/details?id=in.vjtechacademy.android


UNIT-I Programs

DATE: 28 January 2025


 Program: Write Java Program to Create Addition class and perform
addition between two values.
class Addition
{
int a,b,c;
void getdata()
{
a=100;
b=50;
}
void display()
{
c=a+b;
System.out.println("Addition="+c);
}
public static void main(String args[])
{
Addition a1=new Addition();
a1.getdata();
a1.display();
}
}

Contact Us: +91 7743909870 Website: https://www.vjtechacademy.in

Android Mobile App: https://play.google.com/store/apps/details?id=in.vjtechacademy.android


UNIT-I Programs

DATE: 29 January 2025


 Program: Write Java Program to Create Student class and having data
members rollno,name and marks.
import java.util.*;
class Student
{
int rollno;
String name;
float marks;
void get_stud_info()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter Student Roll No:");
rollno=sc.nextInt();
System.out.println("Enter Student Name:");
name=sc.next();
System.out.println("Enter Student Marks:");
marks=sc.nextFloat();
}
void disp_stud_info()
{
System.out.println("****STUDENT INFO*****");
System.out.println("Roll NO:"+rollno);
System.out.println("Name:"+name);
System.out.println("Marks:"+marks);
}
public static void main(String args[])
{
Student s1=new Student();
s1.get_stud_info();
s1.disp_stud_info();
}
}
OUTPUT:
=======
Enter Student Roll No:
1010
Enter Student Name:
James
Enter Student Marks:

Contact Us: +91 7743909870 Website: https://www.vjtechacademy.in

Android Mobile App: https://play.google.com/store/apps/details?id=in.vjtechacademy.android


UNIT-I 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();
}
}

Contact Us: +91 7743909870 Website: https://www.vjtechacademy.in

Android Mobile App: https://play.google.com/store/apps/details?id=in.vjtechacademy.android


UNIT-I Programs

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

Contact Us: +91 7743909870 Website: https://www.vjtechacademy.in

Android Mobile App: https://play.google.com/store/apps/details?id=in.vjtechacademy.android


UNIT-I Programs

 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();
}

Contact Us: +91 7743909870 Website: https://www.vjtechacademy.in

Android Mobile App: https://play.google.com/store/apps/details?id=in.vjtechacademy.android


UNIT-I Programs

}
}
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

Contact Us: +91 7743909870 Website: https://www.vjtechacademy.in

Android Mobile App: https://play.google.com/store/apps/details?id=in.vjtechacademy.android


UNIT-I Programs

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:

Contact Us: +91 7743909870 Website: https://www.vjtechacademy.in

Android Mobile App: https://play.google.com/store/apps/details?id=in.vjtechacademy.android


UNIT-I Programs

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

Contact Us: +91 7743909870 Website: https://www.vjtechacademy.in

Android Mobile App: https://play.google.com/store/apps/details?id=in.vjtechacademy.android


UNIT-I Programs

DATE: 30 January 2025


Program: Default Constructor

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();
}
}

Contact Us: +91 7743909870 Website: https://www.vjtechacademy.in

Android Mobile App: https://play.google.com/store/apps/details?id=in.vjtechacademy.android


UNIT-I Programs

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);
}

Contact Us: +91 7743909870 Website: https://www.vjtechacademy.in

Android Mobile App: https://play.google.com/store/apps/details?id=in.vjtechacademy.android


UNIT-I Programs

public static void main(String args[])


{
Addition3 a1=new Addition3();
Addition3 a2=new Addition3(a1);
a1.display();
a2.display();
}
}

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();
}
}

Contact Us: +91 7743909870 Website: https://www.vjtechacademy.in

Android Mobile App: https://play.google.com/store/apps/details?id=in.vjtechacademy.android


UNIT-I Programs

static members (Data , Function)

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

Contact Us: +91 7743909870 Website: https://www.vjtechacademy.in

Android Mobile App: https://play.google.com/store/apps/details?id=in.vjtechacademy.android


UNIT-I Programs

DATE: 31 January 2025


//Array

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

Contact Us: +91 7743909870 Website: https://www.vjtechacademy.in

Android Mobile App: https://play.google.com/store/apps/details?id=in.vjtechacademy.android


UNIT-I Programs

Array: Accept array elements from user and display it

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

Contact Us: +91 7743909870 Website: https://www.vjtechacademy.in

Android Mobile App: https://play.google.com/store/apps/details?id=in.vjtechacademy.android


UNIT-I Programs

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

Contact Us: +91 7743909870 Website: https://www.vjtechacademy.in

Android Mobile App: https://play.google.com/store/apps/details?id=in.vjtechacademy.android


UNIT-I Programs

Array: Two Dimensional Array

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:

Contact Us: +91 7743909870 Website: https://www.vjtechacademy.in

Android Mobile App: https://play.google.com/store/apps/details?id=in.vjtechacademy.android


UNIT-I Programs

10 20 30
40 50 60
70 80 90

Contact Us: +91 7743909870 Website: https://www.vjtechacademy.in

Android Mobile App: https://play.google.com/store/apps/details?id=in.vjtechacademy.android


UNIT-I Programs

DATE: 01 Feb 2025


Write a program to create vector with six elements (10,30,60,70,80,100). Remove element
3rd and 4th position. Insert new element at 3rd position. Display the original and current
size of vector

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

Contact Us: +91 7743909870 Website: https://www.vjtechacademy.in

Android Mobile App: https://play.google.com/store/apps/details?id=in.vjtechacademy.android


UNIT-I Programs

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

Contact Us: +91 7743909870 Website: https://www.vjtechacademy.in

Android Mobile App: https://play.google.com/store/apps/details?id=in.vjtechacademy.android


UNIT-I Programs

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

Contact Us: +91 7743909870 Website: https://www.vjtechacademy.in

Android Mobile App: https://play.google.com/store/apps/details?id=in.vjtechacademy.android


UNIT-I Programs

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

Contact Us: +91 7743909870 Website: https://www.vjtechacademy.in

Android Mobile App: https://play.google.com/store/apps/details?id=in.vjtechacademy.android


UNIT-I Programs

StringBuffer class

class StringBufferDemo
{
public static void main(String args[])
{
StringBuffer str=new StringBuffer("VJTech");

System.out.println("Original String :"+str);


System.out.println("Length of String :"+str.length());
for(int i=0;i<str.length();i++)
{
System.out.println("Character at position "+i+" is "+str.charAt(i));
}
str.setCharAt(3,'T');
System.out.println("Modified String :"+str);
str.append("Academy");
System.out.println("Appended String :"+str) ;
}
}
Original String :VJTech
Length of String :6
Character at position 0 is V
Character at position 1 is J
Character at position 2 is T
Character at position 3 is e
Character at position 4 is c
Character at position 5 is h
Modified String :VJTTch
Appended String :VJTTchAcademy

Contact Us: +91 7743909870 Website: https://www.vjtechacademy.in

Android Mobile App: https://play.google.com/store/apps/details?id=in.vjtechacademy.android


UNIT-I Programs

Contact Us: +91 7743909870 Website: https://www.vjtechacademy.in

Android Mobile App: https://play.google.com/store/apps/details?id=in.vjtechacademy.android

You might also like