[go: up one dir, main page]

0% found this document useful (0 votes)
20 views9 pages

PART-A Programs

JAVA lab program

Uploaded by

nagabhushanaat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views9 pages

PART-A Programs

JAVA lab program

Uploaded by

nagabhushanaat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

PART A: Java Fundamentals OOPs in Java

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

2. Program to perform mathematical operations. Create a class called AddSub with


methods to Add and Subtract. Create another class called MulDiv that extends from
AddSub class to use the member data of the super class. MulDiv should have
methods to Multiply and Divide. A main function should access the methods and
perform the mathematical operations.
import java.io.*;
import java.util.*;
class AddSub
{
int x=15,y=5;
public int Add()
{
int add;
add=x+y;
return add;
}
public int Subtract()
{
int sub;
sub=x-y;
return sub;
}
}
class MulDiv extends AddSub
{
public int Multiply()
{
int mul;
mul=x*y;
return mul;
}
public int Divide()
{
int div;
div=x/y;
return div;
}
}
class pa2
{
public static void main(String args[])
{
AddSub obj1= new AddSub();
MulDiv obj2= new MulDiv();
System.out.println(obj1.Add());
System.out.println(obj1.Subtract());
System.out.println(obj2.Multiply());
System.out.println(obj2.Divide());
}
}

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

4. Progrsm to create a student class with following attributes;


Enrolment no, Name, Mark of Sub1, Mark of Sub2, Mark of Sub3, Total marks.
Total of three marks must be calculated only when the student passes in all three
subjects. The pass marks of each subject is 50. If a candidate fails in any one of the
subjects his total marks must be declared as zero. Using this condition write a
constructor for this class. Write separate functions for accepting and displaying
student details. In the main method create an array of n student objects and display
the details.
import java.util.Scanner;
class Student{
public int ENo,s1,s2,s3,total;
public String name;
Student(int ENo, String name,int s1,int s2,int s3){
this.ENo = ENo;
this.name = name;
this.s1=s1;
this.s2=s2;
this.s3=s3;
}
void calculate()
{
if(s1>=50 && s2>=50 && s3>=50)
this.total=s1+s2+s3;
else
this.total=0;
}
void Display()
{
System.out.println("Enrollment NO: "+this.ENo);
System.out.println("Studnet name: "+this.name);
System.out.println("Subject 1: "+this.s1);
System.out.println("Subject 2: "+this.s2);
System.out.println("Subject 3: "+this.s3);
System.out.println("Total: "+this.total+"\n");
}

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

5. In a college first year class are


class Student {
String name;
int sub1;
int sub2;
int sub3;
int totalMarks;
public Student(String name, int sub1, int sub2, int sub3) {
this.name = name;
this.sub1 = sub1;
this.sub2 = sub2;
this.sub3 = sub3;
this.totalMarks = sub1 + sub2 + sub3;
}
}
class FirstYear {
String className;
String staffName;
int numOfStudents;
Student[] students;
public FirstYear(String className, String staffName, int numOfStudents, Student[] students)
{
this.className = className;
this.staffName = staffName;
this.numOfStudents = numOfStudents;
this.students = students;
}
public Student bestStudent() {
Student best = students[0];
for (int i = 1; i < numOfStudents; i++) {
if (students[i].totalMarks > best.totalMarks) {

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

You might also like