The Poona Gujrati Kelvani Mandal’s
Haribhai V. Desai College of Arts, Science and Commerce, Pune
DEPARTMENT OF COMPUTER SCIENCE
Academic Year: 2025-26
Class – T.Y.B.Sc (Comp. Sci.) Div -A and B
Subject- CS-355 Object oriented programming in Java-I
ASSIGNMENT2 CHAPTER2- classes and Object
Q. Write following java programs .
1. Create an employee class(id,name,deptname,salary). Define a default and parameterized
constructor. Use ‘this’ keyword to initialize instance variables. Keep a count of objects created. Create
objects using parameterized constructor and display the object count after each object is created.
(Use static member and method). Also display the contents of each object.
2. Write a program to create a package name student. Define class StudentInfo with method to
display information about student such as rollno, class, and percentage. Create another class
StudentPer with method to find percentage of the student. Accept student details like rollno, name,
class and marks of 6 subject from user.
3. Write a Java program to create a Package “SY” which has a class SYMarks (members –
ComputerTotal, MathsTotal, and ElectronicsTotal). Create another package TY which has a class
TYMarks (members – Theory, Practicals). Create ‘n’ objects of Student class (having rollNumber,
name, SYMarks and TYMarks). Add the marks of SY and TY computer subjects and calculate the
Grade (‘A’ for >= 70, ‘B’ for >= 60 ‘C’ for >= 50, Pass Class for > =40 else‘FAIL’) and display the result of
the student in proper format.
4. Write a package for Operation, which has two classes, Addition and Maximum. Addition has
two methods add () and subtract (), which are used to add two integers and subtract two, float
values respectively. Maximum has a method max () to display the maximum of two integers
5. Define a class CricketPlayer (name,no_of_innings,no_of_times_notout, totatruns, bat_avg).
Create an array of n player objects. Calculate the batting average for each player using static method
avg(). Define a static sort method which sorts the array on the basis of average. Display the player
details in sorted order.
Solution:
import java.util.Scanner;
class cricket
{
int inning, tofnotout, totalruns;
String name;
float batavg;
int i;
cricket()
{
name= null;
inning= 0;
tofnotout= 0;
totalruns=0;
batavg= 0;
}
void get()
{
Scanner s1= new Scanner(System.in);
System.out.println("name, no of innings, no of time(s) not out, total runs");
name= s1.nextLine();
inning= s1.nextInt();
tofnotout= s1.nextInt();
totalruns= s1.nextInt();
}
void put()
{
System.out.println("name: "+name);
System.out.println("no of innings: "+inning);
System.out.println("no of time(s) not out: "+tofnotout);
System.out.println("total runs: "+totalruns);
System.out.println("batting average: "+batavg);
}
static void avg(int n, cricket c[])
{
for(int i=0; i<n; i++)
{
c[i].batavg= c[i].totalruns/c[i].inning;
}
}
static void sort(int n, cricket c[])
{
String temp1;
int temp2, temp3, temp4;
float temp5;
for(int i=0; i<n; i++)
for(int j=i+1; j<n; j++)
if(c[i].batavg<c[j].batavg)
temp1= c[i].name;
c[i].name= c[j].name;
c[j].name= temp1;
temp2= c[i].inning;
c[i].inning= c[j].inning;
c[j].inning= temp2;
temp3= c[i].tofnotout;
c[i].tofnotout= c[j].tofnotout;
c[j].tofnotout= temp3;
temp4= c[i].totalruns;
c[i].totalruns= c[j].totalruns;
c[j].totalruns= temp4;
temp5= c[i].batavg;
c[i].batavg= c[j].batavg;
c[j].batavg= temp5;
}
}
class setBb
public static void main(String args[])
Scanner s1= new Scanner(System.in);
System.out.println("Enter The Limit: ");
int n= s1.nextInt();
cricket c[]= new cricket[n];
for(int i=0; i<n; i++)
c[i]= new cricket();
c[i].get();
cricket.avg(n, c);
cricket.sort(n,c);
for(int i=0; i<n; i++)
c[i].put();
Write a package for String operation which has two classes Con and Comp. Con class has to
concatenates two strings and comp class compares two strings. Also display proper message on
execution
//program1
package stringoperations;
public class Con {
public static String concatenate(String str1, String str2) {
return str1 + str2;
}
//program2
package stringoperations;
public class Comp {
public static boolean compare(String str1, String str2) {
return str1.equals(str2);
// main program
import stringoperations.Con;
import stringoperations.Comp;
public class StringOp {
public static void main(String[] args) {
String str1 = "Hello";
String str2 = "World";
String concatenatedString = Con.concatenate(str1, str2);
System.out.println("Concatenated String: " + concatenatedString);
boolean areEqual = Comp.compare(str1, str2);
System.out.println("Are the strings equal? " + areEqual);