PART-A Programs
PART-A Programs
1. Program to add two integers and two float numbers. When no arguments are
supplied. Give a default value to calculate the sum. Use function overloading.
import java.io.*;
import java.util.*;
class Overload
{
public int Add()
{
int x=10,y=5,add;
add=x+y;
return add;
}
public int Add(int x,int y)
{
int add;
add=x+y;
return add;
}
public double Add(double x,double y)
{
double add;
add=x+y;
return add;
}
}
class pa1
{
public static void main(String args[])
{
Overload obj= new Overload();
System.out.println(obj.Add());
System.out.println(obj.Add(10,20));
System.out.println(obj.Add(10.5,20.5));
}
}
3. Program with class variable that available for all instances of a class. Use static
variable declaration. Observe the changes that occur in the object’s member
variable values.
class Cube
{
int side; //instances variable
static int objectCount=0; //static variable
Cube()
{
objectCount++;
}
Cube(int x)
{
side=x;
objectCount++;
}
public static void main(String args[])
{
Cube c1=new Cube(5);
Cube c2=new Cube(8);
Cube c3=new Cube(10);
System.out.println("Number of Cube Objects: "+objectCount);
System.out.println("changes occured by using object c1:"+c1.side);
System.out.println("changes occured by using object c2:"+c2.side);
System.out.println("changes occured by using object c3:"+c3.side);
}
}
}
class pa4 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
//Declare Array of class Students
Student stdArray[]= new Student[3];
System.out.println("Enter the total number of students:");
int n;
n=in.nextInt();
for(int i=0; i<n; i++){
//Create a new object of class Student
System.out.println("Enter the details of student"+(i+1)+": (Enrollment no, Name, sub 1, sub
2, sub 3)");
Student newStudent = new
Student(in.nextInt(),in.next(),in.nextInt(),in.nextInt(),in.nextInt());
//Assign the object to the object array
stdArray[i] = newStudent;
stdArray[i].calculate();
}
System.out.println("Students details are:");
for(int i=0; i<n; i++){
//Student.toString() method will be used to output object
stdArray[i].Display();
}
}
}
best = students[i];
}
}
return best;
}
}
public class pa5 {
public static void main(String[] args) {
// Creating students
Student[] students = {
new Student("Alice", 80, 85, 90),
new Student("Bob", 70, 75, 85),
new Student("Charlie", 90, 80, 95)
};
// Creating first year object
FirstYear firstYear = new FirstYear("BCA", "Mr. Smith", 3, students);
// Finding the best student
Student bestStudent = firstYear.bestStudent();
// Displaying details of the best student
System.out.println("Best student details:");
System.out.println("Name: " + bestStudent.name);
System.out.println("Total Marks: " + bestStudent.totalMarks);
}
}
6.
import java.util.Arrays;
import java.util.Comparator;
class Employee {
String name;
String dateOfAppointment;
public Employee(String name, String dateOfAppointment) {
this.name = name;
this.dateOfAppointment = dateOfAppointment;
}
public String getName() {
return name;
}
public String getDateOfAppointment() {
return dateOfAppointment;
}
}
public class pa6 {
public static void main(String[] args) {
// Creating ten employee objects
Employee[] employees = {
new Employee("John", "2022-01-15"),
new Employee("Alice", "2021-03-10"),
new Employee("Bob", "2020-05-20"),
new Employee("Charlie", "2019-11-08"),
new Employee("David", "2023-02-28"),
new Employee("Eva", "2024-07-12"),
new Employee("Frank", "2018-09-05"),
new Employee("Grace", "2020-12-25"),
new Employee("Henry", "2022-08-18"),
new Employee("Ivy", "2023-04-30")
};
// Sorting employees based on their date of appointment
Arrays.sort(employees,Comparator.comparing(Employee::getDateOfAppointment));
// Printing employees in order of seniority
System.out.println("Employees sorted by date of appointment (seniority):");
for (Employee employee : employees) {
System.out.println("Name: " + employee.getName() + ", Date of Appointment: "
+employee.getDateOfAppointment());
}
}
}