0% found this document useful (0 votes)
3 views4 pages

Java Programming Lab Assignments

The document contains multiple Java programming assignments demonstrating various concepts such as sorting students by total marks, counting object instances, dynamic method dispatch with inheritance, calculating interest, and string reversal using interfaces. Each assignment includes code snippets that illustrate the implementation of these concepts. The examples serve as practical applications of Java programming principles.

Uploaded by

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

Java Programming Lab Assignments

The document contains multiple Java programming assignments demonstrating various concepts such as sorting students by total marks, counting object instances, dynamic method dispatch with inheritance, calculating interest, and string reversal using interfaces. Each assignment includes code snippets that illustrate the implementation of these concepts. The examples serve as practical applications of Java programming principles.

Uploaded by

forexxkartikey
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java Lab Assignment

1.
Ans :-
import [Link];
import [Link];

class Student {
String name;
int id;
int mathMarks;
int physicsMarks;
int chemistryMarks;

public Student(String name, int id, int mathMarks, int physicsMarks, int chemistryMarks) {
[Link] = name;
[Link] = id;
[Link] = mathMarks;
[Link] = physicsMarks;
[Link] = chemistryMarks;
}

public int getTotalMarks() {


return mathMarks + physicsMarks + chemistryMarks;
}

public String toString() {


return "Name: " + name + ", Total Marks: " + getTotalMarks();
}
}

public class StudentSort {


public static void main(String[] args) {
Student[] students = {
new Student("Alice", 101, 80, 85, 90),
new Student("Bob", 102, 75, 80, 85),
new Student("Charlie", 103, 90, 85, 95),
new Student("David", 104, 85, 75, 80),
new Student("Emma", 105, 70, 90, 85)
};

// Sort the students array in descending order of total marks


[Link](students, [Link](Student::getTotalMarks).reversed());

// Display the sorted array


[Link]("Students in descending order of total marks:");
for (Student student : students) {
[Link](student);
}
}
}

2.
Ans :-
class MyClass {
private static int objectCount = 0;

public MyClass() {
objectCount++;
}

public static int getObjectCount() {


return objectCount;
}
}

public class ObjectCounter {


public static void main(String[] args) {
MyClass obj1 = new MyClass();
MyClass obj2 = new MyClass();
MyClass obj3 = new MyClass();

[Link]("Number of objects created: " + [Link]());


}
}

3.
Ans :-
class Animal {
public void makeSound() {
[Link]("Animal makes a sound");
}
}

class Dog extends Animal {


@Override
public void makeSound() {
[Link]("Dog barks");
}
}

class Cat extends Animal {


@Override
public void makeSound() {
[Link]("Cat meows");
}
}

public class DynamicDispatchDemo {


public static void main(String[] args) {
Animal animal1 = new Dog(); // Base class reference variable pointing to a Dog object
Animal animal2 = new Cat(); // Base class reference variable pointing to a Cat object

[Link](); // Calls Dog's makeSound() method dynamically


[Link](); // Calls Cat's makeSound() method dynamically
}
}

4.
Ans :-
public final class InterestCalculator {
private final double rate; // final rate of interest

public InterestCalculator(double rate) {


[Link] = rate;
}

public final double calculateSimpleInterest(double principal, double time) {


return (principal * rate * time) / 100.0;
}

public final double calculateCompoundInterest(double principal, double time) {


return principal * ([Link]((1 + rate / 100), time) - 1);
}
}

5.
Ans :-
interface StringReversal {
String reversal(String str);
}

class StringManipulator implements StringReversal {


@Override
public String reversal(String str) {
StringBuilder reversed = new StringBuilder(str);
return [Link]().toString();
}
}

public class StringReversalDemo {


public static void main(String[] args) {
StringReversal stringReversal = new StringManipulator();
String original = "Hello, World!";
String reversed = [Link](original);
[Link]("Original String: " + original);
[Link]("Reversed String: " + reversed);
}
}

You might also like